Add Army value to player statistics

To be accounted as army, the unit needs to have
UpdatesPlayerStatistics.AddToArmyValue to true (false by default)
This commit is contained in:
Clément Bœsch
2018-11-01 09:39:11 +01:00
committed by Paul Chote
parent 4d56ecb3a8
commit 3ec2f23109

View File

@@ -46,6 +46,8 @@ namespace OpenRA.Mods.Common.Traits
public Queue<int> EarnedSamples = new Queue<int>(100);
int earnedAtBeginningOfMinute;
public Queue<int> ArmySamples = new Queue<int>(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<UpdatesPlayerStatistics> { }
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<ValuedInfo>())
cost = self.Info.TraitInfo<ValuedInfo>().Cost;
playerStats = self.Owner.PlayerActor.Trait<PlayerStatistics>();
}
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<ValuedInfo>())
attackerStats.KillsCost += cost;
defenderStats.DeathsCost += cost;
if (includedInArmyValue)
{
var cost = self.Info.TraitInfo<ValuedInfo>().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<PlayerStatistics>();
if (includedInArmyValue)
{
playerStats.ArmyValue -= cost;
newOwnerStats.ArmyValue += cost;
}
playerStats = newOwnerStats;
}
void INotifyActorDisposing.Disposing(Actor self)
{
if (includedInArmyValue)
{
playerStats.ArmyValue -= cost;
includedInArmyValue = false;
}
}
}