334 lines
12 KiB
Lua
334 lines
12 KiB
Lua
|
--[[
|
|||
|
使用说明
|
|||
|
|
|||
|
1、注册积分类型列表,放在GameState的
|
|||
|
PlayerScoreSystem.RegisterInfo(InScoreList) 其中InScoreList 可参考PlayerScoreSystem.Config.ScoreList
|
|||
|
|
|||
|
2、在玩家加入时添加
|
|||
|
PlayerScoreSystem.InitPlayerScoreData(PlayerKey)
|
|||
|
|
|||
|
3、绑定得分表的更新函数,更新函数内可以设置同步参数,参数放置GameState内
|
|||
|
PlayerScoreSystem.BindChangeScoreDataCallBack(Func, Obj)
|
|||
|
刷新绑定的案例:
|
|||
|
(1)GameState加入同步参数PlayerScoreDatas
|
|||
|
|
|||
|
(2)GameState的BeginPlay函数内加入
|
|||
|
if UGCGameSystem.IsServer() then
|
|||
|
-- 绑定更新积分的函数
|
|||
|
PlayerScoreSystem.BindChangeScoreDataCallBack(self.UpdateAllPlayerScoreDatas, self)
|
|||
|
end
|
|||
|
|
|||
|
(3)加入UpdateAllPlayerScoreDatas函数
|
|||
|
function UGCGameState:UpdateAllPlayerScoreDatas()
|
|||
|
self.PlayerScoreDatas = PlayerScoreSystem.GetPlayerScoreDatas()
|
|||
|
end
|
|||
|
|
|||
|
(4)加入UpdateAllPlayerScoreDatas函数
|
|||
|
function UGCGameState:UpdateAllPlayerScoreDatas()
|
|||
|
self.PlayerScoreDatas = PlayerScoreSystem.GetPlayerScoreDatas()
|
|||
|
end
|
|||
|
|
|||
|
(5)加入OnRep_PlayerScoreDatas函数
|
|||
|
function UGCGameState:OnRep_PlayerScoreDatas()
|
|||
|
PlayerScoreSystem.SetPlayerScoreDatas(self.PlayerScoreDatas)
|
|||
|
UGCEventSystem.SendEvent(EventEnum.UpdatePlayerScoreData)
|
|||
|
end
|
|||
|
|
|||
|
-------------------------------------------------------------------------------------------
|
|||
|
|
|||
|
常用函数 -----------
|
|||
|
|
|||
|
--- Server 重置所有玩家的积分信息
|
|||
|
PlayerScoreSystem.ResetAllPlayerScoreData()
|
|||
|
|
|||
|
--- Server 增加玩家积分
|
|||
|
PlayerScoreSystem.AddPlayerScoreData(PlayerKey, ScoreType, AddVal)
|
|||
|
|
|||
|
--- Server 设置玩家积分
|
|||
|
PlayerScoreSystem.SetPlayerScoreDataFromType(PlayerKey, ScoreType, Val)
|
|||
|
|
|||
|
--- Server or Client 获取玩家积分信息
|
|||
|
---@return number
|
|||
|
PlayerScoreSystem.GetPlayerScoreDataFromType(PlayerKey, ScoreType)
|
|||
|
|
|||
|
--- Server or Client 获取玩家得分 返回: ∑得分类型×类型对应得分
|
|||
|
---@return number
|
|||
|
PlayerScoreSystem.GetPlayerScore(PlayerKey)
|
|||
|
|
|||
|
--- Server or Client 获取玩家排名 InPlayerKeys:所需排名的PlayerKeys,若为nil则全部一起排名
|
|||
|
---@return "table {PlayerKey, ...}"
|
|||
|
PlayerScoreSystem.GetRank(InPlayerKeys)
|
|||
|
|
|||
|
--- Server or Client 获取队伍的玩家排名
|
|||
|
---@return "table {[TeamID] = {PlayerKey, ...}, ...}"
|
|||
|
PlayerScoreSystem.GetTeamRank()
|
|||
|
|
|||
|
--- Server 刷新或重设PlayerScoreSystem内的玩家TeamID
|
|||
|
PlayerScoreSystem.UpdatePlayerTeamID(PlayerKey, TeamID)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
-- 特殊情况需要用的函数 -----------
|
|||
|
|
|||
|
--- Server 移除玩家得分信息 NeedNotify是否通知更新 这个比较少用,用于有玩家退出时再进行补人的操作,放在PlayerExit内调用
|
|||
|
PlayerScoreSystem.RemovePlayerScoreInfo(PlayerKey, NeedNotify)
|
|||
|
|
|||
|
--- Server 清除退出的玩家得分信息
|
|||
|
PlayerScoreSystem.ClearExitPlayer()
|
|||
|
|
|||
|
]]
|
|||
|
|
|||
|
PlayerScoreSystem = PlayerScoreSystem or {}
|
|||
|
|
|||
|
PlayerScoreSystem.Config = {}
|
|||
|
|
|||
|
|
|||
|
-- Config ----------------------------------------------------------------
|
|||
|
PlayerScoreSystem.Config.EScoreType = {
|
|||
|
Kills = 1,
|
|||
|
Assists = 2,
|
|||
|
Deaths = 3,
|
|||
|
StarCount = 4,
|
|||
|
}
|
|||
|
|
|||
|
-- Key值对应得分类型,Value值对应该类型每点所获得的积分
|
|||
|
PlayerScoreSystem.Config.ScoreList = {
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Kills] = 1;
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Assists] = 0;
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Deaths] = 0;
|
|||
|
[PlayerScoreSystem.Config.EScoreType.StarCount] = 5;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
-- Rank Info -----------------------------------------------
|
|||
|
|
|||
|
-- 是否显示总得分
|
|||
|
PlayerScoreSystem.Config.ShowTotalScore = true
|
|||
|
|
|||
|
-- 显示排行的得分类型,排行会按该列表顺序显示,并且最后一项默认为得分
|
|||
|
PlayerScoreSystem.Config.ShowRankType = {
|
|||
|
PlayerScoreSystem.Config.EScoreType.Kills,
|
|||
|
PlayerScoreSystem.Config.EScoreType.StarCount,
|
|||
|
}
|
|||
|
|
|||
|
-- 这里只需要获取显示排行的名字
|
|||
|
PlayerScoreSystem.Config.ScoreTypeName = {
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Kills] = "击败";
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Assists] = "助攻";
|
|||
|
[PlayerScoreSystem.Config.EScoreType.Deaths] = "倒地";
|
|||
|
[PlayerScoreSystem.Config.EScoreType.StarCount] = "星星";
|
|||
|
}
|
|||
|
|
|||
|
-- 显示文本的颜色,数量不足则循环获取
|
|||
|
PlayerScoreSystem.Config.TextColor = {
|
|||
|
{SpecifiedColor={R=0.215861,G=0.496933,B=1.000000,A=1.000000},ColorUseRule = 0},
|
|||
|
{SpecifiedColor={R=1.000000,G=0.417907,B=0.000000,A=1.000000},ColorUseRule = 0},
|
|||
|
{SpecifiedColor={R=0.718750,G=0.718750,B=1.718705,A=1.000000},ColorUseRule = 0},
|
|||
|
}
|
|||
|
-- Rank Info End -------------------------------------------
|
|||
|
|
|||
|
|
|||
|
-- Config End ------------------------------------------------------------
|
|||
|
|
|||
|
-- Params ----------------------------------------------------------------
|
|||
|
-- 得分类型对应的分数 [ScoreType] = Score
|
|||
|
PlayerScoreSystem.ScoreList = {};
|
|||
|
-- 得分类型
|
|||
|
PlayerScoreSystem.ScoreType = {};
|
|||
|
-- 玩家得分信息 [PlayerKey] = {[ScoreType] = Score, ...} 额外附加 TeamID(PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID)
|
|||
|
PlayerScoreSystem.PlayerScoreDatas = {};
|
|||
|
-- Params End ------------------------------------------------------------
|
|||
|
|
|||
|
|
|||
|
--- 注册积分类型列表
|
|||
|
---@param InScoreList 可参考PlayerScoreSystem.Config.ScoreList
|
|||
|
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)
|
|||
|
UGCLogSystem.Log("[PlayerScoreSystem_InitPlayerScoreData] TeamID:%s", tostring(PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID))
|
|||
|
-- 更新函数回调
|
|||
|
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
|
|||
|
UGCLogSystem.Log("[PlayerScoreSystem_UpdatePlayerTeamID] TeamID:%s", tostring(PlayerScoreSystem.PlayerScoreDatas[PlayerKey].TeamID))
|
|||
|
PlayerScoreSystem.NotifyChangeScoreDataCallBack()
|
|||
|
end
|
|||
|
|
|||
|
--- Server
|
|||
|
--- 校验玩家积分信息是否初始化
|
|||
|
function PlayerScoreSystem.CheckPlayerScoreDataIsInit(PlayerKey)
|
|||
|
if PlayerScoreSystem.PlayerScoreDatas[PlayerKey] == nil then
|
|||
|
UGCLogSystem.LogError("[PlayerScoreSystem_CheckPlayerScoreDataIsInit] PlayerKey:%s, 玩家信息未注册", tostring(PlayerKey))
|
|||
|
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.PlayerScoreDatas)
|
|||
|
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
|
|||
|
if PlayerScoreSystem.ScoreList[i] then
|
|||
|
Res = Res + PlayerScoreSystem.ScoreList[i] * v
|
|||
|
end
|
|||
|
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[ScoreInfo.TeamID] = {} 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
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|