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