RoomController = {} local M = {} --- Create a new RoomController function RoomController.new() setmetatable(M, { __index = IController }) local self = setmetatable({}, { __index = M }) self.baseType = RoomController self.class = "Room" return self end local function __ConntectGameServer(cmd, room, host, _data, callback) -- local _data = {} _data["session"] = room.session local _game_client = NetClient.new(host, "game") _game_client:connect() print("lingmeng SetGameNetClient code", _game_client) ControllerManager.SetGameNetClient(_game_client) _game_client.onconnect:Add(function(code) print("lingmeng clent code", code) if (code == SocketCode.Connect) then _game_client:send(cmd, _data, function(response) print("lingmeng clent code2", cmd, response.ReturnCode) if (response.ReturnCode == 0) then _game_client.onconnect:Clear() if cmd == Protocol.GAME_WITNESS_ROOM then _game_client.onconnect:Add(ControllerManager.OnConnectWitness) else _game_client.onconnect:Add(ControllerManager.OnConnect) end callback(response) return end _game_client:destroy() if ControllerManager.GameNetClinet == _game_client then ControllerManager.GameNetClinet = nil end callback(response) end) else if ControllerManager.GameNetClinet == _game_client then ControllerManager.GameNetClinet = nil end _game_client:destroy() callback({ ReturnCode = 101 }) end end) -- return _game_client end function M:CreateRoom(game_id, _data, callback) local _client = ControllerManager.WebClient local data = {} data.game_id = game_id data["platform"] = GetPlatform() data.config_data = _data -- local runtime = os.clock() _client:send(Protocol.WEB_CREATE_ROOM, data, function(res) -- if ( res.ReturnCode == Table_Error_code.ERR_IN_ROOM or res.ReturnCode == Table_Error_code.ERR_CREATE_ROOM) then -- self:JoinRoom("000000",callback) -- return -- end if (res.ReturnCode == 0) then local json = res.Data local game_info = json["game_info"] if ExtendHotupdate.CheckVersion(game_info) ~= ExtendHotupdate.VERSION_NORMAL then ExtendHotupdate.UpdateGame(game_info, function() res.ReturnCode = -2 callback(res) end) return end local game_id = game_info.game_id local room_id = json["room_id"] local server_ip = json["server_ip"] local server_port = json["server_port"] local room = ExtendManager.GetExtendConfig(game_id):NewRoom() room.game_id = game_id room.game_info = game_info room.room_id = room_id room.server_host = string.concat(server_ip, ":", server_port) room.session = _client:getSession() DataManager.CurrenRoom = room local game_net = nil local j_data = {} j_data["session"] = room.session if not DataManager.SelfUser.location then DataManager.SelfUser.location = Location.new() end j_data["pos"] = DataManager.SelfUser.location:Location2String() -- game_net = __ConntectGameServer(Protocol.GAME_JOIN_ROOM, room, room.server_host, j_data, function(response) if (response.ReturnCode == 0) then -- game_net:clearEvent() local _s2croom = response.Data -- ControllerManager.SetGameNetClient(game_net) room.owner_id = _s2croom["owner"] room.banker_seat = _s2croom["manor"] if _s2croom.createTime then room.create_time = _s2croom["createTime"] end room.agent = _s2croom.agent == 1 and true or false local extend = ExtendManager.GetExtendConfig(room.game_id) extend:FillRoomData(_s2croom) ControllerManager.ChangeController(GameController) -- -- print("create room:"..(os.clock()-runtime)) callback(response) return end -- game_net:destroy() callback(response) end) else if callback then callback(res) end end end) end local join_room_frame = 0 function M:PublicJoinRoom(cmd, roomid, tem, callback, group_id, pid) printlog("公共进入房间接口=============PublicJoinRoom") print("test,亲友圈ID是:", group_id) -- 同一帧不重复调用 local last_frame = join_room_frame join_room_frame = Time.frameCount if join_room_frame == last_frame then return end -- 防止游戏没有离开控制器 ControllerManager.ChangeController(LoddyController) local _data = {} if cmd == Protocol.WEB_FG_MATCH_ROOM then _data["is_null"] = tem --获得玩法配置定位信息,如果开启了配置则必须强制有定位才能进入房间 local play = DataManager.groups:get(group_id):getPlay(pid) local pos = "" if play and play.config then local config = json.decode(play.config) if config.GPSDetection and config.GPSDetection > 0 then if not DataManager.SelfUser.location then get_gps(nil, { _callback_lad = function(flag) if flag then self:PublicJoinRoom(cmd, roomid, tem, callback, group_id, pid) return end ViewUtil.ErrorMsg(nil, 55, "未开启gps定位,不能创建gps定位房") end }) return end pos = DataManager.SelfUser.location:Location2String() end end _data["pos"] = pos if ControllerManager.GameNetClinet then return end elseif cmd == Protocol.WEB_FG_QUEUE_ROOM then _data["is_null"] = tem _data["room_id"] = roomid else _data["room_id"] = roomid end _data["id"] = group_id _data["pid"] = pid _data["platform"] = GetPlatform() pt(_data) local _client = ControllerManager.GroupClient _client:send(cmd, _data, function(res) printlog("++++++++++++111111111111111111111111111111111111") pt(res) if (res.ReturnCode == 0) then local json = res.Data local game_info = json["game_info"] pt(game_info) if ExtendHotupdate.CheckVersion(game_info) ~= ExtendHotupdate.VERSION_NORMAL then ExtendHotupdate.UpdateGame(game_info, function() -- res.ReturnCode = -2 -- callback(res) self:PublicJoinRoom(Protocol.WEB_FG_JOIN_ROOM, json["room_id"], tem, callback, group_id, pid) end) return end local game_id = game_info.game_id local server_ip = json["server_ip"] local server_port = json["server_port"] local room = ExtendManager.GetExtendConfig(game_id):NewRoom() room.lev = json["lev"] -- 自己在当前房间所在圈子的职位,1盟主,2管理员,3用户,非圈子房间就是3 room.room_id = json["room_id"] room.game_id = game_id room.status = json["status"] room.server_host = string.concat(server_ip, ":", server_port) room.session = _client:getSession() -- 圈子信息,圈子id和玩法id room.group_id = json["groupId"] room.game_info = game_info -- 圈子禁止文字聊天,禁止语音聊天 --room.ban_chat1 = json["ban_chat1"] --room.ban_chat2 = json["ban_chat2"] -- -- 玩法id room.play_id = json["pid"] -- 体力值开关 room.hpOnOff = json["hpOnOff"] -- 体力值倍数 room.score_times = d2ad(json.hp_times) or 1 DataManager.CurrenRoom = room local j_data = {} if not DataManager.SelfUser.location then DataManager.SelfUser.location = Location.new() end j_data["pos"] = DataManager.SelfUser.location:Location2String() printlog("++++++++++++++++++++++++++++++++++++++++++") __ConntectGameServer(Protocol.GAME_JOIN_ROOM, room, room.server_host, j_data, function(res1) printlog("===============================-------------") pt(res1) ControllerManager.IsSendCard = false if (res1.ReturnCode ~= 0) then if (callback) then callback(res1) end else ControllerManager.enterPlayerData = res1.Data.tableInfo.playerData local _s2croom = res1.Data room.owner_id = _s2croom["owner"] if _s2croom.createTime then room.create_time = _s2croom["createTime"] end if _s2croom.manor then room.banker_seat = _s2croom.manor end room.agent = _s2croom.agent == 1 and true or false -- ControllerManager.SetGameNetClient(game_net) local extend = ExtendManager.GetExtendConfig(room.game_id) extend:FillRoomData(_s2croom) ControllerManager.ChangeController(GameController) local gamectr = ControllerManager.GetCurrenController() gamectr.tmpRoomID = room.room_id gamectr.tmpGroupID = room.group_id if callback then callback(res1) end end end) else if callback then callback(res) end end end) end local witness_room_frame = 0 ---comment 房间观战进入协议 ---@param cmd string 协议号 ---@param roomid number 房间号 ---@param group_id number 亲友圈id ---@param callback function 回调函数 ---@param game_id number 游戏id function M:PublicWitnessRoom(cmd, roomid, group_id, callback, game_id, pid) printlog("公共观战房间接口=============PublicWitnessRoom") -- 同一帧不重复调用 local last_frame = witness_room_frame witness_room_frame = Time.frameCount if witness_room_frame == last_frame then return end -- 防止游戏没有离开控制器 ControllerManager.ChangeController(RoomController) local _data = {} _data["room_id"] = roomid _data["id"] = group_id _data["platform"] = GetPlatform() pt(_data) local _client = ControllerManager.GroupClient _client:send(cmd, _data, function(res) pt(res) if (res.ReturnCode == 0) then local json = res.Data local server_ip = json["server_ip"] local server_port = json["server_port"] local room = ExtendManager.GetExtendConfig(game_id):NewRoom() room.room_id = roomid room.game_id = game_id room.server_host = string.concat(server_ip, ":", server_port) room.session = _client:getSession() -- 圈子信息,圈子id和玩法id room.group_id = group_id -- 圈子禁止文字聊天,禁止语音聊天 room.ban_chat1 = 0 room.ban_chat2 = 0 -- 玩法id room.play_id = pid DataManager.CurrenRoom = room local j_data = {} if not DataManager.SelfUser.location then DataManager.SelfUser.location = Location.new() end j_data["pos"] = DataManager.SelfUser.location:Location2String() printlog("++++++++++++++++++++++++++++++++++++++++++") __ConntectGameServer(Protocol.GAME_WITNESS_ROOM, room, room.server_host, j_data, function(res1) printlog("===============================-------------") pt(res1) ControllerManager.IsSendCard = false if (res1.ReturnCode ~= 0) then if (callback) then callback(res1) end else ControllerManager.enterPlayerData = res1.Data.tableInfo.playerData local _s2croom = res1.Data room.owner_id = _s2croom["owner"] if _s2croom.createTime then room.create_time = _s2croom["createTime"] end if _s2croom.manor then room.banker_seat = _s2croom.manor end room.agent = _s2croom.agent == 1 and true or false -- ControllerManager.SetGameNetClient(game_net) local extend = ExtendManager.GetExtendConfig(room.game_id) extend:FillWitnessData(_s2croom) ControllerManager.ChangeController(GameController) local gamectr = ControllerManager.GetCurrenController() gamectr.tmpRoomID = room.room_id gamectr.tmpGroupID = room.group_id if callback then callback(res1) end end end) else if callback then callback(res) end end end) end function M:JoinRoom(room_id, callback) self:PublicJoinRoom(Protocol.WEB_JOIN_ROOM, room_id, false, callback, group_id, group_layer) end function M:WitnessRoom(room_id, callback) self:PublicWitnessRoom(Protocol.WEB_FG_Witness_ROOM, room_id, DataManager.CurrenRoom.group_id, callback, DataManager.CurrenRoom.game_id, DataManager.CurrenRoom.play_id) end function M:ResetJionRoom(callback) local _game = ControllerManager.GetController(GameController) local o_room = DataManager.CurrenRoom self:JoinRoom(o_room.room_id, callback) end function M:ResetWitnessRoom(callback) local _game = ControllerManager.GetController(GameController) local o_room = DataManager.CurrenRoom self:WitnessRoom(o_room.room_id, callback) end