Sort agents by success rate in end game summary (#26058)

* Sort agents by completed objectives

* Use StringBuilder
This commit is contained in:
Crotalus
2024-03-14 16:52:45 +01:00
committed by GitHub
parent c07213c247
commit d674be697e

View File

@@ -12,6 +12,7 @@ using Content.Shared.Random.Helpers;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using System.Linq; using System.Linq;
using System.Text;
namespace Content.Server.Objectives; namespace Content.Server.Objectives;
@@ -82,29 +83,32 @@ public sealed class ObjectivesSystem : SharedObjectivesSystem
totalInCustody += minds.Where(m => IsInCustody(m)).Count(); totalInCustody += minds.Where(m => IsInCustody(m)).Count();
} }
var result = Loc.GetString("objectives-round-end-result", ("count", total), ("agent", agent)); var result = new StringBuilder();
result.AppendLine(Loc.GetString("objectives-round-end-result", ("count", total), ("agent", agent)));
if (agent == Loc.GetString("traitor-round-end-agent-name")) if (agent == Loc.GetString("traitor-round-end-agent-name"))
{ {
result += "\n" + Loc.GetString("objectives-round-end-result-in-custody", ("count", total), ("custody", totalInCustody), ("agent", agent)); result.AppendLine(Loc.GetString("objectives-round-end-result-in-custody", ("count", total), ("custody", totalInCustody), ("agent", agent)));
} }
// next add all the players with its own prepended text // next add all the players with its own prepended text
foreach (var (prepend, minds) in summary) foreach (var (prepend, minds) in summary)
{ {
if (prepend != string.Empty) if (prepend != string.Empty)
result += prepend; result.Append(prepend);
// add space between the start text and player list // add space between the start text and player list
result += "\n"; result.AppendLine();
AddSummary(ref result, agent, minds); AddSummary(result, agent, minds);
} }
ev.AddLine(result + "\n"); ev.AddLine(result.AppendLine().ToString());
} }
} }
private void AddSummary(ref string result, string agent, List<EntityUid> minds) private void AddSummary(StringBuilder result, string agent, List<EntityUid> minds)
{ {
var agentSummaries = new List<(string summary, float successRate, int completedObjectives)>();
foreach (var mindId in minds) foreach (var mindId in minds)
{ {
if (!TryComp(mindId, out MindComponent? mind)) if (!TryComp(mindId, out MindComponent? mind))
@@ -114,25 +118,26 @@ public sealed class ObjectivesSystem : SharedObjectivesSystem
if (title == null) if (title == null)
continue; continue;
result += "\n";
var custody = IsInCustody(mindId, mind) ? Loc.GetString("objectives-in-custody") : string.Empty; var custody = IsInCustody(mindId, mind) ? Loc.GetString("objectives-in-custody") : string.Empty;
var objectives = mind.Objectives; var objectives = mind.Objectives;
if (objectives.Count == 0) if (objectives.Count == 0)
{ {
result += Loc.GetString("objectives-no-objectives", ("custody", custody), ("title", title), ("agent", agent)); agentSummaries.Add((Loc.GetString("objectives-no-objectives", ("custody", custody), ("title", title), ("agent", agent)), 0f, 0));
continue; continue;
} }
result += Loc.GetString("objectives-with-objectives", ("custody", custody), ("title", title), ("agent", agent)); var completedObjectives = 0;
var totalObjectives = 0;
var agentSummary = new StringBuilder();
agentSummary.AppendLine(Loc.GetString("objectives-with-objectives", ("custody", custody), ("title", title), ("agent", agent)));
foreach (var objectiveGroup in objectives.GroupBy(o => Comp<ObjectiveComponent>(o).Issuer)) foreach (var objectiveGroup in objectives.GroupBy(o => Comp<ObjectiveComponent>(o).Issuer))
{ {
//TO DO: //TO DO:
//check for the right group here. Getting the target issuer is easy: objectiveGroup.Key //check for the right group here. Getting the target issuer is easy: objectiveGroup.Key
//It should be compared to the type of the group's issuer. //It should be compared to the type of the group's issuer.
result += "\n" + Loc.GetString($"objective-issuer-{objectiveGroup.Key}"); agentSummary.AppendLine(Loc.GetString($"objective-issuer-{objectiveGroup.Key}"));
foreach (var objective in objectiveGroup) foreach (var objective in objectiveGroup)
{ {
@@ -142,26 +147,39 @@ public sealed class ObjectivesSystem : SharedObjectivesSystem
var objectiveTitle = info.Value.Title; var objectiveTitle = info.Value.Title;
var progress = info.Value.Progress; var progress = info.Value.Progress;
totalObjectives++;
agentSummary.Append("- ");
if (progress > 0.99f) if (progress > 0.99f)
{ {
result += "\n- " + Loc.GetString( agentSummary.AppendLine(Loc.GetString(
"objectives-objective-success", "objectives-objective-success",
("objective", objectiveTitle), ("objective", objectiveTitle),
("markupColor", "green") ("markupColor", "green")
); ));
completedObjectives++;
} }
else else
{ {
result += "\n- " + Loc.GetString( agentSummary.AppendLine(Loc.GetString(
"objectives-objective-fail", "objectives-objective-fail",
("objective", objectiveTitle), ("objective", objectiveTitle),
("progress", (int) (progress * 100)), ("progress", (int) (progress * 100)),
("markupColor", "red") ("markupColor", "red")
); ));
} }
} }
} }
var successRate = totalObjectives > 0 ? (float) completedObjectives / totalObjectives : 0f;
agentSummaries.Add((agentSummary.ToString(), successRate, completedObjectives));
} }
var sortedAgents = agentSummaries.OrderByDescending(x => x.successRate)
.ThenByDescending(x => x.completedObjectives);
foreach (var (summary, _, _) in sortedAgents)
result.AppendLine(summary);
} }
public EntityUid? GetRandomObjective(EntityUid mindId, MindComponent mind, string objectiveGroupProto) public EntityUid? GetRandomObjective(EntityUid mindId, MindComponent mind, string objectiveGroupProto)