Skip to content

Commit

Permalink
Fix GameState.DeepClone to properly handle cloned Factions
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 13, 2024
1 parent 580d452 commit f96fec9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/game-lib/Model/MissionSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ private void Expire(int turnExpired)
private void AssertStatusInvariant()
=> Contract.Assert(IsActive ^ IsExpired ^ WasLaunched);

public MissionSite DeepClone()
public MissionSite DeepClone(Faction clonedFaction)
{
return (MissionSite) MemberwiseClone();
return new MissionSite(
id: Id,
faction: clonedFaction,
difficulty: Difficulty,
turnAppeared: TurnAppeared,
expiresIn: ExpiresIn,
turnDeactivated: TurnDeactivated,
expired: Expired);
}
}
8 changes: 6 additions & 2 deletions src/game-lib/Model/MissionSites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public MissionSites(IEnumerable<MissionSite>? missionSites = null)

public MissionSites Expired => this.Where(missionSite => missionSite.IsExpired).ToMissionSites();

public MissionSites DeepClone()
public MissionSites DeepClone(Factions clonedFactions)
{
return new MissionSites(this.Select(missionSite => missionSite.DeepClone()));
return new MissionSites(this.Select(missionSite =>
{
Faction clonedFaction = clonedFactions.Single(clonedFaction => clonedFaction.Id == missionSite.Faction.Id);
return missionSite.DeepClone(clonedFaction);
}));
}
}
3 changes: 1 addition & 2 deletions src/game-lib/State/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ public GameState Clone(bool useJsonSerialization = false)

private GameState DeepClone()
{
// kja these clonedFactions will need to be passed as input to MissionSites DeepClone
Factions clonedFactions = Factions.DeepClone();
MissionSites clonedMissionSites = MissionSites.DeepClone();
MissionSites clonedMissionSites = MissionSites.DeepClone(clonedFactions);
Missions clonedMissions = Missions.DeepClone(clonedMissionSites);
return new GameState(
updateCount: UpdateCount,
Expand Down

0 comments on commit f96fec9

Please sign in to comment.