shuiguo.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. 'use strict';
  2. var quick = require('quick-pomelo');
  3. var P = quick.Promise;
  4. var _ = require('lodash');
  5. var C = require('../../share/constant');
  6. var conf = require('../config/games').shuiguo || {};
  7. var User = require('../shuiguo/user');
  8. var Table = require('../shuiguo/table');
  9. var configCommon = require('../../share/configCommon');//////TL++配置相关的公共方法
  10. var logger = quick.logger.getLogger('fhmj', __filename);
  11. /////TL++,下面这几行为了记录用户行为
  12. // var log4js = require("log4js");////cssj
  13. // var log4js_config = require("../logConf.json");
  14. // log4js.configure(log4js_config);
  15. // // console.log("log_start start!");
  16. // var LogFile = log4js.getLogger('log_file',__filename);////cssj
  17. // 金币消耗
  18. const COSTS = conf.crt_costs || {};
  19. // 构造方法
  20. var Controller = function (app) {
  21. this.app = app;
  22. this.id = 10005;
  23. this.lastId = 70000;//桌子号70000
  24. console.warn("水果 游戏服务器启动",app.getServerType(),app.getServerId());
  25. // logger.info("奉化2人 游戏服务器启动",app.getServerType(),app.getServerId());
  26. this.users = {};
  27. this.tables = [];
  28. this.predels = [];
  29. this.keepTm = 600000;// 10分钟 删除空闲桌子时间
  30. this.lconfigCommon = new configCommon(this.app);
  31. //ts++60秒执行一下
  32. if (app.getServerType() == 'game') setInterval(() => this.checkDelete(), 60000);//this.keepTm / 2
  33. };
  34. // 导出方法
  35. module.exports = function (app) {
  36. return new Controller(app);
  37. };
  38. // 原型对象
  39. var proto = Controller.prototype;
  40. // 创建玩家
  41. proto._createUser = function (player, table) {
  42. //let today = new Date();
  43. //console.warn("创建玩家",today.getTime());
  44. var user = new User(this, player, table);
  45. this.users[user.id] = user;
  46. return user;
  47. };
  48. // 创建桌子 cell底分 round局数
  49. proto._createTable = function (cPlayerId,cUserId,cName,cHead,cell, round, type,gameKindTL,playerAllCount,upId,other,agentId,yxndlbTime,yxndlbTip) {
  50. // this.lastId += _.random(1, 100);////在区间[1,100]内随机取整数 TL++zs yl
  51. this.lastId += 1;
  52. if(this.lastId>=80000)
  53. {
  54. this.lastId=70000;
  55. }
  56. var serverId = this.app.getServerId();
  57. var prefix = serverId.charAt(serverId.length - 2)+serverId.charAt(serverId.length - 1)+ this.lastId;//ts++ game-server-10 取serverID的最后一个字符
  58. //console.warn("创建桌子",prefix,serverId);
  59. // var str3 = "shuiguo 创建桌子Id:"+prefix
  60. // logger.info(str3);////cssj
  61. var table = new Table(cPlayerId,cUserId,cName,cHead,this, prefix , cell, round, type,gameKindTL,playerAllCount,upId,other,agentId,yxndlbTime,yxndlbTip);
  62. this.tables.push(table);
  63. return table;
  64. };
  65. // 删除桌子
  66. proto.deleteTable = function (tableId) {
  67. // console.warn("-------------------删除桌子----------------------",tableId);
  68. // var str3 = "shuiguo 删除桌子Id:"+tableId
  69. // logger.info(str3);////cssj
  70. var index = _.findIndex(this.tables, { id: tableId });
  71. if (-1 != index) {
  72. this.tables.splice(index, 1);
  73. }
  74. };
  75. // 检查删除 此方法目前(7月26日)每10分钟调用一次
  76. proto.checkDelete = P.coroutine(function* () {
  77. var date1 = new Date(Date.now());
  78. var date2=date1.toLocaleDateString().replace(/\//g, "-") + " " + date1.toTimeString().substr(0, 8);
  79. // console.warn("检查删除 房间数",this.tables.length,date2,this.app.getServerId());
  80. var nowTime = Date.now();
  81. if (this.tables.length > 0) {
  82. var validTables = [];
  83. for (let i = 0; i < this.tables.length;++i) {
  84. let table = this.tables[i];
  85. if (table.stime ==0 && nowTime - table.ctime >= this.keepTm)
  86. {
  87. // console.warn("tableId ccccc",table);
  88. // let str3 = "shuiguo检查删除validTables"+table.id
  89. // logger.info(str3);////cssj
  90. validTables.push(table);
  91. }
  92. }
  93. // console.warn("删除数:",validTables.length);
  94. if (validTables.length > 0) {
  95. let app = this.app;
  96. return app.memdb.goose.transactionAsync(P.coroutine(function* () {
  97. for (let j = 0; j < validTables.length; ++j) {
  98. // let str3 = "shuiguo检查删除SYSTEMEND"+validTables[j].id
  99. // logger.info(str3);////cssj
  100. yield validTables[j].endResultAsync(Table.ENDMODE.SYSTEMEND);//ts++超时解散
  101. }
  102. }), app.getServerId())
  103. .then(() => app.event.emit('transactionSuccess'), () => app.event.emit('transactionFail'));
  104. }
  105. }
  106. });
  107. // 回到桌子
  108. proto.backTableAsync = P.coroutine(function* (user) {
  109. // console.warn("tssg+++++回到桌子playerId:%s",user.id);//ts++
  110. // let str3 = "shuiguo回到桌子 "+user.id
  111. // logger.info(str3);////cssj
  112. // 查找玩家
  113. var player = yield this.app.models.Player.findByIdAsync(user.id, 'tableId gameId gameServerId connectorId');
  114. // let str4 = "shuiguo回到33桌子gameServerId "+player.gameServerId+" state "+ user.table.state +" isGameOk "+user.table.isGameOk
  115. // logger.info(str4);////cssj
  116. if (player.gameServerId) {
  117. // console.warn("-------进不来111",player.gameServerId);
  118. //return { code: C.FAILD, msg: C.TABLE_HAS_ALREADY };
  119. }
  120. var bDel=false;
  121. var usertable = user.table;
  122. if(!usertable)
  123. {
  124. bDel=true;
  125. //console.warn('=======房间已经不存在了=====删除用户:%s', user.id);
  126. //logger.info('=======房间已经不存在了=====删除用户:%s', user.id);
  127. }
  128. if (!bDel && usertable.state == Table.STATE.END) {
  129. bDel=true;
  130. //console.warn('=======房间已经结束=====删除用户:%s', user.id);
  131. //logger.info('=======房间已经结束=====删除用户:%s', user.id);
  132. }
  133. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  134. {
  135. bDel=true;
  136. //console.warn('creatOrjoinTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  137. //logger.info('=======断线时间过长了=====删除用户:%s', user.id);
  138. }
  139. if (bDel) {
  140. delete this.users[user.id];
  141. console.warn("161 bDel:",bDel)
  142. return { code: C.FAILD, msg: C.TABLE_GAME_OVER };
  143. }
  144. if (player.connectorId) {
  145. user.connectorId = player.connectorId;
  146. }
  147. var res = yield user.table.backAsync(user);
  148. // console.warn("ts+++++回到桌子usertable.state ",usertable.state);//ts++
  149. // 保存服务
  150. if (player) {
  151. // let str5 = "shuiguo回到33桌子tableId"+user.table.id
  152. // logger.info(str5);////cssj
  153. player.tableId = user.table.id;
  154. player.gameId = this.id;
  155. player.gameServerId = this.app.getServerId();
  156. yield player.saveAsync();
  157. }
  158. return res;
  159. });
  160. // TL++,判断桌子是否存在 reload=1为重入
  161. proto.isTableExistAsync = P.coroutine(function* (playerId, tableId,reload) {
  162. //console.warn("判断房间是否存在++++:%s,房间号:%s,重入:%d",playerId,tableId,reload);
  163. // let str5 = "shuiguo判断桌子是否存在 "+playerId
  164. // logger.info(str5);////cssj
  165. // 查找玩家
  166. var user = this.users[playerId];
  167. if (user) {//重入优化
  168. var bDel=false;
  169. var usertable = user.table;
  170. if(!usertable)
  171. {
  172. bDel=true;
  173. // let str6 = "shuiguo判断桌子是否存在usertable "+usertable+" state "+usertable.state+" isGameOk "+ usertable.isGameOk
  174. // logger.info(str6);////cssj
  175. //console.warn('=======房间已经不存在了=====删除用户:%s', user.id);
  176. //logger.info('=======房间已经不存在了=====删除用户:%s', user.id);
  177. }
  178. if (!bDel && usertable.state == Table.STATE.END) {
  179. bDel=true;
  180. //console.warn('=======房间已经结束=====删除用户:%s', user.id);
  181. //logger.info('=======房间已经结束=====删除用户:%s', user.id);
  182. }
  183. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  184. {
  185. bDel=true;
  186. // let str6 = "shuiguo判断桌子是否存在断线时间过长user.id "+user.id
  187. // logger.info(str6);////cssj
  188. // console.warn('isTableExistAsync=======断线时间过长了=====删除用户:%s', user.id);
  189. //logger.info('=======断线时间过长了=====删除用户:%s', user.id);
  190. }
  191. if(!bDel )
  192. {
  193. return { code: C.OK, data: {tid:usertable.id} };
  194. }
  195. else
  196. {
  197. // let str6 = "shuiguo判断桌子是否存在==删除用户user.id "+user.id
  198. // logger.info(str6);////cssj
  199. // console.warn('isTableExistAsync==========删除用户==%s', user.id);
  200. //logger.info('==========删除用户==%s', user.id);
  201. delete this.users[playerId];
  202. }
  203. }
  204. // 查找桌子
  205. var table = _.find(this.tables, { id: tableId });
  206. // let str6 = "shuiguo判断桌子是否存在==查找桌子 "
  207. // logger.info(str6);////cssj
  208. if (!table) {
  209. console.warn("remove 230")
  210. return { code: C.FAILD, msg: C.TABLE_NOT_FOUND };
  211. }
  212. var table = _.find(this.tables, { id: tableId });
  213. // let str7 = "shuiguo222判断桌子是否存在==查找桌子 state"+table.state +" isGameOk "+ table.isGameOk + "isFull" +table.isFull()
  214. // logger.info(str7);////cssj
  215. // 状态结束
  216. if (table.state == Table.STATE.END) {
  217. console.warn("161 table:",table)
  218. return { code: C.FAILD, msg: C.TABLE_GAME_OVER };
  219. }
  220. // 是否坐满
  221. if (table.isFull(playerId)) {
  222. console.warn("remove 243")
  223. return { code: C.FAILD, msg: C.TABLE_IS_FULL };
  224. }
  225. return { code: C.OK, data: {tid:tableId} };
  226. });
  227. ////TL++创建或者加入桌子,用于玩家通过战绩分享进来之后
  228. proto.creatOrjoinTableAsync = P.coroutine(function* (playerId, round,type, kind,playercount,upId,other,agentId) {
  229. // let str6 = "shuiguo创建或者加入桌子 "+playerId+ " "+ round+ " "+type+ " "+ kind+ " "+playercount+ " "+upId+ " "+other+ " "+agentId
  230. // logger.info(str6);////cssj
  231. // let str64 = "shuiguo创建或者加入桌子 "+ typeof(playerId)+ " "+ typeof(round)+ " "+typeof(type)+ " "+ typeof(kind)+ " "+typeof(playercount)+ " "+typeof(upId)+ " "+typeof(other)+ " "+typeof(agentId)
  232. // logger.info(str64);////cssj
  233. // console.warn("创建或者加入桌子,用于玩家通过战绩分享进来之后:%s",playerId);//ts++
  234. // console.warn(str6);
  235. // console.warn(str64);
  236. let player = yield this.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond userId name headurl');
  237. let cUserId = String(player.userId);
  238. let cName = String(player.name);
  239. let cHead = String(player.headurl);
  240. //// 检查推广黑白名单功能,判断玩家是否可以进入(创建或者加入桌子)
  241. let chekRes = yield this.checkAgreeInfo(playerId,agentId,cUserId,cName,cHead);
  242. if(chekRes.code != C.OK){
  243. return chekRes;
  244. }
  245. // 查找玩家
  246. var user = this.users[playerId];
  247. if (user) {
  248. var bDel=false;
  249. var usertable = user.table;
  250. if(!usertable)
  251. {
  252. bDel=true;
  253. //console.warn('=======房间已经不存在了=====删除用户:%s', user.id);
  254. // logger.info('creatOrjoinTableAsync=======房间已经不存在了=====删除用户:%s', user.id);
  255. }
  256. if (!bDel && usertable.state == Table.STATE.END) {
  257. bDel=true;
  258. //console.warn('=======房间已经结束=====删除用户:%s', user.id);
  259. // logger.info('creatOrjoinTableAsync=======房间已经结束=====删除用户:%s', user.id);
  260. }
  261. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  262. {
  263. bDel=true;
  264. //console.warn('creatOrjoinTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  265. // logger.info('creatOrjoinTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  266. }
  267. if(bDel)
  268. {
  269. delete this.users[playerId];
  270. // logger.info('creatOrjoinTableAsync==========删除用户==%s', user.id);
  271. }
  272. else
  273. {
  274. return this.backTableAsync(user);
  275. }
  276. }
  277. // console.warn("得到下一局的信息查找桌子不存在就创建",data);
  278. // var table = _.find(this.tables, { upId: data.upId });
  279. var table = _.find(this.tables, { upId: upId });
  280. // console.warn("创建或者加入桌子,用于玩家通过战绩分享进来之后: table.state "+table.state);
  281. if (!table || table.isFull())
  282. {
  283. // var agentId=data.agentId || "";
  284. var agentId=agentId || "";
  285. // console.warn("ts++下一局----------------新建");
  286. // return this.createTableAsync(playerId, data.round, 0,data.kind,data.playercount,data.upId,data.other,agentId);
  287. return this.createTableAsync(playerId, round, type,kind,playercount,upId,other,agentId);
  288. }
  289. else
  290. {
  291. // console.warn("ts++下一局----------------加入:"+table.id);
  292. return this.joinTableAsync(playerId, table.id);
  293. }
  294. });
  295. // 加入桌子
  296. proto.joinTableAsync = P.coroutine(function* (playerId, tableId) {
  297. // console.warn("shuiguo加入桌子playerId: "+playerId+" ,tableId: "+tableId);
  298. // tjbjlmryxrs
  299. // let isXTCF = yield this.lconfigCommon.checkIsSystemCreat(playerId);//是否系统创房
  300. // if(isXTCF) return tableId;
  301. // let str6 = "shuiguo加入桌子playerId:"+playerId+",tableId:"+tableId;
  302. // logger.info(str6);////cssj
  303. ////检查是否维护状态
  304. let whdata = yield this.app.models.WHstate.findByIdReadOnlyAsync('wh', 'stateWH whTip');
  305. // console.warn("22检查是否维护状态",whdata);
  306. if (whdata) {
  307. // console.warn("--33检查是否维护状态",whdata.stateWH,whdata.whTip);
  308. if(whdata.stateWH){
  309. /////维护状态中
  310. return { code: C.FAILD, tip: whdata.whTip };
  311. }
  312. }
  313. else{
  314. // console.warn("创建数据库");
  315. // var whsj = new this.app.models.WHstate({
  316. // _id: 'wh',
  317. // stateWH: 0,
  318. // whTip: '游戏正在维护,请稍后创建房间',
  319. // rebaterate: 60
  320. // });
  321. // yield whsj.saveAsync();
  322. // console.warn("检查是否维护状态");
  323. }
  324. // 查找玩家
  325. var user = this.users[playerId];
  326. if (user) {
  327. var bDel=false;
  328. var usertable = user.table;
  329. if(!usertable)
  330. {
  331. bDel=true;
  332. // console.warn('=======房间已经不存在了=====加入桌子:%s', user.id);
  333. // logger.info('joinTableAsync=======房间已经不存在了=====删除用户:%s', user.id);
  334. }
  335. if (!bDel && usertable.state == Table.STATE.END) {
  336. bDel=true;
  337. // console.warn('=======房间已经结束=====加入桌子:%s', user.id);
  338. // logger.info('joinTableAsync=======房间已经结束=====删除用户:%s', user.id);
  339. }
  340. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  341. {
  342. bDel=true;
  343. // console.warn('creatOrjoinTableAsync=======断线时间过长了=====加入桌子:%s', user.id);
  344. // logger.info('joinTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  345. }
  346. if (bDel) {
  347. delete this.users[user.id];
  348. if(usertable.id==tableId)
  349. {
  350. // console.warn("到这里返回了");
  351. //return { code: C.FAILD, msg: C.TABLE_NOT_FOUND };
  352. }
  353. }
  354. else
  355. {
  356. return this.backTableAsync(user);
  357. }
  358. }
  359. // 查找桌子
  360. var table = _.find(this.tables, { id: tableId });
  361. if (!table) {
  362. console.warn("remove 389")
  363. // console.warn("桌子没找到 到这里返回了 ",tableId);
  364. return { code: C.FAILD, msg: C.TABLE_NOT_FOUND };
  365. }
  366. // table.agentId 数据库中agenter(推广员)数据表的主键(_id)
  367. // console.warn("=======加入桌子==table "+ table.agentId+ " playerId "+playerId);
  368. let player2 = yield this.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond userId name headurl');
  369. let cUserId = String(player2.userId);/////TL++房主
  370. let cName = String(player2.name);/////TL++房主
  371. let cHead = String(player2.headurl);/////TL++房主
  372. //// 检查推广黑白名单功能,判断玩家是否可以进入(加入桌子)
  373. let chekRes = yield this.checkAgreeInfo(playerId,table.agentId,cUserId,cName,cHead);
  374. if(chekRes.code != C.OK){
  375. return chekRes;
  376. }
  377. // delete require.cache[require.resolve('../../share/configCommon')];
  378. // configCommon = require('../../share/configCommon');
  379. // this.lconfigCommon = new configCommon(this.app);
  380. // // console.warn("加入房间 table.agentId "+table.agentId+" table.isNeverStart "+table.isNeverStart)
  381. // let chekRes2 = yield this.lconfigCommon.checkIsCanJoin(this.id,table.agentId,2);
  382. // if(chekRes2.code != C.OK){
  383. // // console.warn("加入房间 不在比赛期间内 table.isNeverStart "+table.isNeverStart)
  384. // // return chekRes2;
  385. // if(table.isNeverStart){
  386. // return chekRes2;//不在比赛期间内,而且游戏从未开始过,不让进去
  387. // }
  388. // }
  389. // 状态结束
  390. if (table.state == Table.STATE.END) {
  391. console.warn("413 table:",table)
  392. //return { code: C.FAILD, msg: C.TABLE_GAME_OVER };
  393. }
  394. // 是否坐满
  395. console.warn("users:",table.users)
  396. if (table.isFull()) {
  397. console.warn("remove 425")
  398. // console.warn("创建的时候发现桌子坐满了");
  399. return { code: C.FAILD, msg: C.TABLE_IS_FULL };
  400. }
  401. // 查找玩家
  402. var player = yield this.app.models.Player.findByIdAsync(playerId, 'account userId name sex headurl tableId gameId gameServerId connectorId diamond cost spreadId registerTime lastLoginTime');
  403. if (player.gameServerId) {
  404. // console.warn("-------进不来666",player.gameServerId);
  405. //return { code: C.FAILD, msg: C.TABLE_HAS_ALREADY };
  406. }
  407. // 创建玩家
  408. var user = this._createUser(player, table);
  409. // 加入桌子
  410. logger.info('==========joinTableAsync==%s', playerId);
  411. var res = yield table.joinAsync(user);
  412. // 保存服务
  413. if (player) {
  414. player.tableId = tableId;
  415. player.rTableId = parseInt(tableId);//ts++重入时使用
  416. player.gameId = this.id;
  417. player.gameServerId = this.app.getServerId();
  418. player.headurl = playerId+".png";
  419. yield player.saveAsync();
  420. }
  421. // console.warn(" shuiguo加入桌子.... table.state "+table.state);
  422. // let str1 = "shuiguo 22加入桌子 "+playerId;
  423. // logger.info(str1);////cssj
  424. // console.warn("shuiguo加入桌子 res : "+JSON.stringify( res));
  425. return res;
  426. });
  427. ////检查推广黑白名单功能
  428. proto.checkAgreeInfo = P.coroutine(function* (playerId,agentId,cUserId,cName,cHead) {
  429. // console.warn("检查推广黑白名单功能11 "+playerId);
  430. ////查询该推广员的白名单信息 检查玩家是否处于该推广员的白名单中,如果已经得到同意才能创建和加入房间
  431. // var agentId = "xxxx"//_agentId
  432. if(!agentId) return { code: C.OK};
  433. var agreeInfo = yield this.app.models.AgenterAgree.findByIdReadOnlyAsync(agentId, '_id isOpenAgree infoList');
  434. if (agreeInfo) {
  435. // console.warn("该推广有白名单信息 ");
  436. // console.warn("该推广有白名单信息00 +agreeInfo.infoList "+" isOpenAgree "+agreeInfo.isOpenAgree );
  437. let isInAgreeInfoList = false;
  438. let isAgreedPlayer = false;//该玩家是否被同意
  439. //只要该玩家被拒绝,无论同意功能是否开启该玩家都不能进入
  440. for (var i = agreeInfo.infoList.length - 1; i >= 0; i--) {
  441. if(agreeInfo.infoList[i].playerId == playerId){
  442. isInAgreeInfoList = true;
  443. isAgreedPlayer = agreeInfo.infoList[i].agreeState == "1";
  444. if(agreeInfo.infoList[i].agreeState == "2"){
  445. console.warn("remove 477")
  446. return { code: C.FAILD, msg: "您已被推广员拒绝" };
  447. }
  448. break;
  449. }
  450. }
  451. // console.warn("该推广有白名单信息22 改玩家未被拒绝 "+" isOpenAgree "+agreeInfo.isOpenAgree + " isInAgreeInfoList "+isInAgreeInfoList);
  452. //如果功能开启,
  453. if(agreeInfo.isOpenAgree == "1"){
  454. if(!isInAgreeInfoList){
  455. //此玩家未记录在同意或拒绝信息列表中则将该玩家加入待同意名单
  456. agreeInfo.infoList.push({
  457. _id: playerId,
  458. playerId: playerId,
  459. agreeState:'0',
  460. userId: cUserId,
  461. name: cName,
  462. headurl: cHead
  463. });
  464. yield agreeInfo.saveAsync();
  465. // console.warn(" 功能开启 此玩家未记录 ");
  466. return { code: C.FAILD, msg: "等待推广员同意" };
  467. }
  468. else{
  469. // console.warn(" 功能开启 此玩家已经有记录 不是拒绝也不是同意 ");
  470. //此玩家以记录在同意或待同意信息列表中,则判断该玩家能否进入游戏
  471. if(!isAgreedPlayer){
  472. return { code: C.FAILD, msg: "等待推广员同意2" };
  473. }
  474. }
  475. }
  476. }
  477. else{
  478. // console.warn("该推广尚未有白名单信息则创建该推广的白名单信息");
  479. //该推广尚未有白名单信息则创建该推广的默认白名单信息
  480. var agenteragree = new this.app.models.AgenterAgree({
  481. _id: agentId,//记录
  482. isOpenAgree: "0",
  483. infoList: []
  484. });
  485. // console.warn("agenteragree new",agenteragree);
  486. yield agenteragree.saveAsync();
  487. }
  488. return { code: C.OK};
  489. });
  490. // 创建桌子
  491. proto.createTableAsync = P.coroutine(function* (playerId, round, type,gameKind,playerAllCount,upId,other,agentId) {
  492. ////检查是否维护状态
  493. let whdata = yield this.app.models.WHstate.findByIdReadOnlyAsync('wh', 'stateWH whTip yxndlbTime yxndlbTip');
  494. // console.warn("22检查是否维护状态",whdata);
  495. if (whdata) {
  496. whdata.yxndlbTime = 120;
  497. yield whdata.saveAsync();
  498. // console.warn("--33检查是否维护状态",whdata.stateWH,whdata.whTip);
  499. if(whdata.stateWH){
  500. /////维护状态中
  501. return { code: C.SERVER_WEIHU, msg: whdata.whTip };
  502. }
  503. }
  504. else{
  505. // console.warn("创建数据库");
  506. // var whsj = new this.app.models.WHstate({
  507. // _id: 'wh',
  508. // stateWH: 0,
  509. // whTip: '游戏正在维护,请稍后创建房间',
  510. // rebaterate: 60
  511. // });
  512. // yield whsj.saveAsync();
  513. // console.warn("检查是否维护状态");
  514. }
  515. // console.warn("44检查是否维护状态",whdata.stateWH,whdata.whTip);
  516. var user = this.users[playerId];
  517. if (user) {
  518. var bDel=false;
  519. var usertable = user.table;
  520. if(!usertable)
  521. {
  522. bDel=true;
  523. //console.warn('=======房间已经不存在了=====删除用户:%s', user.id);
  524. // logger.info('createTableAsync=======房间已经不存在了=====删除用户:%s', user.id);
  525. }
  526. if (!bDel && usertable.state == Table.STATE.END) {
  527. bDel=true;
  528. //console.warn('=======房间已经结束=====删除用户:%s', user.id);
  529. // logger.info('createTableAsync=======房间已经结束=====删除用户:%s', user.id);
  530. }
  531. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  532. {
  533. bDel=true;
  534. //console.warn('createTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  535. // logger.info('createTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  536. }
  537. if(bDel)
  538. {
  539. delete this.users[playerId];
  540. // logger.info('createTableAsync==========删除用户==%s', user.id);
  541. }
  542. else
  543. {
  544. return this.backTableAsync(user);
  545. }
  546. }
  547. // 金币消耗 /////TLzs8月6号,创房间不做钻石限制
  548. // var num_cost = parseInt(COSTS[round]);
  549. // if(gameKind == 2){
  550. // num_cost = 90;//////冲刺扣90
  551. // }
  552. // var crt_cost = num_cost >= 0 ? num_cost : round * 10;
  553. // if (crt_cost > 0) {
  554. // let player = yield this.app.models.Player.findByIdAsync(playerId, 'diamond cost');
  555. // if (!player || player.diamond < crt_cost) {
  556. // return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  557. // }
  558. // // player.diamond -= crt_cost;
  559. // // player.cost += crt_cost;
  560. // // yield player.saveAsync();
  561. // }
  562. // 创建桌子
  563. //logger.info('==========createTableAsync==%s', playerId);
  564. /////// 判断一个玩家是否还能再创建房间,限制每个玩家只能最多创建10个游戏未开始的房间
  565. let today = new Date();
  566. let time =today.getTime() -60000;
  567. //let opts = {registerTime: { $gte: time},playerId: playerId};
  568. //let count = yield this.app.models.CTableRecord.countMongoAsync(opts)
  569. // delete require.cache[require.resolve('../../share/configCommon')];
  570. // configCommon = require('../../share/configCommon');
  571. // this.lconfigCommon = new configCommon(this.app);
  572. let cUserId = "";/////TL++房主
  573. let cName = "";/////TL++房主
  574. let cHead = "";/////TL++房主
  575. let opts = {ctime: { $gte: time},ownerId: playerId};
  576. let count = yield this.app.models.GameTable.countMongoAsync(opts)
  577. if(count >= 10){
  578. return { code: C.FAILD, msg: C.TABLE_CREATETOOMUCH };
  579. }
  580. let player = yield this.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond userId name headurl');
  581. // if (!player || player.diamond < crt_cost) {/////TLzs8月6号,创房间不做钻石限制
  582. // return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  583. // }
  584. cUserId = String(player.userId);/////TL++房主
  585. cName = String(player.name);/////TL++房主
  586. cHead = String(player.headurl);/////TL++房主
  587. //// 检查推广黑白名单功能,判断玩家是否可以进入(创建桌子)
  588. let chekRes = yield this.checkAgreeInfo(playerId,agentId,cUserId,cName,cHead);
  589. if(chekRes.code != C.OK){
  590. return chekRes;
  591. }
  592. let chekRes2 = yield this.lconfigCommon.checkIsCanJoin(this.id,agentId,1);
  593. if(chekRes2.code != C.OK){
  594. return chekRes2;
  595. }
  596. ////////创建游戏桌子
  597. let cell=1;//底分
  598. let yxndlbTime = 6000;
  599. if(whdata && whdata.yxndlbTime) yxndlbTime = whdata.yxndlbTime;
  600. let yxndlbTip = "";
  601. if(whdata && whdata.yxndlbTip) yxndlbTip = whdata.yxndlbTip;
  602. var table = this._createTable(playerId,cUserId,cName,cHead,cell, round, type,gameKind,playerAllCount,upId,other,agentId,yxndlbTime,yxndlbTip);
  603. var gametable = new this.app.models.GameTable({
  604. _id: table.recordid,//记录
  605. tableNo: table.id,
  606. ownerId: table.ownerId,
  607. agentId: table.agentId,
  608. type: table.type,
  609. kind: table.gameKindTL,//玩法 2冲刺
  610. other: table.other,//////TL++2人3人的游戏规则
  611. playerCount: table.playerAllCount,//游戏人数
  612. round: table.round,//
  613. over: table.over,//游戏局数
  614. ctime: table.ctime,//创建时间
  615. // stime: 0,//开始时间
  616. // time: 0,//结束时间
  617. endMode: 0//结束类型
  618. // gameCost: 0,//游戏消耗
  619. // agentRebate: 0//代理返利
  620. });
  621. yield gametable.saveAsync();
  622. // 加入桌子
  623. return this.joinTableAsync(playerId, table.id);
  624. });
  625. // 离开桌子
  626. proto.leaveTableAsync = P.coroutine(function* (playerId, isOffline) {
  627. // console.warn("shuiguo 离开桌子==%s",playerId,isOffline);
  628. // logger.info("shuiguo 离开桌子==%s",playerId);
  629. var userdel = false;//ts++删除用户
  630. // 内存离开
  631. var user = this.users[playerId];
  632. if (user) {
  633. // console.warn('shuiguo 内存玩家==%s,状态%d',playerId,user.state,user.isPlaying());
  634. yield user.table.zsbgjlAsync(user);
  635. // logger.info('shuiguo 内存玩家==%s,状态%d',playerId,user.state);
  636. if (!user.isPlaying()) {
  637. // console.warn("离开桌子+++++用户空闲中");
  638. user.state = User.STATE.FREE;
  639. yield user.table.leaveAsync(user);
  640. delete this.users[playerId];
  641. userdel = true;
  642. //logger.info('shuiguo 玩家删除==%s', playerId);
  643. } else {
  644. // console.warn("离开桌子+++++用户游戏中");
  645. if (user.isOffline()) {
  646. return { code: C.OK };
  647. }
  648. // console.warn("离开桌子+++++提示玩家断线");
  649. yield user.table.offlineAsync(user);
  650. //logger.info('shuiguo 玩家断线==%s', playerId);
  651. //return { code: C.OK };//ts++游戏开始后不让退出房间
  652. }
  653. //console.warn("offlineAsync+++++状态%d",user.state);//ts++
  654. }
  655. // 置空服务
  656. //if (!isOffline) {
  657. let player = yield this.app.models.Player.findByIdAsync(playerId, 'tableId rTableId gameId gameServerId');
  658. //if (player && player.gameServerId) {
  659. //ts++upd
  660. if (player) {
  661. //console.warn("shuiguo 用户信息==",player);////css
  662. if(userdel)//ts++删除用户 重入后清空
  663. {
  664. player.rTableId = 0;
  665. }
  666. // console.warn("在这里指控了服务shuiguo");
  667. player.tableId = '';
  668. player.gameId = 0;
  669. player.gameServerId = '';
  670. yield player.saveAsync();
  671. }
  672. //}
  673. //console.warn("shuiguo 离开成功==%s",playerId);
  674. // logger.info("shuiguo 离开成功==%s",playerId);
  675. return { code: C.OK };
  676. });
  677. // 准备(结束本小局)
  678. proto.readyGameAsync = P.coroutine(function* (playerId) {
  679. let str5 = "shuiguo 准备 "+playerId;
  680. logger.warn(str5);////cssj
  681. // 查找玩家
  682. var user = this.users[playerId];
  683. if (!user) {
  684. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  685. }
  686. let str2 = "shuiguo 准备 chairId "+user.chairId+" utstate "+user.table.state+" utate2 "+user.state2;
  687. logger.warn(str2);////cssj
  688. if (user.chairId == -1) {
  689. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  690. }
  691. if (user.table.state != Table.STATE.PLAYING)
  692. {
  693. // 状态校验
  694. //return { code: C.FAILD, msg: C.TABLE_NOT_PLAYING };
  695. }
  696. // console.warn("sgj玩家准备 ",user.state,user.state2 , User.STATE.PLAYING);
  697. if (user.state2 != User.STATE.PLAYING || user.state2 != User.STATE.PLAYING) {
  698. return { code: C.FAILD, msg: C.PLAYER_NOT_PLAYING };
  699. }
  700. // let str6 = "shuiguo 22准备 "+playerId;
  701. // logger.info(str6);////cssj
  702. return user.table.readyGameAsync(user);
  703. });
  704. // 带入
  705. proto.dairuAsync = P.coroutine(function* (playerId) {
  706. // console.warn("sgj带入+++++++++playerId:%s",playerId);
  707. // let str2 = "shuiguo 角色信息 "+playerId;
  708. // logger.info(str2);////cssj
  709. // 查找玩家
  710. var user = this.users[playerId];
  711. if (!user) {
  712. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  713. }
  714. if (user.chairId == -1) {
  715. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  716. }
  717. return user.table.dairuAsync(playerId,user.chairId);
  718. });
  719. // 开始游戏
  720. proto.startGameAsync = P.coroutine(function* (playerId,yazhus) {
  721. // console.warn("sgj开始游戏+++++++++playerId:%s",playerId);
  722. // let str6 = "shuiguo 开始游戏 "+playerId;
  723. // logger.info(str6);////cssj
  724. // 查找玩家
  725. var user = this.users[playerId];
  726. if (!user) {
  727. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  728. }
  729. if (user.chairId == -1) {
  730. //return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  731. }
  732. // 状态校验
  733. var table = user.table;
  734. if (table.state != Table.STATE.FREE && table.state != Table.STATE.FREE2) {
  735. //return { code: C.FAILD, msg: 'table is not free' };
  736. //return { code: C.FAILD, msg: C.TABLE_NOT_FREE };
  737. }
  738. // 是否房主
  739. if (!user.isOwner()) {
  740. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  741. }
  742. // 是否准备
  743. if (!table.isReady2()) {
  744. // console.warn("走到这里了???? ");
  745. //return { code: C.FAILD, msg: C.TABLE_NOT_READY };
  746. }
  747. if (user.diamond < _.sum(yazhus)) {
  748. return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  749. }
  750. // let str5 = "shuiguo 222开始游戏 "+playerId;
  751. // logger.info(str5);////cssj
  752. return table.startGameAsync(yazhus);
  753. });
  754. // 划分
  755. proto.huafenAsync = P.coroutine(function* (playerId,type,value) {
  756. // console.warn("sgj划分+++++++++playerId:%s",playerId);
  757. // let str6 = "shuiguo 划分 "+playerId;
  758. // logger.info(str6);////cssj
  759. // 查找玩家
  760. var user = this.users[playerId];
  761. if (!user) {
  762. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  763. }
  764. if (user.chairId == -1) {
  765. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  766. }
  767. // 状态校验 桌子是否游戏中
  768. var table = user.table;
  769. if (table.state != Table.STATE.PLAYING) {
  770. //return { code: C.FAILD, msg: C.TABLE_NOT_PLAYING };
  771. }
  772. // 玩家是否房主
  773. if (!user.isOwner()) {
  774. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  775. }
  776. // 玩家是否游戏中
  777. if (!user.isPlaying()) {
  778. return { code: C.FAILD, msg: C.PLAYER_NOT_PLAYING };
  779. }
  780. return table.huafenAsync(type,value);
  781. });
  782. // 请求结束
  783. proto.overTableAsync = P.coroutine(function* (playerId) {
  784. // 查找玩家
  785. // console.warn("请求结束 "+playerId);
  786. var user = this.users[playerId];
  787. if (!user) {
  788. // console.warn("请求结束2222 "+playerId);
  789. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  790. }
  791. // 是否房主
  792. if (!user.isOwner()) {
  793. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  794. }
  795. return user.table.overTableAsync(user);////TL++参数userID userState
  796. });
  797. // 得到战绩
  798. proto.getRecordAsync = P.coroutine(function* (playerId) {
  799. let str5 = "shuiguo 得到战绩 "+playerId;
  800. logger.info(str5);////cssj
  801. // 查找玩家
  802. var user = this.users[playerId];
  803. if (!user) {
  804. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  805. }
  806. if (user.chairId == -1) {
  807. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  808. }
  809. return user.table.getRecordAsync(user);
  810. });
  811. // 角色信息
  812. proto.roleInfoAsync = P.coroutine(function* (playerId) {
  813. // console.warn("角色信息+++++++++playerId:%s",playerId);
  814. // let str2 = "shuiguo 角色信息 "+playerId;
  815. // logger.info(str2);////cssj
  816. // 查找玩家
  817. var user = this.users[playerId];
  818. if (!user) {
  819. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  820. }
  821. if (user.chairId == -1) {
  822. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  823. }
  824. // let str1 = "shuiguo 22角色信息 "+playerId;
  825. // logger.info(str1);////cssj
  826. return user.table.roleInfoAsync(playerId,user.chairId);
  827. });
  828. // 得到排行榜
  829. proto.getPaiHangAsync = P.coroutine(function* (playerId) {
  830. // console.warn("得到排行榜+++++++++playerId:%s",playerId);
  831. // let str2 = "shuiguo 得到排行榜 "+playerId;
  832. // logger.info(str2);////cssj
  833. // 查找玩家
  834. var user = this.users[playerId];
  835. if (!user) {
  836. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  837. }
  838. if (user.chairId == -1) {
  839. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  840. }
  841. // let str1 = "shuiguo 22得到排行榜 "+playerId;
  842. // logger.info(str1);////cssj
  843. return user.table.getPaiHangAsync(playerId);
  844. });
  845. // 上传真实姓名支付宝账号
  846. proto.upZFBInfoAsync = P.coroutine(function* (playerId,realName,zfbAcc) {
  847. // 查找玩家
  848. var user = this.users[playerId];
  849. if (!user) {
  850. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  851. }
  852. return user.table.upZFBInfoAsync(playerId,realName,zfbAcc);
  853. });
  854. // 得到玩家任务信息,里面包含任务信息
  855. proto.getTaskInfoAsync = P.coroutine(function* (playerId) {
  856. // console.warn("sgj角色信息+++++++++playerId:%s",playerId);
  857. // let str2 = "shuiguo 角色信息 "+playerId;
  858. // logger.info(str2);////cssj
  859. // 查找玩家
  860. var user = this.users[playerId];
  861. if (!user) {
  862. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  863. }
  864. if (user.chairId == -1) {
  865. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  866. }
  867. // let str1 = "shuiguo 22角色信息 "+playerId;
  868. // logger.info(str1);////cssj
  869. return user.table.getTaskInfoAsync(playerId,user.chairId);
  870. });
  871. // 处理任务
  872. proto.dealRenWuAsync = P.coroutine(function* (playerId,item) {
  873. // console.warn("shuiguo 处理任务+++++++++playerId:%s",playerId,item);
  874. // let str2 = "shuiguo 处理任务 "+playerId;
  875. // logger.info(str2);////cssj
  876. // 查找玩家
  877. var user = this.users[playerId];
  878. if (!user) {
  879. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  880. }
  881. if (user.chairId == -1) {
  882. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  883. }
  884. return user.table.dealRenWuAsync(playerId,user.chairId,item);
  885. });
  886. // 任务领奖
  887. proto.renWuLingjiangAsync = P.coroutine(function* (playerId,item) {
  888. // console.warn("shuiguo 任务领奖+++++++++playerId:%s",playerId,item);
  889. // let str2 = "shuiguo 任务领奖 "+playerId;
  890. // logger.info(str2);////cssj
  891. // 查找玩家
  892. var user = this.users[playerId];
  893. if (!user) {
  894. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  895. }
  896. if (user.chairId == -1) {
  897. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  898. }
  899. return user.table.renWuLingjiangAsync(playerId,user.chairId,item);
  900. });
  901. // 得到玩家已完成的任务列表
  902. proto.getOverTasksAsync = P.coroutine(function* (playerId) {
  903. // console.warn("sgj角色信息+++++++++playerId:%s",playerId);
  904. // let str2 = "shuiguo 角色信息 "+playerId;
  905. // logger.info(str2);////cssj
  906. // 查找玩家
  907. var user = this.users[playerId];
  908. if (!user) {
  909. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  910. }
  911. if (user.chairId == -1) {
  912. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  913. }
  914. // let str1 = "shuiguo 22角色信息 "+playerId;
  915. // logger.info(str1);////cssj
  916. return user.table.getOverTasksAsync(playerId,user.chairId);
  917. });