Skip to content

Commit

Permalink
make tests use the new GameSession[Turn|Controller]; add test: TurnCo…
Browse files Browse the repository at this point in the history
…ntrollerAdvancesTurns
  • Loading branch information
Konrad Jamrozik committed Jun 7, 2024
1 parent dc1a5fe commit fe60208
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/game-lib-tests/AIPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void TearDown()

private void AIPlayerPlaysGameUntilConclusion(AIPlayer.Intellect intellect, int turnLimit)
{
var controller = new GameSessionController(_config, _log, new GameSession(_randomGen));
var controller = new GameSessionController2(_config, _log, new GameSession2(_randomGen));
var aiPlayer = new AIPlayer(_log, intellect);

// Act
Expand Down
50 changes: 50 additions & 0 deletions src/game-lib-tests/GameSessionControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Lib.Contracts;
using Lib.OS;
using UfoGameLib.Controller;
using UfoGameLib.Lib;
using UfoGameLib.Players;
using UfoGameLib.State;

namespace UfoGameLib.Tests;

public class GameSessionControllerTests
{
private Configuration _config = null!;
private ILog _log = null!;
private readonly RandomGen _randomGen = new RandomGen(new Random());

[SetUp]
public void Setup()
{
_config = new Configuration(new FileSystem());
_log = new Log(_config);
}


[Test]
public void TurnControllerAdvancesTurns()
{
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));

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

int newTurn = session.CurrentGameState.Timeline.CurrentTurn;

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


[TearDown]
public void TearDown()
{
_log.Dispose();
}
}
63 changes: 32 additions & 31 deletions src/game-lib-tests/GameSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ public void Setup()
[Test]
public void BasicHappyPathGameSessionWorks()
{
var session = new GameSession(_randomGen);
var controller = new GameSessionController(_config, _log, session);
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;

GameState startingState = session.CurrentGameState;
GameState state = session.CurrentGameState;

Assert.Multiple(
() =>
{
Assert.That(startingState.Timeline.CurrentTurn, Is.EqualTo(1));
Assert.That(startingState.Assets.Agents, Has.Count.EqualTo(0));
Assert.That(startingState.Missions, Has.Count.EqualTo(0));
Assert.That(state.Timeline.CurrentTurn, Is.EqualTo(1));
Assert.That(state.Assets.Agents, Has.Count.EqualTo(0));
Assert.That(state.Missions, Has.Count.EqualTo(0));
});

// Act
turnController.HireAgents(count: 3);
controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime(state);
controller.AdvanceTime(state);
MissionSite site = controller.CurrentGameStatePlayerView.MissionSites.First();
turnController.LaunchMission(site, agentCount: 3);
controller.AdvanceTime();
controller.AdvanceTime(state);

GameState finalState = session.CurrentGameState;

Expand All @@ -89,14 +89,15 @@ public void BasicHappyPathGameSessionWorks()
Assert.That(finalState.Missions, Has.Count.EqualTo(1), "missionsLaunchedCount");
Assert.That(
startingState,
state,
Is.EqualTo(finalState),
"starting state should be equal to final state");
Assert.That(startingState.Assets.Agents, Is.EqualTo(finalState.Assets.Agents));
Assert.That(startingState.Missions, Is.EqualTo(finalState.Missions));
Assert.That(state.Assets.Agents, Is.EqualTo(finalState.Assets.Agents));
Assert.That(state.Missions, Is.EqualTo(finalState.Missions));
});
}


/// <summary>
/// Given:
/// A non-trivial game state
Expand All @@ -111,8 +112,8 @@ public void BasicHappyPathGameSessionWorks()
[Test]
public void LoadingPreviousGameStateOverridesCurrentState()
{
var session = new GameSession(_randomGen);
var controller = new GameSessionController(_config, _log, session);
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);

GameStatePlayerView stateView = controller.CurrentGameStatePlayerView;
int savedTurn = stateView.CurrentTurn;
Expand All @@ -122,7 +123,7 @@ public void LoadingPreviousGameStateOverridesCurrentState()
controller.SaveCurrentGameStateToFile();

// Act 2: Advance time, thus modifying the current game state
controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);

// Assert that advancing time didn't modify reference to the current game state
Assert.That(stateView.StateReferenceEquals(controller.CurrentGameStatePlayerView));
Expand Down Expand Up @@ -156,29 +157,29 @@ public void LoadingPreviousGameStateOverridesCurrentState()
[Test]
public void RoundTrippingSavingAndLoadingGameStateBehavesCorrectly()
{
var session = new GameSession(_randomGen);
var controller = new GameSessionController(_config, _log, session);
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;

controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
turnController.HireAgents(5);
// Need to advance time here so that hired agents are no longer InTransit and can be
// sent on a mission.
controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);
turnController.SackAgents(agentsIds: [0]);

