Skip to content

Commit

Permalink
fix bug with TurnController not updating state
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 7, 2024
1 parent fe60208 commit 607d0d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
13 changes: 5 additions & 8 deletions src/game-lib-tests/GameSessionControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,24 @@ public void Setup()
_log = new Log(_config);
}


[Test]
public void TurnControllerAdvancesTurns()
public void CurrentTurnControllerKeepsTrackOfTheTurn()
{
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;

int initialTurn = session.CurrentGameState.Timeline.CurrentTurn;

Assert.That(turnController.CurrentTurn, Is.EqualTo(session.CurrentGameState.Timeline.CurrentTurn));
Assert.That(controller.CurrentTurnController.CurrentTurn, Is.EqualTo(initialTurn));

// Act
controller.PlayGameSession(2, new AIPlayer(_log, AIPlayer.Intellect.DoNothing));

int newTurn = session.CurrentGameState.Timeline.CurrentTurn;
int nextTurn = session.CurrentGameState.Timeline.CurrentTurn;
Contract.Assert(initialTurn + 1 == nextTurn);

Contract.Assert(initialTurn + 1 == newTurn);
Assert.That(turnController.CurrentTurn, Is.EqualTo(session.CurrentGameState.Timeline.CurrentTurn));
Assert.That(controller.CurrentTurnController.CurrentTurn, Is.EqualTo(nextTurn));
}


[TearDown]
public void TearDown()
Expand Down
6 changes: 3 additions & 3 deletions src/game-lib-tests/GameSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void BasicHappyPathGameSessionWorks()
{
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;
var turnController = controller.CurrentTurnController;

GameState state = session.CurrentGameState;

Expand Down Expand Up @@ -159,7 +159,7 @@ public void RoundTrippingSavingAndLoadingGameStateBehavesCorrectly()
{
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;
var turnController = controller.CurrentTurnController;

controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
Expand Down Expand Up @@ -222,7 +222,7 @@ public void RoundTrippingSavingAndLoadingGameStateWithActiveMissionBehavesCorrec
{
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;
var turnController = controller.CurrentTurnController;
GameStatePlayerView state = controller.CurrentGameStatePlayerView;

controller.AdvanceTime(session.CurrentGameState);
Expand Down
21 changes: 13 additions & 8 deletions src/game-lib/Controller/GameSessionController2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ namespace UfoGameLib.Controller;
/// </summary>
public class GameSessionController2
{
public readonly GameTurnController TurnController;

protected readonly GameSession2 GameSession;
private readonly Configuration _config;
private readonly ILog _log;
public GameTurnController CurrentTurnController { get; private set; }

public GameSessionController2(Configuration config, ILog log, GameSession2 gameSession)
{
_config = config;
_log = log;
GameSession = gameSession;
TurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
CurrentTurnController = new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
}

public GameStatePlayerView CurrentGameStatePlayerView
Expand Down Expand Up @@ -117,9 +116,9 @@ private void PlayGameUntilOver(IPlayer player, int turnLimit)
_log.Info($"===== Turn {GameSession.CurrentGameState.Timeline.CurrentTurn}");
_log.Info("");

player.PlayGameTurn(CurrentGameStatePlayerView, TurnController);
player.PlayGameTurn(CurrentGameStatePlayerView, CurrentTurnController);

List<PlayerActionEvent> playerActionEvents = TurnController.GetAndDeleteRecordedPlayerActionEvents();
List<PlayerActionEvent> playerActionEvents = CurrentTurnController.GetAndDeleteRecordedPlayerActionEvents();
GameSession.CurrentGameEvents.AddRange(playerActionEvents);

if (GameSession.CurrentGameState.IsGameOver)
Expand All @@ -138,12 +137,18 @@ private void PlayGameUntilOver(IPlayer player, int turnLimit)
// This state diff shows the result of advancing time.
DiffGameStates(GameSession.CurrentGameState, nextTurnStartState);

GameSession.Turns.Add(new GameSessionTurn2(
eventsUntilStartState: [advanceTimePlayerActionEvent, ..worldEvents],
startState: nextTurnStartState));
CurrentTurnController = NewTurn(advanceTimePlayerActionEvent, worldEvents, nextTurnStartState);
}
}

private GameTurnController NewTurn(PlayerActionEvent advanceTimePlayerActionEvent, List<WorldEvent> worldEvents, GameState nextTurnStartState)
{
GameSession.Turns.Add(new GameSessionTurn2(
eventsUntilStartState: [advanceTimePlayerActionEvent, ..worldEvents],
startState: nextTurnStartState));
return new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState);
}

private List<WorldEvent> GetAndDeleteRecordedWorldEvents()
{
// kja to implement GetAndDeleteRecordedWorldEvents
Expand Down
3 changes: 1 addition & 2 deletions src/game-lib/State/GameSession2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class GameSession2
{
public readonly RandomGen RandomGen;

public List<GameSessionTurn2> Turns;

public readonly List<GameSessionTurn2> Turns;

public GameSessionTurn2 CurrentTurn => Turns.Last();

Expand Down

0 comments on commit 607d0d7

Please sign in to comment.