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

432 lines
14 KiB
Lua
Raw Normal View History

2025-04-01 10:48:36 +08:00
LoddyController = {}
local M = {}
--- Create a new LoddyController
function LoddyController.new()
2025-04-11 12:49:08 +08:00
setmetatable(M, { __index = IController })
local self = setmetatable({}, { __index = M })
2025-04-01 10:48:36 +08:00
self.baseType = LoddyController
self.class = "Loddy"
self.recordList = {}
return self
end
function M.OnEnter(self)
2025-04-11 12:49:08 +08:00
---- print(vardump(self,"loddy controller enter"))
2025-04-01 10:48:36 +08:00
end
function M.OnExit(self)
2025-04-11 12:49:08 +08:00
---- print(vardump(self,"loddy controller exit"))
2025-04-01 10:48:36 +08:00
end
local function __FillRoomData(s2croom)
local room = DataManager.CurrenRoom
local extend = ExtendManager.GetExtendConfig(room.game_id)
extend:FillRoomData(s2croom)
end
2025-04-11 12:49:08 +08:00
local function __ConntectGameServer(cmd, room, host, _data, callback)
2025-04-01 10:48:36 +08:00
-- local _data = {}
_data["session"] = room.session
local _game_client = NetClient.new(host, "game")
_game_client:connect()
ControllerManager.SetGameNetClient(_game_client)
_game_client.onconnect:Add(function(code)
if (code == SocketCode.Connect) then
2025-04-11 12:49:08 +08:00
_game_client:send(cmd, _data, function(response)
2025-04-01 10:48:36 +08:00
if (response.ReturnCode == 0) then
_game_client.onconnect:Clear()
_game_client.onconnect:Add(ControllerManager.OnConnect)
callback(response)
return
end
_game_client:destroy()
if ControllerManager.GameNetClinet == _game_client then
2025-04-11 12:49:08 +08:00
ControllerManager.GameNetClinet = nil
2025-04-01 10:48:36 +08:00
end
callback(response)
end)
else
if ControllerManager.GameNetClinet == _game_client then
2025-04-11 12:49:08 +08:00
ControllerManager.GameNetClinet = nil
2025-04-01 10:48:36 +08:00
end
_game_client:destroy()
2025-04-11 12:49:08 +08:00
callback({ ReturnCode = 101 })
2025-04-01 10:48:36 +08:00
end
end)
end
function M:CreateRoom(game_id, _data, callback)
2025-04-11 12:49:08 +08:00
local _client = ControllerManager.WebClient
local data = {}
data.game_id = game_id
2025-04-01 10:48:36 +08:00
data["platform"] = GetPlatform()
data.config_data = _data
-- local runtime = os.clock()
_client:send(Protocol.WEB_CREATE_ROOM, data, function(res)
2025-04-11 12:49:08 +08:00
if (res.ReturnCode == Table_Error_code.ERR_IN_ROOM or res.ReturnCode == Table_Error_code.ERR_CREATE_ROOM) then
self:JoinRoom("000000", callback)
2025-04-01 10:48:36 +08:00
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
2025-04-11 12:49:08 +08:00
ExtendHotupdate.UpdateGame(game_info, function()
2025-04-01 10:48:36 +08:00
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"]
2025-04-11 12:49:08 +08:00
local room = ExtendManager.GetExtendConfig(game_id):NewRoom()
2025-04-01 10:48:36 +08:00
room.game_id = game_id
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
2025-04-11 12:49:08 +08:00
DataManager.SelfUser.location = Location.new()
2025-04-01 10:48:36 +08:00
end
j_data["pos"] = DataManager.SelfUser.location:Location2String()
2025-04-11 12:49:08 +08:00
-- game_net =
__ConntectGameServer(Protocol.GAME_JOIN_ROOM, room, room.server_host, j_data, function(response)
2025-04-01 10:48:36 +08:00
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"]
2025-04-11 12:49:08 +08:00
end
2025-04-01 10:48:36 +08:00
room.agent = _s2croom.agent == 1 and true or false
local extend = ExtendManager.GetExtendConfig(room.game_id)
extend:FillRoomData(_s2croom)
ControllerManager.ChangeController(GameController)
2025-04-11 12:49:08 +08:00
-- -- print("create room:"..(os.clock()-runtime))
2025-04-01 10:48:36 +08:00
callback(response)
return
end
-- game_net:destroy()
callback(response)
end)
else
2025-04-11 12:49:08 +08:00
if callback then callback(res) end
2025-04-01 10:48:36 +08:00
end
end)
end
local join_room_frame = 0
2025-04-11 12:49:08 +08:00
function M:PublicJoinRoom(cmd, room_id, callback, group_id, group_layer)
2025-04-01 10:48:36 +08:00
-- 同一帧不重复调用
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 = {}
_data["room_id"] = room_id
_data["group_id"] = group_id
_data["floor"] = group_layer
_data["platform"] = GetPlatform()
2025-08-23 16:07:53 +08:00
_data["pos"] = DataManager.SelfUser.location:Location2String()
2025-04-01 10:48:36 +08:00
local _client = ControllerManager.WebClient;
2025-04-11 12:49:08 +08:00
_client:send(cmd, _data, function(res)
2025-04-01 10:48:36 +08:00
if (res.ReturnCode == 0) then
local json = res.Data
local game_info = json["game_info"]
if ExtendHotupdate.CheckVersion(game_info) ~= ExtendHotupdate.VERSION_NORMAL then
2025-04-11 12:49:08 +08:00
ExtendHotupdate.UpdateGame(game_info, function()
2025-04-01 10:48:36 +08:00
res.ReturnCode = -2
callback(res)
end)
return
end
local game_id = game_info.game_id
local server_ip = json["server_ip"]
local server_port = json["server_port"]
2025-04-11 12:49:08 +08:00
local room = ExtendManager.GetExtendConfig(game_id):NewRoom()
2025-04-01 10:48:36 +08:00
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()
room.group_id = json["groupId"]
room.play_id = json["pid"]
-- 体力值开关
room.hpOnOff = json["hpOnOff"]
-- 体力值倍数
room.score_times = json.hp_times and d2ad(json.hp_times) or 1
DataManager.CurrenRoom = room
local j_data = {}
if not DataManager.SelfUser.location then
2025-04-11 12:49:08 +08:00
DataManager.SelfUser.location = Location.new()
2025-04-01 10:48:36 +08:00
end
j_data["pos"] = DataManager.SelfUser.location:Location2String()
-- local game_net = nil
2025-04-11 12:49:08 +08:00
---- print(vardump(room))
-- game_net =
__ConntectGameServer(Protocol.GAME_JOIN_ROOM, room, room.server_host, j_data, function(res1)
if (res1.ReturnCode ~= 0) then
2025-04-01 10:48:36 +08:00
if (callback) then callback(res1) end
else
local _s2croom = res1.Data
room.owner_id = _s2croom["owner"]
if _s2croom.createTime then
room.create_time = _s2croom["createTime"]
2025-04-11 12:49:08 +08:00
end
2025-04-01 10:48:36 +08:00
if _s2croom.manor then
room.banker_seat = _s2croom.manor
end
room.agent = _s2croom.agent == 1 and true or false
-- ControllerManager.SetGameNetClient(game_net)
2025-04-11 12:49:08 +08:00
local extend = ExtendManager.GetExtendConfig(room.game_id)
2025-04-01 10:48:36 +08:00
extend:FillRoomData(_s2croom)
ControllerManager.ChangeController(GameController)
if callback then callback(res1) end
end
2025-04-11 12:49:08 +08:00
end)
2025-04-01 10:48:36 +08:00
-- callback(res)
else
2025-04-11 12:49:08 +08:00
if callback then callback(res) end
2025-04-01 10:48:36 +08:00
end
end)
end
2025-04-11 12:49:08 +08:00
function M:JoinRoom(room_id, callback, group_id, group_layer)
self:PublicJoinRoom(Protocol.WEB_JOIN_ROOM, room_id, callback, group_id, group_layer)
2025-04-01 10:48:36 +08:00
end
function M:ResetJionRoom(callback)
local _game = ControllerManager.GetController(GameController)
local o_room = DataManager.CurrenRoom
local j_data = {}
if not DataManager.SelfUser.location then
DataManager.SelfUser.location = Location.new()
end
j_data["pos"] = DataManager.SelfUser.location:Location2String()
2025-04-11 12:49:08 +08:00
local room = ExtendManager.GetExtendConfig(o_room.game_id):NewRoom()
2025-04-01 10:48:36 +08:00
room.lev = o_room.lev
room.room_id = o_room.room_id
room.game_id = o_room.game_id
room.status = o_room.status
room.server_host = o_room.server_host
room.session = o_room.session
room.group_id = o_room.group_id
-- 体力值倍数
room.score_times = o_room.score_times
room.play_id = o_room.play_id
-- 体力值开关
room.hpOnOff = o_room.hpOnOff
room.owner_id = o_room.owner_id
room.create_time = o_room.create_time
room:SetReloadStatus(true)
DataManager.CurrenRoom = room
2025-04-11 12:49:08 +08:00
__ConntectGameServer(Protocol.GAME_JOIN_ROOM, room, room.server_host, j_data, function(res1)
2025-04-01 10:48:36 +08:00
room:SetReloadStatus(false)
2025-04-11 12:49:08 +08:00
if (res1.ReturnCode ~= 0) then
2025-04-01 10:48:36 +08:00
if (callback) then callback(res1) end
else
printlog("ResetJionRoom==>>>")
pt(res1)
2025-04-11 12:49:08 +08:00
ControllerManager.enterPlayerData = res1.Data.tableInfo.playerData
2025-04-01 10:48:36 +08:00
local _s2croom = res1.Data
local extend = ExtendManager.GetExtendConfig(room.game_id)
extend:FillRoomData(_s2croom)
ControllerManager.ChangeController(GameController)
if callback then callback(res1) end
end
end)
end
function M:UpdatePlayerInfo(callback)
2025-04-11 12:49:08 +08:00
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_UPDATE_INFO, nil, function(res)
2025-04-01 10:48:36 +08:00
if res.ReturnCode == 0 then
DataManager.SelfUser.diamo = res.Data.diamo
DataManager.SelfUser.invited = res.Data.invitation
DataManager.SelfUser.group_id = res.Data.groupId
2025-04-01 10:48:36 +08:00
if callback then callback(true, res.Data) end
-- DataManager.SelfUser.raffle = res.Data.raffle
else
if callback then callback(false) end
end
end)
end
2025-04-11 12:49:08 +08:00
function M:UpdateNotice(id, callback)
local _client = ControllerManager.WebClient
local _data = {}
2025-04-01 10:48:36 +08:00
_data.id = id
2025-04-11 12:49:08 +08:00
_client:send(Protocol.WEB_UPDATE_NOTICE, _data, function(res)
---- print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ",res)
2025-04-01 10:48:36 +08:00
--pt(res)
if res.ReturnCode == 0 and #res.Data.notice_list > 0 then
callback(true, res.Data)
else
callback(false)
end
2025-04-11 12:49:08 +08:00
end)
2025-04-01 10:48:36 +08:00
end
function M:RequestRecordList(callback, roomid)
local _client = ControllerManager.WebClient
local _data = {}
_data["platform"] = GetPlatform()
local proto
if not roomid then
2025-04-11 12:49:08 +08:00
proto = Protocol.WEB_GET_MILITARY
else
2025-04-01 10:48:36 +08:00
proto = Protocol.WEB_GET_MILITARY_BY_ROOMID
_data.roomId = roomid
end
2025-04-11 12:49:08 +08:00
_client:send(proto, _data, function(res)
2025-04-01 10:48:36 +08:00
self.recordList = nil
2025-04-11 12:49:08 +08:00
-- -- print(vardump(res))
if (res.ReturnCode == 0 or res.ReturnCode == 19) then
2025-04-01 10:48:36 +08:00
--self:RequestRankList(callback)
self.recordList = {}
if res.ReturnCode == 19 then return end
local recordData = res.Data
local record_info_list = recordData["military_list"]
if record_info_list ~= nil then
for i = 1, #record_info_list do
local record_list_item = record_info_list[i]
local record_list = MResult.new() -- new 战绩
record_list.GameId = tonumber(record_list_item["game_id"])
record_list.GameInfo = record_list_item["game_info"]
record_list.RoomId = record_list_item["room_id"]
record_list.group_id = record_list_item["groupId"]
record_list.Time = record_list_item["create_time"]
record_list.PlayBackId = record_list_item["military_id"]
record_list.hpData = record_list_item["hpData"]
record_list.hpOnOff = record_list_item["hpOnOff"]
record_list.hp_times = d2ad(record_list_item["hp_times"])
local total_score_str = record_list_item["totalScore"]
if total_score_str then
local total_score_item = json.decode(total_score_str)
for j = 1, #total_score_item do
local m_playerScore = MPlayerScore.new()
local total_score_str = total_score_item[j]
m_playerScore.Name = total_score_str["nick"]
m_playerScore.Score = total_score_str["score"]
m_playerScore.Id = total_score_str["accId"]
m_playerScore.Portrait = total_score_str["portrait"]
record_list.TotalScoreList[#record_list.TotalScoreList + 1] = m_playerScore
end
end
local round_count = record_list_item["round"]
2025-04-11 12:49:08 +08:00
for j = 1, round_count do
local round_score_str = record_list_item["round_" .. j]
2025-04-01 10:48:36 +08:00
local m_round_game = MGameTimes.new()
if round_score_str then
local round_score_item = json.decode(round_score_str)
for k = 1, #round_score_item do
local m_player_score = MPlayerScore.new()
m_player_score.Name = round_score_item[k]["nick"]
m_player_score.Score = round_score_item[k]["score"]
m_round_game.PlayerList[#m_round_game.PlayerList + 1] = m_player_score
end
record_list.GameTimes[#record_list.GameTimes + 1] = m_round_game
end
end
self.recordList[#self.recordList + 1] = record_list
end
end
2025-05-30 13:35:36 +08:00
callback(res)
2025-04-01 10:48:36 +08:00
else
callback(res.ReturnCode)
end
end)
end
2025-04-11 12:49:08 +08:00
function M:RequestPlayBack(_data, callback, game_info)
2025-04-01 10:48:36 +08:00
if game_info and ExtendHotupdate.CheckVersion(game_info) ~= ExtendHotupdate.VERSION_NORMAL then
2025-04-11 12:49:08 +08:00
ExtendHotupdate.UpdateGame(game_info, function()
self:RequestPlayBack(_data, callback)
2025-04-01 10:48:36 +08:00
end)
return
else
local _client = ControllerManager.WebClient
2025-04-11 12:49:08 +08:00
_client:send(Protocol.WEB_GET_PLAY_BACK, _data, function(res)
2025-04-01 10:48:36 +08:00
if res.ReturnCode == 0 then
local data = json.decode(res.Data.playback)
local cmdList = data.cmdList
local info = data.info
local extend = ExtendManager.GetExtendConfig(info.game_id)
2025-04-11 12:49:08 +08:00
local room = extend:NewRoom()
2025-04-01 10:48:36 +08:00
room.game_id = info.game_id
DataManager.CurrenRoom = room
extend:FillPlayBackData(data)
if not room.self_player then
room.self_player = room:GetPlayerBySeat(1)
end
2025-04-11 12:49:08 +08:00
callback(res.ReturnCode, data)
2025-04-01 10:48:36 +08:00
else
2025-04-11 12:49:08 +08:00
callback(res.ReturnCode, nil)
2025-04-01 10:48:36 +08:00
end
end)
end
end
--获取手机验证码
2025-04-11 12:49:08 +08:00
function M:GetPhoneCode(phone, callback)
2025-04-01 10:48:36 +08:00
local _client = ControllerManager.WebClient
local _data = {}
2025-04-11 12:49:08 +08:00
_data["phone"] = phone
2025-04-01 10:48:36 +08:00
_client:send(Protocol.WEB_GET_VERIFCATION_CODE, _data, function(res)
callback(res)
end)
end
function M:GetUserInfo(callback)
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_GET_USER_INFO, nil, function(res)
if res.ReturnCode == 0 then
local user = DataManager.SelfUser
local data = res.Data
user.real_info = data.real_info
user.address = data.address
user.invitation = data.invitation
user.phone = data.phone
user.password = data.password
end
callback(res)
end)
end
2025-04-11 12:49:08 +08:00
function M:UpdateUserInfo(_data, callback)
2025-04-01 10:48:36 +08:00
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_UPDATE_USER_INFO, _data, function(res)
callback(res)
end)
end
-- 设置被邀请开关
2025-04-11 12:49:08 +08:00
function M:SetInvited(on, callback)
2025-04-01 10:48:36 +08:00
local _client = ControllerManager.WebClient
local _data = {}
_data["invitation"] = on
_client:send(Protocol.WEB_SET_GROUP_INVITATED, _data, function(res)
callback(res)
end)
2025-04-11 12:49:08 +08:00
end