Skip to content

Commit

Permalink
integrate MissionSiteModifiers stub with the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 22, 2024
1 parent fee043d commit 8201661
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/game-lib-tests/GameStateFixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static GameState Get()
bool isExpired = i % 2 == 0;
var missionSite = new MissionSite(
id: i,
gs.Factions[i % gs.Factions.Count],
faction: gs.Factions[i % gs.Factions.Count],
modifiers: new MissionSiteModifiers(),
difficulty: 1,
turnAppeared: 1,
expiresIn: isExpired ? null : 1,
Expand Down
11 changes: 10 additions & 1 deletion src/game-lib/Model/Faction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ public static Faction Init(
intelInvested: 0);

public Faction DeepClone()
=> new(Id, Name, Power, MissionSiteCountdown, PowerIncrease, PowerAcceleration, AccumulatedPowerAcceleration, IntelInvested);
=> new Faction(
Id,
Name,
Power,
MissionSiteCountdown,
PowerIncrease,
PowerAcceleration,
AccumulatedPowerAcceleration,
IntelInvested);

public List<MissionSite> CreateMissionSites(
ILog log,
Expand Down Expand Up @@ -99,6 +107,7 @@ public List<MissionSite> CreateMissionSites(
var site = new MissionSite(
siteId,
this,
new MissionSiteModifiers(1),
difficulty,
turnAppeared: state.Timeline.CurrentTurn,
expiresIn: Ruleset.MissionSiteTurnsUntilExpiration);
Expand Down
5 changes: 5 additions & 0 deletions src/game-lib/Model/MissionSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MissionSite : IIdentifiable
public int Id { get; }

public readonly Faction Faction;

public readonly MissionSiteModifiers Modifiers;
public readonly int Difficulty;
public readonly int TurnAppeared;

Expand All @@ -31,6 +33,7 @@ public class MissionSite : IIdentifiable
public MissionSite(
int id,
Faction faction,
MissionSiteModifiers modifiers,
int difficulty,
int turnAppeared,
int? expiresIn,
Expand All @@ -41,6 +44,7 @@ public MissionSite(
Contract.Assert(ExpiresIn is null or >= 0);
Id = id;
Faction = faction;
Modifiers = modifiers;
Difficulty = difficulty;
TurnAppeared = turnAppeared;
TurnDeactivated = turnDeactivated;
Expand Down Expand Up @@ -109,6 +113,7 @@ public MissionSite DeepClone(Faction clonedFaction)
return new MissionSite(
id: Id,
faction: clonedFaction,
modifiers: Modifiers.DeepClone(),
difficulty: Difficulty,
turnAppeared: TurnAppeared,
expiresIn: ExpiresIn,
Expand Down
67 changes: 48 additions & 19 deletions src/game-lib/Model/MissionSiteModifiers.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
using System.Text.Json.Serialization;

namespace UfoGameLib.Model;

// kja Changes to MissionSite to make:
// - add props for (money, intel, funding, support), both reward if won and penalty if lost or expired.
// - add props for: faction damage: reduction of (power, power increase, power acceleration), if mission won
public record MissionSiteModifiers
{
private readonly int _moneyReward;
private readonly int _intelReward;
private readonly int _fundingReward;
private readonly int _supportReward;
private readonly int _powerDamageReward;
private readonly int _powerIncreaseDamageReward;
private readonly int _powerAccelerationDamageReward;
private readonly int _fundingPenalty;
private readonly int _supportPenalty;
public readonly int MoneyReward;
public readonly int IntelReward;
public readonly int FundingReward;
public readonly int SupportReward;
public readonly int PowerDamageReward;
public readonly int PowerIncreaseDamageReward;
public readonly int PowerAccelerationDamageReward;
public readonly int FundingPenalty;
public readonly int SupportPenalty;

[JsonConstructor]
public MissionSiteModifiers(
int moneyReward,
int intelReward,
int fundingReward,
int supportReward,
int powerDamageReward,
int powerIncreaseDamageReward,
int powerAccelerationDamageReward,
int fundingPenalty,
int supportPenalty)
{
MoneyReward = moneyReward;
IntelReward = intelReward;
FundingReward = fundingReward;
SupportReward = supportReward;
PowerDamageReward = powerDamageReward;
PowerIncreaseDamageReward = powerIncreaseDamageReward;
PowerAccelerationDamageReward = powerAccelerationDamageReward;
FundingPenalty = fundingPenalty;
SupportPenalty = supportPenalty;
}

public MissionSiteModifiers(
int? moneyReward = null,
Expand All @@ -24,16 +49,20 @@ public MissionSiteModifiers(
int? powerIncreaseDamageReward = null,
int? powerAccelerationDamageReward = null,
int? fundingPenalty = null,
int? supportPenalty = null)
int? supportPenalty = null) : this(
moneyReward ?? 0,
intelReward ?? 0,
fundingReward ?? 0,
supportReward ?? 0,
powerDamageReward ?? 0,
powerIncreaseDamageReward ?? 0,
powerAccelerationDamageReward ?? 0,
fundingPenalty ?? 0,
supportPenalty ?? 0
)
{
_moneyReward = moneyReward ?? 0;
_intelReward = intelReward ?? 0;
_fundingReward = fundingReward ?? 0;
_supportReward = supportReward ?? 0;
_powerDamageReward = powerDamageReward ?? 0;
_powerIncreaseDamageReward = powerIncreaseDamageReward ?? 0;
_powerAccelerationDamageReward = powerAccelerationDamageReward ?? 0;
_fundingPenalty = fundingPenalty ?? 0;
_supportPenalty = supportPenalty ?? 0;
}

public MissionSiteModifiers DeepClone()
=> (MissionSiteModifiers)MemberwiseClone();
}
3 changes: 3 additions & 0 deletions src/game-lib/State/GameStateJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public override GameState Read(ref Utf8JsonReader reader, Type typeToConvert, Js
JsonNode gameStateNode = JsonNode(ref reader);

int updateCount = DeserializeInt(gameStateNode, nameof(GameState.UpdateCount));
// kja should this instead be Deserialize<List<Faction>> ? Like terminated agents below.
// I think right now factions may just be empty due to empty ctor.
Factions factions = Deserialize<Factions>(gameStateNode);
Timeline timeline = Deserialize<Timeline>(gameStateNode);

Expand All @@ -127,6 +129,7 @@ public override GameState Read(ref Utf8JsonReader reader, Type typeToConvert, Js
=> new MissionSite(
id: DeserializeInt(missionSiteObj, nameof(MissionSite.Id)),
faction: faction!,
modifiers: Deserialize<MissionSiteModifiers>(missionSiteObj, nameof(MissionSite.Modifiers)),
difficulty: DeserializeInt(missionSiteObj, nameof(MissionSite.Difficulty)),
turnAppeared: DeserializeInt(missionSiteObj, nameof(MissionSite.TurnAppeared)),
expiresIn: DeserializeIntOrNull(missionSiteObj, nameof(MissionSite.ExpiresIn)),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Json/JsonConverterSupportingReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ protected void ReplaceArrayObjectsPropertiesWithRefs(JsonNode parent, string obj
protected TItem Deserialize<TItem>(JsonNode parent)
=> parent[typeof(TItem).Name].Deserialize<TItem>(SerializationOptions)!;

protected TItem Deserialize<TItem>(JsonNode parent, string name)
=> parent[name].Deserialize<TItem>(SerializationOptions)!;
protected TItem Deserialize<TItem>(JsonNode parent, string propName)
=> parent[propName].Deserialize<TItem>(SerializationOptions)!;

protected List<TItem> DeserializeList<TItem>(JsonNode parent, string propName)
=> parent[propName].Deserialize<List<TItem>>(SerializationOptions)!;
Expand Down

0 comments on commit 8201661

Please sign in to comment.