30 protected ref map<int, ref SCR_ScoreInfo> m_mPlayerScores =
new map<int, ref SCR_ScoreInfo>();
33 protected ref map<Faction, ref SCR_ScoreInfo> m_mFactionScores =
new map<Faction, ref SCR_ScoreInfo>();
42 if (!m_OnPlayerScoreChangedInvoker)
45 return m_OnPlayerScoreChangedInvoker;
55 if (!m_OnFactionScoreChangedInvoker)
58 return m_OnFactionScoreChangedInvoker;
71 return m_OnPlayerAdded;
81 if (!m_OnPlayerRemoved)
84 return m_OnPlayerRemoved;
91 override bool RplSave(ScriptBitWriter writer)
93 super.RplSave(writer);
95 int players = m_mPlayerScores.Count();
96 writer.WriteInt(players);
98 foreach (
int playerId,
SCR_ScoreInfo scoreInfo : m_mPlayerScores)
100 writer.WriteInt(playerId);
101 scoreInfo.RplSave(writer);
104 int factions = m_mFactionScores.Count();
105 writer.WriteInt(factions);
109 int factionId =
GetGame().GetFactionManager().GetFactionIndex(faction);
110 writer.WriteInt(factionId);
111 scoreInfo.RplSave(writer);
121 override bool RplLoad(ScriptBitReader reader)
123 super.RplLoad(reader);
126 if (!reader.ReadInt(players))
129 array<int> playerIds = {};
130 array<ref SCR_ScoreInfo> playerScores = {};
132 for (
int i = 0; i < players; i++)
137 if (!reader.ReadInt(playerId))
140 if (!scoreInfo.RplLoad(reader))
143 playerIds.Insert(playerId);
144 playerScores.Insert(scoreInfo);
148 if (!reader.ReadInt(factions))
151 array<int> factionIds = {};
152 array<ref SCR_ScoreInfo> factionScores = {};
155 for (
int i = 0; i < factions; i++)
160 if (!reader.ReadInt(factionId))
163 if (!scoreInfo.RplLoad(reader))
166 factionIds.Insert(factionId);
167 factionScores.Insert(scoreInfo);
171 m_mPlayerScores.Clear();
172 for (
int i = 0; i < players; i++)
174 m_mPlayerScores.Insert(playerIds[i], playerScores[i]);
177 FactionManager factionManager =
GetGame().GetFactionManager();
180 Print(
"Score deserialization fail, no FactionManager present in the world, faction scoring will not work properly!", LogLevel.WARNING);
184 array<Faction> _ = {};
185 int facCnt = factionManager.GetFactionsList(_);
186 m_mFactionScores.Clear();
188 for (
int i = 0; i < factions; i++)
190 int factionIndex = factionIds[i];
191 Print(
string.Format(
"idx=%1, facIdx=%2, facMan=%3, facCnt=%4", i, factionIndex, factionManager, facCnt), LogLevel.NORMAL);
192 faction = factionManager.GetFactionByIndex(factionIndex);
193 m_mFactionScores.Insert(faction, factionScores[i]);
203 private int GetPlayerFactionIndex(
int playerId)
208 Faction playerFaction = factionManager.GetPlayerFaction(playerId);
209 return factionManager.GetFactionIndex(playerFaction);
218 private Faction GetFactionByIndex(
int factionIndex)
220 if (factionIndex < 0)
223 return GetGame().GetFactionManager().GetFactionByIndex(factionIndex);
229 private int GetFactionIndex(
Faction faction)
234 return GetGame().GetFactionManager().GetFactionIndex(faction);
238 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
239 private void RpcDo_AddKill(
int playerId,
int factionIdx,
int count)
242 playerScore.m_iKills += count;
243 OnPlayerScoreChanged(playerId, playerScore);
245 Faction faction = GetFactionByIndex(factionIdx);
249 factionScore.m_iKills += count;
250 OnFactionScoreChanged(faction, factionScore);
259 void AddKill(
int playerId,
int count = 1)
265 int factionIdx = GetPlayerFactionIndex(playerId);
266 RpcDo_AddKill(playerId, factionIdx, count);
267 Rpc(RpcDo_AddKill, playerId, factionIdx, count);
271 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
272 private void RpcDo_AddDeath(
int playerId,
int factionIdx,
int count)
275 playerScore.m_iDeaths += count;
276 OnPlayerScoreChanged(playerId, playerScore);
278 Faction faction = GetFactionByIndex(factionIdx);
282 factionScore.m_iDeaths += count;
283 OnFactionScoreChanged(faction, factionScore);
292 void AddDeath(
int playerId,
int count = 1)
298 int factionIdx = GetPlayerFactionIndex(playerId);
299 RpcDo_AddDeath(playerId, factionIdx, count);
300 Rpc(RpcDo_AddDeath, playerId, factionIdx, count);
304 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
305 private void RpcDo_AddTeamKill(
int playerId,
int factionIdx,
int count)
308 playerScore.m_iTeamKills += count;
309 OnPlayerScoreChanged(playerId, playerScore);
311 Faction faction = GetFactionByIndex(factionIdx);
315 factionScore.m_iTeamKills += count;
316 OnFactionScoreChanged(faction, factionScore);
325 void AddTeamKill(
int playerId,
int count = 1)
331 int factionIdx = GetPlayerFactionIndex(playerId);
332 RpcDo_AddTeamKill(playerId, factionIdx, count);
333 Rpc(RpcDo_AddTeamKill, playerId, factionIdx, count);
337 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
338 private void RpcDo_AddSuicide(
int playerId,
int factionIdx,
int count)
341 playerScore.m_iSuicides += count;
342 OnPlayerScoreChanged(playerId, playerScore);
344 Faction faction = GetFactionByIndex(factionIdx);
348 factionScore.m_iSuicides += count;
349 OnFactionScoreChanged(faction, factionScore);
358 void AddSuicide(
int playerId,
int count = 1)
364 int factionIdx = GetPlayerFactionIndex(playerId);
365 RpcDo_AddSuicide(playerId, factionIdx, count);
366 Rpc(RpcDo_AddSuicide, playerId, factionIdx, count);
370 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
371 private void RpcDo_AddObjective(
int playerId,
int factionIdx,
int count)
374 playerScore.m_iObjectives += count;
375 OnPlayerScoreChanged(playerId, playerScore);
377 Faction faction = GetFactionByIndex(factionIdx);
381 factionScore.m_iObjectives += count;
382 OnFactionScoreChanged(faction, factionScore);
392 void AddObjective(
int playerId,
int count = 1,
bool addToFaction =
true)
400 factionIdx = GetPlayerFactionIndex(playerId);
402 RpcDo_AddObjective(playerId, factionIdx, count);
403 Rpc(RpcDo_AddObjective, playerId, factionIdx, count);
407 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast)]
408 private void RpcDo_AddFactionObjective(
int factionIdx,
int count)
410 Faction faction = GetFactionByIndex(factionIdx);
414 factionScore.m_iObjectives += count;
415 OnFactionScoreChanged(faction, factionScore);
424 void AddFactionObjective(notnull
Faction faction,
int count = 1)
430 int factionIdx = GetFactionIndex(faction);
431 RpcDo_AddFactionObjective(factionIdx, count);
432 Rpc(RpcDo_AddFactionObjective, factionIdx, count);
439 protected void OnPlayerScoreChanged(
int playerId,
SCR_ScoreInfo scoreInfo)
441 if (m_OnPlayerScoreChangedInvoker)
442 m_OnPlayerScoreChangedInvoker.Invoke(playerId, scoreInfo);
446 statsApi.PlayerScore(playerId, scoreInfo);
455 if (m_OnFactionScoreChangedInvoker)
456 m_OnFactionScoreChangedInvoker.Invoke(faction, scoreInfo);
466 int val = (info.m_iKills + info.m_iObjectives) - info.m_iSuicides - info.m_iTeamKills;
475 int GetPlayerScore(
int playerId)
481 return CalculateScore(info);
488 if (!m_mPlayerScores.Contains(playerId))
491 return m_mPlayerScores[playerId];
496 int GetFactionScore(notnull
Faction faction)
502 return CalculateScore(info);
509 if (!m_mFactionScores.Contains(faction))
512 return m_mFactionScores[faction];
516 override void OnPlayerRegistered(
int playerId)
518 super.OnPlayerRegistered(playerId);
521 if (!m_mPlayerScores.Contains(playerId))
524 m_mPlayerScores.Insert(playerId, scoreInfo);
525 if (m_OnPlayerScoreChangedInvoker)
526 m_OnPlayerScoreChangedInvoker.Invoke(playerId, scoreInfo);
530 m_OnPlayerAdded.Invoke(playerId);
534 override void OnPlayerDisconnected(
int playerId,
KickCauseCode cause,
int timeout)
536 super.OnPlayerDisconnected(playerId, cause, timeout);
538 if (m_OnPlayerRemoved)
539 m_OnPlayerRemoved.Invoke(playerId);
543 override void EOnDiag(IEntity owner,
float timeSlice)
545 super.EOnDiag(owner, timeSlice);
550 string title = Type().ToString() +
" Diagnostics";
553 DbgUI.Text(
"--Faction Scoring--");
554 string factionFormat =
"[%1] Score=%2 || Kills=%3 | Deaths=%4 | TeamKills=%5 | Suicides=%6 | Objectives=%7";
557 int score = CalculateScore(scoreInfo);
558 string factionText =
string.Format(factionFormat, faction.GetFactionKey(), score, scoreInfo.m_iKills, scoreInfo.m_iDeaths, scoreInfo.m_iTeamKills, scoreInfo.m_iSuicides, scoreInfo.m_iObjectives);
559 DbgUI.Text(factionText);
562 DbgUI.Text(
"--Player Scoring--");
563 string playerFormat =
"pId=%1: \"%2\" Score=%3 || Kills=%4 | Deaths=%5 | TeamKills=%6 | Suicides=%7 | Objectives=%8";
564 foreach (
int playerId,
SCR_ScoreInfo scoreInfo : m_mPlayerScores)
566 string name =
GetGame().GetPlayerManager().GetPlayerName(playerId);
567 int score = CalculateScore(scoreInfo);
568 string playerText =
string.Format(playerFormat, playerId, name, score, scoreInfo.m_iKills, scoreInfo.m_iDeaths, scoreInfo.m_iTeamKills, scoreInfo.m_iSuicides, scoreInfo.m_iObjectives);
569 DbgUI.Text(playerText);
575 DbgUI.InputInt(
"Player Id", targetId);
578 DbgUI.InputInt(
"n (count): ", cnt);
580 if (DbgUI.Button(
"Add n Kills"))
581 AddKill(targetId, cnt);
582 if (DbgUI.Button(
"Add n Deaths"))
583 AddDeath(targetId, cnt);
584 if (DbgUI.Button(
"Add n TeamKills"))
585 AddTeamKill(targetId, cnt);
586 if (DbgUI.Button(
"Add n Suicides"))
587 AddSuicide(targetId, cnt);
588 if (DbgUI.Button(
"Add n Objectives"))
589 AddObjective(targetId, cnt);
590 if (DbgUI.Button(
"Add n Faction Objectives (Player Faction Only)"))
592 int facIdx = GetPlayerFactionIndex(targetId);
593 Faction f = GetFactionByIndex(facIdx);
594 AddFactionObjective(f, cnt);
608 override void EOnInit(IEntity owner)
610 super.EOnInit(owner);
612 FactionManager factionManager =
GetGame().GetFactionManager();
615 array<Faction> factions = {};
616 factionManager.GetFactionsList(factions);
618 foreach (
Faction faction : factions)
625 DiagMenu.RegisterBool(
SCR_DebugMenuID.DEBUGUI_SCORING_SYSTEM,
"",
"Scoring System",
"GameMode");
630 override void OnPostInit(IEntity owner)
632 super.OnPostInit(owner);
634 SetEventMask(owner, EntityEvent.INIT);
636 ConnectToDiagSystem(owner);
641 override void OnDelete(IEntity owner)
644 DisconnectFromDiagSystem(owner);
647 super.OnDelete(owner);