From 3ec2f231094262db7536011be7f4e1cb5356adf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Thu, 1 Nov 2018 09:39:11 +0100 Subject: [PATCH] Add Army value to player statistics To be accounted as army, the unit needs to have UpdatesPlayerStatistics.AddToArmyValue to true (false by default) --- .../Traits/Player/PlayerStatistics.cs | 76 +++++++++++++++++-- 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Player/PlayerStatistics.cs b/OpenRA.Mods.Common/Traits/Player/PlayerStatistics.cs index 99a299f3bf..2ad873aeae 100644 --- a/OpenRA.Mods.Common/Traits/Player/PlayerStatistics.cs +++ b/OpenRA.Mods.Common/Traits/Player/PlayerStatistics.cs @@ -46,6 +46,8 @@ namespace OpenRA.Mods.Common.Traits public Queue EarnedSamples = new Queue(100); int earnedAtBeginningOfMinute; + public Queue ArmySamples = new Queue(100); + public int KillsCost; public int DeathsCost; @@ -55,6 +57,8 @@ namespace OpenRA.Mods.Common.Traits public int BuildingsKilled; public int BuildingsDead; + public int ArmyValue; + public PlayerStatistics(Actor self) { } void INotifyCreated.Created(Actor self) @@ -71,10 +75,20 @@ namespace OpenRA.Mods.Common.Traits EarnedSamples.Dequeue(); } + void UpdateArmyThisMinute() + { + ArmySamples.Enqueue(ArmyValue); + if (ArmySamples.Count > 100) + ArmySamples.Dequeue(); + } + void ITick.Tick(Actor self) { if (self.World.WorldTick % 1500 == 1) + { UpdateEarnedThisMinute(); + UpdateArmyThisMinute(); + } } public void ResolveOrder(Actor self, Order order) @@ -106,10 +120,29 @@ namespace OpenRA.Mods.Common.Traits } [Desc("Attach this to a unit to update observer stats.")] - public class UpdatesPlayerStatisticsInfo : TraitInfo { } - - public class UpdatesPlayerStatistics : INotifyKilled + public class UpdatesPlayerStatisticsInfo : ITraitInfo { + [Desc("Add to army value in statistics")] + public bool AddToArmyValue = false; + + public object Create(ActorInitializer init) { return new UpdatesPlayerStatistics(this, init.Self); } + } + + public class UpdatesPlayerStatistics : INotifyKilled, INotifyCreated, INotifyOwnerChanged, INotifyActorDisposing + { + UpdatesPlayerStatisticsInfo info; + PlayerStatistics playerStats; + int cost = 0; + bool includedInArmyValue = false; + + public UpdatesPlayerStatistics(UpdatesPlayerStatisticsInfo info, Actor self) + { + this.info = info; + if (self.Info.HasTraitInfo()) + cost = self.Info.TraitInfo().Cost; + playerStats = self.Owner.PlayerActor.Trait(); + } + void INotifyKilled.Killed(Actor self, AttackInfo e) { if (self.Owner.WinState != WinState.Undefined) @@ -128,11 +161,40 @@ namespace OpenRA.Mods.Common.Traits defenderStats.UnitsDead++; } - if (self.Info.HasTraitInfo()) + attackerStats.KillsCost += cost; + defenderStats.DeathsCost += cost; + if (includedInArmyValue) { - var cost = self.Info.TraitInfo().Cost; - attackerStats.KillsCost += cost; - defenderStats.DeathsCost += cost; + defenderStats.ArmyValue -= cost; + includedInArmyValue = false; + } + } + + void INotifyCreated.Created(Actor self) + { + includedInArmyValue = info.AddToArmyValue; + if (includedInArmyValue) + playerStats.ArmyValue += cost; + } + + void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) + { + var newOwnerStats = newOwner.PlayerActor.Trait(); + if (includedInArmyValue) + { + playerStats.ArmyValue -= cost; + newOwnerStats.ArmyValue += cost; + } + + playerStats = newOwnerStats; + } + + void INotifyActorDisposing.Disposing(Actor self) + { + if (includedInArmyValue) + { + playerStats.ArmyValue -= cost; + includedInArmyValue = false; } } }