587 lines
18 KiB
Lua
587 lines
18 KiB
Lua
--- Base GameEvent
|
||
|
||
GroupMgrEvent = {
|
||
AddPlay = "add_play",
|
||
DelPlay = "del_play",
|
||
UpdatePlay = "update_play",
|
||
AddRoom = "add_room",
|
||
DelRoom = "del_room",
|
||
UpdateRoom = "update_room",
|
||
UpdatePlayerInfo = "update_player_info",
|
||
UpdateMessage = "update_msg",
|
||
BeInvited = "be_invited",
|
||
UpdateGroup = "UpdateGroup",
|
||
NewMailTip = "NewMailTip",
|
||
InviteResponse = "InviteResponse",
|
||
IsOpenChatRoom = "IsOpenChatRoom",
|
||
ChatRoomData = "ChatRoomData",
|
||
OnNewApply = "OnNewApply",
|
||
OnNewRecord = "OnNewRecord",
|
||
OnMemberState = "OnMemberState",
|
||
Onpush_assistant = "Onpush_assistant",
|
||
OnFamilyReflash = "OnFamilyReflash",
|
||
OnFamilyRoomReflash = "OnFamilyRoomReflash",
|
||
OnFamilyMemberOut = "OnFamilyMemberOut",
|
||
}
|
||
|
||
GroupMgrController = {
|
||
_name = "",
|
||
--事件MAP
|
||
_eventmap = nil,
|
||
--事件缓存
|
||
_cacheEvent = nil,
|
||
--事件发送器
|
||
_dispatcher = nil
|
||
}
|
||
|
||
local M = GroupMgrController
|
||
|
||
--- Create a new GroupMgrController
|
||
function GroupMgrController.new()
|
||
setmetatable(M, { __index = IController })
|
||
local self = setmetatable({}, { __index = M })
|
||
self.baseType = GroupMgrController
|
||
self._cacheEvent = Queue.new(1000)
|
||
self._eventmap = {}
|
||
self._dispatcher = {}
|
||
|
||
self._eventmap[Protocol.FGMGR_EVT_ADD_PLAY] = self.OnEvtAddPlay
|
||
self._eventmap[Protocol.FGMGR_EVT_DEL_PLAY] = self.OnEvtDelPlay
|
||
self._eventmap[Protocol.FGMGR_EVT_UPDATE_PLAY] = self.OnEvtUpdatePlay
|
||
self._eventmap[Protocol.FGMGR_EVT_ADD_ROOM] = self.OnEvtAddRoom
|
||
self._eventmap[Protocol.FGMGR_EVT_DEL_ROOM] = self.OnEvtDelRoom
|
||
self._eventmap[Protocol.FGMGR_EVT_UPDATE_ROOM] = self.OnEvtUpdateRoom
|
||
self._eventmap[Protocol.FGMGR_EVT_UPDATE_PLAYER_INFO] = self.OnEvtUpdatePlayerInfo
|
||
self._eventmap[Protocol.FGMGR_EVT_MESSAGE] = self.OnEvtMessage
|
||
self._eventmap[Protocol.FGMGR_EVT_INVITED] = self.OnEvtInvited
|
||
self._eventmap[Protocol.FGMGR_EVT_UPDATE_GROUP] = self.OnEvtUpdateGroup
|
||
self._eventmap[Protocol.FGMGR_EVT_NEW_MAIL] = self.OnEvtNewMailTip
|
||
self._eventmap[Protocol.FGMGR_RESPONSE_INVITE] = self.FG_ResponseInvited
|
||
self._eventmap[Protocol.FGMGR_EVT_ISOPEN_CHATROOM] = self.FG_Isopen_ChatRoom
|
||
self._eventmap[Protocol.FGMGR_EVT_DATA_CHATROOM] = self.FG_Data_ChatRoom
|
||
self._eventmap[Protocol.FGMGR_EVT_NEWCHAT] = self.FG_Data_NewChat
|
||
self._eventmap[Protocol.FGMGR_EVT_Member_State] = self.FG_Data_Member_State
|
||
self._eventmap[Protocol.FGMGR_EVT_Push_Assistant] = self.OnEVT_Push_Assistant
|
||
self._eventmap[Protocol.WEB_FG_REFLASH] = self.FG_Data_Family_Reflash
|
||
self._eventmap[Protocol.WEB_FG_MEMBER_OUT] = self.OnMemberOut
|
||
-- self:connect(callback)
|
||
return self
|
||
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:RemoveEventListener(evt_name)
|
||
self._dispatcher[evt_name] = nil
|
||
end
|
||
|
||
----------------------请求------------------------------------
|
||
function M:connect(host, groupId, callback)
|
||
self.host = host
|
||
self.groupId = groupId
|
||
self.connecting = true
|
||
if self._mgr_client then
|
||
self._mgr_client:destroy()
|
||
self._mgr_client = nil
|
||
end
|
||
-- print("666666666666666666666666666 ", host)
|
||
local _mgr_client = NetClient.new(self.host, "mgr_group")
|
||
self._mgr_client = _mgr_client
|
||
_mgr_client:connect()
|
||
_mgr_client.onconnect:Add(function(code)
|
||
self.code = code
|
||
if (code == SocketCode.Connect) then
|
||
local _data = {}
|
||
_data.session = ControllerManager.WebClient:getSession()
|
||
if _data.session == nil then
|
||
self.connecting = false
|
||
_mgr_client:destroy()
|
||
ViewUtil.ErrorTip(1234567, "圈子数据异常!!!")
|
||
return
|
||
end
|
||
_data.groupId = self.groupId
|
||
--printlog(debug.traceback())
|
||
--printlog("session===1111111111>>>", _data.session)
|
||
--pt(_data)
|
||
_mgr_client:send(Protocol.FGMGR_ENTER_GROUP, _data, function(res)
|
||
self.connecting = false
|
||
if res.ReturnCode == 0 then
|
||
-- printlog("2222222222222222222222222222222")
|
||
-- pt(res)
|
||
_mgr_client.onevent:Add(self.__OnNetEvent, self)
|
||
_mgr_client.onconnect:Add(self.__OnConnect, self)
|
||
if callback then
|
||
self:OnEnter()
|
||
end
|
||
local group = DataManager.groups:get(groupId)
|
||
group:clearPlay()
|
||
local play_list = res.Data.play_list
|
||
printlog("2222222222222222")
|
||
pt(res.Data)
|
||
pt(play_list)
|
||
for i = 1, #play_list do
|
||
local m = play_list[i]
|
||
group:addPlay(m)
|
||
end
|
||
local rooms = res.Data.rooms
|
||
for i = 1, #rooms do
|
||
local m = rooms[i]
|
||
if m.status ~= 2 then
|
||
group:addRoom(m)
|
||
end
|
||
end
|
||
group.ban = res.Data.ban
|
||
group.ban_ip = res.Data.ban_ip
|
||
group.ban_gps = res.Data.ban_gps
|
||
group.hp = res.Data.hp
|
||
group.lev = res.Data.lev
|
||
group.joins = res.Data.joins
|
||
group.kick_opt = res.Data.kick_opt
|
||
group.partnerLev = res.Data.partnerLev
|
||
group.dissolve_opt = res.Data.dissolve_opt
|
||
group.permission = res.Data.permission
|
||
group.diamo = res.Data.diamo or 0
|
||
group.apply = res.Data.ban_apply or 0
|
||
group.update_room = true
|
||
group.mail_tip = res.Data.mail_tip
|
||
group.ban_chat1 = res.Data.ban_chat1
|
||
group.ban_chat2 = res.Data.ban_chat2
|
||
group.isvip = res.Data.isvip
|
||
group.isWatch = res.Data.isWatch
|
||
group.joinsData = {}
|
||
else
|
||
self.code = SocketCode.ExceptionOnConnect
|
||
_mgr_client:destroy()
|
||
end
|
||
if callback then
|
||
callback(res)
|
||
end
|
||
end)
|
||
else
|
||
self.connecting = false
|
||
_mgr_client:destroy()
|
||
if callback then
|
||
callback({ ReturnCode = 101 })
|
||
end
|
||
end
|
||
end)
|
||
end
|
||
|
||
function M:disconnect()
|
||
self.connecting = false
|
||
if self._mgr_client then
|
||
self._mgr_client:destroy()
|
||
end
|
||
self.host = nil
|
||
self.groupId = nil
|
||
if self._reconnect then
|
||
self._reconnect:Stop()
|
||
self._reconnect = nil
|
||
end
|
||
end
|
||
|
||
--------------------事件-----------------------------------
|
||
|
||
|
||
-- 添加玩法
|
||
function M:OnEvtAddPlay(evt_data)
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group:addPlay(evt_data)
|
||
local pid = evt_data.id
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.AddPlay, pid)
|
||
end
|
||
|
||
-- 删除玩法
|
||
function M:OnEvtDelPlay(evt_data)
|
||
local pid = evt_data.pid
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group:delPlay(pid)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.DelPlay)
|
||
end
|
||
|
||
-- 更新玩法
|
||
function M:OnEvtUpdatePlay(evt_data)
|
||
---- print("更新玩法=============》》》")
|
||
--pt(evt_data)
|
||
local pid = evt_data.pid
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group:addPlay(evt_data)
|
||
group.update_play = true
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.UpdatePlay)
|
||
end
|
||
|
||
-- 添加房间
|
||
function M:OnEvtAddRoom(evt_data)
|
||
-- local group = DataManager.groups:get(self.groupId)
|
||
-- group:addRoom(evt_data)
|
||
-- group.update_room = true
|
||
-- DispatchEvent(self._dispatcher,GroupMgrEvent.AddRoom)
|
||
end
|
||
|
||
-- 删除玩法
|
||
function M:OnEvtDelRoom(evt_data)
|
||
-- local roomid = evt_data.roomid
|
||
-- local group = DataManager.groups:get(self.groupId)
|
||
-- group:delRoom(roomid)
|
||
-- group.update_room = true
|
||
-- DispatchEvent(self._dispatcher,GroupMgrEvent.DelRoom)
|
||
end
|
||
|
||
-- 更新房间
|
||
function M:OnEvtUpdateRoom(evt_data)
|
||
local group = DataManager.groups:get(self.groupId)
|
||
local cmds = evt_data.cmds
|
||
for i = 1, #cmds do
|
||
local cmd = cmds[i]
|
||
local ct = cmd["$ct"]
|
||
if ct == 3 then
|
||
group:delRoom(cmd.roomid)
|
||
else
|
||
group:addRoom(cmd)
|
||
end
|
||
end
|
||
group.update_room = true
|
||
|
||
-- ↓↓刷新成员状态
|
||
|
||
for _, player in pairs(group.members) do
|
||
player.playing = "stopPlaying"
|
||
end
|
||
|
||
for _, room in pairs(cmds) do
|
||
for _, palyingUser in pairs(room.plist or {}) do
|
||
local _player = group.memberMap[palyingUser.aid]
|
||
_player.playing = "startPlaying"
|
||
end
|
||
end
|
||
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnFamilyRoomReflash, self.groupId)
|
||
end
|
||
|
||
function M:OnEvtUpdatePlayerInfo(evt_data)
|
||
--type:1体力值 2管理员等级 3合伙人等级
|
||
local itype = evt_data.type
|
||
local value = evt_data.value
|
||
local group = DataManager.groups:get(self.groupId)
|
||
local memb = group:getMember(DataManager.SelfUser.account_id)
|
||
if itype == 1 then
|
||
if memb then
|
||
memb.hp = value
|
||
end
|
||
group.hp = value
|
||
elseif itype == 2 then
|
||
memb.lev = value
|
||
group.lev = value
|
||
elseif itype == 3 then
|
||
memb.partnerLev = value
|
||
group.partnerLev = value
|
||
elseif itype == 4 then
|
||
group.diamo = evt_data.diamo
|
||
end
|
||
group.update_info = true
|
||
-- DispatchEvent(self._dispatcher,GroupMgrEvent.UpdatePlayerInfo)
|
||
end
|
||
|
||
-- 更新申请列表
|
||
function M:OnEvtMessage(evt_data)
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group.joins = evt_data.joins
|
||
group.update_joins = true
|
||
|
||
local msg = {}
|
||
msg.nick = evt_data.nick
|
||
msg.id = evt_data.uid
|
||
msg.portrait = evt_data.portrait
|
||
msg.tag = evt_data.remark
|
||
|
||
group.joinsData = group.joinsData or {}
|
||
|
||
group.joinsData[#group.joinsData + 1] = msg
|
||
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnNewApply, evt_data)
|
||
end
|
||
|
||
-- 被邀请事件
|
||
function M:OnEvtInvited(evt_data)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.BeInvited, evt_data)
|
||
end
|
||
|
||
-- 更新圈子
|
||
function M:OnEvtUpdateGroup(evt_data)
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group.name = evt_data.name
|
||
group.ban = evt_data.ban == 1
|
||
group.notice = evt_data.notice
|
||
group.option = evt_data.option
|
||
group.show_num = evt_data.show_num
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.UpdateGroup, evt_data)
|
||
end
|
||
|
||
-- 未读邮件通知
|
||
function M:OnEvtNewMailTip(evt_data)
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group.mail_tip = 1
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.NewMailTip, evt_data)
|
||
end
|
||
|
||
-- 获取在线玩家
|
||
function M:FG_GetOnlinePlayers(callback)
|
||
self._mgr_client:send(Protocol.FGMGR_GET_ONLINE_PLAYERS, nil, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
--设置允许成员可以查看聊天室
|
||
function M:FG_OPEN_ISOPEN_CHATROOM(groupId, callback)
|
||
local _data = {}
|
||
_data.groupId = groupId
|
||
_data.uid = DataManager.SelfUser.account_id
|
||
self._mgr_client:send(Protocol.FGMGR_EVT_OPEN_ISOPEN_CHATROOM, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 设置不允许成员可以查看聊天室
|
||
function M:FG_CLOSE_ISOPEN_CHATROOM(groupId, callback)
|
||
local _data = {}
|
||
_data.groupId = groupId
|
||
_data.uid = DataManager.SelfUser.account_id
|
||
self._mgr_client:send(Protocol.FGMGR_EVT_CLOSE_ISOPEN_CHATROOM, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 拉取聊天室数据
|
||
function M:FG_ENTER_CHATROOM(groupId, getData, callback)
|
||
local _data = {}
|
||
_data.groupId = groupId
|
||
_data.uid = DataManager.SelfUser.account_id
|
||
_data.getData = getData
|
||
self._mgr_client:send(Protocol.FGMGR_EVT_ENTER_CHATROOM, _data, function(res)
|
||
--[[
|
||
if _data.getData then
|
||
local group = DataManager.groups:get(_data.groupId)
|
||
group.records = res.Data.records
|
||
end
|
||
]]
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 邀请在线玩家
|
||
function M:FG_InvitePlayer(group_id, tag, player_id, roomid, pid, game_name, callback)
|
||
local _data = {}
|
||
_data.groupId = group_id
|
||
_data.tagId = tag
|
||
_data.player_id = player_id
|
||
_data.roomid = roomid
|
||
_data.pid = pid
|
||
_data.g_name = game_name
|
||
self._mgr_client:send(Protocol.FGMGR_INVITE_PLAYER, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 进入亲友圈
|
||
function M:FG_Get_Online_Member(groupId, callback)
|
||
local _data = {}
|
||
_data.id = groupId
|
||
_data.uid = DataManager.SelfUser.account_id
|
||
--local _client = ControllerManager.GroupClient
|
||
self._mgr_client:send(Protocol.WEB_FG_GET_ONLINE_MEMBER, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 退出亲友圈
|
||
function M:FG_Get_Offline_Member(groupId, callback)
|
||
local _data = {}
|
||
_data.id = groupId
|
||
_data.uid = DataManager.SelfUser.account_id
|
||
--local _client = ControllerManager.GroupClient
|
||
self._mgr_client:send(Protocol.WEB_FG_GET_OFFLINE_MEMBER, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
-- 回复邀请
|
||
-- function M:FG_ResponseInvited(id, refuse)
|
||
-- local _data = {}
|
||
-- _data.invi_id = id
|
||
-- _data.refuse = refuse
|
||
-- self._mgr_client:send(Protocol.FGMGR_RESPONSE_INVITE, _data)
|
||
-- end
|
||
|
||
--被邀请玩家收到邀请
|
||
function M:FG_ResponseInvited(evt_data)
|
||
local invite_id = evt_data.invite_id
|
||
local g_name = evt_data.g_name
|
||
local roomid = evt_data.roomid
|
||
local pid = evt_data.pid
|
||
local groupid = evt_data.groupId
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.InviteResponse, invite_id, g_name, roomid, pid, groupid)
|
||
end
|
||
|
||
-- 设置助理,来用给对面刷新界面 tagId=助理
|
||
function M:FG_Update_Assistant(groupId, tagId, callback)
|
||
local _data = {}
|
||
_data.id = groupId
|
||
_data.tagId = tagId
|
||
self._mgr_client:send(Protocol.FGMGR_EVT_Update_Assistant, _data, function(res)
|
||
callback(res)
|
||
end)
|
||
end
|
||
|
||
function M:OnEVT_Push_Assistant(evt_data)
|
||
print("收到助理刷新推送")
|
||
pt(evt_data)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.Onpush_assistant, evt_data)
|
||
end
|
||
|
||
function M:FG_Isopen_ChatRoom(evt_data)
|
||
print("收到是否开启聊天室推送")
|
||
pt(evt_data)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.IsOpenChatRoom, evt_data)
|
||
end
|
||
|
||
function M:FG_Data_ChatRoom(evt_data)
|
||
print("收到聊天室数据")
|
||
pt(evt_data)
|
||
|
||
if evt_data.getData then
|
||
local group = DataManager.groups:get(evt_data.groupId)
|
||
group.records = evt_data.records
|
||
end
|
||
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.ChatRoomData, evt_data)
|
||
end
|
||
|
||
function M:FG_Data_NewChat(evt_data)
|
||
print("收到新聊天室推送")
|
||
pt(evt_data)
|
||
local gid = evt_data.gid
|
||
local messageCount = evt_data.messageCount
|
||
local group = DataManager.groups:get(gid)
|
||
group.messageCount = tonumber(messageCount)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnNewRecord, evt_data)
|
||
end
|
||
|
||
function M:FG_Data_Member_State(evt_data)
|
||
print("收到成员状态推送")
|
||
pt(evt_data)
|
||
local offlineUserId = evt_data.offlineUserId
|
||
local onlineUserId = evt_data.onlineUserId
|
||
local playingUserId = evt_data.playingUserId
|
||
|
||
local group = DataManager.groups:get(self.groupId)
|
||
|
||
for _, uid in pairs(offlineUserId) do
|
||
local player = group.memberMap[uid]
|
||
if player then
|
||
player.online = 0
|
||
end
|
||
end
|
||
|
||
for _, uid in pairs(onlineUserId) do
|
||
local player = group.memberMap[uid]
|
||
if player then
|
||
player.online = 1
|
||
end
|
||
end
|
||
|
||
for _, p in pairs(group.memberMap) do
|
||
p.playing = "stopPlaying"
|
||
end
|
||
for _, uid in pairs(playingUserId) do
|
||
local player = group.memberMap[uid]
|
||
if player then
|
||
player.playing = "startPlaying"
|
||
end
|
||
end
|
||
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnMemberState, evt_data)
|
||
end
|
||
|
||
function M:FG_Data_Family_Reflash(evt_data)
|
||
print("收到家族刷新推送")
|
||
pt(evt_data)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnFamilyReflash, evt_data)
|
||
end
|
||
|
||
function M:OnMemberOut(evt_data)
|
||
print("收到成员退出推送")
|
||
pt(evt_data)
|
||
DispatchEvent(self._dispatcher, GroupMgrEvent.OnFamilyMemberOut, evt_data)
|
||
end
|
||
|
||
function M:PopEvent()
|
||
local _cacheEvent = self._cacheEvent
|
||
if (_cacheEvent:Count() > 0) then
|
||
return _cacheEvent:Dequeue()
|
||
end
|
||
end
|
||
|
||
function M:OnEnter()
|
||
self._reconnect = Timer.New(function()
|
||
if not self.connecting and self.code ~= SocketCode.Connect then
|
||
local _client = self._mgr_client
|
||
if _client then
|
||
_client:destroy()
|
||
end
|
||
if not self.host or not self.groupId then return end
|
||
self:connect(self.host, self.groupId)
|
||
end
|
||
end, 2, -1, true)
|
||
self._reconnect:Start()
|
||
-- self._reconnect = coroutine.start(function()
|
||
-- while true do
|
||
-- coroutine.wait(2)
|
||
-- if not self.connecting and self.code ~= SocketCode.Connect then
|
||
-- self:connect()
|
||
-- end
|
||
-- end
|
||
-- end)
|
||
local _client = self._mgr_client
|
||
end
|
||
|
||
function M:OnExit()
|
||
if self._reconnect then
|
||
self._reconnect:Stop()
|
||
self._reconnect = nil
|
||
end
|
||
local _client = self._mgr_client
|
||
if _client then
|
||
_client:destroy()
|
||
end
|
||
local group = DataManager.groups:get(self.groupId)
|
||
group:clear()
|
||
self._cacheEvent:Clear()
|
||
end
|
||
|
||
function M:__OnNetEvent(msg)
|
||
print("消息ID===>>" .. msg.Command)
|
||
pt(msg)
|
||
local func = self._eventmap[msg.Command]
|
||
if (func ~= nil) then func(self, msg.Data) end
|
||
end
|
||
|
||
function M:__OnConnect(code)
|
||
self.code = code
|
||
if code ~= SocketCode.Connect then
|
||
local _client = self._mgr_client
|
||
self._mgr_client = nil
|
||
_client:destroy()
|
||
self._cacheEvent:Clear()
|
||
end
|
||
end
|
||
|
||
return M
|