bisai.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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').csjiang || {};
  7. // var User = require('../csjiang/user');
  8. // var Table = require('../csjiang/table');
  9. var configCommon = require('../../share/configCommon');//////TL++配置相关的公共方法
  10. var logger = quick.logger.getLogger('bisai', __filename);
  11. // 金币消耗
  12. const COSTS = conf.crt_costs || {};
  13. // 构造方法
  14. var Controller = function (app) {
  15. //目前这种模式仅仅支持同一时间只有一场比赛(即只有一个比赛链接在进行游戏)若要同一时间有多场比赛则需要处理aid
  16. this.app = app;
  17. this.wantBSUsers = {};//点击了开始比赛或者继续比赛的玩家
  18. this.nowTableList = {};//当前系统创建的用于比赛的空闲桌子(在游戏开始的时候删除)
  19. this.usedTableList = {};//当前已被使用的空闲桌子(在广播玩家进入房间的时候添加)
  20. this.nowBSInfo = {}; //当前比赛信息
  21. this.notTogthers = [];//不能同桌的玩家信息
  22. // console.warn("???????????????? 构造方法");
  23. };
  24. // 导出方法
  25. module.exports = function (app) {
  26. return new Controller(app);
  27. };
  28. // 原型对象
  29. var proto = Controller.prototype;
  30. //广播比赛人数变化
  31. proto.gbbsrsbh = P.coroutine(function* (agentId,rs) {
  32. let broadcastData = {
  33. type:0,//匹配中人数变化
  34. data:{
  35. aid:agentId,
  36. nowCount: rs,
  37. }
  38. }
  39. return yield this.app.controllers.push.broadcastAsync('broadcast_bs', broadcastData);
  40. });
  41. //广播比赛人元加入房间
  42. proto.gbbsryjrfj = P.coroutine(function* (broadcastData) {
  43. // console.warn("广播比赛人元加入房间 ",JSON.stringify(broadcastData));
  44. return yield this.app.controllers.push.broadcastAsync('broadcast_bs', broadcastData);
  45. });
  46. //判断这桌玩家是否合法
  47. proto.isPlayerHefa = function (notTogthers,res) {
  48. // console.warn("判断这桌玩家是否合法 notTogthers "+JSON.stringify(notTogthers)+" res "+JSON.stringify(res));
  49. let indexList = [];//每个玩家在不能同桌分组中的下标,负数表示该玩家不存在于任何不能同桌的分组中
  50. for (var i = 0; i < res.length; i++) {
  51. indexList[i] = -1*(i+1);//该玩家不存在于任何不能同桌的分组中
  52. //这么写是为了比如三个人都不在任何分组中的话下标也会不一样可以组成一桌
  53. for (var j = 0; j < notTogthers.length; j++) {
  54. if(notTogthers[j].indexOf(res[i]) != -1){
  55. // console.warn("这个玩家在分组中 "+j);
  56. indexList[i] = j;
  57. break;
  58. }
  59. }
  60. }
  61. // console.warn("每个玩家在不能同桌分组中的下标 负数表示该玩家没有被设置不能同桌 "+indexList);
  62. if(indexList.length == 2){
  63. if(indexList[0] != indexList[1]) return true;
  64. }
  65. else if(indexList.length == 3){
  66. if(indexList[0] != indexList[1] && indexList[0] != indexList[2] && indexList[1] != indexList[2]) return true;
  67. }
  68. return false;
  69. }
  70. proto.setNotGameTogther = function (_notTogthers) {
  71. var notTogthers = _.cloneDeep(_notTogthers);//;this.getNotGameTogther2();
  72. // console.warn("不能同桌的玩家信息fff "+JSON.stringify(notTogthers));
  73. let res = [];
  74. let resUid = [];
  75. for (var i = 0; i < notTogthers.length; i++) {
  76. res[res.length] = notTogthers[i].pidList;
  77. resUid[resUid.length] = notTogthers[i].uidList;
  78. }
  79. // console.warn("不能同桌的玩家信息fff "+JSON.stringify(res));
  80. // console.warn("不能同桌的玩家信息fff "+JSON.stringify(resUid));
  81. this.notTogthers = res;
  82. }
  83. //得到一个桌子的玩家列表
  84. proto.getATablePlayerList = function (playerCount,aid) {
  85. // console.warn("得到一个桌子的玩家列表 ",aid,this.wantBSUsers[aid]);
  86. // 打乱匹配中玩家数组,以达到在匹配中随机找人进房间 let ulist = _.shuffle(this.getPlayers(aid))
  87. //下面是在匹配玩家列表中根据不能同桌的玩家限制条件挑选出几个可以一起游戏的人组成一桌
  88. let allPPUsers = this.wantBSUsers[aid];//所有匹配的玩家
  89. let noLimitUsers = [];//没有不能同桌限制的玩家
  90. let limitUsers = [];//有不能同桌限制的玩家
  91. let res = [];
  92. var notTogthers = this.notTogthers; //this.getNotGameTogther();//玩家不能同桌的情况
  93. // console.warn("不能同桌情况 "+JSON.stringify(notTogthers));
  94. let ishf = false;
  95. if(playerCount == 2){
  96. for (let m = 0; m < allPPUsers.length; ++m) {
  97. if(ishf) break;
  98. for (let p = m+1; p < allPPUsers.length; ++p) {
  99. if(p >= allPPUsers.length) break;
  100. res[0] = allPPUsers[m];
  101. res[1] = allPPUsers[p];
  102. if(this.isPlayerHefa(notTogthers,res)) {
  103. // console.warn("2人时这桌合法 "+res);
  104. ishf = true;
  105. break
  106. }
  107. else {
  108. // console.warn("2人时这桌 bu 合法 "+res);
  109. res = [];
  110. }
  111. }
  112. }
  113. }
  114. else if(playerCount == 3){
  115. for (let m = 0; m < allPPUsers.length; ++m) {
  116. if(ishf) break;
  117. for (let p = m+1; p < allPPUsers.length; ++p) {
  118. if(ishf) break;
  119. for (let n = p+1; n < allPPUsers.length; ++n) {
  120. if(n >= allPPUsers.length) break;
  121. res[0] = allPPUsers[m];
  122. res[1] = allPPUsers[p];
  123. res[2] = allPPUsers[n];
  124. if(this.isPlayerHefa(notTogthers,res)) {
  125. // console.warn("3人时这桌合法 "+res);
  126. ishf = true;
  127. break
  128. }
  129. else {
  130. // console.warn("3人时这桌 bu 合法 "+res);
  131. res = [];
  132. }
  133. }
  134. }
  135. }
  136. }
  137. if(res.length > 0){
  138. let newList = [];
  139. for (var i = 0; i < this.wantBSUsers[aid].length; i++) {
  140. if(res.indexOf(this.wantBSUsers[aid][i]) == -1){
  141. newList[newList.length] = this.wantBSUsers[aid][i];
  142. }
  143. }
  144. this.wantBSUsers[aid] = _.cloneDeep(newList);
  145. }
  146. // console.warn("组桌子的玩家 "+res+" 目前匹配中玩家列表 "+this.wantBSUsers[aid]);
  147. return res;
  148. };
  149. //得到该比赛群目前已经点击开始比赛的玩家列表
  150. proto.getPlayers = function (aid) {
  151. let res = [];
  152. if(aid && this.wantBSUsers[aid]) {
  153. res = _.cloneDeep(this.wantBSUsers[aid]);
  154. }
  155. // console.warn("compateDate 得到列表 aid "+aid + " length "+res.length + " res "+ JSON.stringify(res));
  156. return res;
  157. };
  158. ////清空点击了开始比赛的用户列表
  159. proto.clearPlayers = function (aid) {
  160. if(aid){
  161. this.wantBSUsers[aid] = [];
  162. this.nowTableList[aid] = [];
  163. this.usedTableList[aid] = [];
  164. return this.gbbsrsbh(aid,this.wantBSUsers[aid].length);
  165. }
  166. return;
  167. };
  168. ////点击了开始比赛的用户列表里增加用户
  169. proto.addPlayer = function (uid,aid) {
  170. // console.warn("compateDate 列表里++++用户 uid "+uid+" aid "+aid);
  171. if(!aid) return;
  172. if (this.wantBSUsers[aid]) {
  173. // console.warn("compateDate 列表里++++user 存在 "+JSON.stringify(this.wantBSUsers[aid]))
  174. if(this.wantBSUsers[aid].indexOf(uid) == -1){
  175. this.wantBSUsers[aid][this.wantBSUsers[aid].length] = uid;
  176. }
  177. else{
  178. return;
  179. }
  180. }
  181. else {
  182. // console.warn("compateDate 列表里++++user 不存在 "+JSON.stringify(this.wantBSUsers[aid]))
  183. this.wantBSUsers[aid] = [uid];
  184. // console.warn("compateDate 列表里++++user 不存在222 "+JSON.stringify(this.wantBSUsers[aid]))
  185. }
  186. // console.warn("compateDate 列表里++++user 处理完成 this.wantBSUsers "+JSON.stringify(this.wantBSUsers))
  187. // console.warn("compateDate 列表里++++user 处理完成 "+JSON.stringify(this.wantBSUsers[aid]))
  188. return this.gbbsrsbh(aid,this.wantBSUsers[aid].length);
  189. };
  190. ////点击了开始比赛的用户列表里删除用户
  191. proto.delPlayer = function (uid) {
  192. // console.warn("compateDate 列表里----用户 uid "+uid+" wantBSUsers "+JSON.stringify(this.wantBSUsers));
  193. let data = {
  194. code: C.OK
  195. }
  196. let aid = null;
  197. let aidCount = 0;
  198. _.forEach(this.wantBSUsers, function(value, key) {
  199. // console.warn("key "+key+" value "+JSON.stringify(value));
  200. if(value.indexOf(uid) != -1){
  201. aid = key;
  202. aidCount++
  203. // return;
  204. }
  205. });
  206. if(aidCount > 1) {
  207. // console.error("compateDate 列表里----出错了 aidCount不正确 "+aidCount+" uid "+uid);
  208. let str3 = "compateDate 列表里----出错了 aidCount不正确 "+aidCount+" uid "+uid;
  209. logger.warn(str3);////cssj
  210. }
  211. if(aid){
  212. let index = this.wantBSUsers[aid].indexOf(uid);
  213. // console.warn("compateDate 列表里----用户 index "+index+" aid "+aid);
  214. if(index != -1){
  215. // console.warn("compateDate 列表里----用户 111 this.wantBSUsers[aid] "+this.wantBSUsers[aid]);
  216. let newList = this.wantBSUsers[aid].slice(0, index);////数组列表的前i个元素
  217. // console.warn("compateDate 列表里----用户 222 newList "+newList);
  218. newList = newList.concat(this.wantBSUsers[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素
  219. // console.warn("compateDate 列表里----用户 333 newList "+newList);
  220. this.wantBSUsers[aid] = newList;
  221. // console.warn("compateDate 列表里----用户 444 this.wantBSUsers[aid] "+this.wantBSUsers[aid]);
  222. return this.gbbsrsbh(aid,this.wantBSUsers[aid].length);
  223. }
  224. }
  225. return;
  226. };
  227. proto.getTables = function (aid) {
  228. // console.warn("compateDate 得到桌子列表 长度 "+this.nowTableList.length + JSON.stringify(this.nowTableList));
  229. let res = [];
  230. if(!aid) return res;
  231. for (var i = 0; i < this.nowTableList[aid].length; i++) {
  232. let isky = false;//这个房间是否可用
  233. if(this.usedTableList[aid]){
  234. if(this.usedTableList[aid].indexOf(this.nowTableList[aid][i]) == -1){
  235. isky = true;
  236. }
  237. }
  238. else{
  239. isky = true;
  240. }
  241. if(isky) res[res.length] = this.nowTableList[aid][i];
  242. }
  243. return res;
  244. };
  245. ////设置当前比赛信息
  246. proto.setNowBSInfo = function (nowBSInfo,aid) {
  247. // console.warn("compateDate 设置当前比赛信息 nowBSInfo "+ JSON.stringify(nowBSInfo));
  248. let bsInfos = [];
  249. if(aid){
  250. for (var i = 0; i < nowBSInfo.length; i++) {
  251. bsInfos[i] = nowBSInfo[i];
  252. }
  253. this.nowBSInfo[aid] = bsInfos;
  254. }
  255. // console.warn("compateDate 设置当前比赛信息 nowBSInfo222 "+ JSON.stringify(this.nowBSInfo));
  256. }
  257. ////增加系统创建出来供比赛使用的桌子
  258. proto.addTable = function (tid,agentId,playerCount) {
  259. // console.warn("compateDate 列表里++++桌子 tid "+tid+" agentId "+agentId+" playerCount "+playerCount+" this.nowTableList "+ JSON.stringify(this.nowTableList));
  260. let data = {
  261. code: C.OK
  262. }
  263. // this.nowTableList = {};//当前系统创建的用于比赛的空闲桌子(在游戏开始的时候删除)
  264. // this.usedTableList = {};//当前已被使用的空闲桌子(在广播玩家进入房间的时候添加)
  265. if(!agentId) return;
  266. if (this.nowTableList[agentId]) {
  267. // console.warn("compateDate 列表里++++桌子 存在 "+JSON.stringify(this.nowTableList[agentId]))
  268. if(this.nowTableList[agentId].indexOf(tid) == -1){
  269. this.nowTableList[agentId][this.nowTableList[agentId].length] = tid;
  270. }
  271. else{
  272. // data.code = C.FAILD;
  273. // return data;
  274. }
  275. }
  276. else {
  277. // console.warn("compateDate 列表里++++桌子 不存在 "+JSON.stringify(this.nowTableList[agentId]))
  278. this.nowTableList[agentId] = [tid];
  279. // console.warn("compateDate 列表里++++桌子 不存在222 "+JSON.stringify(this.nowTableList[agentId]))
  280. }
  281. let nowTableList = this.getTables(agentId);
  282. // console.warn("目前的空闲房间中 "+nowTableList+" playerCount "+playerCount+" this.nowBSInfo "+ JSON.stringify(this.nowBSInfo));
  283. if(nowTableList.length > 0 && playerCount > 0 && this.wantBSUsers[agentId] && this.wantBSUsers[agentId].length >= playerCount){
  284. //得到需要加入房间的玩家列表
  285. // console.warn("得到需要加入房间的玩家列表111 ",playerCount);
  286. let xyjrfjdwj = this.getATablePlayerList(playerCount,agentId);
  287. for (var i = 0; i < xyjrfjdwj.length; i++) {
  288. this.delPlayer(xyjrfjdwj[i]);
  289. }
  290. // console.warn("得到需要加入房间的玩家列表222 ",JSON.stringify(xyjrfjdwj));
  291. let broadcastData = {
  292. type:1,//广播匹配中玩家加入房间
  293. data:{
  294. aid: agentId,
  295. nowCount: this.wantBSUsers[agentId].length,
  296. xyjrfjdwj: xyjrfjdwj,
  297. tableId: nowTableList[0],
  298. }
  299. }
  300. this.setTableUsed(nowTableList[0],agentId)
  301. // console.warn("得到需要加入房间的玩家列表333 ",JSON.stringify(broadcastData));
  302. return this.gbbsryjrfj(broadcastData);
  303. }
  304. return data;
  305. };
  306. ////删除系统创建出来供比赛使用的桌子
  307. proto.delTable = function (tid) {
  308. // console.warn("compateDate 列表里----桌子111 tid "+tid+" nowTableList "+this.nowTableList+" usedTableList "+this.usedTableList);
  309. let data = {
  310. code: C.OK
  311. }
  312. let aid = null;
  313. let aidCount = 0;
  314. _.forEach(this.usedTableList, function(value, key) {
  315. // console.warn("删除系统创建出来供比赛使用的桌子 key "+key+" value "+JSON.stringify(value));
  316. if(value && value.indexOf(tid) != -1){
  317. aid = key;
  318. aidCount++
  319. // return;
  320. }
  321. });
  322. if(!aid){
  323. _.forEach(this.nowTableList, function(value, key) {
  324. // console.warn("删除系统创建出来供比赛使用的桌子 key "+key+" value "+JSON.stringify(value));
  325. if(value && value.indexOf(tid) != -1){
  326. aid = key;
  327. aidCount++
  328. // return;
  329. }
  330. });
  331. }
  332. if(aidCount > 1) {
  333. // console.error("compateDate 桌子列表里----出错了 aidCount不正确 "+aidCount);
  334. let str3 = "compateDate 桌子列表里----出错了 aidCount不正确 "+aidCount;
  335. logger.warn(str3);////cssj
  336. }
  337. if(!aid){
  338. data.code = C.FAILD;
  339. return data;
  340. }
  341. let index = -1;
  342. if(this.nowTableList[aid]){
  343. // console.warn("compateDate 桌子列表里--- aidCount "+aidCount+" aid "+aid+" aid "+aid);
  344. index = this.nowTableList[aid].indexOf(tid);
  345. }
  346. if(index != -1){
  347. let newList = this.nowTableList[aid].slice(0, index);////数组列表的前i个元素
  348. newList = newList.concat(this.nowTableList[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素
  349. this.nowTableList[aid] = newList;
  350. }
  351. let index2 = -1;
  352. if(this.usedTableList[aid]) index2 = this.usedTableList[aid].indexOf(tid);
  353. if(index2 != -1){
  354. let newList2 = this.usedTableList[aid].slice(0, index2);////数组列表的前i个元素
  355. newList2 = newList2.concat(this.usedTableList[aid].slice(index2 + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素
  356. this.usedTableList[aid] = newList2;
  357. }
  358. // console.warn("compateDate 列表里----桌子333 tid "+tid+" aid "+aid+" index "+index+" index2 "+index2);
  359. if((index == -1 && index2 == -1) || !aid){
  360. data.code = C.FAILD;
  361. }
  362. // console.warn("compateDate 列表里----桌子333 tid "+tid+" nowTableList "+JSON.stringify(this.nowTableList)+" usedTableList "+JSON.stringify(this.usedTableList));
  363. return data;
  364. };
  365. //设置桌子已被使用
  366. proto.setTableUsed = function (tid,aid) {
  367. // console.warn("compateDate 列表里设置桌子已被使用 tid "+tid);
  368. let data = {
  369. code: C.OK
  370. }
  371. let index = -1;
  372. if(this.nowTableList[aid]) index = this.nowTableList[aid].indexOf(tid);
  373. if(index != -1){
  374. let newList = this.nowTableList[aid].slice(0, index);////数组列表的前i个元素
  375. newList = newList.concat(this.nowTableList[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素
  376. this.nowTableList[aid] = newList;
  377. if(!this.usedTableList[aid]) this.usedTableList[aid] = [];
  378. if(this.usedTableList[aid].indexOf(tid) == -1){
  379. this.usedTableList[aid][this.usedTableList[aid].length] = tid;
  380. }
  381. }
  382. else{
  383. data.code = C.FAILD;
  384. }
  385. return data;
  386. };
  387. //得到空闲桌子数量
  388. proto.getKXTables = function (aid) {
  389. if(!this.nowTableList[aid]) this.nowTableList[aid] = [];
  390. // console.warn("compateDate 得到空闲桌子数量 aid "+aid+" nowTableList[aid] "+JSON.stringify(this.nowTableList[aid]));
  391. return this.nowTableList[aid];
  392. };