Skip to content

Commit

Permalink
backend: BuyTransportCapacity now takes param; frontend: excluded Adv…
Browse files Browse the repository at this point in the history
…anceTimePlayerAction where appropriate
  • Loading branch information
Konrad Jamrozik committed Jun 10, 2024
1 parent 5150c0a commit a70f695
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/api/PlayerActionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private Func<PlayerActionEvent> TranslatePlayerActionToControllerAction(GameSess
=> ActionName switch
{
"AdvanceTimePlayerAction" => controller.AdvanceTimeNoWorldEvents,
nameof(BuyTransportCapacityPlayerAction) => () => controller.CurrentTurnController.BuyTransportCapacity(1),
nameof(BuyTransportCapacityPlayerAction) => () => controller.CurrentTurnController.BuyTransportCapacity(TargetId!.Value),
nameof(HireAgentsPlayerAction) => () => controller.CurrentTurnController.HireAgents(TargetId!.Value),
nameof(SackAgentsPlayerAction) => () => controller.CurrentTurnController.SackAgents(Ids!),
nameof(SendAgentsToGenerateIncomePlayerAction) => () => controller.CurrentTurnController.SendAgentsToGenerateIncome(Ids!),
Expand Down
34 changes: 15 additions & 19 deletions web/src/lib/api/playerActionsPayloadsProviders.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,56 @@
// The a bit more advanced typing done in this file was figured out with the help of ChatGPT:
// https://chat.openai.com/share/af4ac2cb-c221-4c7f-a5c6-e3cac23916c0
import type {
PlayerActionName,
PlayerActionNameInTurn,
PlayerActionPayload,
} from '../codesync/PlayerActionPayload'

export const playerActionsPayloadsProviders: {
[actionName in PlayerActionName]: PayloadProviderMap[actionName]
[actionName in PlayerActionNameInTurn]: PayloadProviderMap[actionName]
} = {
AdvanceTimePlayerAction: () => ({
ActionName: 'AdvanceTimePlayerAction' as PlayerActionName,
}),
// Note: currently Cap always buys 1 capacity. See PlayerActionPayload.cs in backend.
BuyTransportCapacityPlayerAction: () => ({
ActionName: 'BuyTransportCapacityPlayerAction' as PlayerActionName,
BuyTransportCapacityPlayerAction: (TargetId: number) => ({
ActionName: 'BuyTransportCapacityPlayerAction' as PlayerActionNameInTurn,
TargetId,
}),
// Note: currently HireAgents always hires 1 agent. See PlayerActionPayload.cs in backend.
HireAgentsPlayerAction: (TargetId: number) => ({
ActionName: 'HireAgentsPlayerAction' as PlayerActionName,
ActionName: 'HireAgentsPlayerAction' as PlayerActionNameInTurn,
TargetId,
}),
SackAgentsPlayerAction: (Ids: number[]) => ({
ActionName: 'SackAgentsPlayerAction' as PlayerActionName,
ActionName: 'SackAgentsPlayerAction' as PlayerActionNameInTurn,
Ids,
}),
SendAgentsToGenerateIncomePlayerAction: (Ids: number[]) => ({
ActionName: 'SendAgentsToGenerateIncomePlayerAction' as PlayerActionName,
ActionName:
'SendAgentsToGenerateIncomePlayerAction' as PlayerActionNameInTurn,
Ids,
}),
SendAgentsToGatherIntelPlayerAction: (Ids: number[]) => ({
ActionName: 'SendAgentsToGatherIntelPlayerAction' as PlayerActionName,
ActionName: 'SendAgentsToGatherIntelPlayerAction' as PlayerActionNameInTurn,
Ids,
}),
SendAgentsToTrainingPlayerAction: (Ids: number[]) => ({
ActionName: 'SendAgentsToTrainingPlayerAction' as PlayerActionName,
ActionName: 'SendAgentsToTrainingPlayerAction' as PlayerActionNameInTurn,
Ids,
}),
RecallAgentsPlayerAction: (Ids: number[]) => ({
ActionName: 'RecallAgentsPlayerAction' as PlayerActionName,
ActionName: 'RecallAgentsPlayerAction' as PlayerActionNameInTurn,
Ids,
}),
LaunchMissionPlayerAction: (Ids: number[], TargetId: number) => ({
ActionName: 'LaunchMissionPlayerAction' as PlayerActionName,
ActionName: 'LaunchMissionPlayerAction' as PlayerActionNameInTurn,
Ids,
TargetId,
}),
}

export type PayloadProvider =
| PayloadFromNothing
| PayloadFromIds
| PayloadFromTargetId
| PayloadFromIdsAndTargetId

export type PayloadFromNothing = () => PlayerActionPayload
export type PayloadFromIds = (Ids: number[]) => PlayerActionPayload
export type PayloadFromTargetId = (TargetId: number) => PlayerActionPayload
export type PayloadFromIdsAndTargetId = (
Expand All @@ -62,7 +59,7 @@ export type PayloadFromIdsAndTargetId = (
) => PlayerActionPayload

type PayloadProviderMap = {
[key in PlayerActionName]: PayloadProvider
[key in PlayerActionNameInTurn]: PayloadProvider
} & {
// Note: this block provides type-safety only against a payload provider for given action
// having too many parameters, but not not enough.
Expand All @@ -71,8 +68,7 @@ type PayloadProviderMap = {
// then this would properly catch it.
// However, if this declares "RecallAgents" should produce payload from "IDs" parameter,
// and the actual implementation would take no parameters, then this would not catch that.
AdvanceTimePlayerAction: PayloadFromNothing
BuyTransportCapacityPlayerAction: PayloadFromNothing
BuyTransportCapacityPlayerAction: PayloadFromTargetId
HireAgentsPlayerAction: PayloadFromTargetId
SackAgentsPlayerAction: PayloadFromIds
SendAgentsToGenerateIncomePlayerAction: PayloadFromIds
Expand Down
7 changes: 6 additions & 1 deletion web/src/lib/codesync/PlayerActionPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// codesync: UfoGameLib.Controller.PlayerAction

export type PlayerActionPayload = {
readonly ActionName: PlayerActionName
readonly ActionName: PlayerActionNameInTurn
readonly Ids?: number[]
readonly TargetId?: number
}
Expand All @@ -20,3 +20,8 @@ export type AgentPlayerActionName =
| 'SendAgentsToGatherIntelPlayerAction'
| 'SendAgentsToTrainingPlayerAction'
| 'RecallAgentsPlayerAction'

export type PlayerActionNameInTurn = Exclude<
PlayerActionName,
'AdvanceTimePlayerAction'
>
2 changes: 1 addition & 1 deletion web/src/lib/gameSession/GameSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class GameSession {
/* c8 ignore stop */
const payloadProvider =
playerActionsPayloadsProviders.BuyTransportCapacityPlayerAction
const payload = payloadProvider()
const payload = payloadProvider(count)
return this.applyPlayerAction(payload)
}

Expand Down

0 comments on commit a70f695

Please sign in to comment.