'use strict'; var quick = require('quick-pomelo'); var P = quick.Promise; var _ = require('lodash'); var C = require('../../share/constant'); var conf = require('../config/games').csjiang || {}; // var User = require('../csjiang/user'); // var Table = require('../csjiang/table'); var configCommon = require('../../share/configCommon');//////TL++配置相关的公共方法 var logger = quick.logger.getLogger('bisai', __filename); // 金币消耗 const COSTS = conf.crt_costs || {}; // 构造方法 var Controller = function (app) { //目前这种模式仅仅支持同一时间只有一场比赛(即只有一个比赛链接在进行游戏)若要同一时间有多场比赛则需要处理aid this.app = app; this.wantBSUsers = {};//点击了开始比赛或者继续比赛的玩家 this.nowTableList = {};//当前系统创建的用于比赛的空闲桌子(在游戏开始的时候删除) this.usedTableList = {};//当前已被使用的空闲桌子(在广播玩家进入房间的时候添加) this.nowBSInfo = {}; //当前比赛信息 this.notTogthers = [];//不能同桌的玩家信息 // console.warn("???????????????? 构造方法"); }; // 导出方法 module.exports = function (app) { return new Controller(app); }; // 原型对象 var proto = Controller.prototype; //广播比赛人数变化 proto.gbbsrsbh = P.coroutine(function* (agentId,rs) { let broadcastData = { type:0,//匹配中人数变化 data:{ aid:agentId, nowCount: rs, } } return yield this.app.controllers.push.broadcastAsync('broadcast_bs', broadcastData); }); //广播比赛人元加入房间 proto.gbbsryjrfj = P.coroutine(function* (broadcastData) { // console.warn("广播比赛人元加入房间 ",JSON.stringify(broadcastData)); return yield this.app.controllers.push.broadcastAsync('broadcast_bs', broadcastData); }); //判断这桌玩家是否合法 proto.isPlayerHefa = function (notTogthers,res) { // console.warn("判断这桌玩家是否合法 notTogthers "+JSON.stringify(notTogthers)+" res "+JSON.stringify(res)); let indexList = [];//每个玩家在不能同桌分组中的下标,负数表示该玩家不存在于任何不能同桌的分组中 for (var i = 0; i < res.length; i++) { indexList[i] = -1*(i+1);//该玩家不存在于任何不能同桌的分组中 //这么写是为了比如三个人都不在任何分组中的话下标也会不一样可以组成一桌 for (var j = 0; j < notTogthers.length; j++) { if(notTogthers[j].indexOf(res[i]) != -1){ // console.warn("这个玩家在分组中 "+j); indexList[i] = j; break; } } } // console.warn("每个玩家在不能同桌分组中的下标 负数表示该玩家没有被设置不能同桌 "+indexList); if(indexList.length == 2){ if(indexList[0] != indexList[1]) return true; } else if(indexList.length == 3){ if(indexList[0] != indexList[1] && indexList[0] != indexList[2] && indexList[1] != indexList[2]) return true; } return false; } proto.setNotGameTogther = function (_notTogthers) { var notTogthers = _.cloneDeep(_notTogthers);//;this.getNotGameTogther2(); // console.warn("不能同桌的玩家信息fff "+JSON.stringify(notTogthers)); let res = []; let resUid = []; for (var i = 0; i < notTogthers.length; i++) { res[res.length] = notTogthers[i].pidList; resUid[resUid.length] = notTogthers[i].uidList; } // console.warn("不能同桌的玩家信息fff "+JSON.stringify(res)); // console.warn("不能同桌的玩家信息fff "+JSON.stringify(resUid)); this.notTogthers = res; } //得到一个桌子的玩家列表 proto.getATablePlayerList = function (playerCount,aid) { // console.warn("得到一个桌子的玩家列表 ",aid,this.wantBSUsers[aid]); // 打乱匹配中玩家数组,以达到在匹配中随机找人进房间 let ulist = _.shuffle(this.getPlayers(aid)) //下面是在匹配玩家列表中根据不能同桌的玩家限制条件挑选出几个可以一起游戏的人组成一桌 let allPPUsers = this.wantBSUsers[aid];//所有匹配的玩家 let noLimitUsers = [];//没有不能同桌限制的玩家 let limitUsers = [];//有不能同桌限制的玩家 let res = []; var notTogthers = this.notTogthers; //this.getNotGameTogther();//玩家不能同桌的情况 // console.warn("不能同桌情况 "+JSON.stringify(notTogthers)); let ishf = false; if(playerCount == 2){ for (let m = 0; m < allPPUsers.length; ++m) { if(ishf) break; for (let p = m+1; p < allPPUsers.length; ++p) { if(p >= allPPUsers.length) break; res[0] = allPPUsers[m]; res[1] = allPPUsers[p]; if(this.isPlayerHefa(notTogthers,res)) { // console.warn("2人时这桌合法 "+res); ishf = true; break } else { // console.warn("2人时这桌 bu 合法 "+res); res = []; } } } } else if(playerCount == 3){ for (let m = 0; m < allPPUsers.length; ++m) { if(ishf) break; for (let p = m+1; p < allPPUsers.length; ++p) { if(ishf) break; for (let n = p+1; n < allPPUsers.length; ++n) { if(n >= allPPUsers.length) break; res[0] = allPPUsers[m]; res[1] = allPPUsers[p]; res[2] = allPPUsers[n]; if(this.isPlayerHefa(notTogthers,res)) { // console.warn("3人时这桌合法 "+res); ishf = true; break } else { // console.warn("3人时这桌 bu 合法 "+res); res = []; } } } } } if(res.length > 0){ let newList = []; for (var i = 0; i < this.wantBSUsers[aid].length; i++) { if(res.indexOf(this.wantBSUsers[aid][i]) == -1){ newList[newList.length] = this.wantBSUsers[aid][i]; } } this.wantBSUsers[aid] = _.cloneDeep(newList); } // console.warn("组桌子的玩家 "+res+" 目前匹配中玩家列表 "+this.wantBSUsers[aid]); return res; }; //得到该比赛群目前已经点击开始比赛的玩家列表 proto.getPlayers = function (aid) { let res = []; if(aid && this.wantBSUsers[aid]) { res = _.cloneDeep(this.wantBSUsers[aid]); } // console.warn("compateDate 得到列表 aid "+aid + " length "+res.length + " res "+ JSON.stringify(res)); return res; }; ////清空点击了开始比赛的用户列表 proto.clearPlayers = function (aid) { if(aid){ this.wantBSUsers[aid] = []; this.nowTableList[aid] = []; this.usedTableList[aid] = []; return this.gbbsrsbh(aid,this.wantBSUsers[aid].length); } return; }; ////点击了开始比赛的用户列表里增加用户 proto.addPlayer = function (uid,aid) { // console.warn("compateDate 列表里++++用户 uid "+uid+" aid "+aid); if(!aid) return; if (this.wantBSUsers[aid]) { // console.warn("compateDate 列表里++++user 存在 "+JSON.stringify(this.wantBSUsers[aid])) if(this.wantBSUsers[aid].indexOf(uid) == -1){ this.wantBSUsers[aid][this.wantBSUsers[aid].length] = uid; } else{ return; } } else { // console.warn("compateDate 列表里++++user 不存在 "+JSON.stringify(this.wantBSUsers[aid])) this.wantBSUsers[aid] = [uid]; // console.warn("compateDate 列表里++++user 不存在222 "+JSON.stringify(this.wantBSUsers[aid])) } // console.warn("compateDate 列表里++++user 处理完成 this.wantBSUsers "+JSON.stringify(this.wantBSUsers)) // console.warn("compateDate 列表里++++user 处理完成 "+JSON.stringify(this.wantBSUsers[aid])) return this.gbbsrsbh(aid,this.wantBSUsers[aid].length); }; ////点击了开始比赛的用户列表里删除用户 proto.delPlayer = function (uid) { // console.warn("compateDate 列表里----用户 uid "+uid+" wantBSUsers "+JSON.stringify(this.wantBSUsers)); let data = { code: C.OK } let aid = null; let aidCount = 0; _.forEach(this.wantBSUsers, function(value, key) { // console.warn("key "+key+" value "+JSON.stringify(value)); if(value.indexOf(uid) != -1){ aid = key; aidCount++ // return; } }); if(aidCount > 1) { // console.error("compateDate 列表里----出错了 aidCount不正确 "+aidCount+" uid "+uid); let str3 = "compateDate 列表里----出错了 aidCount不正确 "+aidCount+" uid "+uid; logger.warn(str3);////cssj } if(aid){ let index = this.wantBSUsers[aid].indexOf(uid); // console.warn("compateDate 列表里----用户 index "+index+" aid "+aid); if(index != -1){ // console.warn("compateDate 列表里----用户 111 this.wantBSUsers[aid] "+this.wantBSUsers[aid]); let newList = this.wantBSUsers[aid].slice(0, index);////数组列表的前i个元素 // console.warn("compateDate 列表里----用户 222 newList "+newList); newList = newList.concat(this.wantBSUsers[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素 // console.warn("compateDate 列表里----用户 333 newList "+newList); this.wantBSUsers[aid] = newList; // console.warn("compateDate 列表里----用户 444 this.wantBSUsers[aid] "+this.wantBSUsers[aid]); return this.gbbsrsbh(aid,this.wantBSUsers[aid].length); } } return; }; proto.getTables = function (aid) { // console.warn("compateDate 得到桌子列表 长度 "+this.nowTableList.length + JSON.stringify(this.nowTableList)); let res = []; if(!aid) return res; for (var i = 0; i < this.nowTableList[aid].length; i++) { let isky = false;//这个房间是否可用 if(this.usedTableList[aid]){ if(this.usedTableList[aid].indexOf(this.nowTableList[aid][i]) == -1){ isky = true; } } else{ isky = true; } if(isky) res[res.length] = this.nowTableList[aid][i]; } return res; }; ////设置当前比赛信息 proto.setNowBSInfo = function (nowBSInfo,aid) { // console.warn("compateDate 设置当前比赛信息 nowBSInfo "+ JSON.stringify(nowBSInfo)); let bsInfos = []; if(aid){ for (var i = 0; i < nowBSInfo.length; i++) { bsInfos[i] = nowBSInfo[i]; } this.nowBSInfo[aid] = bsInfos; } // console.warn("compateDate 设置当前比赛信息 nowBSInfo222 "+ JSON.stringify(this.nowBSInfo)); } ////增加系统创建出来供比赛使用的桌子 proto.addTable = function (tid,agentId,playerCount) { // console.warn("compateDate 列表里++++桌子 tid "+tid+" agentId "+agentId+" playerCount "+playerCount+" this.nowTableList "+ JSON.stringify(this.nowTableList)); let data = { code: C.OK } // this.nowTableList = {};//当前系统创建的用于比赛的空闲桌子(在游戏开始的时候删除) // this.usedTableList = {};//当前已被使用的空闲桌子(在广播玩家进入房间的时候添加) if(!agentId) return; if (this.nowTableList[agentId]) { // console.warn("compateDate 列表里++++桌子 存在 "+JSON.stringify(this.nowTableList[agentId])) if(this.nowTableList[agentId].indexOf(tid) == -1){ this.nowTableList[agentId][this.nowTableList[agentId].length] = tid; } else{ // data.code = C.FAILD; // return data; } } else { // console.warn("compateDate 列表里++++桌子 不存在 "+JSON.stringify(this.nowTableList[agentId])) this.nowTableList[agentId] = [tid]; // console.warn("compateDate 列表里++++桌子 不存在222 "+JSON.stringify(this.nowTableList[agentId])) } let nowTableList = this.getTables(agentId); // console.warn("目前的空闲房间中 "+nowTableList+" playerCount "+playerCount+" this.nowBSInfo "+ JSON.stringify(this.nowBSInfo)); if(nowTableList.length > 0 && playerCount > 0 && this.wantBSUsers[agentId] && this.wantBSUsers[agentId].length >= playerCount){ //得到需要加入房间的玩家列表 // console.warn("得到需要加入房间的玩家列表111 ",playerCount); let xyjrfjdwj = this.getATablePlayerList(playerCount,agentId); for (var i = 0; i < xyjrfjdwj.length; i++) { this.delPlayer(xyjrfjdwj[i]); } // console.warn("得到需要加入房间的玩家列表222 ",JSON.stringify(xyjrfjdwj)); let broadcastData = { type:1,//广播匹配中玩家加入房间 data:{ aid: agentId, nowCount: this.wantBSUsers[agentId].length, xyjrfjdwj: xyjrfjdwj, tableId: nowTableList[0], } } this.setTableUsed(nowTableList[0],agentId) // console.warn("得到需要加入房间的玩家列表333 ",JSON.stringify(broadcastData)); return this.gbbsryjrfj(broadcastData); } return data; }; ////删除系统创建出来供比赛使用的桌子 proto.delTable = function (tid) { // console.warn("compateDate 列表里----桌子111 tid "+tid+" nowTableList "+this.nowTableList+" usedTableList "+this.usedTableList); let data = { code: C.OK } let aid = null; let aidCount = 0; _.forEach(this.usedTableList, function(value, key) { // console.warn("删除系统创建出来供比赛使用的桌子 key "+key+" value "+JSON.stringify(value)); if(value && value.indexOf(tid) != -1){ aid = key; aidCount++ // return; } }); if(!aid){ _.forEach(this.nowTableList, function(value, key) { // console.warn("删除系统创建出来供比赛使用的桌子 key "+key+" value "+JSON.stringify(value)); if(value && value.indexOf(tid) != -1){ aid = key; aidCount++ // return; } }); } if(aidCount > 1) { // console.error("compateDate 桌子列表里----出错了 aidCount不正确 "+aidCount); let str3 = "compateDate 桌子列表里----出错了 aidCount不正确 "+aidCount; logger.warn(str3);////cssj } if(!aid){ data.code = C.FAILD; return data; } let index = -1; if(this.nowTableList[aid]){ // console.warn("compateDate 桌子列表里--- aidCount "+aidCount+" aid "+aid+" aid "+aid); index = this.nowTableList[aid].indexOf(tid); } if(index != -1){ let newList = this.nowTableList[aid].slice(0, index);////数组列表的前i个元素 newList = newList.concat(this.nowTableList[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素 this.nowTableList[aid] = newList; } let index2 = -1; if(this.usedTableList[aid]) index2 = this.usedTableList[aid].indexOf(tid); if(index2 != -1){ let newList2 = this.usedTableList[aid].slice(0, index2);////数组列表的前i个元素 newList2 = newList2.concat(this.usedTableList[aid].slice(index2 + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素 this.usedTableList[aid] = newList2; } // console.warn("compateDate 列表里----桌子333 tid "+tid+" aid "+aid+" index "+index+" index2 "+index2); if((index == -1 && index2 == -1) || !aid){ data.code = C.FAILD; } // console.warn("compateDate 列表里----桌子333 tid "+tid+" nowTableList "+JSON.stringify(this.nowTableList)+" usedTableList "+JSON.stringify(this.usedTableList)); return data; }; //设置桌子已被使用 proto.setTableUsed = function (tid,aid) { // console.warn("compateDate 列表里设置桌子已被使用 tid "+tid); let data = { code: C.OK } let index = -1; if(this.nowTableList[aid]) index = this.nowTableList[aid].indexOf(tid); if(index != -1){ let newList = this.nowTableList[aid].slice(0, index);////数组列表的前i个元素 newList = newList.concat(this.nowTableList[aid].slice(index + 1));////给新数组拼接上原数组第 i+1 个以后的全部数组元素 这句是删除原数组第i个元素 this.nowTableList[aid] = newList; if(!this.usedTableList[aid]) this.usedTableList[aid] = []; if(this.usedTableList[aid].indexOf(tid) == -1){ this.usedTableList[aid][this.usedTableList[aid].length] = tid; } } else{ data.code = C.FAILD; } return data; }; //得到空闲桌子数量 proto.getKXTables = function (aid) { if(!this.nowTableList[aid]) this.nowTableList[aid] = []; // console.warn("compateDate 得到空闲桌子数量 aid "+aid+" nowTableList[aid] "+JSON.stringify(this.nowTableList[aid])); return this.nowTableList[aid]; };