changhong/lua_probject/extend_project/extend/majiang/changsha/EXGameController.lua

427 lines
13 KiB
Lua
Raw Permalink Normal View History

2025-05-24 14:29:14 +08:00
local CS_Protocol = import(".Protocol")
local FZTipList = require("main.majiang.FZData")
local CS_GameEvent = import(".CS_GameEvent")
local M = {}
--- Create a new CS_GameController
function M.new()
setmetatable(M,{__index = GameController})
local self = setmetatable({}, {__index = M})
self:init("测试麻将")
self.class = "CS_GameController"
return self
end
function M:init(name)
GameController.init(self,name)
self._eventmap[CS_Protocol.GAME_EVT_PLAYER_DEAL] = self.OnEventSendCards
self._eventmap[CS_Protocol.GAME_EVT_CHANGE_ACTIVE_PLAYER] = self.OnEventTurn
self._eventmap[CS_Protocol.GAME_EVT_DRAW] = self.OnEventTakeCard
self._eventmap[CS_Protocol.GAME_EVT_DISCARD_TIP] = self.OnEventOutHint
self._eventmap[CS_Protocol.GAME_EVT_DISCARD] = self.OnEventOutCard
self._eventmap[CS_Protocol.GAME_EVT_FZTIPS] = self.OnEventFzTips
self._eventmap[CS_Protocol.GAME_EVT_ACTION] = self.OnEventFzAction
self._eventmap[CS_Protocol.GAME_EVT_HU] = self.OnEventHu
self._eventmap[CS_Protocol.GAME_EVT_RESULT1] = self.OneventResult1
self._eventmap[CS_Protocol.GAME_EVT_RESULT2] = self.OnEventResult2
self._eventmap[CS_Protocol.GAME_EVT_NIAO] = self.OnEventNiao
self._eventmap[CS_Protocol.GAME_EVT_QSTIP] = self.OnEventQSTip
self._eventmap[CS_Protocol.GAME_EVT_QSWIN] = self.OnEventQSAction
self._eventmap[CS_Protocol.GAME_EVT_OPENKONG] = self.OnEventKaiGang
self._eventmap[CS_Protocol.GAME_EVT_HAIDI] = self.OnEventHaidi
self._eventmap[CS_Protocol.GAME_EVT_PIAOTIP] = self.OnEventPiaoTip
self._eventmap[CS_Protocol.GAME_EVT_PIAO] = self.OnEventPiao
self._eventmap[CS_Protocol.GAME_EVT_TING_TIP] = self.OnEventTingTip
self._eventmap[CS_Protocol.GAME_EVT_TING] = self.OnEventTing
self._eventmap[CS_Protocol.GAME_EVENT_XIPAI] = self.OnEventXiPai
self._eventmap[CS_Protocol.GAME_EVENT_NOTIFY_XIPAI] = self.OnEventXiPaiAnim
end
local __pre_delete_card = false
-- 发送出牌指令到服务器
function M:SendOutCard(card, callback)
local _room = self._room
local p = _room.self_player
local _data = {}
_data["card"] = card
_data["card_list"] = p.card_list
if p.outcard_list and #p.outcard_list>0 then
_data["outcard_list"] = p.outcard_list
end
local _client = ControllerManager.GameNetClinet
_client:send(CS_Protocol.GAME_DIS_CARD, _data)
-- 进行预删牌处理
_room.curren_outcard_seat = -1
list_remove(p.card_list,card)
table.sort(p.card_list, ViewUtil.HandCardSort)
p.hand_left_count = p.hand_left_count - 1
if not p.outcard_list then p.outcard_list = {} end
p.outcard_list[#p.outcard_list+1] = card
__pre_delete_card = true
callback()
end
-- 发送放子选择到服务器
function M:SendAction(id)
local _data = {}
_data["id"] = id
local _client = ControllerManager.GameNetClinet
_client:send(CS_Protocol.GAME_ACTION, _data)
end
function M:SendXiPaiAction(callBack)
local _data = {}
local _client = ControllerManager.GameNetClinet
_client:send(CS_Protocol.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,CS_GameEvent.EventXiPai,my_isXiPai,other_isXiPai)
end)
end
function M:OnEventSendCards(evt_data)
if ViewManager.GetCurrenView().dview_class == LobbyView then
self:ReturnToRoom()
return
end
local _room = self._room
-- if(_room.CurnrenState == StateType.Ready) then
-- _room.CurnrenState = StateType.Palying
-- end
-- _room.curren_round = _room.curren_round + 1
_room.curren_round = evt_data["round"]
if _room.curren_round > 0 then _room.playing = true end
-- _room.SelfPlayer.AutoOutCard = false
local handcards = evt_data["card_list"]
local p = _room.self_player
local seat = evt_data["bank_seat"]
local left_count = evt_data["left_count"]
self._cacheEvent:Enqueue(function()
_room.banker_seat = seat
for i=1,#_room.player_list do
_room.player_list[i].hand_left_count = _room.player_list[i].seat == _room.banker_seat and 14 or 13
_room.player_list[i].fz_list = {}
_room.player_list[i].card_list = {}
end
p.card_list = handcards
self._room.self_player.hand_left_count = #handcards
table.sort( handcards, ViewUtil.HandCardSort )
DispatchEvent(self._dispatcher,CS_GameEvent.SendCards, left_count)
end)
end
function M:OnEventOutCard(evt_data)
local seat = evt_data["seat"]
local card = evt_data["card"]
local p = self._room:GetPlayerBySeat(seat)
self._cacheEvent:Enqueue(function()
local _room = self._room
if seat == _room.self_player.seat and __pre_delete_card then
__pre_delete_card = false
else
if seat == _room.self_player.seat then
list_remove(p.card_list, card)
table.sort(p.card_list, ViewUtil.HandCardSort)
end
p.hand_left_count = p.hand_left_count - 1
if not p.outcard_list then p.outcard_list = {} end
p.outcard_list[#p.outcard_list+1] = card
DispatchEvent(self._dispatcher,CS_GameEvent.OutCard, p, card)
end
end)
end
function M:OnEventTakeCard(evt_data)
local _room = self._room
local seat = evt_data["seat"]
local card = evt_data["card"]
local left_count = evt_data["left_count"]
local p = _room:GetPlayerBySeat(seat)
self._cacheEvent:Enqueue(function()
p.hand_left_count = p.hand_left_count +1
if (seat == _room.self_player.seat) then
_room.self_player.card_list[#_room.self_player.card_list+1] = card
-- table.sort( _room.self_player.card_list, ViewUtil.HandCardSort )
end
DispatchEvent(self._dispatcher,CS_GameEvent.GetCard, seat, card, left_count)
end)
end
function M:OnEventOutHint(evt_data)
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.OutHint, evt_data)
end)
end
function M:OnEventTurn(evt_data)
local seat = evt_data["seat"]
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EventTurn, seat)
end)
end
function M:OnEventFzTips(evt_data)
self._cacheEvent:Enqueue(function()
local tiplist = FZTipList.new()
local list = evt_data["tip_list"]
for i=1,#list do
local dtip = list[i]
local tip = {}
tip.id = dtip["id"]
tip.weight = dtip["weight"]
tip.card = dtip["card"]
tip.type = dtip["type"]
tip.opcard = dtip["opcard"]
tiplist:AddTip(tip)
end
DispatchEvent(self._dispatcher,CS_GameEvent.FZTips, tiplist)
end)
end
function M:OnEventFzAction(evt_data)
local _room = self._room
local playerid = evt_data["playerid"]
local card = evt_data["card"]
local actice_card = evt_data["active_card"]
local type = evt_data["type"]
local from_seat = evt_data["from_seat"]
local opcard = evt_data["opcard"]
local opengang = evt_data["opengang"]
-- local openkong = evt_data["openkong"]
local p = _room:GetPlayerById(playerid)
self._cacheEvent:Enqueue(function()
local fz = nil
local index = -1
local ftype = type
if (ftype == FZType.Gang_Peng) then
for i=1,#p.fz_list do
if (p.fz_list[i].card == card) then
p.fz_list[i].card = card
fz = p.fz_list[i]
fz.type = type
index = i -1
break
end
end
end
fz = {}
fz.card = card
fz.type = type
fz.opengang = opengang
if (index == -1) then
if (ftype == FZType.Chi) then
local data ={}
data[1] = opcard[1]
data[2] = card
data[3] = opcard[2]
fz.opcard =data
end
p.fz_list[#p.fz_list+1] = fz
end
fz.from_seat = from_seat
local remove_num = #opcard
if (p == _room.self_player) then
for i=1,remove_num do
list_remove(p.card_list,opcard[i])
end
end
p.hand_left_count = p.hand_left_count - remove_num
if not (fz.type == FZType.Gang_An or (fz.type == FZType.Gang_Peng and not fz.opengang)) then
-- if (fz.Type == FZType.Chi) then card = actice_card end
local fp = _room:GetPlayerBySeat(from_seat)
if card == fp.outcard_list[#fp.outcard_list] then
table.remove(fp.outcard_list,#fp.outcard_list)
else
table.remove(fp.outcard_list,#fp.outcard_list - 1)
end
end
DispatchEvent(self._dispatcher,CS_GameEvent.FangziAction, fz, p, index)
end)
end
function M:OnEventHu(evt_data)
local cards = evt_data["card"]
local win_p = self._room:GetPlayerBySeat(evt_data["seat"])
local lose_p = self._room:GetPlayerBySeat(evt_data["from_seat"])
local win_card = evt_data["win_card"]
local win_list = evt_data["win_list"]
self._cacheEvent:Enqueue(function()
win_p.card_list = cards
table.sort( win_p.card_list, ViewUtil.HandCardSort)
DispatchEvent(self._dispatcher,CS_GameEvent.ZPHuCard, evt_data["seat"], evt_data["from_seat"], win_card, cards, win_list)
end)
end
function M:OneventResult1(evt_data)
local over = evt_data.type
--0小结算 1小大结算 2大结算
self._room.playing = false
if 0 == over then
local result = evt_data.result
self._cacheEvent:Enqueue(function()
for i = 1, #self._room.player_list do
local p = self._room.player_list[i]
p.hand_left_count = 0
p.outcard_list = {}
end
DispatchEvent(self._dispatcher,CS_GameEvent.ZPResult1, result);
end)
elseif 1 == over or 2 == over then
DataManager.CurrenRoom.Over = true
ControllerManager.SetGameNetClient(nil,true)
local total_result = evt_data.total_result
local result = evt_data.result
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.ZPResult2, result, total_result, over);
end)
end
end
function M:OnEventNiao(evt_data)
local list = evt_data["niao"]
local start_seat = evt_data["start_seat"]
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EventNiao, list, start_seat)
end)
end
function M:OnEventQSTip(evt_data)
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EventQSTip, evt_data)
end)
end
function M:OnEventQSAction(evt_data)
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EvnetQSAction, evt_data)
end)
end
function M:OnEventKaiGang(evt_data)
local seat = evt_data["seat"]
local p = self._room:GetPlayerBySeat(seat)
local cardList = evt_data.info.cardList
local card = evt_data.card
self._room.remain_cards = evt_data.left_count
self._cacheEvent:Enqueue(function()
for i = 1, #cardList do
p.outcard_list[#p.outcard_list + 1] = cardList[i]
end
DispatchEvent(self._dispatcher,CS_GameEvent.EventKaiGang, evt_data, self._room.self_player.outcard_list)
end)
end
function M:OnEventHaidi()
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EvnetHaiDi, evt_data)
end)
end
function M:OnEventPiaoTip(evt_data)
if ViewManager.GetCurrenView().dview_class == LobbyView then
self:ReturnToRoom()
return
end
self._cacheEvent:Enqueue(function()
self._room.playing = true
DispatchEvent(self._dispatcher,CS_GameEvent.EvnetPiaoTip, evt_data)
end)
end
function M:OnEventTingTip()
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EventTingTip)
end)
end
function M:OnEventTing(evt_data)
self._cacheEvent:Enqueue(function()
local seat = evt_data.seat
DispatchEvent(self._dispatcher,CS_GameEvent.EventTing, seat)
end)
end
function M:OnEventPiao(evt_data)
self._cacheEvent:Enqueue(function()
DispatchEvent(self._dispatcher,CS_GameEvent.EvnetPiao, evt_data.seat, evt_data.num)
end)
end
function M:GetPosString( seat )
if DataManager.CurrenRoom.room_config.people_num ~= 4 then return"" end
if seat == 1 then
return ""
elseif seat == 2 then
return "西"
elseif seat == 3 then
return ""
elseif seat == 4 then
return ""
end
end
function M:Discard(card)
local _data = {}
_data["card"] = card
local _room = self._room
local _client = ControllerManager.GameNetClinet
-- list_remove(_room.SelfPlayer.Cards,card)
-- table.sort(_room.SelfPlayer.Cards)
_client:send(CS_Protocol.Game_Da, _data)
end
return M