var roomMgr = require("./roommgr");
var userMgr = require("./usermgr");
var db = require("../utils/db");
var crypto = require("../utils/crypto");
var games = {};
var gamesIdBase = 0;
var game_timer = null;
var gameSeatsOfUsers = {};
var ACTION_HASCATTLE = 20;
var ACTION_NOCATTLE = 21;
var WU_NIU = 0; //没牛
var NIU_YI = 1; //牛1
var NIU_ER = 2; //牛2
var NIU_SAN = 3;//牛3
var NIU_SI = 4; //牛4
var NIU_WU = 5; //牛5
var NIU_LIU = 6; //牛6
var NIU_QI = 7; //牛7
var NIU_BA = 8;//牛8
var NIU_JIU = 9; //牛9
var NIU_NIU = 10; //牛牛
//var SI_HUA_CATTLE = 11;//四花牛
var WU_HUA_CATTLE = 12; //五花牛
var Five_CALVES = 13; //五小牛
var BOMB_CATTLE = 14; //炸弹
var TONG_HUA_SHUN = 15;//同花顺哦
//牛牛(1副牌)
function getMJType(id){
//47,7,38,28,9 J 10 2 5
if(id >= 0 && id < 13){
//3,4,5,6,7,8,9,10,J,Q,K,A,2 方块
return 0;
}
else if(id >= 13 && id < 26){
//3,4,5,6,7,8,9,10,J,Q,K,A,2 梅花
return 1;
}
else if(id >= 26 && id < 39){
//3,4,5,6,7,8,9,10,J,Q,K,A,2 红桃
return 2;
}
else if(id >= 39 && id < 52){
//3,4,5,6,7,8,9,10,J,Q,K,A,2 黑桃
return 3;
}
}
//洗牌(从最后开始固定,与随机其前的牌交换)
function shuffle(game) {
var mahjongs = game.mahjongs;
var index = 0;
for(var i = 0 ; i < 52 ; i++){
mahjongs[index] = i;
index ++;
}
for(var i = 0; i < mahjongs.length; ++i){
var lastIndex = mahjongs.length - 1 - i;
var index = Math.floor(Math.random() * lastIndex);
//交换
var t = mahjongs[index];
mahjongs[index] = mahjongs[lastIndex];
mahjongs[lastIndex] = t;
}
}
//派牌
function mopai(game,seatIndex){
if(game.currentIndex == game.mahjongs.length){
return -1;
}
var data = game.gameSeats[seatIndex];
var mahjongs = data.holds;
var pai = game.mahjongs[game.currentIndex];
mahjongs.push(pai);
//统计牌的数目 ,用于快速判定(空间换时间)
var c = data.countMap[pai];
if(c == null) {
c = 0;
}
data.countMap[pai] = c + 1;
game.currentIndex ++;
return pai;
}
//派牌过程
function deal(game){
//强制清0
game.currentIndex = 0;
var renshu = game.roomInfo.renshu;
//每人17张牌,还有3张另放
var seatIndex = 0;
for(var i = 0; i < (renshu * 4); ++i){
//如果没有人则不发
if(game.gameSeats[seatIndex].userId == 0){
seatIndex ++;
seatIndex %= renshu;
continue;
}
var mahjongs = game.gameSeats[seatIndex].holds;
if(mahjongs == null){
mahjongs = [];
game.gameSeats[seatIndex].holds = mahjongs;
}
mopai(game,seatIndex);
seatIndex ++;
seatIndex %= renshu;
}
}
//派最后一张牌
function dealLastOne(game){
//强制清0
game.currentIndex = 0;
var renshu = game.roomInfo.renshu;
//每人17张牌,还有3张另放
var seatIndex = game.button;
for(var i = 0; i < (renshu * 1); ++i){
var mahjongs = game.gameSeats[seatIndex].holds;
if(mahjongs == null){
mahjongs = [];
game.gameSeats[seatIndex].holds = mahjongs;
}
mopai(game,seatIndex);
seatIndex ++;
seatIndex %= renshu;
}
}
function getSeatIndex(userId){
var seatIndex = roomMgr.getUserSeat(userId);
if(seatIndex == null){
return null;
}
return seatIndex;
}
function getGameByUserID(userId){
var roomId = roomMgr.getUserRoom(userId);
if(roomId == null){
return null;
}
var game = games[roomId];
return game;
}
//出牌人转移
function moveToNextUser(game){
var renshu = game.roomInfo.renshu;
//找到下一个没有和牌的玩家
game.turn ++;
game.turn %= renshu;
var ddd = game.gameSeats[game.turn];
ddd.folds = [];//每次出牌时把它的牌组清除
return;
}
function isSameType(type,arr){
for(var i = 0; i < arr.length; ++i){
var t = getMJType(arr[i]);
if(type != -1 && type != t){
return false;
}
type = t;
}
return true;
}
function computeFanScore(game,fan){
if(fan > game.conf.maxFan){
fan = game.conf.maxFan;
}
return (1 << fan) * game.conf.baseScore;
}
//获取数组出现次数
function getCount(mjArr,inStr){
var count = 0;
for(var i in mjArr){
if(mjArr[i] == inStr){
count ++;
}
}
return count;
}
//1表示庄大,2表示闲大
function getMax(dd,sd){
var t1 = getMaxPai(dd.holds);
var t2 = getMaxPai(sd.holds);
if(nTp[t1] > nTp[t2]){
return true;
}
else if(nTp[t1] < nTp[t2]){
return false;
}
else{
//花色判断
if(t1 > t2){
return true;
}
else{
return false;
}
}
}
function getMaxPai(tmpArr){
var max = tmpArr[0];
for(var i = 1; i < tmpArr.length; i++){
if(nTp[max] == 13){
break;
}
var t1 = nTp[max];
var t2 =nTp[tmpArr[i]];
if(t2 > t1){
max = tmpArr[i];
}
}
return max;
}
//清算结果
function calculateResult(game,roomInfo){
var baseScore = game.conf.baseScore;
baseScore = baseScore * game.numOfZhuang;
var dd = game.gameSeats[game.button];
var renshu = roomInfo.renshu;
for(var i = 0; i < renshu; ++i){
if(i == game.button){
continue;
}
var sd = game.gameSeats[i];
if(sd.userId == 0){
continue;
}
if(dd.type > sd.type){//庄赢
var fen = sd.fen * dd.fan * baseScore;
dd.score = dd.score + fen;
sd.score = sd.score - fen;
}
else if(dd.type < sd.type){//闲赢
var fen = sd.fen * sd.fan * baseScore;
dd.score = dd.score - fen;
sd.score = sd.score + fen;
}
else{//相等
//比最大的牌
var flag = getMax(dd,sd);
if(flag){ // 庄大
var fen = sd.fen * dd.fan * baseScore;
dd.score = dd.score + fen;
sd.score = sd.score - fen;
}
else{ //闲大
var fen = sd.fen * sd.fan * baseScore;
dd.score = dd.score - fen;
sd.score = sd.score + fen;
}
}
console.log("sd.score = " + sd.score);
}
console.log("last dd.score = " + dd.score);
}
function doGameOver(game,userId,forceEnd){
//var roomId = roomMgr.getUserRoom(userId);
var roomId = game.roomInfo.id;
if(roomId == null){
return;
}
// var roomInfo = roomMgr.getRoom(roomId);
var roomInfo = game.roomInfo;
if(roomInfo == null){
return;
}
var renshu = roomInfo.renshu;
var results = [];
var dbresult = new Array(renshu);
for(var n = 0; n < renshu; n++){
dbresult[n] = 0;
}
var fnNoticeResult = function(isEnd){
var endinfo = null;
if(isEnd){
endinfo = [];
for(var i = 0; i < roomInfo.renshu; ++i){
var rs = roomInfo.seats[i];
endinfo.push({
numzimo:rs.numZiMo,
numjiepao:rs.numJiePao,
numdianpao:rs.numDianPao,
numangang:rs.numAnGang,
numminggang:rs.numMingGang,
numchadajiao:rs.numChaJiao,
});
//最终结束时存入数据库,作为用户最终结果
}
}
//这边要判断一下用户的余额,没了直接清出去
// userMgr.broacastInRoom('game_over_push',{results:results,endinfo:endinfo},userId,true);
var baseScore = roomInfo.conf.baseScore;
for(var i = 0; i < game.gameSeats.length; ++i){
var s = game.gameSeats[i];
if(s.userId <= 0){
continue;
}
var currentRoomId = roomMgr.getUserRoom(s.userId);
if(currentRoomId != null && currentRoomId == game.roomId){
userMgr.sendMsg(s.userId,'game_over_push',{results:results});
userMgr.sendMsg(s.userId,'game_dao_ji_shi_push',{djs:game.djs});
}
//钱不够或者下线之后,则直接踢出房间
if(s.coins < (baseScore * 50) || (!userMgr.isOnline(s.userId))){
userMgr.broacastInRoom('exit_notify_push',s.userId,s.userId,false);
roomMgr.exitRoom(s.userId);
userMgr.del(s.userId);
}
}
for(var i = 0; i < roomInfo.renshu; ++i){
var t = roomInfo.seats[i];
}
//userMgr.broacastInRoom('game_over_push',{results:results},userId,true);
//userMgr.broacastInRoom('game_dao_ji_shi_push',{djs:game.djs},userId,true);
// 6秒之后还没有做准备操作,则自动准备并开始
game_timer = setTimeout(function(){
// var roomId = game.roomInfo.id;
//var roomId = roomMgr.getUserRoom(userId);
if(roomId != null){
var roomInfo = roomMgr.getRoom(roomId);
if(roomInfo != null){
var game = games[roomId];
//游戏没有开始,如果开始了自动会过滤哦
if(game == null){
var numOfReady = 0;
for(var i = 0; i < roomInfo.seats.length; i++){
var s = roomInfo.seats[i];
if(s.userId == 0){