Skip to content

Commit

Permalink
add Id to GameEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 8, 2024
1 parent 4aa7ce2 commit d9a0e7b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
15 changes: 12 additions & 3 deletions src/game-lib/Controller/GameSessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public GameSessionController(Configuration config, ILog log, GameSession gameSes
_config = config;
_log = log;
GameSession = gameSession;
CurrentTurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
CurrentTurnController = new GameTurnController(
_log,
GameSession.RandomGen,
GameSession.CurrentGameState,
GameSession.NextEventId);
}

public GameStatePlayerView CurrentGameStatePlayerView
Expand Down Expand Up @@ -137,6 +141,7 @@ private void PlayGameUntilOver(IPlayer player, int turnLimit)
// This state diff shows the result of advancing time.
DiffGameStates(GameSession.CurrentGameState, nextTurnStartState);

GameSession.CurrentTurn.AssertInvariants();
NewTurn(worldEvents, nextTurnStartState);
}
}
Expand All @@ -146,7 +151,11 @@ private void NewTurn(List<WorldEvent> worldEvents, GameState nextTurnStartState)
GameSession.Turns.Add(new GameSessionTurn(
eventsUntilStartState: worldEvents,
startState: nextTurnStartState));
CurrentTurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
CurrentTurnController = new GameTurnController(
_log,
GameSession.RandomGen,
GameSession.CurrentGameState,
GameSession.NextEventId);
}

private List<WorldEvent> GetAndDeleteRecordedWorldEvents()
Expand All @@ -156,7 +165,7 @@ private List<WorldEvent> GetAndDeleteRecordedWorldEvents()
}

public PlayerActionEvent AdvanceTime(GameState? state = null)
=> new AdvanceTimePlayerAction(_log, GameSession.RandomGen).Apply(state ?? GameSession.CurrentGameState);
=> new AdvanceTimePlayerAction(_log, GameSession.RandomGen).Apply(state ?? GameSession.CurrentGameState, GameSession.NextEventId);

public GameState SaveCurrentGameStateToFile()
{
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/Controller/GameSessionControllerDeprecated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public GameSessionControllerDeprecated(Configuration config, ILog log, GameSessi
_config = config;
_log = log;
GameSession = gameSession;
TurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
TurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState, 0);
}

public GameStatePlayerView CurrentGameStatePlayerView
Expand Down Expand Up @@ -164,7 +164,7 @@ private List<WorldEvent> GetAndDeleteRecordedWorldEvents()
}

public PlayerActionEvent AdvanceTime()
=> new AdvanceTimePlayerAction(_log, GameSession.RandomGen).Apply(GameSession.CurrentGameState);
=> new AdvanceTimePlayerAction(_log, GameSession.RandomGen).Apply(GameSession.CurrentGameState, 0);

