--- --- Created by 谌建军. --- DateTime: 2017/12/13 11:28 --- local MuShi_Protocol = import(".MuShi_Protocol") local MuShi_GameEvent = import(".MuShi_GameEvent") local MuShi_CardType = import(".MuShi_CardType") local MuShi_PutError = { "必须先出最小的牌", "出牌不符合规定牌型 ", "下家报单,请出最大的牌 ", "炸弹不能拆", "单鬼必须补牌" } local MuShi_GameController = {} local M = MuShi_GameController function M.new() setmetatable(M, {__index = GameController}) local self = setmetatable({}, {__index = M}) self:init("木虱") self.class = "MuShi_GameController" return self end function M:init(name) GameController.init(self, name) self.guiCard = nil self:RegisterEvt() end -- 事件注册 function M:RegisterEvt() self._eventmap[MuShi_Protocol.MuShi_Ming_Card] = self.OnMingCard self._eventmap[MuShi_Protocol.MuShi_Init_Card] = self.OnInitCard -- self._eventmap[MuShi_Protocol.MuShi_Options] = self.OnOptions -- self._eventmap[MuShi_Protocol.MuShi_Index_Move] = self.OnIndexMove --self._eventmap[MuShi_Protocol.MuShi_Play_Succ] = self.OnPlaySucc self._eventmap[MuShi_Protocol.MuShi_Put_Error] = self.OnPutError self._eventmap[MuShi_Protocol.MuShi_Pass_Succ] = self.OnPassSucc self._eventmap[MuShi_Protocol.MuShi_Result] = self.OnResult self._eventmap[MuShi_Protocol.MuShi_Bomb_Score] = self.OnBombScore self._eventmap[MuShi_Protocol.MuShi_Piao_Tip] = self.OnPiaoTip self._eventmap[MuShi_Protocol.MuShi_Piao_Action] = self.OnPiaoAction self._eventmap[MuShi_Protocol.MuShi_ConfirmToNextGameSucc] = self.OnConfrimToNextGameSucc --self._eventmap[MuShi_Protocol.MuShi_Oener] = self.Oener -- self._eventmap[Protocol.GAME_EVT_PLAYER_JOIN] = self.OnEventPlayerEnter self._eventmap[MuShi_Protocol.PT_GAMETUOGUAN] = self.Game_TuoGuan self._eventmap[MuShi_Protocol.MuShi_Gui] = self.GuiPai self._eventmap[MuShi_Protocol.GAME_EVT_USER_START_GAME_RSP] = self.TiQianStar self._eventmap[MuShi_Protocol.GAME_EVT_START_QIANGZHUANG] = self.StarQiangZhuang self._eventmap[MuShi_Protocol.GAME_EVT_QIANGZHUANG_RSP] = self.OtherQiangZhuang self._eventmap[MuShi_Protocol.GAME_EVT_ZHUANGJIA_INFO] = self.DingZhuang self._eventmap[MuShi_Protocol.GAME_EVT_START_XIAZHU] = self.StarBet self._eventmap[MuShi_Protocol.GAME_EVT_XIAZHU_RSP] = self.OtherBet self._eventmap[MuShi_Protocol.GAME_EVT_CUOPAI_FINISH_RSP] = self.FinishCuoPai -- self._eventmap[Protocol.GAME_EVT_READY] = self.OnExTendPlayerReady end function M:OnExTendPlayerReady(evt_data) self._cacheEvent:Enqueue( function() local pid = evt_data['aid'] local p = self._room:GetPlayerById(pid) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnReady, p.seat) end ) end function M:SendQiangZhuang(index) local _data = {} _data["index"] = index --printlog("发送抢庄消息==========",index) local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.GAME_EVT_QIANGZHUANG_REQ, _data) end function M:SendBet(index) local _data = {} _data["index"] = index --printlog("发送下注消息==========",index) local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.GAME_EVT_XIAZHU_REQ, _data) end function M:SendStar() local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.GAME_EVT_USER_START_GAME_REQ) end function M:AgreeStar(isAgree) local _data = {} _data["agree"] = isAgree local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.GAME_EVT_USER_AGREE_START_GAME_REQ, _data) end function M:SendBuPai(bupai) local _data = {} _data["bupai"] = bupai local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_Send_Piao, _data) end function M:SendCard(cards,currentCard) local _data = {} _data["card"] = cards _data["all_card"] = currentCard local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_Send_Card, _data) end function M:SendPiao(piao) local _data = {} _data["id"] = piao local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_Send_Piao, _data) end function M:SendPass() local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_Send_Guo) end function M:SendInitCardEnd() local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_Init_Card_End) end function M:ConformToNextGame() local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.MuShi_ConfirmToNextGame) end function M:StarQiangZhuang() printlog("开始抢庄====================================") self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnQiangZhuang) end ) end function M:FinishCuoPai(evt_data) print("上报搓牌返回==================") local seat = evt_data["seat"] local card = evt_data["card"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnFinishCuoPai,seat,card) end ) end function M:OtherQiangZhuang(evt_data) printlog("其它玩家抢庄信息====================================") pt(evt_data) local seat = evt_data["seat"] local index = evt_data["index"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnOtherQiangZhuang,seat,index) end ) end function M:DingZhuang(evt_data) printlog("确定庄家信息 ====================================") local seat = evt_data["seat"] local index = evt_data["index"] local score = evt_data["score"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnZhuang, seat,index,score) end ) end function M:StarBet(evt_data) printlog("开始下注 ====================================",evt_data["seat"]) local seat = evt_data["seat"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnStarBet,seat) end ) end function M:OtherBet(evt_data) printlog("其它玩家下注信息 ====================================") local seat = evt_data["seat"] local index = evt_data["index"] local score = evt_data["mul"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnBet, seat,index,score) end ) end function M:TiQianStar(evt_data) -- printlog("发起提前开始返回1111====================================") local seat = evt_data["seat"] local agree = evt_data["agree"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnTiQian, seat,agree) end ) end function M:OnAgreeGame(evt_data) --printlog("同意游戏开始====================================") local seat = evt_data["seat"] local agree = evt_data["agree"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnAgree, seat,agree) end ) end function M:GuiPai(evt_data) --printlog("鬼牌====================================") local card = evt_data["guiCard"] self.guiCard = card self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnGuiPai, card) end ) end function M:OnMingCard(evt_data) if ViewManager.GetCurrenView().dview_class == LobbyView then self:ReturnToRoom() return end local card = evt_data["mingpai"] self._cacheEvent:Enqueue( function() self._room.ming_card = card DispatchEvent(self._dispatcher, MuShi_GameEvent.OnMingCard, card) end ) end function M:OnInitCard(evt_data) printlog("发牌==============================") pt(evt_data) if ViewManager.GetCurrenView().dview_class == LobbyView then self:ReturnToRoom() return end --local cardlist = evt_data["cards"] --local round = evt_data["round"] --local cardtype = evt_data["cardtype"] --local seat = evt_data["seat"] --local cardopen = evt_data["cardopen"] local playerAry = evt_data["playerAry"] -- printlog(#playerAry) -- pt(playerAry[1]) local round = playerAry[1].round self._cacheEvent:Enqueue( function() for _, player in ipairs(self._room.player_list) do player:Clear() --player.hand_count = #cardlist end self._room.curren_round = round --self._room.curren_round = round --DispatchEvent(self._dispatcher, MuShi_GameEvent.OnInitCard, round, cardlist,cardtype,seat,cardopen) DispatchEvent(self._dispatcher,MuShi_GameEvent.OnInitCard,round,playerAry) end ) end function M:OnBombScore(evt_data) local scoreList = evt_data["gold_list"] self._cacheEvent:Enqueue( function() for i = 1, #scoreList do local score = scoreList[i].bom_score local player = self._room:GetPlayerById(scoreList[i].aid) player.total_score = player.total_score + score end DispatchEvent(self._dispatcher, MuShi_GameEvent.OnBombScore, scoreList) end ) end function M:OnPlaySucc(evt_data) local seat = evt_data["player"] local card_obj = evt_data["card_obj"] local cards = card_obj["card_list"] local remain = evt_data["remain"] -- 报单 self._cacheEvent:Enqueue( function() local otherList = self:GetOtherSeatList(seat) local player = self._room:GetPlayerBySeat(seat) local out_card_list = self:ChangeCodeByFrom(cards, true) player.hand_count = remain local card_type, number, length, plan_three_count = self:GetCardListInfo(out_card_list) player.out_card_list = self:GetSortOutCardList(out_card_list, card_type, number, plan_three_count) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnPlaySucc, player, remain, card_type, number, otherList,length) end ) end function M:OnPassSucc(evt_data) local seat = evt_data["seat"] self._cacheEvent:Enqueue( function() local p = self._room:GetPlayerBySeat(seat) p.out_card_list = {0} DispatchEvent(self._dispatcher, MuShi_GameEvent.OnPassSucc, p) end ) end function M:OnPutError(evt_data) local code = evt_data["error"] self._cacheEvent:Enqueue( function() local error_str = self:GetErrorStr(code) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnErrorTip, error_str) end ) end function M:TuoGuan(isTuo) local _data = {} _data["tuoguan"] = isTuo local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.SEND_TUOGUAN, _data) end function M:Game_TuoGuan(evt_data) local tuoguan = evt_data["tuoguan"] local seat = evt_data["seat"] self._cacheEvent:Enqueue(function() DispatchEvent(self._dispatcher, MuShi_GameEvent.Game_TuoGuan, tuoguan, seat) end) end function M:OnIndexMove(evt_data) local seat = evt_data["index"] self._cacheEvent:Enqueue( function() self._room.curren_turn_seat = seat self._room.is_new_bout = self:GetIsNewBout(seat) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnIndexMove, seat) end ) end function M:OnOptions(evt_data) local play = evt_data["play"] local pass = evt_data["pass"] self._cacheEvent:Enqueue( function() local lastCardList = self:GetLastCardList(self._room.self_player.seat) local cardType, cardNum, cardLength = self:GetCardListInfo(lastCardList) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnOptions, play, cardType, cardNum, cardLength, pass) end ) end function M:OnPiaoTip(evt_data) --printlog("提示补牌==============================") if ViewManager.GetCurrenView().dview_class == LobbyView then self:ReturnToRoom() return end --local piao = evt_data["piao"] --local reload = evt_data["reload"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnPiaoTips) end ) end function M:SendCuo() local _client = ControllerManager.GameNetClinet _client:send(MuShi_Protocol.GAME_EVT_CUOPAI_REQ) end function M:OnPiaoAction(evt_data) local seat = evt_data["seat"] local piao = evt_data["bupai"] local card = evt_data["card"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnPiaoAction, seat,piao,card) end ) end function M:OnResult(evt_data) -- printlog("结算消息==========================》") --pt(evt_data) local result_type = evt_data["type"] --0小结算 1 大结算 2 解散房间 local info = evt_data["info"] local winseat = evt_data["winseat"] local remaincards = evt_data["remaincards"] if result_type == 1 then local over = 1 ControllerManager.SetGameNetClient(nil, true) self._cacheEvent:Enqueue( function() for i = 1, #info do local p = self._room:GetPlayerBySeat(info[i]["seat"]) p.total_score = info[i]["score"] info[i]["self_user"] = p.self_user end DispatchEvent(self._dispatcher, MuShi_GameEvent.OnResult, over, info, winseat, remaincards) end ) elseif result_type == 0 then local over = 0 self._cacheEvent:Enqueue( function() for i = 1, #info do local p = self._room:GetPlayerBySeat(info[i]["seat"]) p.total_score = info[i]["score"] info[i]["self_user"] = p.self_user end DispatchEvent(self._dispatcher, MuShi_GameEvent.OnResult, over, info, winseat, remaincards) end ) else local dissolve = 1 ControllerManager.SetGameNetClient(nil, true) for i = 1, #info do local p = self._room:GetPlayerBySeat(info[i]["seat"]) p.total_score = info[i]["score"] info[i]["self_user"] = p.self_user end -- ControllerManager.ChangeController(LoddyController) DispatchEvent(self._dispatcher, MuShi_GameEvent.OnResultByDissolve, over, info, winseat, dissolve) end end function M:OnConfrimToNextGameSucc(evt_data) local aid = evt_data["aid"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, MuShi_GameEvent.OnConfrimToNextGameSucc, aid) end ) end function M:Game_TuoGuan(evt_data) local tuoguan = evt_data["tuoguan"] local seat = evt_data["seat"] self._cacheEvent:Enqueue(function() DispatchEvent(self._dispatcher, MuShi_GameEvent.Game_TuoGuan, tuoguan, seat) end) end function M:ChangeCodeByFrom(cardList, isSort) isSort = isSort or false local new_card_list = {} for i = 1, #cardList do local flower = math.floor(cardList[i] / 100) local number = cardList[i] % 100 if number == 2 then number = 15 end local card = number * 10 + flower new_card_list[#new_card_list + 1] = card end return isSort == true and table.sort(new_card_list) or new_card_list end function M:GetOtherSeatList(seat) local list = {} for i = 1, self._room.room_config.people_num do if seat ~= i then list[#list + 1] = i end end return list end -- function M:GetIsNewBout(seat) local passCount = 0 for i = 1, #self._room.player_list do local player = self._room.player_list[i] if seat ~= player.seat then local isPass = self:GetIsPass(player.out_card_list) if isPass then passCount = passCount + 1 end end end if passCount == self._room.room_config.people_num - 1 then return true else return false end end function M:GetLastSeat(seat) local last_seat = seat - 1 if last_seat < 1 then last_seat = last_seat + self._room.room_config.people_num end return last_seat end function M:GetLastCardList(seat) local last_seat = self:GetLastSeat(seat) local player = self._room:GetPlayerBySeat(last_seat) local isPass = self:GetIsPass(player.out_card_list) if isPass then if self._room.room_config.people_num == 2 then return {} end local last_seat_2 = self:GetLastSeat(last_seat) local player_2 = self._room:GetPlayerBySeat(last_seat_2) local isPass_2 = self:GetIsPass(player_2.out_card_list) if isPass_2 then return {} else return player_2.out_card_list end else return player.out_card_list end end function M:GetErrorStr(code) return MuShi_PutError[code + 1] end function M:GetSortOutCardList(outCardList, cardType, cardNumber, plan_three_count) -- if cardType == 3 or cardType == 5 or cardType == 6 then -- local removeList = {} -- for i = #outCardList, 1, -1 do -- local card = outCardList[i] -- if math.floor(card / 10) == cardNumber then -- removeList[#removeList + 1] = card -- table.remove(outCardList, i) -- end -- end -- for i = 1, #removeList do -- table.insert(outCardList, 1, removeList[i]) -- end -- elseif cardType >= 7 and cardType <= 9 then -- local removeList = {} -- for i = #outCardList, 1, -1 do -- local card = outCardList[i] -- if math.floor(card / 10) <= cardNumber and math.floor(card / 10) > cardNumber - plan_three_count then -- removeList[#removeList + 1] = card -- table.remove(outCardList, i) -- end -- end -- for i = 1, #removeList do -- table.insert(outCardList, 1, removeList[i]) -- end -- end return outCardList end --None = 0, --OneCard = 1, --OnePair = 2, --Three = 3, --Pairs = 4, --ThreeAndTwo = 5, --ThreeAndOne = 6, --Plane = 7, --PlaneAndTwo = 8, --PlaneAndOne = 9, --Straight = 10, --Bomb = 11 -- 牌型,大小, 长度 function M:GetCardListInfo(cardlist) if #cardlist == 0 then return 0, 0, 0, 0 end -- 检测牌型 local card_type, card_num, card_length, plan_three_count = MuShi_CardType.None, 0, #cardlist, 0 local card_map = self:GetCardMapByList(cardlist) if #cardlist == 1 then card_type = MuShi_CardType.OneCard card_num = math.floor(cardlist[1] / 10) elseif #cardlist == 2 then card_type = MuShi_CardType.OnePair card_num = math.floor(cardlist[1] / 10) elseif #cardlist == 3 then card_num = math.floor(cardlist[1] / 10) if card_num==14 and DataManager.CurrenRoom.room_config.threeA==1 then -- body card_type = MuShi_CardType.Bomb else card_type = MuShi_CardType.Three end elseif #cardlist == 4 then local max_key = 0 for k, v in pairs(card_map) do if #v == 4 then card_type = MuShi_CardType.Bomb card_num = k elseif #v == 3 then card_type = MuShi_CardType.ThreeAndOne card_num = k elseif #v == 2 then if k > max_key then max_key = k end card_type = MuShi_CardType.Pairs card_num = max_key end end elseif #cardlist == 5 then local count, max_key = 0, 0 for k, v in pairs(card_map) do if #v >= 3 then card_type = MuShi_CardType.ThreeAndTwo card_num = k elseif #v == 1 then count = count + 1 if k > max_key then max_key = k end if count == 5 then card_type = MuShi_CardType.Straight card_num = max_key end end end elseif #cardlist == 7 then local count, max_key = 0, 0 for k, v in pairs(card_map) do if #v >= 4 then card_type = MuShi_CardType.FourAndtThree card_num = k elseif #v == 1 then count = count + 1 if k > max_key then max_key = k end if count == 7 then card_type = MuShi_CardType.Straight card_num = max_key end end end else local one_count, two_count, three_count = 0, 0, 0 local max_one_key, max_two_key, max_three_key = 0, 0, 0 for k, v in pairs(card_map) do if #v == 2 then if k > max_two_key then max_two_key = k end two_count = two_count + 1 if two_count == #cardlist / 2 then card_type = MuShi_CardType.Pairs card_num = max_two_key end elseif #v == 1 then if k > max_one_key then max_one_key = k end one_count = one_count + 1 if one_count == #cardlist then card_type = MuShi_CardType.Straight card_num = max_one_key end elseif #v == 3 then if max_three_key == 0 then max_three_key = k three_count = three_count + 1 elseif k > max_three_key and k == max_three_key + 1 then max_three_key = k three_count = three_count + 1 elseif k < max_three_key and k == max_three_key - 1 then max_three_key = k three_count = three_count + 1 -- else -- max_three_key = k end --three_count = three_count + 1 end end -- plan_three_count = three_count -- if three_count * 5 >= #cardlist then -- card_type = MuShi_CardType.PlaneAndTwo -- card_num = max_three_key -- elseif three_count * 4 >= #cardlist then -- card_type = MuShi_CardType.PlaneAndOne -- card_num = max_three_key -- elseif three_count * 3 >= #cardlist then -- card_type = MuShi_CardType.Plane -- card_num = max_three_key -- end plan_three_count = three_count if three_count * 3 == #cardlist then card_type = MuShi_CardType.Plane card_num = max_three_key elseif three_count * 4 >= #cardlist and #cardlist%4==0 then card_type = MuShi_CardType.PlaneAndOne card_num = max_three_key elseif three_count * 5 >= #cardlist and #cardlist%5==0 then card_type = MuShi_CardType.PlaneAndTwo card_num = max_three_key end end return card_type, card_num, card_length, plan_three_count end function M:GetCardMapByList(cardlist) local card_map = {} for i = 1, #cardlist do local card = cardlist[i] local card_num = math.floor(cardlist[i] / 10) if card_map[card_num] == nil then card_map[card_num] = {card} else card_map[card_num][#card_map[card_num] + 1] = card end end return card_map end function M:GetIsPass(cardlist) if #cardlist == 0 then return true end if cardlist[1] ~= nil and cardlist[1] == 0 then return true end return false end --请求离开房间 function M:LevelRoom(callBack) local _client = ControllerManager.GameNetClinet if not _client then return end _client:send( Protocol.GAME_EXIT_ROOM, nil, function(res) if res.ReturnCode == 0 then ControllerManager.ChangeController(LoddyController) elseif res.ReturnCode == 27 then ViewUtil.ErrorTip(res.ReturnCode, "退出房间失败!") end callBack(res) end ) end return M