hengyang_client/lua_probject/extend_project/extend/poker/duoduo/EXGameController.lua

752 lines
23 KiB
Lua

---
--- Created by 谌建军.
--- DateTime: 2017/12/13 11:28
---
local EXProtocol = import(".EXProtocol")
local EXGameEvent = import(".EXGameEvent")
local EXCardType = import(".EXCardType")
local EXPutError = {
"必须先出最小的牌",
"出牌不符合规定牌型 ",
"下家报单,请出最大的牌 ",
"炸弹不能拆"
}
local EXGameController = {}
local M = EXGameController
function M.new()
setmetatable(M, { __index = GameController })
local self = setmetatable({}, { __index = M })
self:init("跑得快")
self.class = "EXGameController"
return self
end
function M:init(name)
GameController.init(self, name)
self:RegisterEvt()
end
-- 事件注册
function M:RegisterEvt()
self._eventmap[EXProtocol.EXMing_Card] = self.OnMingCard
self._eventmap[EXProtocol.EXInit_Card] = self.OnInitCard
self._eventmap[EXProtocol.EXOptions] = self.OnOptions
self._eventmap[EXProtocol.EXIndex_Move] = self.OnIndexMove
self._eventmap[EXProtocol.EXPlay_Succ] = self.OnPlaySucc
self._eventmap[EXProtocol.EXPut_Error] = self.OnPutError
self._eventmap[EXProtocol.EXPass_Succ] = self.OnPassSucc
self._eventmap[EXProtocol.EXResult] = self.OnResult
self._eventmap[EXProtocol.EXBomb_Score] = self.OnBombScore
self._eventmap[EXProtocol.EXPiao_Tip] = self.OnPiaoTip
self._eventmap[EXProtocol.EXPiao_Action] = self.OnPiaoAction
self._eventmap[EXProtocol.EXConfirmToNextGameSucc] = self.OnConfrimToNextGameSucc
--self._eventmap[EXProtocol.EXOener] = self.Oener
-- self._eventmap[Protocol.GAME_EVT_PLAYER_JOIN] = self.OnEventPlayerEnter
self._eventmap[EXProtocol.PT_GAMETUOGUAN] = self.Game_TuoGuan
self._eventmap[EXProtocol.GAME_EVENT_XIPAI] = self.OnEventXiPai
self._eventmap[EXProtocol.GAME_EVENT_NOTIFY_XIPAI] = self.OnEventXiPaiAnim
self._eventmap[EXProtocol.GAME_EVT_CARDINHAND] = self.OnPlaySuccCheckHandCard
self._eventmap[EXProtocol.EXBomb_Score_New] = self.OnBompScoreNew
self._eventmap[EXProtocol.EXCheckCard_Succ] = self.OnCheckCard
end
-- function M:Oener(evt_data)
-- local seat = evt_data["owner"]
-- self._cacheEvent:Enqueue(function()
-- DispatchEvent(self._dispatcher,EXGameEvent.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(EXProtocol.GAME_XIPAI, _data)
self.XiPaiCallBack = callBack
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, EXGameEvent.EventXiPai, my_isXiPai, other_isXiPai)
end)
end
function M:SendCard(cards, currentCard)
local _data = {}
_data["card"] = cards
_data["all_card"] = currentCard
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXSend_Card, _data)
end
function M:SendCheckCard()
local _data = {}
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXCheckCard, _data)
end
function M:SendCheckCardSend(data)
local _data = {}
_data = data
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXCheckCard_Send, _data)
end
function M:SendPiao(piao)
local _data = {}
_data["id"] = piao
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXSend_Piao, _data)
end
function M:SendPass()
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXSend_Guo)
end
function M:SendInitCardEnd()
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXInit_Card_End)
end
function M:ConformToNextGame()
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.EXConfirmToNextGame)
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, EXGameEvent.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"]
local bank_seat = evt_data["bank_seat"]
self._cacheEvent:Enqueue(
function()
for _, player in ipairs(self._room.player_list) do
player:Clear()
player.hand_count = #cardlist
player.hand_list = cardlist
end
self._room.curren_round = round
self._room.banker_seat = bank_seat
DispatchEvent(self._dispatcher, EXGameEvent.OnInitCard, round, cardlist)
end
)
end
function M:OnBombScore(evt_data)
printlog("lingmeng OnBombScore")
-- 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, EXGameEvent.OnBombScore, scoreList)
-- end
-- )
printlog("lingmeng OnBombScoreend")
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 player = self._room:GetPlayerBySeat(seat)
player.hand_count = remain
player.out_card_list = cards
DispatchEvent(self._dispatcher, EXGameEvent.OnPlaySucc, player, remain)
end
)
end
function M:OnPlaySuccCheckHandCard(evt_data)
local seat = evt_data["player"]
local cards = evt_data["handCards"]
self._cacheEvent:Enqueue(
function()
DispatchEvent(self._dispatcher, EXGameEvent.OnPassSuccCheckCard, seat, cards)
end
)
end
function M:OnPassSucc(evt_data)
local seat = evt_data["seat"]
self._cacheEvent:Enqueue(
function()
print("lingmeng OnPassSucc",seat)
local p = self._room:GetPlayerBySeat(seat)
p.out_card_list = { 0 }
DispatchEvent(self._dispatcher, EXGameEvent.OnPassSucc, p)
print("lingmeng OnPassSuccend",seat)
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, EXGameEvent.OnErrorTip, error_str)
end
)
end
function M:TuoGuan(isTuo)
local _data = {}
_data["tuoguan"] = isTuo
local _client = ControllerManager.GameNetClinet
_client:send(EXProtocol.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, EXGameEvent.Game_TuoGuan, tuoguan, seat)
end)
end
function M:OnIndexMove(evt_data)
local seat = evt_data["index"]
self._cacheEvent:Enqueue(
function()
printlog("lingmeng OnIndexMove", seat)
self._room.curren_turn_seat = seat
self._room.is_new_bout = self:GetIsNewBout(seat)
DispatchEvent(self._dispatcher, EXGameEvent.OnIndexMove, seat)
printlog("lingmeng OnIndexMoveend", seat)
end
)
end
function M:OnOptions(evt_data)
local play = evt_data["play"]
local pass = evt_data["pass"]
self._cacheEvent:Enqueue(
function()
printlog("lingmeng OnOptions")
local lastCardList = self:GetLastCardList(self._room.self_player.seat)
local cardType, cardNum, cardLength = self:GetCardListInfo(lastCardList)
DispatchEvent(self._dispatcher, EXGameEvent.OnOptions, play, cardType, cardNum, cardLength, pass,
lastCardList)
printlog("lingmeng OnOptionsend")
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, EXGameEvent.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, EXGameEvent.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"])
print("lingmeng log200", info[i]["score"], p.total_score)
info[i].card_score = info[i]["score"] - p.total_score
p.total_score = info[i]["score"]
info[i]["self_user"] = p.self_user
end
DispatchEvent(self._dispatcher, EXGameEvent.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"])
print("lingmeng log200", info[i]["score"], p.total_score)
info[i].card_score = info[i]["score"] - p.total_score
p.total_score = info[i]["score"]
info[i]["self_user"] = p.self_user
end
DispatchEvent(self._dispatcher, EXGameEvent.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, EXGameEvent.OnResultByDissolve, result_type, info, winseat, dissolve)
end
end
function M:OnConfrimToNextGameSucc(evt_data)
local aid = evt_data["aid"]
self._cacheEvent:Enqueue(
function()
DispatchEvent(self._dispatcher, EXGameEvent.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, EXGameEvent.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 EXPutError[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 = EXCardType.None, 0, #cardlist, 0
local card_map = self:GetCardMapByList(cardlist)
if #cardlist == 1 then
card_type = EXCardType.OneCard
card_num = math.floor(cardlist[1] / 10)
elseif #cardlist == 2 then
card_type = EXCardType.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 = EXCardType.Bomb
else
card_type = EXCardType.Three
end
elseif #cardlist == 4 then
local max_key = 0
for k, v in pairs(card_map) do
if #v == 4 then
card_type = EXCardType.Bomb
card_num = k
elseif #v == 3 then
card_type = EXCardType.ThreeAndOne
card_num = k
elseif #v == 2 then
if k > max_key then
max_key = k
end
card_type = EXCardType.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 = EXCardType.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 = EXCardType.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 = EXCardType.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 = EXCardType.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 = EXCardType.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 = EXCardType.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 = EXCardType.PlaneAndTwo
-- card_num = max_three_key
-- elseif three_count * 4 >= #cardlist then
-- card_type = EXCardType.PlaneAndOne
-- card_num = max_three_key
-- elseif three_count * 3 >= #cardlist then
-- card_type = EXCardType.Plane
-- card_num = max_three_key
-- end
plan_three_count = three_count
if three_count * 3 == #cardlist then
card_type = EXCardType.Plane
card_num = max_three_key
elseif three_count * 4 >= #cardlist and #cardlist % 4 == 0 then
card_type = EXCardType.PlaneAndOne
card_num = max_three_key
elseif three_count * 5 >= #cardlist and #cardlist % 5 == 0 then
card_type = EXCardType.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
--炸弹分显示,新协议
function M:OnBompScoreNew(evt_data)
local scoreList = evt_data["PlayerScoreList"]
self._cacheEvent:Enqueue(
function()
for i = 1, #scoreList do
local score = scoreList[i].pomSore
local player = self._room:GetPlayerBySeat(scoreList[i].seat)
player.total_score = player.total_score + score
end
DispatchEvent(self._dispatcher, EXGameEvent.OnBompScoreNew, scoreList)
end
)
end
function M:OnCheckCard(evt_data)
self._cacheEvent:Enqueue(
function()
DispatchEvent(self._dispatcher, EXGameEvent.OnCheckCard, evt_data)
end
)
end
return M