1
0

TeamDeathMatchRuleSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.First() == "UnitedNations")
  77. {
  78. return;
  79. }
  80. if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team2)
  81. {
  82. component.Team1 = npc.Factions.First();
  83. }
  84. }
  85. }
  86. if (component.Team2 == "")
  87. {
  88. if (TryComp<NpcFactionMemberComponent>(ev.Mob, out var npc))
  89. {
  90. if (npc.Factions.First() == "UnitedNations")
  91. {
  92. return;
  93. }
  94. if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team1)
  95. {
  96. component.Team2 = npc.Factions.First();
  97. }
  98. }
  99. }
  100. }
  101. }
  102. private void OnKillReported(ref KillReportedEvent ev)
  103. {
  104. var query = EntityQueryEnumerator<TeamDeathMatchRuleComponent, GameRuleComponent>();
  105. while (query.MoveNext(out var uid, out var dm, out var rule))
  106. {
  107. if (!GameTicker.IsGameRuleActive(uid, rule))
  108. continue;
  109. bool killedPlayerWasInSquad = false;
  110. // Check if the killed entity is part of either team using FactionSystem
  111. if (HasComp<NpcFactionMemberComponent>(ev.Entity))
  112. {
  113. string killedTeam = "";
  114. ShowFactionIconsComponent? factIcons = null; // Resolve component once
  115. TryComp<ShowFactionIconsComponent>(ev.Entity, out factIcons);
  116. if (_factionSystem.IsMember(ev.Entity, dm.Team1))
  117. {
  118. dm.Team1Deaths += 1;
  119. dm.Team2Kills += 1;
  120. killedTeam = dm.Team1;
  121. if (factIcons != null && factIcons.AssignedSquadNameKey != null)
  122. {
  123. killedPlayerWasInSquad = true;
  124. }
  125. }
  126. else if (_factionSystem.IsMember(ev.Entity, dm.Team2))
  127. {
  128. dm.Team2Deaths += 1;
  129. dm.Team1Kills += 1;
  130. killedTeam = dm.Team2;
  131. if (factIcons != null && factIcons.AssignedSquadNameKey != null)
  132. {
  133. killedPlayerWasInSquad = true;
  134. }
  135. }
  136. if (killedPlayerWasInSquad)
  137. {
  138. CivTDMFactionsComponent? civTDMComp = null;
  139. // Attempt to find the CivTDMFactionsComponent.
  140. // This query assumes it's a somewhat global component (e.g., on a map or game rule entity).
  141. var civQuery = _entities.EntityQueryEnumerator<CivTDMFactionsComponent>();
  142. if (civQuery.MoveNext(out _, out civTDMComp)) // Use the first one found
  143. {
  144. _factionIconsSystem.RecalculateAllCivFactionSquadCounts(civTDMComp);
  145. Log.Debug($"Player {ToPrettyString(ev.Entity)} died while in squad {factIcons?.AssignedSquadNameKey}. Recalculating CivTDMFaction squad counts.");
  146. }
  147. else
  148. {
  149. Log.Warning($"Player {ToPrettyString(ev.Entity)} died in a squad, but CivTDMFactionsComponent was not found to update counts.");
  150. }
  151. }
  152. // Track individual player stats
  153. if (ev.Primary is KillPlayerSource playerSource)
  154. {
  155. var playerIdStr = playerSource.PlayerId.ToString();
  156. if (!dm.KDRatio.ContainsKey(playerIdStr))
  157. {
  158. // Find player name from sessions
  159. string playerName = "Unknown";
  160. foreach (var session in _player.Sessions)
  161. {
  162. if (session.UserId == playerSource.PlayerId)
  163. {
  164. playerName = session.Name;
  165. break;
  166. }
  167. }
  168. string playerTeam = killedTeam == dm.Team1 ? dm.Team2 : dm.Team1;
  169. dm.KDRatio[playerIdStr] = new PlayerKDStats
  170. {
  171. Name = playerName,
  172. Team = playerTeam
  173. };
  174. }
  175. dm.KDRatio[playerIdStr].Kills++;
  176. }
  177. // Track deaths for the killed player
  178. if (_entities.TryGetComponent(ev.Entity, out ActorComponent? actorComponent))
  179. {
  180. var playerIdStrKilled = actorComponent.PlayerSession.UserId.ToString();
  181. if (!dm.KDRatio.ContainsKey(playerIdStrKilled))
  182. {
  183. dm.KDRatio[playerIdStrKilled] = new PlayerKDStats
  184. {
  185. Name = actorComponent.PlayerSession.Name,
  186. Team = killedTeam
  187. };
  188. }
  189. dm.KDRatio[playerIdStrKilled].Deaths++;
  190. }
  191. }
  192. }
  193. }
  194. protected override void AppendRoundEndText(EntityUid uid, TeamDeathMatchRuleComponent component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
  195. {
  196. // If we are using points, use them to display winner
  197. if (component.Team1Points > 0 && component.Team2Points > 0)
  198. {
  199. if (component.Team1Points > component.Team2Points)
  200. {
  201. args.AddLine($"[color=lime]{component.Team1}[/color] has won!");
  202. }
  203. else if (component.Team1Points < component.Team2Points)
  204. {
  205. args.AddLine($"[color=lime]{component.Team2}[/color] has won!");
  206. }
  207. else
  208. {
  209. args.AddLine("The round ended in a [color=yellow]draw[/color]!");
  210. }
  211. }
  212. args.AddLine("");
  213. args.AddLine($"[color=cyan]{component.Team1}[/color]: {component.Team1Kills} Kills, {component.Team1Deaths} Deaths");
  214. args.AddLine("");
  215. args.AddLine($"[color=cyan]{component.Team2}[/color]: {component.Team2Kills} Kills, {component.Team2Deaths} Deaths");
  216. // Display K/D ratio per player, sorted by K/D ratio
  217. args.AddLine("");
  218. args.AddLine("[color=yellow]Player Statistics:[/color]");
  219. // Sort players by K/D ratio (highest first)
  220. var sortedPlayers = component.KDRatio
  221. .OrderByDescending(p => p.Value.KDRatio)
  222. .ThenByDescending(p => p.Value.Kills)
  223. .ToList();
  224. // Display team 1 players
  225. args.AddLine($"[color=cyan]{component.Team1}[/color] Players:");
  226. foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team1))
  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. // Display team 2 players
  232. args.AddLine("");
  233. args.AddLine($"[color=cyan]{component.Team2}[/color] Players:");
  234. foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team2))
  235. {
  236. var kdRatio = player.Value.Deaths == 0 ? player.Value.Kills.ToString() : (player.Value.Kills / (float)player.Value.Deaths).ToString("F2");
  237. args.AddLine($" [color=white]{player.Value.Name}[/color]: {player.Value.Kills} Kills, {player.Value.Deaths} Deaths, K/D: {kdRatio}");
  238. }
  239. }
  240. }