From a9c350cf77c606afa44cebc68ef720c715ceb33e Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Sun, 9 Jun 2024 02:03:08 -0700 Subject: [PATCH] fix: reset turn before delegating it to AI --- src/api/AdvanceTurnsRoute2.cs | 4 ++++ src/game-lib/Players/BasicAIPlayerIntellect.cs | 8 ++++---- web/src/lib/codesync/GameSessionTurn.ts | 8 +------- web/src/lib/gameSession/GameSession.ts | 12 +++++++++--- web/src/lib/gameSession/GameSessionData.ts | 4 ++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/api/AdvanceTurnsRoute2.cs b/src/api/AdvanceTurnsRoute2.cs index ff8b8887..30e2ab7c 100644 --- a/src/api/AdvanceTurnsRoute2.cs +++ b/src/api/AdvanceTurnsRoute2.cs @@ -41,6 +41,10 @@ async Task RouteFunc() $"Cannot advance turns with turnLimit: {turnLimit}. " + $"Input game state turn is {turn.StartState.Timeline.CurrentTurn}. turnLimit must be higher than that."); + if (delegateToAi == true && (turn?.EventsInTurn.Any() == true)) + throw new ArgumentException( + "Cannot delegate to AI when there are player actions in the input turn."); + return AdvanceTurnsInternal(turnLimit, delegateToAi, turn); } } diff --git a/src/game-lib/Players/BasicAIPlayerIntellect.cs b/src/game-lib/Players/BasicAIPlayerIntellect.cs index 0db541a1..fc07394c 100644 --- a/src/game-lib/Players/BasicAIPlayerIntellect.cs +++ b/src/game-lib/Players/BasicAIPlayerIntellect.cs @@ -35,13 +35,13 @@ public void PlayGameTurn(GameStatePlayerView state, GameTurnController controlle } private int ComputeTransportCapacityToBuy(GameStatePlayerView state) - => state.Assets.Money >= MoneyThresholdToBuyTransportCapacity + => state.Assets.Money >= MoneyThresholdToBuyTransportCapacity // The number of current agents has to be at least the current max transport capacity, // which is still significantly below full complement. // There is no point in increasing the transport capacity if we cannot even // get close to the full complement of agents. - && state.Assets.Agents.Count >= state.Assets.MaxTransportCapacity - ? 1 + && state.Assets.Agents.Count >= state.Assets.MaxTransportCapacity + ? 1 : 0; private static bool NoMissionsAvailable(GameStatePlayerView state) => !state.MissionSites.Active.Any(); @@ -277,4 +277,4 @@ private void AssignAvailableAgents(GameStatePlayerView state, GameTurnController Contract.Assert(state.Assets.Agents.InTraining.Count == initialAgentsInTraining + agentsSentToTraining); Contract.Assert(state.Assets.Agents.Available.Count == 0); } -} \ No newline at end of file +} diff --git a/web/src/lib/codesync/GameSessionTurn.ts b/web/src/lib/codesync/GameSessionTurn.ts index 0e758473..e1fd65b7 100644 --- a/web/src/lib/codesync/GameSessionTurn.ts +++ b/web/src/lib/codesync/GameSessionTurn.ts @@ -26,13 +26,7 @@ export function getGameEvents( return _.flatMap(turns, (turn) => getEvents(turn)) } -export function removeAdvanceTimeEvent( - turn: GameSessionTurn | undefined, -): GameSessionTurn | undefined { - if (_.isUndefined(turn)) { - return undefined - } - +export function removeAdvanceTimeEvent(turn: GameSessionTurn): GameSessionTurn { return { ...turn, AdvanceTimeEvent: undefined, diff --git a/web/src/lib/gameSession/GameSession.ts b/web/src/lib/gameSession/GameSession.ts index 3ca5848d..f7c5de33 100644 --- a/web/src/lib/gameSession/GameSession.ts +++ b/web/src/lib/gameSession/GameSession.ts @@ -10,6 +10,7 @@ import { playerActionsPayloadsProviders } from '../api/playerActionsPayloadsProv import type { GameEvent } from '../codesync/GameEvent' import { removeAdvanceTimeEvent, + resetTurn, type GameSessionTurn, } from '../codesync/GameSessionTurn' import { initialTurn, type Assets, type GameState } from '../codesync/GameState' @@ -73,9 +74,14 @@ export class GameSession { targetTurn: number, delegateToAi?: boolean | undefined, ): Promise { - const startGameTurn: GameSessionTurn | undefined = removeAdvanceTimeEvent( - this.getTurnAtUnsafe(startTurn), - ) + let startGameTurn: GameSessionTurn | undefined = + this.getTurnAtUnsafe(startTurn) + if (!_.isUndefined(startGameTurn)) { + startGameTurn = removeAdvanceTimeEvent(startGameTurn) + if (delegateToAi ?? true) { + startGameTurn = resetTurn(startGameTurn) + } + } const newTurns = await callAdvanceTurnsApi({ setLoading: this.setLoading, diff --git a/web/src/lib/gameSession/GameSessionData.ts b/web/src/lib/gameSession/GameSessionData.ts index 4da3442c..8df6c222 100644 --- a/web/src/lib/gameSession/GameSessionData.ts +++ b/web/src/lib/gameSession/GameSessionData.ts @@ -164,8 +164,8 @@ export class GameSessionData { public revertToPreviousTurn(): void { const turnsBeforeCurrentTurn = this.getTurnsBeforeCurrentTurn() const turnBeforeCurrentTurn = removeAdvanceTimeEvent( - turnsBeforeCurrentTurn.at(-1), - )! + turnsBeforeCurrentTurn.at(-1)!, + ) const newData: GameSessionDataType = { turns: [...turnsBeforeCurrentTurn.slice(0, -1), turnBeforeCurrentTurn], }