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. //return { code: C.FAILD, msg: C.TABLE_GAME_OVER };
  392. }
  393. // 是否坐满
  394. console.warn("users:",table.users)
  395. if (table.isFull()) {
  396. console.warn("remove 425")
  397. // console.warn("创建的时候发现桌子坐满了");
  398. return { code: C.FAILD, msg: C.TABLE_IS_FULL };
  399. }
  400. // 查找玩家
  401. var player = yield this.app.models.Player.findByIdAsync(playerId, 'account userId name sex headurl tableId gameId gameServerId connectorId diamond cost spreadId registerTime lastLoginTime');
  402. if (player.gameServerId) {
  403. // console.warn("-------进不来666",player.gameServerId);
  404. //return { code: C.FAILD, msg: C.TABLE_HAS_ALREADY };
  405. }
  406. // 创建玩家
  407. var user = this._createUser(player, table);
  408. // 加入桌子
  409. logger.info('==========joinTableAsync==%s', playerId);
  410. var res = yield table.joinAsync(user);
  411. // 保存服务
  412. if (player) {
  413. player.tableId = tableId;
  414. player.rTableId = parseInt(tableId);//ts++重入时使用
  415. player.gameId = this.id;
  416. player.gameServerId = this.app.getServerId();
  417. player.headurl = playerId+".png";
  418. yield player.saveAsync();
  419. }
  420. // console.warn(" shuiguo加入桌子.... table.state "+table.state);
  421. // let str1 = "shuiguo 22加入桌子 "+playerId;
  422. // logger.info(str1);////cssj
  423. // console.warn("shuiguo加入桌子 res : "+JSON.stringify( res));
  424. return res;
  425. });
  426. ////检查推广黑白名单功能
  427. proto.checkAgreeInfo = P.coroutine(function* (playerId,agentId,cUserId,cName,cHead) {
  428. // console.warn("检查推广黑白名单功能11 "+playerId);
  429. ////查询该推广员的白名单信息 检查玩家是否处于该推广员的白名单中,如果已经得到同意才能创建和加入房间
  430. // var agentId = "xxxx"//_agentId
  431. if(!agentId) return { code: C.OK};
  432. var agreeInfo = yield this.app.models.AgenterAgree.findByIdReadOnlyAsync(agentId, '_id isOpenAgree infoList');
  433. if (agreeInfo) {
  434. // console.warn("该推广有白名单信息 ");
  435. // console.warn("该推广有白名单信息00 +agreeInfo.infoList "+" isOpenAgree "+agreeInfo.isOpenAgree );
  436. let isInAgreeInfoList = false;
  437. let isAgreedPlayer = false;//该玩家是否被同意
  438. //只要该玩家被拒绝,无论同意功能是否开启该玩家都不能进入
  439. for (var i = agreeInfo.infoList.length - 1; i >= 0; i--) {
  440. if(agreeInfo.infoList[i].playerId == playerId){
  441. isInAgreeInfoList = true;
  442. isAgreedPlayer = agreeInfo.infoList[i].agreeState == "1";
  443. if(agreeInfo.infoList[i].agreeState == "2"){
  444. console.warn("remove 477")
  445. return { code: C.FAILD, msg: "您已被推广员拒绝" };
  446. }
  447. break;
  448. }
  449. }
  450. // console.warn("该推广有白名单信息22 改玩家未被拒绝 "+" isOpenAgree "+agreeInfo.isOpenAgree + " isInAgreeInfoList "+isInAgreeInfoList);
  451. //如果功能开启,
  452. if(agreeInfo.isOpenAgree == "1"){
  453. if(!isInAgreeInfoList){
  454. //此玩家未记录在同意或拒绝信息列表中则将该玩家加入待同意名单
  455. agreeInfo.infoList.push({
  456. _id: playerId,
  457. playerId: playerId,
  458. agreeState:'0',
  459. userId: cUserId,
  460. name: cName,
  461. headurl: cHead
  462. });
  463. yield agreeInfo.saveAsync();
  464. // console.warn(" 功能开启 此玩家未记录 ");
  465. return { code: C.FAILD, msg: "等待推广员同意" };
  466. }
  467. else{
  468. // console.warn(" 功能开启 此玩家已经有记录 不是拒绝也不是同意 ");
  469. //此玩家以记录在同意或待同意信息列表中,则判断该玩家能否进入游戏
  470. if(!isAgreedPlayer){
  471. return { code: C.FAILD, msg: "等待推广员同意2" };
  472. }
  473. }
  474. }
  475. }
  476. else{
  477. // console.warn("该推广尚未有白名单信息则创建该推广的白名单信息");
  478. //该推广尚未有白名单信息则创建该推广的默认白名单信息
  479. var agenteragree = new this.app.models.AgenterAgree({
  480. _id: agentId,//记录
  481. isOpenAgree: "0",
  482. infoList: []
  483. });
  484. // console.warn("agenteragree new",agenteragree);
  485. yield agenteragree.saveAsync();
  486. }
  487. return { code: C.OK};
  488. });
  489. // 创建桌子
  490. proto.createTableAsync = P.coroutine(function* (playerId, round, type,gameKind,playerAllCount,upId,other,agentId) {
  491. ////检查是否维护状态
  492. let whdata = yield this.app.models.WHstate.findByIdReadOnlyAsync('wh', 'stateWH whTip yxndlbTime yxndlbTip');
  493. // console.warn("22检查是否维护状态",whdata);
  494. if (whdata) {
  495. whdata.yxndlbTime = 120;
  496. yield whdata.saveAsync();
  497. // console.warn("--33检查是否维护状态",whdata.stateWH,whdata.whTip);
  498. if(whdata.stateWH){
  499. /////维护状态中
  500. return { code: C.SERVER_WEIHU, msg: whdata.whTip };
  501. }
  502. }
  503. else{
  504. // console.warn("创建数据库");
  505. // var whsj = new this.app.models.WHstate({
  506. // _id: 'wh',
  507. // stateWH: 0,
  508. // whTip: '游戏正在维护,请稍后创建房间',
  509. // rebaterate: 60
  510. // });
  511. // yield whsj.saveAsync();
  512. // console.warn("检查是否维护状态");
  513. }
  514. // console.warn("44检查是否维护状态",whdata.stateWH,whdata.whTip);
  515. var user = this.users[playerId];
  516. if (user) {
  517. var bDel=false;
  518. var usertable = user.table;
  519. if(!usertable)
  520. {
  521. bDel=true;
  522. //console.warn('=======房间已经不存在了=====删除用户:%s', user.id);
  523. // logger.info('createTableAsync=======房间已经不存在了=====删除用户:%s', user.id);
  524. }
  525. if (!bDel && usertable.state == Table.STATE.END) {
  526. bDel=true;
  527. //console.warn('=======房间已经结束=====删除用户:%s', user.id);
  528. // logger.info('createTableAsync=======房间已经结束=====删除用户:%s', user.id);
  529. }
  530. if(!bDel && user.offlinetime>0 && Date.now()-user.offlinetime>60000)
  531. {
  532. bDel=true;
  533. //console.warn('createTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  534. // logger.info('createTableAsync=======断线时间过长了=====删除用户:%s', user.id);
  535. }
  536. if(bDel)
  537. {
  538. delete this.users[playerId];
  539. // logger.info('createTableAsync==========删除用户==%s', user.id);
  540. }
  541. else
  542. {
  543. return this.backTableAsync(user);
  544. }
  545. }
  546. // 金币消耗 /////TLzs8月6号,创房间不做钻石限制
  547. // var num_cost = parseInt(COSTS[round]);
  548. // if(gameKind == 2){
  549. // num_cost = 90;//////冲刺扣90
  550. // }
  551. // var crt_cost = num_cost >= 0 ? num_cost : round * 10;
  552. // if (crt_cost > 0) {
  553. // let player = yield this.app.models.Player.findByIdAsync(playerId, 'diamond cost');
  554. // if (!player || player.diamond < crt_cost) {
  555. // return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  556. // }
  557. // // player.diamond -= crt_cost;
  558. // // player.cost += crt_cost;
  559. // // yield player.saveAsync();
  560. // }
  561. // 创建桌子
  562. //logger.info('==========createTableAsync==%s', playerId);
  563. /////// 判断一个玩家是否还能再创建房间,限制每个玩家只能最多创建10个游戏未开始的房间
  564. let today = new Date();
  565. let time =today.getTime() -60000;
  566. //let opts = {registerTime: { $gte: time},playerId: playerId};
  567. //let count = yield this.app.models.CTableRecord.countMongoAsync(opts)
  568. // delete require.cache[require.resolve('../../share/configCommon')];
  569. // configCommon = require('../../share/configCommon');
  570. // this.lconfigCommon = new configCommon(this.app);
  571. let cUserId = "";/////TL++房主
  572. let cName = "";/////TL++房主
  573. let cHead = "";/////TL++房主
  574. let opts = {ctime: { $gte: time},ownerId: playerId};
  575. let count = yield this.app.models.GameTable.countMongoAsync(opts)
  576. if(count >= 10){
  577. return { code: C.FAILD, msg: C.TABLE_CREATETOOMUCH };
  578. }
  579. let player = yield this.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond userId name headurl');
  580. // if (!player || player.diamond < crt_cost) {/////TLzs8月6号,创房间不做钻石限制
  581. // return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  582. // }
  583. cUserId = String(player.userId);/////TL++房主
  584. cName = String(player.name);/////TL++房主
  585. cHead = String(player.headurl);/////TL++房主
  586. //// 检查推广黑白名单功能,判断玩家是否可以进入(创建桌子)
  587. let chekRes = yield this.checkAgreeInfo(playerId,agentId,cUserId,cName,cHead);
  588. if(chekRes.code != C.OK){
  589. return chekRes;
  590. }
  591. let chekRes2 = yield this.lconfigCommon.checkIsCanJoin(this.id,agentId,1);
  592. if(chekRes2.code != C.OK){
  593. return chekRes2;
  594. }
  595. ////////创建游戏桌子
  596. let cell=1;//底分
  597. let yxndlbTime = 6000;
  598. if(whdata && whdata.yxndlbTime) yxndlbTime = whdata.yxndlbTime;
  599. let yxndlbTip = "";
  600. if(whdata && whdata.yxndlbTip) yxndlbTip = whdata.yxndlbTip;
  601. var table = this._createTable(playerId,cUserId,cName,cHead,cell, round, type,gameKind,playerAllCount,upId,other,agentId,yxndlbTime,yxndlbTip);
  602. var gametable = new this.app.models.GameTable({
  603. _id: table.recordid,//记录
  604. tableNo: table.id,
  605. ownerId: table.ownerId,
  606. agentId: table.agentId,
  607. type: table.type,
  608. kind: table.gameKindTL,//玩法 2冲刺
  609. other: table.other,//////TL++2人3人的游戏规则
  610. playerCount: table.playerAllCount,//游戏人数
  611. round: table.round,//
  612. over: table.over,//游戏局数
  613. ctime: table.ctime,//创建时间
  614. // stime: 0,//开始时间
  615. // time: 0,//结束时间
  616. endMode: 0//结束类型
  617. // gameCost: 0,//游戏消耗
  618. // agentRebate: 0//代理返利
  619. });
  620. yield gametable.saveAsync();
  621. // 加入桌子
  622. return this.joinTableAsync(playerId, table.id);
  623. });
  624. // 离开桌子
  625. proto.leaveTableAsync = P.coroutine(function* (playerId, isOffline) {
  626. // console.warn("shuiguo 离开桌子==%s",playerId,isOffline);
  627. // logger.info("shuiguo 离开桌子==%s",playerId);
  628. var userdel = false;//ts++删除用户
  629. // 内存离开
  630. var user = this.users[playerId];
  631. if (user) {
  632. // console.warn('shuiguo 内存玩家==%s,状态%d',playerId,user.state,user.isPlaying());
  633. yield user.table.zsbgjlAsync(user);
  634. // logger.info('shuiguo 内存玩家==%s,状态%d',playerId,user.state);
  635. if (!user.isPlaying()) {
  636. // console.warn("离开桌子+++++用户空闲中");
  637. user.state = User.STATE.FREE;
  638. yield user.table.leaveAsync(user);
  639. delete this.users[playerId];
  640. userdel = true;
  641. //logger.info('shuiguo 玩家删除==%s', playerId);
  642. } else {
  643. // console.warn("离开桌子+++++用户游戏中");
  644. if (user.isOffline()) {
  645. return { code: C.OK };
  646. }
  647. // console.warn("离开桌子+++++提示玩家断线");
  648. yield user.table.offlineAsync(user);
  649. //logger.info('shuiguo 玩家断线==%s', playerId);
  650. //return { code: C.OK };//ts++游戏开始后不让退出房间
  651. }
  652. //console.warn("offlineAsync+++++状态%d",user.state);//ts++
  653. }
  654. // 置空服务
  655. //if (!isOffline) {
  656. let player = yield this.app.models.Player.findByIdAsync(playerId, 'tableId rTableId gameId gameServerId');
  657. //if (player && player.gameServerId) {
  658. //ts++upd
  659. if (player) {
  660. //console.warn("shuiguo 用户信息==",player);////css
  661. if(userdel)//ts++删除用户 重入后清空
  662. {
  663. player.rTableId = 0;
  664. }
  665. // console.warn("在这里指控了服务shuiguo");
  666. player.tableId = '';
  667. player.gameId = 0;
  668. player.gameServerId = '';
  669. yield player.saveAsync();
  670. }
  671. //}
  672. //console.warn("shuiguo 离开成功==%s",playerId);
  673. // logger.info("shuiguo 离开成功==%s",playerId);
  674. return { code: C.OK };
  675. });
  676. // 准备(结束本小局)
  677. proto.readyGameAsync = P.coroutine(function* (playerId) {
  678. let str5 = "shuiguo 准备 "+playerId;
  679. logger.warn(str5);////cssj
  680. // 查找玩家
  681. var user = this.users[playerId];
  682. if (!user) {
  683. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  684. }
  685. let str2 = "shuiguo 准备 chairId "+user.chairId+" utstate "+user.table.state+" utate2 "+user.state2;
  686. logger.warn(str2);////cssj
  687. if (user.chairId == -1) {
  688. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  689. }
  690. if (user.table.state != Table.STATE.PLAYING)
  691. {
  692. // 状态校验
  693. //return { code: C.FAILD, msg: C.TABLE_NOT_PLAYING };
  694. }
  695. // console.warn("sgj玩家准备 ",user.state,user.state2 , User.STATE.PLAYING);
  696. if (user.state2 != User.STATE.PLAYING || user.state2 != User.STATE.PLAYING) {
  697. return { code: C.FAILD, msg: C.PLAYER_NOT_PLAYING };
  698. }
  699. // let str6 = "shuiguo 22准备 "+playerId;
  700. // logger.info(str6);////cssj
  701. return user.table.readyGameAsync(user);
  702. });
  703. // 带入
  704. proto.dairuAsync = P.coroutine(function* (playerId) {
  705. // console.warn("sgj带入+++++++++playerId:%s",playerId);
  706. // let str2 = "shuiguo 角色信息 "+playerId;
  707. // logger.info(str2);////cssj
  708. // 查找玩家
  709. var user = this.users[playerId];
  710. if (!user) {
  711. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  712. }
  713. if (user.chairId == -1) {
  714. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  715. }
  716. return user.table.dairuAsync(playerId,user.chairId);
  717. });
  718. // 开始游戏
  719. proto.startGameAsync = P.coroutine(function* (playerId,yazhus) {
  720. // console.warn("sgj开始游戏+++++++++playerId:%s",playerId);
  721. // let str6 = "shuiguo 开始游戏 "+playerId;
  722. // logger.info(str6);////cssj
  723. // 查找玩家
  724. var user = this.users[playerId];
  725. if (!user) {
  726. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  727. }
  728. if (user.chairId == -1) {
  729. //return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  730. }
  731. // 状态校验
  732. var table = user.table;
  733. if (table.state != Table.STATE.FREE && table.state != Table.STATE.FREE2) {
  734. //return { code: C.FAILD, msg: 'table is not free' };
  735. //return { code: C.FAILD, msg: C.TABLE_NOT_FREE };
  736. }
  737. // 是否房主
  738. if (!user.isOwner()) {
  739. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  740. }
  741. // 是否准备
  742. if (!table.isReady2()) {
  743. // console.warn("走到这里了???? ");
  744. //return { code: C.FAILD, msg: C.TABLE_NOT_READY };
  745. }
  746. if (user.diamond < _.sum(yazhus)) {
  747. return { code: C.FAILD, msg: C.GAME_DIAMOND_LOW };
  748. }
  749. // let str5 = "shuiguo 222开始游戏 "+playerId;
  750. // logger.info(str5);////cssj
  751. return table.startGameAsync(yazhus);
  752. });
  753. // 划分
  754. proto.huafenAsync = P.coroutine(function* (playerId,type,value) {
  755. // console.warn("sgj划分+++++++++playerId:%s",playerId);
  756. // let str6 = "shuiguo 划分 "+playerId;
  757. // logger.info(str6);////cssj
  758. // 查找玩家
  759. var user = this.users[playerId];
  760. if (!user) {
  761. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  762. }
  763. if (user.chairId == -1) {
  764. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  765. }
  766. // 状态校验 桌子是否游戏中
  767. var table = user.table;
  768. if (table.state != Table.STATE.PLAYING) {
  769. //return { code: C.FAILD, msg: C.TABLE_NOT_PLAYING };
  770. }
  771. // 玩家是否房主
  772. if (!user.isOwner()) {
  773. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  774. }
  775. // 玩家是否游戏中
  776. if (!user.isPlaying()) {
  777. return { code: C.FAILD, msg: C.PLAYER_NOT_PLAYING };
  778. }
  779. return table.huafenAsync(type,value);
  780. });
  781. // 请求结束
  782. proto.overTableAsync = P.coroutine(function* (playerId) {
  783. // 查找玩家
  784. // console.warn("请求结束 "+playerId);
  785. var user = this.users[playerId];
  786. if (!user) {
  787. // console.warn("请求结束2222 "+playerId);
  788. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  789. }
  790. // 是否房主
  791. if (!user.isOwner()) {
  792. //return { code: C.FAILD, msg: C.TABLE_NOT_OWNER };
  793. }
  794. return user.table.overTableAsync(user);////TL++参数userID userState
  795. });
  796. // 得到战绩
  797. proto.getRecordAsync = P.coroutine(function* (playerId) {
  798. let str5 = "shuiguo 得到战绩 "+playerId;
  799. logger.info(str5);////cssj
  800. // 查找玩家
  801. var user = this.users[playerId];
  802. if (!user) {
  803. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  804. }
  805. if (user.chairId == -1) {
  806. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  807. }
  808. return user.table.getRecordAsync(user);
  809. });
  810. // 角色信息
  811. proto.roleInfoAsync = P.coroutine(function* (playerId) {
  812. // console.warn("角色信息+++++++++playerId:%s",playerId);
  813. // let str2 = "shuiguo 角色信息 "+playerId;
  814. // logger.info(str2);////cssj
  815. // 查找玩家
  816. var user = this.users[playerId];
  817. if (!user) {
  818. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  819. }
  820. if (user.chairId == -1) {
  821. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  822. }
  823. // let str1 = "shuiguo 22角色信息 "+playerId;
  824. // logger.info(str1);////cssj
  825. return user.table.roleInfoAsync(playerId,user.chairId);
  826. });
  827. // 得到排行榜
  828. proto.getPaiHangAsync = P.coroutine(function* (playerId) {
  829. // console.warn("得到排行榜+++++++++playerId:%s",playerId);
  830. // let str2 = "shuiguo 得到排行榜 "+playerId;
  831. // logger.info(str2);////cssj
  832. // 查找玩家
  833. var user = this.users[playerId];
  834. if (!user) {
  835. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  836. }
  837. if (user.chairId == -1) {
  838. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  839. }
  840. // let str1 = "shuiguo 22得到排行榜 "+playerId;
  841. // logger.info(str1);////cssj
  842. return user.table.getPaiHangAsync(playerId);
  843. });
  844. // 上传真实姓名支付宝账号
  845. proto.upZFBInfoAsync = P.coroutine(function* (playerId,realName,zfbAcc) {
  846. // 查找玩家
  847. var user = this.users[playerId];
  848. if (!user) {
  849. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  850. }
  851. return user.table.upZFBInfoAsync(playerId,realName,zfbAcc);
  852. });
  853. // 得到玩家任务信息,里面包含任务信息
  854. proto.getTaskInfoAsync = P.coroutine(function* (playerId) {
  855. // console.warn("sgj角色信息+++++++++playerId:%s",playerId);
  856. // let str2 = "shuiguo 角色信息 "+playerId;
  857. // logger.info(str2);////cssj
  858. // 查找玩家
  859. var user = this.users[playerId];
  860. if (!user) {
  861. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  862. }
  863. if (user.chairId == -1) {
  864. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  865. }
  866. // let str1 = "shuiguo 22角色信息 "+playerId;
  867. // logger.info(str1);////cssj
  868. return user.table.getTaskInfoAsync(playerId,user.chairId);
  869. });
  870. // 处理任务
  871. proto.dealRenWuAsync = P.coroutine(function* (playerId,item) {
  872. // console.warn("shuiguo 处理任务+++++++++playerId:%s",playerId,item);
  873. // let str2 = "shuiguo 处理任务 "+playerId;
  874. // logger.info(str2);////cssj
  875. // 查找玩家
  876. var user = this.users[playerId];
  877. if (!user) {
  878. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  879. }
  880. if (user.chairId == -1) {
  881. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  882. }
  883. return user.table.dealRenWuAsync(playerId,user.chairId,item);
  884. });
  885. // 任务领奖
  886. proto.renWuLingjiangAsync = P.coroutine(function* (playerId,item) {
  887. // console.warn("shuiguo 任务领奖+++++++++playerId:%s",playerId,item);
  888. // let str2 = "shuiguo 任务领奖 "+playerId;
  889. // logger.info(str2);////cssj
  890. // 查找玩家
  891. var user = this.users[playerId];
  892. if (!user) {
  893. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  894. }
  895. if (user.chairId == -1) {
  896. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  897. }
  898. return user.table.renWuLingjiangAsync(playerId,user.chairId,item);
  899. });
  900. // 得到玩家已完成的任务列表
  901. proto.getOverTasksAsync = P.coroutine(function* (playerId) {
  902. // console.warn("sgj角色信息+++++++++playerId:%s",playerId);
  903. // let str2 = "shuiguo 角色信息 "+playerId;
  904. // logger.info(str2);////cssj
  905. // 查找玩家
  906. var user = this.users[playerId];
  907. if (!user) {
  908. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  909. }
  910. if (user.chairId == -1) {
  911. return { code: C.FAILD, msg: C.TABLE_NOT_USER };
  912. }
  913. // let str1 = "shuiguo 22角色信息 "+playerId;
  914. // logger.info(str1);////cssj
  915. return user.table.getOverTasksAsync(playerId,user.chairId);
  916. });