public GameState SaveCurrentGameStateToFile()
{
Expand Down
8 changes: 5 additions & 3 deletions src/game-lib/Controller/GameTurnController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public class GameTurnController
{
private readonly ILog _log;
private readonly GameState _gameState;
private int _nextEventId;
private readonly List<PlayerActionEvent> _recordedPlayerActionEvents = new();

public GameTurnController(ILog log, RandomGen randomGen, GameState gameState)
public GameTurnController(ILog log, RandomGen randomGen, GameState gameState, int nextEventId)
{
_log = log;
RandomGen = randomGen;
_gameState = gameState;
_nextEventId = nextEventId;
}

public RandomGen RandomGen { get; }
Expand Down Expand Up @@ -97,9 +99,9 @@ private Agents GetAgentsByIds(int[] agentsIds) =>

private PlayerActionEvent ExecuteAndRecordAction(PlayerAction action)
{
// This assertion is here to prevent the player of doing anything if they caused the game to be over.
// This assertion is here to prevent the player from doing anything if they caused the game to be over.
Contract.Assert(!_gameState.IsGameOver);
PlayerActionEvent playerActionEvent = action.Apply(_gameState);
PlayerActionEvent playerActionEvent = action.Apply(_gameState, _nextEventId++);
_recordedPlayerActionEvents.Add(playerActionEvent);
return playerActionEvent;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/Controller/PlayerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UfoGameLib.Controller;

public abstract class PlayerAction
{
public PlayerActionEvent Apply(GameState state)
public PlayerActionEvent Apply(GameState state, int nextEventId)
{
Contract.Assert(!state.IsGameOver,
$"money: {state.Assets.Money} " +
Expand All @@ -28,7 +28,7 @@ public PlayerActionEvent Apply(GameState state)
$"support: {state.Assets.Support}");
}

return new PlayerActionEvent(GetType().Name, applyResultDetails);
return new PlayerActionEvent(nextEventId, GetType().Name, applyResultDetails);

}
protected abstract string ApplyImpl(GameState state);
Expand Down
4 changes: 3 additions & 1 deletion src/game-lib/Events/GameEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

public abstract class GameEvent
{
public readonly int Id;
public readonly string Type;
public readonly string Details;

public GameEvent(string type, string details)
public GameEvent(int id, string type, string details)
{
Id = id;
Type = type;
Details = details;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game-lib/Events/PlayerActionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class PlayerActionEvent : GameEvent
{
public PlayerActionEvent(string type, string details) : base(type, details)
public PlayerActionEvent(int id, string type, string details) : base(id, type, details)
{
}

public override PlayerActionEvent Clone()
=> new(Type, Details);
=> new(Id, Type, Details);
}
5 changes: 3 additions & 2 deletions src/game-lib/Events/WorldEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

public class WorldEvent : GameEvent
{
public WorldEvent(string type, string details) : base(type, details)
public WorldEvent(int id, string type, string details) : base(id, type, details)
{
}

public override WorldEvent Clone()
=> new(Type, Details);
=> new(Id, Type, Details);
}
7 changes: 7 additions & 0 deletions src/game-lib/State/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class GameSession

public readonly List<GameSessionTurn> Turns;

public int NextEventId => GameEvents.Count;

public GameSessionTurn CurrentTurn => Turns.Last();

public GameState CurrentGameState => CurrentTurn.EndState;
Expand All @@ -36,4 +38,9 @@ public IReadOnlyList<GameState> GameStates
=> Turns.SelectMany<GameSessionTurn, GameState>(turn => [turn.StartState, turn.EndState])
.ToList()
.AsReadOnly();

public IReadOnlyList<GameEvent> GameEvents
=> Turns.SelectMany<GameSessionTurn, GameEvent>(turn => turn.GameEvents)
.ToList()
.AsReadOnly();
}
21 changes: 20 additions & 1 deletion src/game-lib/State/GameSessionTurn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lib.Contracts;
using Lib.Primitives;
using UfoGameLib.Controller;
using UfoGameLib.Events;

Expand Down Expand Up @@ -30,7 +31,7 @@ public GameSessionTurn(
AssertInvariants();
}

private void AssertInvariants()
public void AssertInvariants()
{
Contract.Assert(
StartState.Timeline.CurrentTurn == EndState.Timeline.CurrentTurn,
Expand All @@ -55,8 +56,26 @@ private void AssertInvariants()
Contract.Assert(
EventsInTurn.Count == EndState.UpdateCount - StartState.UpdateCount,
"Number of events in turn must match the number of updates between the game states.");

IReadOnlyList<GameEvent> events = GameEvents;
if (events.Any())
{
int firstId = events[0].Id;
for (int i = 0; i < events.Count; i++)
{
int expectedId = firstId + i;
Contract.Assert(
events[i].Id == expectedId,
$"Event id {events[i].Id} is not equal to expected {expectedId}.");
}
}
}

public IReadOnlyList<GameEvent> GameEvents
=> ((List<GameEvent>) [..EventsUntilStartState, ..EventsInTurn])
.Concat((AdvanceTimeEvent as GameEvent)?.WrapInList() ?? [])
.ToList().AsReadOnly();

public GameSessionTurn Clone()
=> DeepClone();

Expand Down

0 comments on commit d9a0e7b

Please sign in to comment.