shuiguo.js 38 KB

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