hengyang_client/lua_probject/base_project/Game/Controller/GroupMgrController.lua

373 lines
11 KiB
Lua
Raw Normal View History

2025-04-01 10:48:36 +08:00
--- 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",
}
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: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: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
2025-04-11 12:49:08 +08:00
-- print("666666666666666666666666666 ", host)
2025-04-01 10:48:36 +08:00
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(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
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)
2025-04-11 12:49:08 +08:00
---- print("更新玩法=============》》》")
2025-04-01 10:48:36 +08:00
--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
end
function M:OnEvtUpdatePlayerInfo(evt_data)
--type1体力值 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
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_InvitePlayer(tag, roomid, pid, game_name, callback)
local _data = {}
_data.tagId = tag
_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_ResponseInvited(id, refuse)
local _data = {}
_data.invi_id = id
_data.refuse = refuse
self._mgr_client:send(Protocol.FGMGR_RESPONSE_INVITE, _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)
2025-04-11 12:49:08 +08:00
---- print("消息ID===>>"..msg.Command)
2025-04-01 10:48:36 +08:00
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