626 lines
17 KiB
Lua
626 lines
17 KiB
Lua
--- Base GameEvent
|
||
|
||
GameEvent = {
|
||
-- 状态
|
||
PlayerState = 'PlayerState',
|
||
-- 聊天
|
||
Interaction = 'Interaction',
|
||
-- 玩家离开
|
||
PlayerLeave = 'PlayerLeave',
|
||
-- 玩家进入
|
||
PlayerEnter = 'PlayerEnter',
|
||
-- 玩家准备
|
||
PlayerReady = 'PlayerReady',
|
||
-- 解散
|
||
DeskBreak = 'DeskBreak',
|
||
-- 被踢出房间
|
||
OnKicked = 'OnKicked',
|
||
-- 更新玩家信息
|
||
OnUpdateInfo = 'OnUpdateInfo',
|
||
|
||
--打开托管
|
||
TupGuanOpen='TupGuanOpen',
|
||
--关闭托管
|
||
TupGuanClose='TupGuanClose',
|
||
|
||
--麻将修改牌大小
|
||
MJModifySzie='MJModifySzie',
|
||
}
|
||
|
||
--- Base GameController
|
||
GameController = {
|
||
_name = '',
|
||
--事件MAP
|
||
_eventmap = nil,
|
||
--事件缓存
|
||
_cacheEvent = nil,
|
||
--事件发送器
|
||
_dispatcher = nil
|
||
}
|
||
|
||
local M = GameController
|
||
|
||
setmetatable(M, {__index = IController})
|
||
|
||
function M:init(name)
|
||
self._name = name
|
||
self.baseType = GameController
|
||
self._cacheEvent = Queue.new(1000)
|
||
self._eventmap = {}
|
||
self._dispatcher = {}
|
||
self._eventmap[Protocol.FGMGR_EVT_UPDATE_RECONECT]=self.ResetConnect
|
||
|
||
self._eventmap[Protocol.GAME_EVT_PLAYER_JOIN] = self.OnEventPlayerEnter
|
||
self._eventmap[Protocol.GAME_EVT_PLAYER_NET_STATE] = self.OnEventOnlineState
|
||
self._eventmap[Protocol.GAME_EVT_PLAYER_EXIT] = self.OnEventPlayerLeave
|
||
self._eventmap[Protocol.GAME_EVT_READY] = self.OnEventPlayerReady
|
||
self._eventmap[Protocol.GAME_EVT_READY_AND_XIPAI] = self.OnEventPlayerXiPaiReady
|
||
|
||
self._eventmap[Protocol.GAME_EVT_EXIT_ROOM_DISMISS] = self.OnEventExitRoomDismiss
|
||
self._eventmap[Protocol.GAME_EVT_DISMISS_ROOM] = self.OnEventDismissRoom
|
||
self._eventmap[Protocol.GAME_EVT_DISMISS_ROOM_VOTE] = self.OnEventDismissRoomVote
|
||
self._eventmap[Protocol.GAME_EVT_DISMISS_ROOM_FAIL] = self.OnEventDismissRoomFail
|
||
|
||
self._eventmap[Protocol.GAME_EVT_INTERACTION] = self.OnEventInteraction
|
||
self._eventmap[Protocol.GAME_EVT_UPDATE_GPS] = self.OnEventUpdateGPS
|
||
|
||
self._eventmap[Protocol.GAME_EVT_KICKED] = self.OnEventKicked
|
||
self._eventmap[Protocol.GAME_EVT_UPDATE_PLAYERINFO] = self.OnEvtUpdateInfo
|
||
|
||
self._eventmap[Protocol.GAME_EVT_READY_ENTRUST] = self.OnEvtOpenTupGTips
|
||
self._eventmap[Protocol.GAME_EVT_CANCEL_READY_ENTRUST] = self.OnEvtCloseTupGTips
|
||
|
||
--self._eventmap[Protocol.GAME_AUTO_CARD] = self.OnEvtOpenGameHuTuoGtips
|
||
|
||
|
||
end
|
||
|
||
function DispatchEvent(_dispatcher, evt_name, ...)
|
||
local func = _dispatcher[evt_name]
|
||
if func then
|
||
func(...)
|
||
end
|
||
end
|
||
|
||
function M:AddEventListener(evt_name, func)
|
||
self._dispatcher[evt_name] = func
|
||
end
|
||
|
||
|
||
function M:ResetConnect()
|
||
-- print("断线重连================")
|
||
--ControllerManager.OnConnect(SocketCode.TimeoutDisconnect)
|
||
ViewManager.refreshGameView()
|
||
end
|
||
|
||
----------------------请求------------------------------------
|
||
|
||
--请求准备
|
||
function M:PlayerReady()
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
_client:send(Protocol.GAME_READY)
|
||
end
|
||
|
||
function M:PlayerXiPai()
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
_client:send(Protocol.GAME_READY_AND_XIPAI)
|
||
end
|
||
|
||
--请求准备
|
||
function M:StartGame()
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
_client:send(Protocol.GAME_START)
|
||
end
|
||
|
||
--聊天
|
||
--<param name="playid"></param>
|
||
--<param name="type">1表情 2固定语音 3语音 4文本 5互动</param>
|
||
--<param name="parm"></param>
|
||
function M:SendInteraction(playid, type, parm, callback)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
local _data = {}
|
||
_data['playerid'] = playid
|
||
_data['type'] = type
|
||
_data['parm'] = parm
|
||
_client:send(Protocol.GAME_INTERACTION, _data)
|
||
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)
|
||
end
|
||
callBack(res)
|
||
end
|
||
)
|
||
end
|
||
|
||
--请求解散房间
|
||
function M:AskDismissRoom()
|
||
local _curren_msg = MsgWindow.new(self._root_view, '是否发起解散?', MsgWindow.MsgMode.OkAndCancel)
|
||
_curren_msg.onOk:Add(function()
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
_client:send(Protocol.GAME_ASK_DISMISS_ROOM,nil,function (res)
|
||
if res.ReturnCode == 84 then
|
||
ViewUtil.ErrorTip(res.ReturnCode,"解散失败")
|
||
end
|
||
end)
|
||
end)
|
||
_curren_msg:Show()
|
||
end
|
||
|
||
--解散房间投票
|
||
function M:DismissRoomVote(agree)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
-- print(agree)
|
||
local _data = {}
|
||
_data['result'] = agree
|
||
_client:send(Protocol.GAME_DISMISS_ROOM_VOTE, _data)
|
||
end
|
||
|
||
--发送GPS坐标
|
||
function M:SendGPS(str)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
local _data = {}
|
||
_data['pos'] = str
|
||
_client:send(Protocol.GAME_SEND_GPS, _data)
|
||
end
|
||
|
||
--------------------事件-----------------------------------
|
||
|
||
-- 房主退出 房间解散
|
||
function M:OnEventExitRoomDismiss(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
ControllerManager.ChangeController(LoddyController)
|
||
DispatchEvent(self._dispatcher, GameEvent.DeskBreak, 1)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 显示投票选择
|
||
function M:OnEventDismissRoom(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local room = DataManager.CurrenRoom
|
||
local req_aid = evt_data['req_aid']
|
||
evt_data.req_p = room:GetPlayerById(req_aid)
|
||
local player_list = room.player_list
|
||
local tem_list = {}
|
||
local list = evt_data['list']
|
||
|
||
for k = 1, #player_list do
|
||
local p = nil
|
||
for i = 1, #list do
|
||
local tem = list[i]
|
||
if tem.aid == player_list[k].self_user.account_id then
|
||
tem.player = player_list[k]
|
||
p = tem
|
||
break
|
||
end
|
||
end
|
||
if not p then
|
||
p = {}
|
||
p.player = player_list[k]
|
||
p.result = 0
|
||
end
|
||
tem_list[k] = p
|
||
end
|
||
evt_data['list'] = tem_list
|
||
|
||
DispatchEvent(self._dispatcher, GameEvent.DeskBreak, 0, evt_data)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- -- 投票结果
|
||
-- function M:OnEventDismissRoomVote(evt_data)
|
||
-- local aid = evt_data["aid"]
|
||
-- local result = evt_data["result"]
|
||
-- local p = DataManager.CurrenRoom:GetPlayerById(aid)
|
||
-- DispatchEvent(self._dispatcher,GameEvent.DeskBreak, 2,p,result)
|
||
-- end
|
||
|
||
-- 解散失敗
|
||
function M:OnEventDismissRoomFail(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
DispatchEvent(self._dispatcher, GameEvent.DeskBreak, 3)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 玩家进
|
||
function M:OnEventPlayerEnter(evt_data)
|
||
--print("进入房间++++++++++++++++++++++++++++++++++++++")
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
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.cur_hp = evt_data['cur_hp'] or 0
|
||
p.spectator = evt_data['spectator']
|
||
-- p.total_hp = evt_data["total_hp"] or 0
|
||
if evt_data['hp_info'] then
|
||
p.cur_hp = evt_data.hp_info.cur_hp
|
||
-- p.total_hp = evt_data.hp_info.total_hp
|
||
end
|
||
p.self_user = _user
|
||
p.line_state = 1
|
||
DataManager.CurrenRoom:AddPlayer(p)
|
||
DispatchEvent(self._dispatcher, GameEvent.PlayerEnter, p)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 玩家离开
|
||
function M:OnEventPlayerLeave(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local playerid = evt_data['aid']
|
||
local p = DataManager.CurrenRoom:GetPlayerById(playerid)
|
||
self._room:RemovePlayer(p)
|
||
DispatchEvent(self._dispatcher, GameEvent.PlayerLeave, p)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 网络状态更新
|
||
function M:OnEventOnlineState(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local playerid = evt_data['aid']
|
||
local online = evt_data['online']
|
||
local player = DataManager.CurrenRoom:GetPlayerById(playerid)
|
||
if player ~= nil then
|
||
player.line_state = online
|
||
DispatchEvent(self._dispatcher, GameEvent.PlayerState, player)
|
||
end
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 玩家准备
|
||
function M:OnEventPlayerReady(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local pid = evt_data['aid']
|
||
local p = self._room:GetPlayerById(pid)
|
||
p.ready = true
|
||
if evt_data.start~=nil then
|
||
if evt_data.start==1 then
|
||
p.isSendCardState=true
|
||
else
|
||
p.isSendCardState=false
|
||
end
|
||
|
||
else
|
||
p.isSendCardState=false
|
||
end
|
||
|
||
DispatchEvent(self._dispatcher, GameEvent.PlayerReady, p)
|
||
end
|
||
)
|
||
end
|
||
|
||
|
||
function M:OnEventPlayerXiPaiReady(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local pid = evt_data['aid']
|
||
local p = self._room:GetPlayerById(pid)
|
||
p.ready = true
|
||
if evt_data.start~=nil then
|
||
if evt_data.start==1 then
|
||
p.isSendCardState=true
|
||
else
|
||
p.isSendCardState=false
|
||
end
|
||
|
||
else
|
||
p.isSendCardState=false
|
||
end
|
||
|
||
DispatchEvent(self._dispatcher, GameEvent.PlayerReady, p)
|
||
end
|
||
)
|
||
end
|
||
|
||
|
||
|
||
-- 聊天事件
|
||
function M:OnEventInteraction(evt_data)
|
||
if self._room.ban_chat1 == false or self._room.ban_chat2 == false then
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local playerid = evt_data['playerid']
|
||
local p = self._room:GetPlayerById(playerid)
|
||
local type1 = evt_data['type']
|
||
local parm = evt_data['parm']
|
||
DispatchEvent(self._dispatcher, GameEvent.Interaction, p, type1, parm)
|
||
end
|
||
)
|
||
end
|
||
end
|
||
|
||
-- GPS更新事件
|
||
function M:OnEventUpdateGPS(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local seat = evt_data['seat']
|
||
local pos = evt_data['pos']
|
||
if seat == 0 or seat == 'skip' then
|
||
return
|
||
end
|
||
local p = self._room:GetPlayerBySeat(seat)
|
||
p.self_user.location = Location.new(pos)
|
||
end
|
||
)
|
||
end
|
||
|
||
|
||
-- 被踢出房间事件
|
||
function M:OnEventKicked()
|
||
if ViewManager.GetCurrenView().dview_class == LobbyView then
|
||
DataManager.CurrenRoom = nil
|
||
ControllerManager.SetGameNetClient(nil, true)
|
||
ControllerManager.ChangeController(LoddyController)
|
||
return
|
||
end
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
DataManager.CurrenRoom = nil
|
||
ControllerManager.SetGameNetClient(nil, true)
|
||
ControllerManager.ChangeController(LoddyController)
|
||
DispatchEvent(self._dispatcher, GameEvent.OnKicked)
|
||
end
|
||
)
|
||
end
|
||
|
||
-- 托管
|
||
function M:Entrust(ok, info)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
local data = {}
|
||
data.ok = ok
|
||
data.info = info
|
||
_client:send(Protocol.GAME_ENTRUST, data)
|
||
end
|
||
|
||
-- 入座
|
||
function M:JoinSeat(callback)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
_client:send(Protocol.GAME_JOIN_SEAT, nil, function(res)
|
||
if res.ReturnCode == 0 then
|
||
self._room.self_player.spectator = false
|
||
end
|
||
if callback then
|
||
callback(res)
|
||
end
|
||
end)
|
||
end
|
||
|
||
-- 更新事件
|
||
function M:OnEvtUpdateInfo(evt_data)
|
||
self._cacheEvent:Enqueue(
|
||
function()
|
||
local pid = evt_data['aid']
|
||
local p = self._room:GetPlayerById(pid)
|
||
local t = evt_data['type']
|
||
if t == 5 then
|
||
p.entrust = evt_data['entrust']
|
||
end
|
||
DispatchEvent(self._dispatcher, GameEvent.OnUpdateInfo, p, t)
|
||
end
|
||
)
|
||
end
|
||
|
||
function M:ClearPlayerData()
|
||
local _room = self._room
|
||
for i = 1, #_room.player_list do
|
||
_room.player_list[i]:Clear()
|
||
end
|
||
end
|
||
|
||
function M:PopEvent()
|
||
local _cacheEvent = self._cacheEvent
|
||
if (_cacheEvent:Count() > 0) then
|
||
return _cacheEvent:Dequeue()
|
||
end
|
||
end
|
||
|
||
local function list_add(list, val)
|
||
if not list_check(list, val) then
|
||
table.insert(list, val)
|
||
end
|
||
end
|
||
-- 检查 gps距离、ip地址
|
||
function M:CheckGPS()
|
||
local plist = {}
|
||
local warnList = {}
|
||
local ipList = {}
|
||
local player_list = self._room.player_list
|
||
-- GPS
|
||
for i = 1, #player_list do
|
||
local player1 = player_list[i]
|
||
if player1.self_user ~= DataManager.SelfUser then
|
||
local loc1 = player1.self_user.location
|
||
if not loc1 or loc1.default then
|
||
-- 数据类型:gps警告,只有此处使用了
|
||
-- type:0.没有打开gps,1.距离过近,2.ip地址一样
|
||
local warn = {}
|
||
warn.type = 0
|
||
warn.seat = player1.seat
|
||
return true
|
||
elseif i < #player_list then
|
||
for j = i + 1, #player_list do
|
||
local player2 = player_list[j]
|
||
if player2.self_user ~= DataManager.SelfUser then
|
||
local loc2 = player2.self_user.location
|
||
if not loc2 or loc2.default then
|
||
local warn = {}
|
||
warn.type = 0
|
||
warn.seat = player2.seat
|
||
return true
|
||
else
|
||
local dist = loc1:CalcDistance(loc2)
|
||
if dist < 0.2 then
|
||
local warn = {}
|
||
warn.type = 1
|
||
warn.seat = player1.seat
|
||
warn.seat2 = player2.seat
|
||
return true
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
local p = player1
|
||
if p.self_user ~= DataManager.SelfUser then
|
||
local ip = p.self_user.host_ip
|
||
if not list_check(ipList, ip) then
|
||
table.insert(ipList, ip)
|
||
else
|
||
return true
|
||
end
|
||
end
|
||
end
|
||
return false
|
||
end
|
||
|
||
function M:GetGPS()
|
||
-- if not DataManager.SelfUser.location.default then return end
|
||
-- 获取GPS
|
||
get_gps(
|
||
function()
|
||
self:SendGPS(DataManager.SelfUser.location:Location2String())
|
||
end
|
||
)
|
||
end
|
||
|
||
function M:OnEnter()
|
||
if (debug_print) then
|
||
print(self._name .. '进入Game控制器')
|
||
end
|
||
self._room = DataManager.CurrenRoom
|
||
local _client = ControllerManager.GameNetClinet
|
||
_client.onevent:Add(self.__OnNetEvent, self)
|
||
|
||
-- self:GetGPS()
|
||
end
|
||
|
||
function M:OnExit()
|
||
if (debug_print) then
|
||
print(self._name .. ' 离开Game控制器')
|
||
end
|
||
ControllerManager.SetGameNetClient(nil)
|
||
self._cacheEvent:Clear()
|
||
end
|
||
|
||
function M:__OnNetEvent(msg)
|
||
--print("Game消息ID===>>"..msg.Command)
|
||
local func = self._eventmap[msg.Command]
|
||
if (func ~= nil) then
|
||
func(self, msg.Data)
|
||
end
|
||
end
|
||
|
||
function M:ReturnToRoom()
|
||
local roomCtr = ControllerManager.GetController(RoomController)
|
||
roomCtr:PublicJoinRoom(
|
||
Protocol.WEB_FG_JOIN_ROOM,
|
||
self.tmpRoomID,
|
||
false,
|
||
function(response)
|
||
if (response.ReturnCode == -1) then
|
||
ViewUtil.CloseModalWait('join_room')
|
||
RestartGame()
|
||
return
|
||
end
|
||
ViewManager.ChangeView(ViewManager.View_Main, DataManager.CurrenRoom.game_id)
|
||
ViewUtil.CloseModalWait('join_room')
|
||
end,
|
||
self.tmpGroupID
|
||
)
|
||
|
||
end
|
||
|
||
|
||
function M:OnEvtOpenTupGTips(msg)
|
||
--print("显示托管倒计时=====================")
|
||
pt(msg)
|
||
local pid = msg['aid']
|
||
local p = self._room:GetPlayerById(pid)
|
||
local t=msg['time']
|
||
DispatchEvent(self._dispatcher, GameEvent.TupGuanOpen, p,true, t)
|
||
end
|
||
|
||
|
||
function M:OnEvtCloseTupGTips(msg)
|
||
--print("关闭托管倒计时=================")
|
||
--pt(msg)
|
||
local pid = msg['aid']
|
||
local p = self._room:GetPlayerById(pid)
|
||
local t=msg['time']
|
||
DispatchEvent(self._dispatcher, GameEvent.TupGuanOpen, p,false, t)
|
||
end
|
||
|
||
|
||
function M:DispatchEventTuoGuan(p,isShow,t)
|
||
DispatchEvent(self._dispatcher, GameEvent.TupGuanOpen, p,isShow, t)
|
||
end
|
||
|
||
|
||
|
||
function M:OnEvtOpenGameHuTuoGtips(isAuto)
|
||
local _client = ControllerManager.GameNetClinet
|
||
if not _client then
|
||
return
|
||
end
|
||
local data = {}
|
||
data.autoCard = isAuto
|
||
_client:send(Protocol.GAME_AUTO_CARD, data)
|
||
end |