TeamDeathMatchRuleSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Linq;
  2. using Content.Server.Administration.Commands;
  3. using Content.Server.GameTicking.Rules.Components;
  4. using Content.Server.KillTracking;
  5. using Content.Server.Mind;
  6. using Content.Server.Points;
  7. using Content.Server.RoundEnd;
  8. using Content.Server.Station.Systems;
  9. using Content.Server.NPC.Systems;
  10. using Content.Shared.GameTicking;
  11. using Content.Shared.GameTicking.Components;
  12. using Content.Shared.NPC.Components;
  13. using Content.Shared.Points;
  14. using Content.Shared.Storage;
  15. using Robust.Server.GameObjects;
  16. using Robust.Server.Player;
  17. using Robust.Shared.Utility;
  18. using Content.Shared.NPC.Systems;
  19. using Robust.Shared.Player;
  20. using Content.Server.Overlays; // Added for FactionIconsSystem
  21. using Content.Shared.Overlays;
  22. using Content.Shared.Civ14.CivTDMFactions; // Added for CivTDMFactionsComponent
  23. namespace Content.Server.GameTicking.Rules;
  24. /// <summary>
  25. /// Manages <see cref="TeamDeathMatchRuleComponent"/>
  26. /// </summary>
  27. public sealed class TeamDeathMatchRuleSystem : GameRuleSystem<TeamDeathMatchRuleComponent>
  28. {
  29. [Dependency] private readonly IPlayerManager _player = default!;
  30. [Dependency] private readonly MindSystem _mind = default!;
  31. [Dependency] private readonly PointSystem _point = default!;
  32. [Dependency] private readonly RespawnRuleSystem _respawn = default!;
  33. [Dependency] private readonly RoundEndSystem _roundEnd = default!;
  34. [Dependency] private readonly StationSpawningSystem _stationSpawning = default!;
  35. [Dependency] private readonly NpcFactionSystem _factionSystem = default!; // Added dependency
  36. [Dependency] private readonly TransformSystem _transform = default!;
  37. [Dependency] private readonly IEntityManager _entities = default!;
  38. [Dependency] private readonly FactionIconsSystem _factionIconsSystem = default!;
  39. public override void Initialize()
  40. {
  41. base.Initialize();
  42. SubscribeLocalEvent<PlayerBeforeSpawnEvent>(OnBeforeSpawn);
  43. SubscribeLocalEvent<PlayerSpawnCompleteEvent>(OnSpawnComplete);
  44. SubscribeLocalEvent<KillReportedEvent>(OnKillReported);
  45. }
  46. private void OnBeforeSpawn(PlayerBeforeSpawnEvent ev)
  47. {
  48. var query = EntityQueryEnumerator<TeamDeathMatchRuleComponent, RespawnTrackerComponent, GameRuleComponent>();
  49. while (query.MoveNext(out var uid, out var dm, out var tracker, out var rule))
  50. {
  51. if (!GameTicker.IsGameRuleActive(uid, rule))
  52. continue;
  53. var newMind = _mind.CreateMind(ev.Player.UserId, ev.Profile.Name);
  54. _mind.SetUserId(newMind, ev.Player.UserId);
  55. var mobMaybe = _stationSpawning.SpawnPlayerCharacterOnStation(ev.Station, null, ev.Profile);
  56. DebugTools.AssertNotNull(mobMaybe);
  57. var mob = mobMaybe!.Value;
  58. _mind.TransferTo(newMind, mob);
  59. EnsureComp<KillTrackerComponent>(mob);
  60. ev.Handled = true;
  61. break;
  62. }
  63. }
  64. private void OnSpawnComplete(PlayerSpawnCompleteEvent ev)
  65. {
  66. EnsureComp<KillTrackerComponent>(ev.Mob);
  67. var query = EntityQueryEnumerator<TeamDeathMatchRuleComponent, GameRuleComponent>();
  68. while (query.MoveNext(out var uid, out var component, out var rule))
  69. {
  70. if (!GameTicker.IsGameRuleActive(uid, rule))
  71. continue;
  72. if (component.Team1 == "")
  73. {
  74. if (TryComp<NpcFactionMemberComponent>(ev.Mob, out var npc))
  75. {
  76. if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team2)
  77. {
  78. component.Team1 = npc.Factions.First();
  79. }
  80. }
  81. }
  82. if (component.Team2 == "")
  83. {
  84. if (TryComp<NpcFactionMemberComponent>(ev.Mob, out var npc))
  85. {
  86. if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team1)
  87. {
  88. component.Team2 = npc.Factions.First();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. private void OnKillReported(ref KillReportedEvent ev)
  95. {
  96. var query = EntityQueryEnumerator<TeamDeathMatchRuleComponent, GameRuleComponent>();
  97. while (query.MoveNext(out var uid, out var dm, out var rule))
  98. {
  99. if (!GameTicker.IsGameRuleActive(uid, rule))
  100. continue;
  101. bool killedPlayerWasInSquad = false;
  102. // Check if the killed entity is part of either team using FactionSystem
  103. if (HasComp<NpcFactionMemberComponent>(ev.Entity))
  104. {
  105. string killedTeam = "";
  106. ShowFactionIconsComponent? factIcons = null; // Resolve component once
  107. TryComp<ShowFactionIconsComponent>(ev.Entity, out factIcons);
  108. if (_factionSystem.IsMember(ev.Entity, dm.Team1))
  109. {
  110. dm.Team1Deaths += 1;
  111. dm.Team2Kills += 1;
  112. killedTeam = dm.Team1;
  113. if (factIcons != null && factIcons.AssignedSquadNameKey != null)
  114. {
  115. killedPlayerWasInSquad = true;
  116. }
  117. }
  118. else if (_factionSystem.IsMember(ev.Entity, dm.Team2))
  119. {
  120. dm.Team2Deaths += 1;
  121. dm.Team1Kills += 1;
  122. killedTeam = dm.Team2;
  123. if (factIcons != null && factIcons.AssignedSquadNameKey != null)
  124. {
  125. killedPlayerWasInSquad = true;
  126. }
  127. }
  128. if (killedPlayerWasInSquad)
  129. {
  130. CivTDMFactionsComponent? civTDMComp = null;
  131. // Attempt to find the CivTDMFactionsComponent.
  132. // This query assumes it's a somewhat global component (e.g., on a map or game rule entity).
  133. var civQuery = _entities.EntityQueryEnumerator<CivTDMFactionsComponent>();
  134. if (civQuery.MoveNext(out _, out civTDMComp)) // Use the first one found
  135. {
  136. _factionIconsSystem.RecalculateAllCivFactionSquadCounts(civTDMComp);
  137. Log.Debug($"Player {ToPrettyString(ev.Entity)} died while in squad {factIcons?.AssignedSquadNameKey}. Recalculating CivTDMFaction squad counts.");
  138. }
  139. else
  140. {
  141. Log.Warning($"Player {ToPrettyString(ev.Entity)} died in a squad, but CivTDMFactionsComponent was not found to update counts.");
  142. }
  143. }
  144. // Track individual player stats
  145. if (ev.Primary is KillPlayerSource playerSource)
  146. {
  147. var playerIdStr = playerSource.PlayerId.ToString();
  148. if (!dm.KDRatio.ContainsKey(playerIdStr))
  149. {
  150. // Find player name from sessions
  151. string playerName = "Unknown";
  152. foreach (var session in _player.Sessions)
  153. {
  154. if (session.UserId == playerSource.PlayerId)
  155. {
  156. playerName = session.Name;
  157. break;
  158. }
  159. }
  160. string playerTeam = killedTeam == dm.Team1 ? dm.Team2 : dm.Team1;
  161. dm.KDRatio[playerIdStr] = new PlayerKDStats
  162. {
  163. Name = playerName,
  164. Team = playerTeam
  165. };
  166. }
  167. dm.KDRatio[playerIdStr].Kills++;
  168. }
  169. // Track deaths for the killed player
  170. if (_entities.TryGetComponent(ev.Entity, out ActorComponent? actorComponent))
  171. {
  172. var playerIdStrKilled = actorComponent.PlayerSession.UserId.ToString();
  173. if (!dm.KDRatio.ContainsKey(playerIdStrKilled))
  174. {
  175. dm.KDRatio[playerIdStrKilled] = new PlayerKDStats
  176. {
  177. Name = actorComponent.PlayerSession.Name,
  178. Team = killedTeam
  179. };
  180. }
  181. dm.KDRatio[playerIdStrKilled].Deaths++;
  182. }
  183. }
  184. }
  185. }
  186. protected override void AppendRoundEndText(EntityUid uid, TeamDeathMatchRuleComponent component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
  187. {
  188. // If we are using points, use them to display winner
  189. if (component.Team1Points > 0 && component.Team2Points > 0)
  190. {
  191. if (component.Team1Points > component.Team2Points)
  192. {
  193. args.AddLine($"[color=lime]{component.Team1}[/color] has won!");
  194. }
  195. else if (component.Team1Points < component.Team2Points)
  196. {
  197. args.AddLine($"[color=lime]{component.Team2}[/color] has won!");
  198. }
  199. else
  200. {
  201. args.AddLine("The round ended in a [color=yellow]draw[/color]!");
  202. }
  203. }
  204. args.AddLine("");
  205. args.AddLine($"[color=cyan]{component.Team1}[/color]: {component.Team1Kills} Kills, {component.Team1Deaths} Deaths");
  206. args.AddLine("");
  207. args.AddLine($"[color=cyan]{component.Team2}[/color]: {component.Team2Kills} Kills, {component.Team2Deaths} Deaths");
  208. // Display K/D ratio per player, sorted by K/D ratio
  209. args.AddLine("");
  210. args.AddLine("[color=yellow]Player Statistics:[/color]");
  211. // Sort players by K/D ratio (highest first)
  212. var sortedPlayers = component.KDRatio
  213. .OrderByDescending(p => p.Value.KDRatio)
  214. .ThenByDescending(p => p.Value.Kills)
  215. .ToList();
  216. // Display team 1 players
  217. args.AddLine($"[color=cyan]{component.Team1}[/color] Players:");
  218. foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team1))
  219. {
  220. var kdRatio = player.Value.Deaths == 0 ? player.Value.Kills.ToString() : (player.Value.Kills / (float)player.Value.Deaths).ToString("F2");
  221. args.AddLine($" [color=white]{player.Value.Name}[/color]: {player.Value.Kills} Kills, {player.Value.Deaths} Deaths, K/D: {kdRatio}");
  222. }
  223. // Display team 2 players
  224. args.AddLine("");
  225. args.AddLine($"[color=cyan]{component.Team2}[/color] Players:");
  226. foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team2))
  227. {
  228. var kdRatio = player.Value.Deaths == 0 ? player.Value.Kills.ToString() : (player.Value.Kills / (float)player.Value.Deaths).ToString("F2");
  229. args.AddLine($" [color=white]{player.Value.Name}[/color]: {player.Value.Kills} Kills, {player.Value.Deaths} Deaths, K/D: {kdRatio}");
  230. }
  231. }
  232. }