492 lines
No EOL
17 KiB
Lua
492 lines
No EOL
17 KiB
Lua
XTeamManagerCreator = function()
|
||
local XTeamManager = {}
|
||
|
||
local TeamDataKey = "STAGE_TEAM_DATA_"
|
||
|
||
local MaxPos = CS.XGame.Config:GetInt("TeamMaxPos") -- 默认一个队伍的位置数
|
||
local CaptainPos -- 队长位
|
||
local FirstFightPos -- 首发位
|
||
local EmptyTeam = {
|
||
TeamData = {},
|
||
CaptainPos = 1,
|
||
FirstFightPos = 1,
|
||
-- 0表示仅保存在本地
|
||
TeamId = 0,
|
||
}
|
||
|
||
local PlayerTeamGroupData = {}
|
||
local PlayerTeamPrefabData = {}
|
||
|
||
local METHOD_NAME = {
|
||
SetTeam = "TeamSetTeamRequest",
|
||
}
|
||
|
||
--SetTeamPos
|
||
function XTeamManager.Init()
|
||
CaptainPos = 0
|
||
FirstFightPos = 0
|
||
for _, cfg in pairs(XTeamConfig.GetTeamCfg()) do
|
||
if cfg.IsCaptain and CaptainPos == 0 then
|
||
CaptainPos = cfg.Id
|
||
end
|
||
|
||
if cfg.IsFirstFight and FirstFightPos == 0 then
|
||
FirstFightPos = cfg.Id
|
||
end
|
||
end
|
||
|
||
for i = 1, MaxPos do
|
||
EmptyTeam.TeamData[i] = 0
|
||
end
|
||
EmptyTeam = XReadOnlyTable.Create(EmptyTeam)
|
||
XTeamManager.EmptyTeam = EmptyTeam
|
||
end
|
||
|
||
function XTeamManager.GetTeamId(typeId, stageId)
|
||
local teams = XTeamConfig.GetTeamsByTypeId(typeId)
|
||
if teams == nil then
|
||
return nil
|
||
end
|
||
|
||
local sectionId = 0
|
||
local chapterId = 0
|
||
if stageId ~= nil then
|
||
local stageInfo = XDataCenter.FubenManager.GetStageInfo(stageId)
|
||
sectionId = stageInfo.SectionId
|
||
chapterId = stageInfo.ChpaterId
|
||
if sectionId == nil or chapterId == nil then
|
||
return nil
|
||
end
|
||
end
|
||
|
||
-- 匹配规则:chapterId, sectionId, stageId 逐级查找,某一项为 nil 时,表示匹配上一级
|
||
for _, val in pairs(teams) do
|
||
if #val.ChapterId <= 0 then
|
||
return val.TeamId -- 匹配 TypeId
|
||
end
|
||
|
||
for _, cId in pairs(val.ChapterId) do
|
||
if chapterId > 0 and cId == chapterId then
|
||
if #val.SectionId <= 0 then
|
||
return val.TeamId -- 匹配 chapterId
|
||
end
|
||
|
||
for _, sId in pairs(val.SectionId) do
|
||
if sectionId > 0 and sId == sectionId then
|
||
if #val.StageId <= 0 then
|
||
return val.TeamId -- 匹配 sectionId
|
||
end
|
||
|
||
for _, stId in pairs(val.StageId) do
|
||
if stId == stageId then
|
||
return val.TeamId -- 匹配 stageId
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
return nil
|
||
end
|
||
|
||
-- 玩家队伍中队长的位置Id
|
||
function XTeamManager.GetTeamCaptainKey(teamId)
|
||
return teamId << 8
|
||
end
|
||
|
||
function XTeamManager.GetValidPos(teamData)
|
||
local posId = 1
|
||
for k, v in pairs(teamData) do
|
||
if v > 0 then
|
||
posId = k
|
||
break
|
||
end
|
||
end
|
||
return posId
|
||
end
|
||
|
||
local function GetTeamKey(stageId)
|
||
local info = XDataCenter.FubenManager.GetStageInfo(stageId)
|
||
return string.format("%s%s_%d_%s", TeamDataKey, tostring(XPlayer.Id), info.Type, tostring(stageId))
|
||
end
|
||
|
||
-- 使用stageId作为Key本地保存编队信息
|
||
function XTeamManager.SaveTeamLocal(curTeam, stageId)
|
||
if not stageId then
|
||
XLog.Warning("stageId is nil !!")
|
||
return
|
||
end
|
||
XSaveTool.SaveData(GetTeamKey(stageId), curTeam)
|
||
end
|
||
|
||
-- 使用stageId作为Key读取本地编队信息
|
||
function XTeamManager.LoadTeamLocal(stageId)
|
||
if not stageId then
|
||
XLog.Warning("stageId is nil")
|
||
return EmptyTeam
|
||
end
|
||
|
||
local team = XSaveTool.GetData(GetTeamKey(stageId)) or EmptyTeam
|
||
for _, v in ipairs(team.TeamData) do
|
||
if v ~= 0 and not XRobotManager.CheckIsRobotId(v) and not XDataCenter.CharacterManager.IsOwnCharacter(v) then
|
||
return EmptyTeam
|
||
end
|
||
end
|
||
|
||
return team
|
||
end
|
||
|
||
-- 使用TeamSetTeamRequest协议保存使用TeamId的XTeamData
|
||
function XTeamManager.SetPlayerTeam(curTeam, isPrefab, cb)
|
||
local curTeamId = curTeam.TeamId
|
||
if curTeamId == 0 then
|
||
XTeamManager.SetPlayerTeamLocal(curTeam, isPrefab, cb)
|
||
return
|
||
end
|
||
|
||
local params = {}
|
||
params.TeamData = {}
|
||
params.TeamId = curTeamId
|
||
XMessagePack.MarkAsTable(params.TeamData)
|
||
for k, v in pairs(curTeam.TeamData) do
|
||
params.TeamData[k] = v
|
||
end
|
||
params.CaptainPos = curTeam.CaptainPos
|
||
params.FirstFightPos = curTeam.FirstFightPos
|
||
params.TeamName = curTeam.TeamName
|
||
|
||
local req = { TeamData = params, IsPrefab = isPrefab }
|
||
XNetwork.Call(METHOD_NAME.SetTeam, req, function(res)
|
||
if res.Code ~= XCode.Success then
|
||
XUiManager.TipCode(res.Code)
|
||
return
|
||
end
|
||
XTeamManager.SetPlayerTeamLocal(curTeam, isPrefab, cb)
|
||
end)
|
||
end
|
||
|
||
-- 更新TeamId的数据缓存,服务器的XTeamData只在登录的时候下发
|
||
function XTeamManager.SetPlayerTeamLocal(curTeam, isPrefab, cb)
|
||
local curTeamId = curTeam.TeamId
|
||
local characterCheckTable = {}
|
||
|
||
local changeCharacter = {} -- 更改成员的Id数组
|
||
local playCvCharacterInfo = {} -- 要播放语音的角色信息
|
||
playCvCharacterInfo.Id = 0
|
||
playCvCharacterInfo.IsCaptain = false
|
||
|
||
-- 更改成员数,数量等于1,播放该角色的入队语音,数量大于等于2,只播放队长语音,没有队长就不播放语音
|
||
local changeCount = 0
|
||
|
||
local playerTeamData = isPrefab and PlayerTeamPrefabData or PlayerTeamGroupData
|
||
-- 更新客户端队伍缓存
|
||
if playerTeamData[curTeamId] == nil then
|
||
playerTeamData[curTeamId] = {}
|
||
else
|
||
for _, characterId in pairs(playerTeamData[curTeamId].TeamData) do
|
||
characterCheckTable[characterId] = true
|
||
end
|
||
|
||
for pos, characterId in pairs(curTeam.TeamData) do
|
||
if (not characterCheckTable[characterId]) and (characterId ~= 0) then
|
||
|
||
changeCount = changeCount + 1
|
||
if pos == curTeam.CaptainPos then
|
||
playCvCharacterInfo.Id = characterId
|
||
playCvCharacterInfo.IsCaptain = true
|
||
break
|
||
end
|
||
|
||
table.insert(changeCharacter, characterId)
|
||
end
|
||
end
|
||
|
||
-- 更改角色不是队长,但只更改了一个角色,播放该角色的入队语音
|
||
if playCvCharacterInfo.Id == 0 and changeCount == 1 then
|
||
playCvCharacterInfo.Id = changeCharacter[1]
|
||
end
|
||
|
||
XEventManager.DispatchEvent(XEventId.EVENT_TEAM_MEMBER_CHANGE, curTeamId, playCvCharacterInfo.Id, playCvCharacterInfo.IsCaptain)
|
||
end
|
||
playerTeamData[curTeamId].TeamId = curTeamId
|
||
playerTeamData[curTeamId].CaptainPos = curTeam.CaptainPos
|
||
playerTeamData[curTeamId].FirstFightPos = curTeam.FirstFightPos
|
||
playerTeamData[curTeamId].TeamData = curTeam.TeamData
|
||
playerTeamData[curTeamId].TeamName = curTeam.TeamName
|
||
if cb then cb() end
|
||
|
||
XEventManager.DispatchEvent(XEventId.EVENT_TEAM_PREFAB_CHANGE, curTeamId, playerTeamData[curTeamId])
|
||
end
|
||
|
||
function XTeamManager.SetExpeditionTeamData(curTeam, cb)
|
||
local curTeamId = curTeam.TeamId
|
||
local params = {}
|
||
params.TeamData = {}
|
||
params.TeamId = curTeamId
|
||
XMessagePack.MarkAsTable(params.TeamData)
|
||
for k, v in pairs(curTeam.TeamData) do
|
||
params.TeamData[k] = v
|
||
end
|
||
params.CaptainPos = curTeam.CaptainPos
|
||
params.FirstFightPos = curTeam.FirstFightPos
|
||
local req = { TeamData = params, IsPrefab = false }
|
||
XNetwork.Call(METHOD_NAME.SetTeam, req, function(res)
|
||
if res.Code ~= XCode.Success then
|
||
XUiManager.TipCode(res.Code)
|
||
return
|
||
end
|
||
local characterCheckTable = {}
|
||
local playerTeamData = PlayerTeamGroupData
|
||
-- 更新客户端队伍缓存
|
||
if playerTeamData[curTeamId] == nil then
|
||
playerTeamData[curTeamId] = {}
|
||
else
|
||
for _, baseId in pairs(playerTeamData[curTeamId].TeamData) do
|
||
characterCheckTable[baseId] = true
|
||
end
|
||
|
||
for pos, baseId in pairs(curTeam.TeamData) do
|
||
if not characterCheckTable[baseId] then
|
||
local charId = XExpeditionConfig.GetCharacterIdByBaseId(baseId)
|
||
XEventManager.DispatchEvent(XEventId.EVENT_TEAM_MEMBER_CHANGE, curTeamId, charId, pos == curTeam.CaptainPos)
|
||
end
|
||
end
|
||
end
|
||
playerTeamData[curTeamId].TeamId = curTeamId
|
||
playerTeamData[curTeamId].CaptainPos = curTeam.CaptainPos
|
||
playerTeamData[curTeamId].FirstFightPos = curTeam.FirstFightPos
|
||
playerTeamData[curTeamId].TeamData = curTeam.TeamData
|
||
playerTeamData[curTeamId].TeamName = curTeam.TeamName
|
||
|
||
if cb then cb() end
|
||
|
||
XEventManager.DispatchEvent(XEventId.EVENT_TEAM_PREFAB_CHANGE, curTeamId, playerTeamData[curTeamId])
|
||
end)
|
||
end
|
||
|
||
function XTeamManager.GetPlayerTeamData(teamId)
|
||
return PlayerTeamGroupData[teamId] or false
|
||
end
|
||
|
||
function XTeamManager.GetTeamData(teamId)
|
||
if not XTeamConfig.GetTeamTypeCfg(teamId) and teamId ~= 0 then
|
||
XLog.Error("XTeamManager.GetTeamCaptainPos, 缺少TeamTypeCfg,teamId = ", tostring(teamId))
|
||
return
|
||
end
|
||
|
||
local teamData = nil
|
||
if PlayerTeamGroupData[teamId] ~= nil then
|
||
teamData = PlayerTeamGroupData[teamId].TeamData
|
||
end
|
||
|
||
if teamData == nil or next(teamData) == nil then
|
||
teamData = {}
|
||
for i = 1, MaxPos do
|
||
teamData[i] = 0
|
||
end
|
||
end
|
||
return teamData
|
||
end
|
||
|
||
function XTeamManager.GetTeamCaptainPos(teamId)
|
||
if not XTeamConfig.GetTeamTypeCfg(teamId) and teamId ~= 0 then
|
||
XLog.Error("XTeamManager.GetTeamCaptainPos, 缺少TeamTypeCfg,teamId = ", tostring(teamId))
|
||
return
|
||
end
|
||
|
||
local captainPos = XTeamManager.GetCaptainPos()
|
||
if PlayerTeamGroupData[teamId] ~= nil then
|
||
captainPos = PlayerTeamGroupData[teamId].CaptainPos
|
||
end
|
||
return captainPos
|
||
end
|
||
|
||
---==========================================
|
||
--- 根据'teamId'得到当前的首发位置,中间是1,左边是2,右边是3
|
||
--- 若第一次进入玩法,没有设置相应的'teamId‘数据,则初始位置为1
|
||
---@param teamId number
|
||
---@return number
|
||
---==========================================
|
||
function XTeamManager.GetTeamFirstFightPos(teamId)
|
||
if not XTeamConfig.GetTeamTypeCfg(teamId) and teamId ~= 0 then
|
||
XLog.Error("XTeamManager.GetTeamFirstFightPos, 缺少TeamTypeCfg,teamId = ", tostring(teamId))
|
||
return
|
||
end
|
||
|
||
-- 初始位置为1
|
||
local posId = 1
|
||
|
||
-- 是否设置过该teamId数据
|
||
if PlayerTeamGroupData[teamId] ~= nil then
|
||
posId = PlayerTeamGroupData[teamId].FirstFightPos
|
||
end
|
||
return posId
|
||
end
|
||
|
||
function XTeamManager.GetTeamCaptainId(teamId)
|
||
if not XTeamConfig.GetTeamTypeCfg(teamId) and teamId ~= 0 then
|
||
XLog.Error("XTeamManager.GetTeamCaptainId, 缺少TeamTypeCfg,teamId = ", tostring(teamId))
|
||
return
|
||
end
|
||
|
||
if PlayerTeamGroupData[teamId] == nil then
|
||
return nil
|
||
end
|
||
|
||
local captainPos = PlayerTeamGroupData[teamId].CaptainPos
|
||
return PlayerTeamGroupData[teamId].TeamData[captainPos]
|
||
end
|
||
|
||
---==========================================
|
||
--- 根据'teamId'得到当前的首发位的角色Id
|
||
---@param teamId number
|
||
---@return number
|
||
---==========================================
|
||
function XTeamManager.GetTeamFirstFightId(teamId)
|
||
if not XTeamConfig.GetTeamTypeCfg(teamId) and teamId ~= 0 then
|
||
XLog.Error("XTeamManager.GetTeamFirstFightId, 缺少TeamTypeCfg,teamId = ", tostring(teamId))
|
||
return
|
||
end
|
||
|
||
if PlayerTeamGroupData[teamId] == nil then
|
||
return nil
|
||
end
|
||
|
||
local firstFightPos = PlayerTeamGroupData[teamId].FirstFightPos
|
||
return PlayerTeamGroupData[teamId].TeamData[firstFightPos]
|
||
end
|
||
|
||
-- 得到对应玩法的TeamId与Team数据
|
||
function XTeamManager.GetPlayerTeam(typeId, stageId)
|
||
local curTeamId = XTeamManager.GetTeamId(typeId, stageId)
|
||
if curTeamId == nil then
|
||
XLog.ErrorTableDataNotFound("XTeamManager.GetPlayerTeam", "curTeamId",
|
||
TABLE_PATH, "typeId : stageId :", tostring(typeId) .. tostring(stageId))
|
||
return nil
|
||
end
|
||
|
||
local CurTeam = {
|
||
["TeamId"] = curTeamId,
|
||
["TeamData"] = XTeamManager.GetTeamData(curTeamId),
|
||
["CaptainPos"] = XTeamManager.GetTeamCaptainPos(curTeamId),
|
||
["FirstFightPos"] = XTeamManager.GetTeamFirstFightPos(curTeamId),
|
||
}
|
||
return CurTeam
|
||
end
|
||
|
||
function XTeamManager.CheckInTeam(characterId)
|
||
local typeId = CS.XGame.Config:GetInt("TypeIdMainLine")
|
||
local curTeamId = XTeamManager.GetTeamId(typeId)
|
||
if curTeamId == nil then
|
||
XLog.ErrorTableDataNotFound("XTeamManager.CheckInTeam", "curTeamId", TABLE_PATH, "typeId : stageId :", tostring(typeId))
|
||
return nil
|
||
end
|
||
|
||
local teamData = XTeamManager.GetTeamData(curTeamId)
|
||
for _, v in pairs(teamData) do
|
||
if characterId == v then
|
||
return true
|
||
end
|
||
end
|
||
return false
|
||
end
|
||
|
||
function XTeamManager.GetInTeamCheckTable()
|
||
local inTeamCheckTable = {}
|
||
|
||
local typeId = CS.XGame.Config:GetInt("TypeIdMainLine")
|
||
local curTeamId = XTeamManager.GetTeamId(typeId)
|
||
local teamData = XTeamManager.GetTeamData(curTeamId)
|
||
for _, v in pairs(teamData) do
|
||
if v > 0 then
|
||
inTeamCheckTable[v] = true
|
||
end
|
||
end
|
||
|
||
return inTeamCheckTable
|
||
end
|
||
|
||
-- 在NotifyLogin中获取队伍数据
|
||
function XTeamManager.InitTeamGroupData(teamGroupData)
|
||
if teamGroupData == nil then
|
||
return
|
||
end
|
||
|
||
for key, value in pairs(teamGroupData) do
|
||
local teamTemp = {}
|
||
for teamDataKey, teamDataValue in pairs(value.TeamData) do
|
||
teamTemp[teamDataKey] = teamDataValue
|
||
end
|
||
|
||
PlayerTeamGroupData[key] = {}
|
||
PlayerTeamGroupData[key].TeamId = value.TeamId
|
||
PlayerTeamGroupData[key].CaptainPos = value.CaptainPos
|
||
PlayerTeamGroupData[key].FirstFightPos = value.FirstFightPos
|
||
PlayerTeamGroupData[key].TeamData = teamTemp
|
||
end
|
||
end
|
||
|
||
|
||
-- 在NotifyLogin中获取预编译队伍
|
||
function XTeamManager.InitTeamPrefabData(teamPrefabData)
|
||
if teamPrefabData == nil then
|
||
return
|
||
end
|
||
|
||
for key, value in pairs(teamPrefabData) do
|
||
local teamTemp = {}
|
||
for teamDataKey, teamDataValue in pairs(value.TeamData) do
|
||
teamTemp[teamDataKey] = teamDataValue
|
||
end
|
||
|
||
PlayerTeamPrefabData[key] = {}
|
||
PlayerTeamPrefabData[key].TeamId = value.TeamId
|
||
PlayerTeamPrefabData[key].CaptainPos = value.CaptainPos
|
||
PlayerTeamPrefabData[key].FirstFightPos = value.FirstFightPos
|
||
PlayerTeamPrefabData[key].TeamData = teamTemp
|
||
PlayerTeamPrefabData[key].TeamName = value.TeamName
|
||
end
|
||
end
|
||
|
||
|
||
|
||
function XTeamManager.GetCaptainPos()
|
||
return CaptainPos
|
||
end
|
||
|
||
function XTeamManager.GetFirstFightPos()
|
||
return FirstFightPos
|
||
end
|
||
|
||
function XTeamManager.GetMaxPos()
|
||
return MaxPos
|
||
end
|
||
|
||
function XTeamManager.GetTeamMemberColor(id)
|
||
local colorStr = XTeamConfig.GetTeamCfgById(id).Color
|
||
local color = XUiHelper.Hexcolor2Color(colorStr)
|
||
return color
|
||
end
|
||
|
||
function XTeamManager.GetTeamPrefabData()
|
||
return PlayerTeamPrefabData
|
||
end
|
||
|
||
function XTeamManager.ResetTeamData(teamId)
|
||
local teamInfos = XTeamManager.GetPlayerTeamData(teamId)
|
||
if not teamInfos then return end
|
||
teamInfos.CaptainPos = 1
|
||
for index in pairs(teamInfos.TeamData) do
|
||
teamInfos.TeamData[index] = 0
|
||
end
|
||
end
|
||
|
||
XTeamManager.Init()
|
||
return XTeamManager
|
||
end
|
||
|
||
XRpc.NotifyTeamClear = function(data)
|
||
XDataCenter.TeamManager.ResetTeamData(data.TeamId)
|
||
end |