using System.Linq;
using Content.Server.Administration.Commands;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.KillTracking;
using Content.Server.Mind;
using Content.Server.Points;
using Content.Server.RoundEnd;
using Content.Server.Station.Systems;
using Content.Server.NPC.Systems;
using Content.Shared.GameTicking;
using Content.Shared.GameTicking.Components;
using Content.Shared.NPC.Components;
using Content.Shared.Points;
using Content.Shared.Storage;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Utility;
using Content.Shared.NPC.Systems;
using Robust.Shared.Player;
using Content.Server.Overlays; // Added for FactionIconsSystem
using Content.Shared.Overlays;
using Content.Shared.Civ14.CivTDMFactions; // Added for CivTDMFactionsComponent
namespace Content.Server.GameTicking.Rules;
///
/// Manages
///
public sealed class TeamDeathMatchRuleSystem : GameRuleSystem
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly MindSystem _mind = default!;
[Dependency] private readonly PointSystem _point = default!;
[Dependency] private readonly RespawnRuleSystem _respawn = default!;
[Dependency] private readonly RoundEndSystem _roundEnd = default!;
[Dependency] private readonly StationSpawningSystem _stationSpawning = default!;
[Dependency] private readonly NpcFactionSystem _factionSystem = default!; // Added dependency
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly IEntityManager _entities = default!;
[Dependency] private readonly FactionIconsSystem _factionIconsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnBeforeSpawn);
SubscribeLocalEvent(OnSpawnComplete);
SubscribeLocalEvent(OnKillReported);
}
private void OnBeforeSpawn(PlayerBeforeSpawnEvent ev)
{
var query = EntityQueryEnumerator();
while (query.MoveNext(out var uid, out var dm, out var tracker, out var rule))
{
if (!GameTicker.IsGameRuleActive(uid, rule))
continue;
var newMind = _mind.CreateMind(ev.Player.UserId, ev.Profile.Name);
_mind.SetUserId(newMind, ev.Player.UserId);
var mobMaybe = _stationSpawning.SpawnPlayerCharacterOnStation(ev.Station, null, ev.Profile);
DebugTools.AssertNotNull(mobMaybe);
var mob = mobMaybe!.Value;
_mind.TransferTo(newMind, mob);
EnsureComp(mob);
ev.Handled = true;
break;
}
}
private void OnSpawnComplete(PlayerSpawnCompleteEvent ev)
{
EnsureComp(ev.Mob);
var query = EntityQueryEnumerator();
while (query.MoveNext(out var uid, out var component, out var rule))
{
if (!GameTicker.IsGameRuleActive(uid, rule))
continue;
if (component.Team1 == "")
{
if (TryComp(ev.Mob, out var npc))
{
if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team2)
{
component.Team1 = npc.Factions.First();
}
}
}
if (component.Team2 == "")
{
if (TryComp(ev.Mob, out var npc))
{
if (npc.Factions.Count > 0 && npc.Factions.First() != component.Team1)
{
component.Team2 = npc.Factions.First();
}
}
}
}
}
private void OnKillReported(ref KillReportedEvent ev)
{
var query = EntityQueryEnumerator();
while (query.MoveNext(out var uid, out var dm, out var rule))
{
if (!GameTicker.IsGameRuleActive(uid, rule))
continue;
bool killedPlayerWasInSquad = false;
// Check if the killed entity is part of either team using FactionSystem
if (HasComp(ev.Entity))
{
string killedTeam = "";
ShowFactionIconsComponent? factIcons = null; // Resolve component once
TryComp(ev.Entity, out factIcons);
if (_factionSystem.IsMember(ev.Entity, dm.Team1))
{
dm.Team1Deaths += 1;
dm.Team2Kills += 1;
killedTeam = dm.Team1;
if (factIcons != null && factIcons.AssignedSquadNameKey != null)
{
killedPlayerWasInSquad = true;
}
}
else if (_factionSystem.IsMember(ev.Entity, dm.Team2))
{
dm.Team2Deaths += 1;
dm.Team1Kills += 1;
killedTeam = dm.Team2;
if (factIcons != null && factIcons.AssignedSquadNameKey != null)
{
killedPlayerWasInSquad = true;
}
}
if (killedPlayerWasInSquad)
{
CivTDMFactionsComponent? civTDMComp = null;
// Attempt to find the CivTDMFactionsComponent.
// This query assumes it's a somewhat global component (e.g., on a map or game rule entity).
var civQuery = _entities.EntityQueryEnumerator();
if (civQuery.MoveNext(out _, out civTDMComp)) // Use the first one found
{
_factionIconsSystem.RecalculateAllCivFactionSquadCounts(civTDMComp);
Log.Debug($"Player {ToPrettyString(ev.Entity)} died while in squad {factIcons?.AssignedSquadNameKey}. Recalculating CivTDMFaction squad counts.");
}
else
{
Log.Warning($"Player {ToPrettyString(ev.Entity)} died in a squad, but CivTDMFactionsComponent was not found to update counts.");
}
}
// Track individual player stats
if (ev.Primary is KillPlayerSource playerSource)
{
var playerIdStr = playerSource.PlayerId.ToString();
if (!dm.KDRatio.ContainsKey(playerIdStr))
{
// Find player name from sessions
string playerName = "Unknown";
foreach (var session in _player.Sessions)
{
if (session.UserId == playerSource.PlayerId)
{
playerName = session.Name;
break;
}
}
string playerTeam = killedTeam == dm.Team1 ? dm.Team2 : dm.Team1;
dm.KDRatio[playerIdStr] = new PlayerKDStats
{
Name = playerName,
Team = playerTeam
};
}
dm.KDRatio[playerIdStr].Kills++;
}
// Track deaths for the killed player
if (_entities.TryGetComponent(ev.Entity, out ActorComponent? actorComponent))
{
var playerIdStrKilled = actorComponent.PlayerSession.UserId.ToString();
if (!dm.KDRatio.ContainsKey(playerIdStrKilled))
{
dm.KDRatio[playerIdStrKilled] = new PlayerKDStats
{
Name = actorComponent.PlayerSession.Name,
Team = killedTeam
};
}
dm.KDRatio[playerIdStrKilled].Deaths++;
}
}
}
}
protected override void AppendRoundEndText(EntityUid uid, TeamDeathMatchRuleComponent component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
{
// If we are using points, use them to display winner
if (component.Team1Points > 0 && component.Team2Points > 0)
{
if (component.Team1Points > component.Team2Points)
{
args.AddLine($"[color=lime]{component.Team1}[/color] has won!");
}
else if (component.Team1Points < component.Team2Points)
{
args.AddLine($"[color=lime]{component.Team2}[/color] has won!");
}
else
{
args.AddLine("The round ended in a [color=yellow]draw[/color]!");
}
}
args.AddLine("");
args.AddLine($"[color=cyan]{component.Team1}[/color]: {component.Team1Kills} Kills, {component.Team1Deaths} Deaths");
args.AddLine("");
args.AddLine($"[color=cyan]{component.Team2}[/color]: {component.Team2Kills} Kills, {component.Team2Deaths} Deaths");
// Display K/D ratio per player, sorted by K/D ratio
args.AddLine("");
args.AddLine("[color=yellow]Player Statistics:[/color]");
// Sort players by K/D ratio (highest first)
var sortedPlayers = component.KDRatio
.OrderByDescending(p => p.Value.KDRatio)
.ThenByDescending(p => p.Value.Kills)
.ToList();
// Display team 1 players
args.AddLine($"[color=cyan]{component.Team1}[/color] Players:");
foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team1))
{
var kdRatio = player.Value.Deaths == 0 ? player.Value.Kills.ToString() : (player.Value.Kills / (float)player.Value.Deaths).ToString("F2");
args.AddLine($" [color=white]{player.Value.Name}[/color]: {player.Value.Kills} Kills, {player.Value.Deaths} Deaths, K/D: {kdRatio}");
}
// Display team 2 players
args.AddLine("");
args.AddLine($"[color=cyan]{component.Team2}[/color] Players:");
foreach (var player in sortedPlayers.Where(p => p.Value.Team == component.Team2))
{
var kdRatio = player.Value.Deaths == 0 ? player.Value.Kills.ToString() : (player.Value.Kills / (float)player.Value.Deaths).ToString("F2");
args.AddLine($" [color=white]{player.Value.Name}[/color]: {player.Value.Kills} Kills, {player.Value.Deaths} Deaths, K/D: {kdRatio}");
}
}
}