GameStatePlayerView state = controller.CurrentGameStatePlayerView;
turnController.LaunchMission(
state.MissionSites.Active.First(),
agentCount: state.Assets.CurrentTransportCapacity);

controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);
controller.AdvanceTime(session.CurrentGameState);

Assert.Multiple(
() =>
Expand Down Expand Up @@ -219,16 +220,16 @@ public void RoundTrippingSavingAndLoadingGameStateBehavesCorrectly()
[Test]
public void RoundTrippingSavingAndLoadingGameStateWithActiveMissionBehavesCorrectly()
{
var session = new GameSession(_randomGen);
var controller = new GameSessionController(_config, _log, session);
var session = new GameSession2(_randomGen);
var controller = new GameSessionController2(_config, _log, session);
var turnController = controller.TurnController;
GameStatePlayerView state = controller.CurrentGameStatePlayerView;

controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);
turnController.HireAgents(5);
// Need to advance time here so that hired agents are no longer InTransit and can be
// sent on a mission.
controller.AdvanceTime();
controller.AdvanceTime(session.CurrentGameState);

Assert.That(state.MissionSites.Active.Any(), Is.True);

Expand All @@ -242,7 +243,7 @@ public void RoundTrippingSavingAndLoadingGameStateWithActiveMissionBehavesCorrec
VerifyGameSatesByJsonDiff(controller);
}

private static void VerifyGameSatesByJsonDiff(GameSessionController controller)
private static void VerifyGameSatesByJsonDiff(GameSessionController2 controller)
{
// Act 1/2 and 2/2
var lastSavedGameState = controller.SaveCurrentGameStateToFile();
Expand Down
1 change: 0 additions & 1 deletion src/game-lib/Controller/GameSessionController2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public void PlayGameSession(int turnLimit, IPlayer player)
Contract.Assert(turnLimit <= GameState.MaxTurnLimit);
Contract.Assert(turnLimit >= CurrentGameStatePlayerView.CurrentTurn);


PlayGameUntilOver(player, turnLimit);

var endState = GameSession.CurrentGameState;
Expand Down
2 changes: 2 additions & 0 deletions src/game-lib/Controller/GameTurnController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public GameTurnController(ILog log, RandomGen randomGen, GameState gameState)

public RandomGen RandomGen { get; }

public int CurrentTurn => _gameState.Timeline.CurrentTurn;

public PlayerActionEvent SackAgents(int[] agentsIds) => SackAgents(GetAgentsByIds(agentsIds));

public PlayerActionEvent SendAgentsToTraining(int[] agentsIds)
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib/Lib/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Configuration

public readonly bool LoggingEnabled;

public Configuration(IFileSystem fs, bool loggingEnabled = false)
public Configuration(IFileSystem fs, bool loggingEnabled = true)
{
// Given expected starting path on .NET 8, using the Simplified Output Paths [1]
// [repo_root]/artifacts/bin/game-lib/debug/.
Expand Down
2 changes: 1 addition & 1 deletion src/game-lib/Players/BasicAIPlayerIntellect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void LaunchMissions(GameStatePlayerView state, GameTurnController contro
if (NoMissionsAvailable(state) || NoAgentsCanBeSentOnMission(state) || NoTransportCapacityAvailable(state))
return;

var missionSitesOrdByDifficulty = state.MissionSites.Active.OrderBy(site => site.Difficulty).ToMissionSites();
MissionSites missionSitesOrdByDifficulty = state.MissionSites.Active.OrderBy(site => site.Difficulty).ToMissionSites();

while (missionSitesOrdByDifficulty.Any() && TransportCapacityAvailable(state))
{
Expand Down
17 changes: 9 additions & 8 deletions src/game-lib/State/GameSessionTurn2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public GameSessionTurn2(

private void AssertInvariants()
{
Contract.AssertImplication(
EventsUntilStartState.Any(),
EventsUntilStartState.First().Type == nameof(AdvanceTimePlayerAction),
"If there are any events leading up to the start game state, the first one must be the Advance Time player action.");
if (EventsUntilStartState.Any())
{
Contract.Assert(
EventsUntilStartState.First().Type == nameof(AdvanceTimePlayerAction),
"If there are any events leading up to the start game state, the first one must be the Advance Time player action.");

Contract.AssertImplication(
EventsUntilStartState.Any(),
EventsUntilStartState.Skip(1).All(gameEvent => gameEvent is WorldEvent),
"If there are any events leading up to the start game state, all of them except the first one must be world events.");
Contract.Assert(
EventsUntilStartState.Skip(1).All(gameEvent => gameEvent is WorldEvent),
"If there are any events leading up to the start game state, all of them except the first one must be world events.");
}

Contract.Assert(
StartState.Timeline.CurrentTurn == EndState.Timeline.CurrentTurn,
Expand Down

0 comments on commit fe60208

Please sign in to comment.