From 607d0d779848fdfc638cb6f9ca6e568de6e70e68 Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Fri, 7 Jun 2024 01:31:14 -0700 Subject: [PATCH] fix bug with TurnController not updating state --- .../GameSessionControllerTests.cs | 13 +++++------- src/game-lib-tests/GameSessionTests.cs | 6 +++--- .../Controller/GameSessionController2.cs | 21 ++++++++++++------- src/game-lib/State/GameSession2.cs | 3 +-- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/game-lib-tests/GameSessionControllerTests.cs b/src/game-lib-tests/GameSessionControllerTests.cs index fcc43f4c..a89060fe 100644 --- a/src/game-lib-tests/GameSessionControllerTests.cs +++ b/src/game-lib-tests/GameSessionControllerTests.cs @@ -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() diff --git a/src/game-lib-tests/GameSessionTests.cs b/src/game-lib-tests/GameSessionTests.cs index 6af37a6f..ee173c0d 100644 --- a/src/game-lib-tests/GameSessionTests.cs +++ b/src/game-lib-tests/GameSessionTests.cs @@ -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; @@ -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); @@ -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); diff --git a/src/game-lib/Controller/GameSessionController2.cs b/src/game-lib/Controller/GameSessionController2.cs index 3d937414..4c8e5772 100644 --- a/src/game-lib/Controller/GameSessionController2.cs +++ b/src/game-lib/Controller/GameSessionController2.cs @@ -40,18 +40,17 @@ namespace UfoGameLib.Controller; /// 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 @@ -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 playerActionEvents = TurnController.GetAndDeleteRecordedPlayerActionEvents(); + List playerActionEvents = CurrentTurnController.GetAndDeleteRecordedPlayerActionEvents(); GameSession.CurrentGameEvents.AddRange(playerActionEvents); if (GameSession.CurrentGameState.IsGameOver) @@ -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 worldEvents, GameState nextTurnStartState) + { + GameSession.Turns.Add(new GameSessionTurn2( + eventsUntilStartState: [advanceTimePlayerActionEvent, ..worldEvents], + startState: nextTurnStartState)); + return new GameTurnController(_log, GameSession.RandomGen, GameSession.CurrentGameState); + } + private List GetAndDeleteRecordedWorldEvents() { // kja to implement GetAndDeleteRecordedWorldEvents diff --git a/src/game-lib/State/GameSession2.cs b/src/game-lib/State/GameSession2.cs index 7eaa9cd6..deba50fe 100644 --- a/src/game-lib/State/GameSession2.cs +++ b/src/game-lib/State/GameSession2.cs @@ -18,8 +18,7 @@ public class GameSession2 { public readonly RandomGen RandomGen; - public List Turns; - + public readonly List Turns; public GameSessionTurn2 CurrentTurn => Turns.Last();