Files
Main/Assets/GameAssets/Resources/Lua/Logic/PlayerRoleList/PlayerRoleListSystem.lua
2025-01-25 04:38:09 +08:00

330 lines
12 KiB
Lua

------------------------------------------------
--作者: gzg
--日期: 2020-11-12
--文件: PlayerRoleListSystem.lua
--模块: PlayerRoleListSystem
--描述: 玩家角色列表系统
------------------------------------------------
local RandomNameMaker = require("Logic.PlayerRoleList.RandomNameMaker")
local PlayerRoleInfo = require("Logic.PlayerRoleList.PlayerRoleInfo")
local ServerCharInfo = require("Logic.ServerList.ServerCharInfo");
local TimeUtils = CS.Thousandto.Core.Base.TimeUtils
local LocalPlayerRoot = CS.Thousandto.Code.Logic.LocalPlayerRoot
local PlayerRoleListSystem = {
--名字最大长度和最小长度
MaxNameLength = 7,
MinNameLength = 2,
--服务器发来的选择角色ID
SerSelectedRoleID = 0,
--角色列表
RoleList = List:New(),
--当前服务器列表
ServerID = -1,
--延迟删除等级
DelayDeleteLevel = 100,
--随机名字生成器
RandNameMaker = nil,
--职业开启列表
OccOpenList = nil,
}
--初始化
function PlayerRoleListSystem:Initialize(clearLoginData)
Debug.Log("PlayerRoleListSystem:Initialize:" .. tostring(clearLoginData));
--重新关联事件,因为所有注册事件都会在一个轮回中清理掉.
GameCenter.RegFixEventHandle(LogicLuaEventDefine.EID_EVENT_GAME_LOGIN_SUCCESS,self.GS2U_ResLoginGameSuccess,self);
--玩家角色列表系统被清理后的初始化
if clearLoginData then
--角色名字最大和最小长度
local _cfg = DataConfig.DataGlobal[47]
if _cfg then
local _val = _cfg.Params;
if _val then
local _t = Utils.SplitNumber(_val,"_");
if _t:Count() >= 2 then
self.MaxNameLength = _t[1];
self.MinNameLength = _t[2];
end
end
end
--延迟删除等级
_cfg = DataConfig.DataGlobal[1810]
if _cfg then
local _val = _cfg.Params;
if _val then
local _t = tonumber(_val);
if _t > 0 then
self.DelayDeleteLevel = _t;
end
end
end
else
--玩家角色列表系统不被清理之后的处理
self:RefreshFromLocalPlayer();
end
end
--卸载处理
function PlayerRoleListSystem:UnInitialize(clearLoginData)
Debug.Log("PlayerRoleListSystem:UnInitialize:" .. tostring(clearLoginData));
--重新关联事件,因为所有注册事件都会在一个轮回中清理掉.
GameCenter.UnRegFixEventHandle(LogicLuaEventDefine.EID_EVENT_GAME_LOGIN_SUCCESS,self.GS2U_ResLoginGameSuccess,self);
if clearLoginData then
end
end
--获取随机名
function PlayerRoleListSystem:GetRandomName(occ)
if not self.RandNameMaker then
self.RandNameMaker = RandomNameMaker:New();
self.RandNameMaker:Initialize();
end
return self.RandNameMaker:RandName(Utils.OccToSex(occ) == 0)
end
--获取职业的名字
function PlayerRoleListSystem:GetOccName(occ)
local _cfg = DataConfig.DataPlayerOccupation[occ];
if _cfg then
return _cfg.JobName;
end
return "";
end
--职业是否有效
function PlayerRoleListSystem:OccIsValid(occ)
if not self.OccOpenList then
local _cfg = DataConfig.DataGlobal[GlobalName.Open_Occupatio_List];
Debug.LogError(_cfg.Params);
self.OccOpenList = Utils.SplitNumber(_cfg.Params, '_');
end
--fix yy
return true
-- return self.OccOpenList:Contains(occ);
end
--通过当前角色列表获取服务器角色列表
function PlayerRoleListSystem:GetServerCharList()
local _result = List:New();
for i = 1, self.RoleList:Count()do
local _scInfo = ServerCharInfo:New();
_scInfo.ID = self.RoleList[i].RoleId;
_scInfo.Career = self.RoleList[i].Career;
_scInfo.Level = self.RoleList[i].Level;
_scInfo.Name = self.RoleList[i].Name;
_scInfo.PowerValue = self.RoleList[i].PowerValue;
_result:Add(_scInfo);
end
return result;
end
--获取正在使用的角色ID
function PlayerRoleListSystem:GetUsedCharacter()
if self.SerSelectedRoleID <= 0 then
return AppPersistData.LastRoleID;
else
return self.SerSelectedRoleID;
end
end
--判断是否要创建角色
function PlayerRoleListSystem:CheckInCreateRole()
if (self.RoleList:Count() > 0) then
--当没有选择的角色,创建的角色数量还不满
if (self.SerSelectedRoleID == -1 and self.RoleList:Count() < 4) then
return true;
end
return false;
end
return true;
end
--通过本地角色刷新角色信息
function PlayerRoleListSystem:RefreshFromLocalPlayer()
local _lp = LocalPlayerRoot.LocalPlayer;
if _lp then
self.SerSelectedRoleID = _lp.ID;
GameCenter.LoginSystem.SerSelectedRoleID = _lp.ID;
local _findRole = nil;
for i = 1, self.RoleList:Count() do
if self.RoleList[i].RoleId == self.SerSelectedRoleID then
_findRole = self.RoleList[i];
_findRole:FillFromLocalPlayer(_lp);
break;
end
end
if not _findRole then
_findRole = PlayerRoleInfo:New(lp);
self.RoleList:Add(_findRole);
end
end
end
--#region //发送网络消息
--发送创建角色消息
function PlayerRoleListSystem:SendCreateRoleMsg(name, career, isRandom)
local _req = ReqMsg.MSG_Register.ReqCreateCharacter:New();
_req.playerName = name;
_req.career = career or 0;
_req.isRandom = isRandom or false;
local _csDevice = GameCenter.BISystem:CreatDevictData();
_req.device = {
appId = _csDevice.appId,
roleId = _csDevice.roleId,
channelId = _csDevice.channelId,
sourceId = _csDevice.sourceId,
deviceId = _csDevice.deviceId,
platform = _csDevice.platform,
app_version = _csDevice.app_version,
merchant = _csDevice.merchant,
net_type = _csDevice.net_type,
screen = _csDevice.screen,
os = _csDevice.os,
os_version = _csDevice.os_version,
server_name = _csDevice.server_name,
cpgameId = _csDevice.cpgameId,
cpdid = _csDevice.cpdid,
cpdevice_name = _csDevice.cpdevice_name,
cpplatformId = _csDevice.cpplatformId,
cpuserid = _csDevice.cpuserid,
cpuserName = _csDevice.cpuserName,
cpgameName = _csDevice.cpgameName,
cpPlatformGname = _csDevice.cpPlatformGname,
};
_req:Send();
end
--删除角色
function PlayerRoleListSystem:SendDeleteRole(roleId)
local _reqMsg = ReqMsg.MSG_Register.ReqDeleteRole:New()
_reqMsg.roleId = roleId
_reqMsg:Send()
end
--恢复角色
function PlayerRoleListSystem:SendRegainRole(roleId)
local _reqMsg = ReqMsg.MSG_Register.ReqRegainRole:New()
_reqMsg.roleId = roleId
_reqMsg:Send()
end
--#endregion
--#region //网络协议回调
--删除角色成功
function PlayerRoleListSystem:GS2U_ResDeleteRoleSuccess(result)
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
if (result.res == 0) then
--删除角色成功
local _item = self.RoleList:Find(function (x)
return x.RoleId == result.playerId;
end);
if _item then
if (_item.Level >= self.DelayDeleteLevel) then
_item.DeleteTime = TimeUtils.GetNow();
else
self.RoleList:Remove(_item);
end
end
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_CREATEPLAYER_DELPLAYER, result.playerId);
elseif result.res ==1 then
--删除角色失败
Debug.Log("删除角色失败,返回原因码:" .. result.res);
Utils.ShowMsgBoxAndBtn(nil, "C_MSGBOX_OK", nil, "C_DELETE_FAIL_ROLE_IS_CHAIRMAN")
else
--删除角色失败
Debug.Log("删除角色失败,返回原因码:" .. result.res);
Utils.ShowMsgBoxAndBtn(nil, "C_MSGBOX_OK", nil, "C_DELETE_FAIL_ROLE_UNKNOW")
end
end
--恢复角色结果
function PlayerRoleListSystem:GS2U_ResRegainRoleResult(result)
if result.result == 0 then
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_CREATEPLAYER_RECOVERPLAYER, result.roleId);
local _item = self.RoleList:Find(function (x)
return x.RoleId == result.roleId;
end);
if _item then
_item.DeleteTime = -1;
end
else
Utils.ShowMsgBoxAndBtn(nil, "C_MSGBOX_OK", nil, "C_REGISTER_REGAINROLE_FAIL");
end
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
end
--创建角色返回
function PlayerRoleListSystem:GS2U_ResCreateRoleRet(result)
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
if result.reason == 0 then
--创建角色成功
Debug.Log("CreatePlayer success");
GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_UPDATE_RECORDER_STEP, { CS.Thousandto.Update.Recorder.StepType.CreateRole });
GameCenter.SDKSystem.CreateRoleTime = result.time;
GameCenter.SDKSystem.IsCreatePlayerFlag = true;
GameCenter.SDKSystem:SetLevelInfoWhileFirstEnterGame(1);
GameCenter.SDKSystem:DealMessage(5);
GameCenter.LoginSystem.MapLogic:SetState(LoginMapStateCode.CreatePlayerOK);
else
GameCenter.SDKSystem.IsCreatePlayerFlag = false;
-- 失败原因( 1-已经有角色了 2-名字太长 3-名字含有非法字符 4-重名 5-参数错误)
local _errstrList ={
[1] = DataConfig.DataMessageString.Get("C_MSG_REGISTER_CREATEPLAYER_FAIL_EXIST"),
[2] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_NAMELEN",self.MinNameLength,self.MaxNameLength),
[3] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_INVALID"),
[4] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_SIGNED"),
[5] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_SYMBOL"),
[6] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_SYMBOL"),
[7] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_REG_MAX"),
[8] = DataConfig.DataMessageString.Get("C_CREATEPLAYER_ERROR_REG_MAX"),
};
local _errorStr = _errstrList[result.reason];
if not _errorStr then
_errorStr = DataConfig.DataMessageString.Get("C_MSG_REGISTER_CREATEPLAYER_FAIL_UNKNOWN");
end
GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_UPDATE_RECORDER_STEP, { CS.Thousandto.Update.Recorder.StepType.CreateRole, result.reason, errorStr });
Debug.LogError("CreatePlayer Failed!!::" .. tostring(result.reason) .."::".. tostring(_errorStr));
GameCenter.MsgPromptSystem:ShowMsgBox(_errorStr, DataConfig.DataMessageString.Get("C_MSGBOX_OK"));
--Utils.ShowMsgBoxAndBtn(nil, "C_MSGBOX_CANCEL", "C_MSGBOX_OK", _errorStr)
end
end
--选择角色进入游戏失败
function PlayerRoleListSystem:GS2U_ResSelectCharacterFailed(result)
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_PLAYER_FORBIDDEN, result.forbidTime);
end
--登录游戏成功的消息处理---在LoginSystem中调用.
function PlayerRoleListSystem:GS2U_ResLoginGameSuccess(result)
Debug.Log("登录成功!! selectedRoleID:" .. tostring(result.roleId));
self.SerSelectedRoleID = result.roleId;
GameCenter.LoginSystem.SerSelectedRoleID = result.roleId;
self.RoleList:Clear();
if result.infoList then
Debug.Log("charListCount:" .. tostring(#(result.infoList)));
for index, value in ipairs(result.infoList) do
local _pri = PlayerRoleInfo:New();
_pri:FillFromServerMsg(value);
self.RoleList:Add(_pri);
end
end
end
--#endregion
return PlayerRoleListSystem