--- --- Created by 谌建军. --- DateTime: 2017/12/13 11:28 --- local RunFast_Protocol = import(".RunFast_Protocol") local RunFast_GameEvent = import(".RunFast_GameEvent") local RunFast_CardType = import(".RunFast_CardType") local RunFast_PutError = { "必须先出最小的牌", "出牌不符合规定牌型 ", "下家报单,请出最大的牌 ", "炸弹不能拆" } local RunFast_GameController = {} local M = RunFast_GameController function M.new() setmetatable(M, { __index = GameController }) local self = setmetatable({}, { __index = M }) self:init("跑得快") self.class = "RunFast_GameController" return self end function M:init(name) GameController.init(self, name) self:RegisterEvt() end -- 事件注册 function M:RegisterEvt() self._eventmap[RunFast_Protocol.RunFast_Ming_Card] = self.OnMingCard self._eventmap[RunFast_Protocol.RunFast_Init_Card] = self.OnInitCard self._eventmap[RunFast_Protocol.RunFast_Options] = self.OnOptions self._eventmap[RunFast_Protocol.RunFast_Index_Move] = self.OnIndexMove self._eventmap[RunFast_Protocol.RunFast_Play_Succ] = self.OnPlaySucc self._eventmap[RunFast_Protocol.RunFast_Put_Error] = self.OnPutError self._eventmap[RunFast_Protocol.RunFast_Pass_Succ] = self.OnPassSucc self._eventmap[RunFast_Protocol.RunFast_Result] = self.OnResult self._eventmap[RunFast_Protocol.RunFast_Bomb_Score] = self.OnBombScore self._eventmap[RunFast_Protocol.RunFast_Piao_Tip] = self.OnPiaoTip self._eventmap[RunFast_Protocol.RunFast_Piao_Action] = self.OnPiaoAction self._eventmap[RunFast_Protocol.RunFast_ConfirmToNextGameSucc] = self.OnConfrimToNextGameSucc --self._eventmap[RunFast_Protocol.RunFast_Oener] = self.Oener -- self._eventmap[Protocol.GAME_EVT_PLAYER_JOIN] = self.OnEventPlayerEnter self._eventmap[RunFast_Protocol.PT_GAMETUOGUAN] = self.Game_TuoGuan self._eventmap[RunFast_Protocol.GAME_EVENT_XIPAI] = self.OnEventXiPai self._eventmap[RunFast_Protocol.GAME_EVENT_NOTIFY_XIPAI] = self.OnEventXiPaiAnim self._eventmap[RunFast_Protocol.GAME_EVT_CARDINHAND] = self.OnPlaySuccCheckHandCard self._eventmap[RunFast_Protocol.GAME_EVT_DANIAO_TIP] = self.OnDaNiao end -- function M:Oener(evt_data) -- local seat = evt_data["owner"] -- self._cacheEvent:Enqueue(function() -- DispatchEvent(self._dispatcher,RunFast_GameEvent.Oener,seat) -- end) -- end -- function M:OnEventPlayerEnter(evt_data) -- local p = self._room:NewPlayer() -- local _user -- _user = User.new() -- _user.account_id = evt_data["aid"] -- _user.host_ip = evt_data["ip"] -- _user.nick_name = evt_data["nick"] -- _user.head_url = evt_data["portrait"] -- _user.sex = evt_data["sex"] -- _user.location = Location.new(evt_data["pos"] or "") -- p.seat = evt_data["seat"] -- p.ready = evt_data["ready"] == 1 and true or false -- p.self_user = _user -- p.line_state = 1 -- p.total_score=evt_data["score"] -- p.hp_info = evt_data["hp_info"] -- -- p.total_score=self._room.room_config.energyTab==0 and evt_data["score"] or evt_data["score"]/10 -- DataManager.CurrenRoom:AddPlayer(p) -- DispatchEvent(self._dispatcher,GameEvent.PlayerEnter, p) -- end function M:SendXiPaiAction(callBack) local _data = {} local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.GAME_XIPAI, _data) self.XiPaiCallBack = callBack end function M:SendDaNiaoAction(flag) local _data = {} _data["niaoflag"] = flag local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.GAME_DANIAO, _data) end function M:OnEventXiPai(evt_data) if evt_data["result"] == 0 then if self.XiPaiCallBack then self.XiPaiCallBack() end else ViewUtil.ErrorTip(1000000, "申请洗牌失败") end end function M:OnEventXiPaiAnim(evt_data) printlog("洗牌动画===》》》》") pt(evt_data) local playeridList = evt_data["list"] local my_isXiPai = false local other_isXiPai = false if playeridList and #playeridList > 0 then for i = 1, #playeridList do local p = self._room:GetPlayerById(playeridList[i]) if p == self._room.self_player then my_isXiPai = true else other_isXiPai = true end end end self._cacheEvent:Enqueue(function() DispatchEvent(self._dispatcher, RunFast_GameEvent.EventXiPai, my_isXiPai, other_isXiPai) end) end function M:SendCard(cards, currentCard) print("lingmengSendCard", os.time()) local _data = {} _data["card"] = cards _data["all_card"] = currentCard local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.RunFast_Send_Card, _data) end function M:SendPiao(piao) local _data = {} _data["id"] = piao local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.RunFast_Send_Piao, _data) end function M:SendPass() local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.RunFast_Send_Guo) end function M:SendInitCardEnd() local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.RunFast_Init_Card_End) end function M:ConformToNextGame() local _client = ControllerManager.GameNetClinet _client:send(RunFast_Protocol.RunFast_ConfirmToNextGame) 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, RunFast_GameEvent.OnMingCard, card) end ) end function M:OnInitCard(evt_data) if ViewManager.GetCurrenView().dview_class == LobbyView then self:ReturnToRoom() return end local cardlist = evt_data["cards"] local round = evt_data["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 DispatchEvent(self._dispatcher, RunFast_GameEvent.OnInitCard, round, cardlist) 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, RunFast_GameEvent.OnBombScore, scoreList) end ) end function M:OnPlaySucc(evt_data) if pcall( self.OnPlaySuccCheck, self, evt_data ) then else printlog("数据异常OnPlaySucc==>>>") end end function M:OnPlaySuccCheck(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, RunFast_GameEvent.OnPlaySucc, player, remain, card_type, number, otherList, length) end ) end function M:OnPlaySuccCheckHandCard(evt_data) local seat = evt_data["player"] local cards = evt_data["handCards"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, RunFast_GameEvent.OnPassSuccCheckCard, seat, cards) end ) end function M:OnDaNiao(evt_data) self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, RunFast_GameEvent.OnDaNiao) ControllerManager.IsSendCard = true end ) end function M:OnPassSucc(evt_data) print("lingmeng OnPassSucc", os.time()) local seat = evt_data["seat"] self._cacheEvent:Enqueue( function() local p = self._room:GetPlayerBySeat(seat) p.out_card_list = { 0 } DispatchEvent(self._dispatcher, RunFast_GameEvent.OnPassSucc, p) end ) print("lingmeng OnPassSucc2", os.time()) end function M:OnPutError(evt_data) local code = evt_data["error"] self._cacheEvent:Enqueue( function() local error_str = self:GetErrorStr(code) DispatchEvent(self._dispatcher, RunFast_GameEvent.OnErrorTip, error_str) end ) end function M:TuoGuan(isTuo) local _data = {} _data["tuoguan"] = isTuo local _client = ControllerManager.GameNetClinet _client:send(RunFast_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, RunFast_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, RunFast_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, RunFast_GameEvent.OnOptions, play, cardType, cardNum, cardLength, pass) end ) end function M:OnPiaoTip(evt_data) 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, RunFast_GameEvent.OnPiaoTips, piao, reload) end ) end function M:OnPiaoAction(evt_data) local seat = evt_data["seat"] local piao = evt_data["piao"] self._cacheEvent:Enqueue( function() DispatchEvent(self._dispatcher, RunFast_GameEvent.OnPiaoAction, seat, piao) end ) end function M:OnResult(evt_data) local result_type = evt_data["type"] local info = evt_data["info"] local winseat = evt_data["winseat"] local remaincards = evt_data["remaincards"] DataManager.CurrenRoom.xipaiScore = evt_data["xipai_score"] --printlog("wwwwwwwwwwwwwwwwwwwww1111111 ",result_type) --pt(evt_data) 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, RunFast_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, RunFast_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, RunFast_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, RunFast_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, RunFast_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 RunFast_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 = RunFast_CardType.None, 0, #cardlist, 0 local card_map = self:GetCardMapByList(cardlist) if #cardlist == 1 then card_type = RunFast_CardType.OneCard card_num = math.floor(cardlist[1] / 10) elseif #cardlist == 2 then card_type = RunFast_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 = RunFast_CardType.Bomb else card_type = RunFast_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 = RunFast_CardType.Bomb card_num = k elseif #v == 3 then card_type = RunFast_CardType.ThreeAndOne card_num = k elseif #v == 2 then if k > max_key then max_key = k end card_type = RunFast_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 = RunFast_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 = RunFast_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 = RunFast_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 = RunFast_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 = RunFast_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 = RunFast_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 = RunFast_CardType.PlaneAndTwo -- card_num = max_three_key -- elseif three_count * 4 >= #cardlist then -- card_type = RunFast_CardType.PlaneAndOne -- card_num = max_three_key -- elseif three_count * 3 >= #cardlist then -- card_type = RunFast_CardType.Plane -- card_num = max_three_key -- end plan_three_count = three_count if three_count * 3 == #cardlist then card_type = RunFast_CardType.Plane card_num = max_three_key elseif three_count * 4 >= #cardlist and #cardlist % 4 == 0 then card_type = RunFast_CardType.PlaneAndOne card_num = max_three_key elseif three_count * 5 >= #cardlist and #cardlist % 5 == 0 then card_type = RunFast_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