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

436 lines
13 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

LoddyController = {}
local M = {}
--- Create a new LoddyController
function LoddyController.new()
setmetatable(M, {__index = IController})
local self = setmetatable({}, {__index = M})
self.baseType = LoddyController
self.class = "Loddy"
self.recordList = {}
return self
end
function M.OnEnter(self)
--print(vardump(self,"loddy controller enter"))
end
function M.OnExit(self)
--print(vardump(self,"loddy controller exit"))
end
local function __FillRoomData(s2croom)
local room = DataManager.CurrenRoom
local extend = ExtendManager.GetExtendConfig(room.game_id)
extend:FillRoomData(s2croom)
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()
ControllerManager.SetGameNetClient(_game_client)
_game_client.onconnect:Add(function(code)
if (code == SocketCode.Connect) then
_game_client:send(cmd, _data, function (response)
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
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)
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.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,room_id,callback,group_id,group_layer)
-- 同一帧不重复调用
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()
local _client = ControllerManager.WebClient;
_client:send(cmd, _data, function( res)
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 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()
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
DataManager.SelfUser.location = Location.new()
end
j_data["pos"] = DataManager.SelfUser.location:Location2String()
-- local game_net = nil
--print(vardump(room))
-- game_net =
__ConntectGameServer(Protocol.GAME_JOIN_ROOM,room,room.server_host, j_data,function ( res1)
if (res1.ReturnCode ~= 0 ) then
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"]
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)
if callback then callback(res1) end
end
end)
-- callback(res)
else
if callback then callback(res) end
end
end)
end
function M:JoinRoom(room_id,callback,group_id,group_layer)
self:PublicJoinRoom(Protocol.WEB_JOIN_ROOM,room_id,callback,group_id,group_layer)
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()
local room = ExtendManager.GetExtendConfig(o_room.game_id):NewRoom()
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
__ConntectGameServer(Protocol.GAME_JOIN_ROOM,room,room.server_host, j_data,function ( res1)
room:SetReloadStatus(false)
if (res1.ReturnCode ~= 0 ) then
if (callback) then callback(res1) end
else
printlog("ResetJionRoom==>>>")
pt(res1)
ControllerManager.enterPlayerData=res1.Data.tableInfo.playerData
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)
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_UPDATE_INFO, nil, function (res)
if res.ReturnCode == 0 then
DataManager.SelfUser.diamo = res.Data.diamo
DataManager.SelfUser.invited = res.Data.invitation
if callback then callback(true, res.Data) end
-- DataManager.SelfUser.raffle = res.Data.raffle
else
if callback then callback(false) end
end
end)
end
function M:UpdateNotice(id,callback)
local _client = ControllerManager.WebClient
local _data = {}
_data.id = id
_client:send(Protocol.WEB_UPDATE_NOTICE, _data, function (res)
--pt(res)
if res.ReturnCode == 0 and #res.Data.notice_list > 0 then
callback(true, res.Data)
else
callback(false)
end
end)
end
function M:RequestRecordList(callback, roomid)
local _client = ControllerManager.WebClient
local _data = {}
_data["platform"] = GetPlatform()
local proto
if not roomid then
proto = Protocol.WEB_GET_MILITARY
else
proto = Protocol.WEB_GET_MILITARY_BY_ROOMID
_data.roomId = roomid
end
_client:send(proto, _data, function (res)
self.recordList = nil
-- print(vardump(res))
if (res.ReturnCode == 0 or res.ReturnCode == 19 ) then
--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"]
for j = 1,round_count do
local round_score_str = record_list_item["round_"..j]
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
callback(res.ReturnCode)
else
callback(res.ReturnCode)
end
end)
end
function M:RequestPlayBack(_data,callback,game_info)
if game_info and ExtendHotupdate.CheckVersion(game_info) ~= ExtendHotupdate.VERSION_NORMAL then
ExtendHotupdate.UpdateGame(game_info,function()
self:RequestPlayBack(_data,callback)
end)
return
else
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_GET_PLAY_BACK , _data , function (res)
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)
local room = extend:NewRoom()
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
callback(res.ReturnCode,data)
else
callback(res.ReturnCode,nil)
end
end)
end
end
--获取手机验证码
function M:GetPhoneCode(phone,callback)
local _client = ControllerManager.WebClient
local _data = {}
_data["phone"]=phone
_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
function M:UpdateUserInfo(_data,callback)
local _client = ControllerManager.WebClient
_client:send(Protocol.WEB_UPDATE_USER_INFO, _data, function(res)
callback(res)
end)
end
-- 设置被邀请开关
function M:SetInvited(on, callback)
local _client = ControllerManager.WebClient
local _data = {}
_data["invitation"] = on
_client:send(Protocol.WEB_SET_GROUP_INVITATED, _data, function(res)
callback(res)
end)
end