2025-01-04 23:00:19 +08:00

195 lines
6.8 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

PlayerScoreSystem = PlayerScoreSystem or {}
-- 得分类型对应的分数 [ScoreType] = Score
PlayerScoreSystem.ScoreList = {};
-- 得分类型
PlayerScoreSystem.ScoreType = {};
-- 玩家得分信息 [PlayerKey] = {[ScoreType] = Score, ...} 额外附加 TeamID(PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID)
PlayerScoreSystem.PlayerScoreDatas = {};
--- 注册积分类型列表
function PlayerScoreSystem.RegisterInfo(InScoreList)
if type(InScoreList) ~= "table" then
UGCLogSystem.LogError("[PlayerScoreSystem_RegisterInfo] InScoreType 类型错误")
end
PlayerScoreSystem.ScoreType = table.getKeys(InScoreList)
PlayerScoreSystem.ScoreList = InScoreList
UGCLogSystem.LogTree("[PlayerScoreSystem_RegisterInfo] ScoreType", PlayerScoreSystem.ScoreType)
UGCLogSystem.LogTree("[PlayerScoreSystem_RegisterInfo] ScoreList", PlayerScoreSystem.ScoreList)
end
--- Server or Client
--- 获取得分信息
function PlayerScoreSystem.GetPlayerScoreDatas()
return PlayerScoreSystem.PlayerScoreDatas
end
--- Server
--- 设置全部的得分信息 此操作不进行函数回调因为有可能是GameState的参数传进来的所以有可能导致死循环
function PlayerScoreSystem.SetPlayerScoreDatas(InPlayerScoreDatas)
PlayerScoreSystem.PlayerScoreDatas = InPlayerScoreDatas
end
--- Server
--- 绑定GameState的更新函数简化其他操作所以只能绑一个
function PlayerScoreSystem.BindChangeScoreDataCallBack(Func, Obj)
PlayerScoreSystem.ChangeScoreDataCallBackFunc = Func
PlayerScoreSystem.ChangeScoreDataCallBackObj = Obj
end
--- Server
--- 通知回调
function PlayerScoreSystem.NotifyChangeScoreDataCallBack()
if PlayerScoreSystem.ChangeScoreDataCallBackFunc then
if PlayerScoreSystem.ChangeScoreDataCallBackObj then
PlayerScoreSystem.ChangeScoreDataCallBackFunc(PlayerScoreSystem.ChangeScoreDataCallBackObj)
else
PlayerScoreSystem.ChangeScoreDataCallBackFunc()
end
end
end
--- Server
--- 初始化玩家的积分信息,也可作为重置函数使用
function PlayerScoreSystem.InitPlayerScoreData(PlayerKey, IsReset)
if PlayerScoreSystem.PlayerScoreDatas[PlayerKey] == nil or IsReset then
-- 初始化列表
PlayerScoreSystem.PlayerScoreDatas[PlayerKey] = {}
-- 初始化信息
for i, v in pairs(PlayerScoreSystem.ScoreType) do
PlayerScoreSystem.PlayerScoreDatas[PlayerKey][v] = 0
end
-- 设置队伍ID
PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID = UGCPlayerStateSystem.GetTeamID(PlayerKey)
-- 更新函数回调
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
end
--- Server
--- 刷新玩家TeamID
function PlayerScoreSystem.UpdatePlayerTeamID(PlayerKey, TeamID)
if TeamID then
PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID = TeamID
else
PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID = UGCPlayerStateSystem.GetTeamID(PlayerKey)
end
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
--- Server
--- 校验玩家积分信息是否初始化
function PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
if PlayerScoreSystem.PlayerScoreDatas[PlayerKey] == nil then
UGCLogSystem.LogError("[PlayerScoreSystem_CheckPlayerScoreDataIsInit] 玩家信息未注册")
PlayerScoreSystem.InitPlayerScoreData(PlayerKey)
end
end
--- Server
--- 重置所有玩家的积分信息
function PlayerScoreSystem.ResetAllPlayerScoreData()
for PlayerKey, v in pairs(PlayerScoreSystem.PlayerScoreDatas) do
PlayerScoreSystem.InitPlayerScoreData(PlayerKey, true)
end
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
--- Server
--- 添加玩家积分信息
function PlayerScoreSystem.AddPlayerScoreData(PlayerKey, ScoreType, AddVal)
PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
PlayerScoreSystem.PlayerScoreDatas[PlayerKey][ScoreType] = PlayerScoreSystem.PlayerScoreDatas[PlayerKey][ScoreType] + AddVal
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
--- Server
--- 移除玩家得分信息
function PlayerScoreSystem.RemovePlayerScoreInfo(PlayerKey, NeedNotify)
PlayerScoreSystem.PlayerScoreDatas[PlayerKey] = nil
if NeedNotify == nil or NeedNotify then
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
end
--- Server
--- 清除退出的玩家得分信息
function PlayerScoreSystem.ClearExitPlayer()
local AllPlayerKey = table.getKeys(PlayerScoreSystem.PlayerScoreData)
for i, v in pairs(AllPlayerKey) do
if UGCGameSystem.GetPlayerControllerByPlayerKey(v) == nil then
PlayerScoreSystem.RemovePlayerScoreInfo(v, false)
end
end
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
--- Server
--- 设置玩家积分信息
function PlayerScoreSystem.SetPlayerScoreDataFromType(PlayerKey, ScoreType, Val)
PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
PlayerScoreSystem.PlayerScoreDatas[PlayerKey][ScoreType] = Val
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
end
--- Server or Client
--- 获取玩家积分信息
function PlayerScoreSystem.GetPlayerScoreDataFromType(PlayerKey, ScoreType)
PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
return PlayerScoreSystem.PlayerScoreDatas[PlayerKey][ScoreType]
end
--- Server or Client
--- 获取玩家得分
---@return number
function PlayerScoreSystem.GetPlayerScore(PlayerKey)
PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
local Res = 0
for i, v in pairs(PlayerScoreSystem.PlayerScoreDatas[PlayerKey]) do
Res = Res + PlayerScoreSystem.ScoreList[i] * v
end
return Res
end
--- Server or Client
--- 获取玩家排名
---@param InPlayerKeys "所需排名的PlayerKeys,若为nil则全部一起排名"
---@return "table {PlayerKey, ...}"
function PlayerScoreSystem.GetRank(InPlayerKeys)
local AllPlayerKey = {}
if InPlayerKeys == nil or type(InPlayerKeys) ~= "table" then
AllPlayerKey = table.getKeys(PlayerScoreSystem.PlayerScoreDatas)
else
AllPlayerKey = InPlayerKeys
end
table.sort(AllPlayerKey, function(a, b) return PlayerScoreSystem.GetPlayerScore(a) > PlayerScoreSystem.GetPlayerScore(b) end)
return AllPlayerKey
end
--- Server or Client
--- 获取队伍的玩家排名
---@return "table {[TeamID] = {PlayerKey, ...}, ...}"
function PlayerScoreSystem.GetTeamRank()
local Res = {}
-- 获取队伍玩家PlayerKey
local TeamPlayers = {}
for PlayerKey, ScoreInfo in pairs(PlayerScoreSystem.PlayerScoreDatas) do
if TeamPlayers[ScoreInfo.TeamID] == nil then TeamPlayers = {} end
TeamPlayers[ScoreInfo.TeamID][#TeamPlayers[ScoreInfo.TeamID] + 1] = PlayerKey
end
-- 获取队伍内排名
for TeamID, PlayerKeys in pairs(TeamPlayers) do
Res[TeamID] = PlayerScoreSystem.GetRank(PlayerKeys)
end
return Res
end