Skip to content

Commit

Permalink
fix: reset turn before delegating it to AI
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 9, 2024
1 parent 9a1b0b3 commit a9c350c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/api/AdvanceTurnsRoute2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ async Task<AdvanceTurnsSuccessResponse> 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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/game-lib/Players/BasicAIPlayerIntellect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
8 changes: 1 addition & 7 deletions web/src/lib/codesync/GameSessionTurn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions web/src/lib/gameSession/GameSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -73,9 +74,14 @@ export class GameSession {
targetTurn: number,
delegateToAi?: boolean | undefined,
): Promise<boolean> {
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,
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/gameSession/GameSessionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}
Expand Down

0 comments on commit a9c350c

Please sign in to comment.