Skip to content

Commit

Permalink
refactoring: name fixups and reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jul 10, 2024
1 parent a22930e commit 3d1d45a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
57 changes: 43 additions & 14 deletions web/src/lib/api/playerActionsPayloadsProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,45 @@
import type { PlayerActionNameInTurn } from '../codesync/PlayerActionName'
import type { PlayerActionPayload } from '../codesync/PlayerActionPayload'

// kja2-refact this map could be consolidated with PlayerActionEvent.ts and renderGameEvent.ts
export const playerActionsPayloadsProviders: {
[actionName in PlayerActionNameInTurn]: PayloadProviderMap[actionName]
} = {
// Note: currently Cap always buys 1 capacity. See PlayerActionPayload.cs in backend.
BuyTransportCapacityPlayerAction: (TargetId: number) => ({
Name: 'BuyTransportCapacityPlayerAction' as PlayerActionNameInTurn,
Name: 'BuyTransportCapacityPlayerAction' as const,
TargetId,
}),
HireAgentsPlayerAction: (TargetId: number) => ({
Name: 'HireAgentsPlayerAction' as PlayerActionNameInTurn,
Name: 'HireAgentsPlayerAction' as const,
TargetId,
}),
SackAgentsPlayerAction: (Ids: number[]) => ({
Name: 'SackAgentsPlayerAction' as PlayerActionNameInTurn,
Name: 'SackAgentsPlayerAction' as const,
Ids,
}),
SendAgentsToGenerateIncomePlayerAction: (Ids: number[]) => ({
Name: 'SendAgentsToGenerateIncomePlayerAction' as PlayerActionNameInTurn,
Name: 'SendAgentsToGenerateIncomePlayerAction' as const,
Ids,
}),
SendAgentsToGatherIntelPlayerAction: (Ids: number[]) => ({
Name: 'SendAgentsToGatherIntelPlayerAction' as PlayerActionNameInTurn,
Name: 'SendAgentsToGatherIntelPlayerAction' as const,
Ids,
}),
SendAgentsToTrainingPlayerAction: (Ids: number[]) => ({
Name: 'SendAgentsToTrainingPlayerAction' as PlayerActionNameInTurn,
Name: 'SendAgentsToTrainingPlayerAction' as const,
Ids,
}),
RecallAgentsPlayerAction: (Ids: number[]) => ({
Name: 'RecallAgentsPlayerAction' as PlayerActionNameInTurn,
Name: 'RecallAgentsPlayerAction' as const,
Ids,
}),
LaunchMissionPlayerAction: (Ids: number[], TargetId: number) => ({
Name: 'LaunchMissionPlayerAction' as PlayerActionNameInTurn,
Name: 'LaunchMissionPlayerAction' as const,
Ids,
TargetId,
}),
InvestIntelPlayerAction: (Ids: number[], TargetId: number) => ({
Name: 'InvestIntelPlayerAction' as PlayerActionNameInTurn,
Name: 'InvestIntelPlayerAction' as const,
Ids,
TargetId,
}),
Expand Down Expand Up @@ -72,11 +71,41 @@ type PayloadProviderMap = {
// and the actual implementation would take no parameters, then this would not catch that.
BuyTransportCapacityPlayerAction: PayloadFromTargetId
HireAgentsPlayerAction: PayloadFromTargetId
InvestIntelPlayerAction: PayloadFromIdsAndTargetId
LaunchMissionPlayerAction: PayloadFromIdsAndTargetId
RecallAgentsPlayerAction: PayloadFromIds
SackAgentsPlayerAction: PayloadFromIds
SendAgentsToGenerateIncomePlayerAction: PayloadFromIds
SendAgentsToGatherIntelPlayerAction: PayloadFromIds
SendAgentsToGenerateIncomePlayerAction: PayloadFromIds
SendAgentsToTrainingPlayerAction: PayloadFromIds
RecallAgentsPlayerAction: PayloadFromIds
LaunchMissionPlayerAction: PayloadFromIdsAndTargetId
InvestIntelPlayerAction: PayloadFromIdsAndTargetId
}

// kja searching e.g. for 'SendAgentsToGatherIntelPlayerAction:' shows it usage in following maps:
// - playerActionsPayloadsProviders, to determine how to call the backend for this action
// - agentPlayerActionConditionMap, to determine when this action can be performed on given agent
// - batchAgentPlayerActionOptionLabel, to show label in player actions dropdown
// - gameEventDisplayMap, to determine how to display corresponding event in event log
// - this has both the displayed type, and templated details
//
// I feel it would be better to consolidate all of these maps into one, by introducing a class
// representing this player action. This class would have methods like:
// - getBackendPayloadProvider(),
// - getCanBeAppliedToAgent(agent),
// - getLabel(),
// - getEventDisplay()
//
// The underlying maps may still be useful, and the method implementations may call into those maps.
//
// But instead of doing this:
// const payloadProvider = playerActionsPayloadsProviders[playerActionName]
// I would do this:
// playerAction.getBackendPayloadProvider()
//
// Instead of doing this:
// action: BatchAgentPlayerActionOption,
// return agentPlayerActionConditionMap[action](rowAgent)
// I would do this:
// playerAction.getCanBeAppliedToAgent(rowAgent)
//
// I would sometimes need to retrieve the player action class based on its name,
// so I would likely have a map from player action names to player action classes.
8 changes: 4 additions & 4 deletions web/src/lib/codesync/GameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ export type GameEventWithTurn = GameEvent & {
readonly Turn: number
}

export function isWorldEvent(event: GameEvent): event is WorldEvent {
return _.includes(WorldEventNameVal, event.Type)
}

export function isPlayerActionEvent(
event: GameEvent,
): event is PlayerActionEvent {
return _.includes(PlayerActionNameVal, event.Type)
}

export function isWorldEvent(event: GameEvent): event is WorldEvent {
return _.includes(WorldEventNameVal, event.Type)
}

export function addTurnToGameEvent(
event: GameEvent,
turn: GameSessionTurn,
Expand Down
6 changes: 3 additions & 3 deletions web/src/lib/codesync/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export function requiredSurvivingAgentsForSuccess(site: MissionSite): number {
export const agentPlayerActionConditionMap: {
[action in AgentPlayerActionName]: (agent: Agent) => boolean
} = {
SendAgentsToGenerateIncomePlayerAction: canBeSentOnMission,
SendAgentsToGatherIntelPlayerAction: canBeSentOnMission,
SendAgentsToTrainingPlayerAction: canBeSentToTraining,
RecallAgentsPlayerAction: canBeRecalled,
SackAgentsPlayerAction: canBeSacked,
SendAgentsToGatherIntelPlayerAction: canBeSentOnMission,
SendAgentsToGenerateIncomePlayerAction: canBeSentOnMission,
SendAgentsToTrainingPlayerAction: canBeSentToTraining,
}

type EstimatableAssets = Pick<Assets, 'Money' | 'Intel'>
Expand Down
8 changes: 3 additions & 5 deletions web/src/lib/rendering/renderGameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
type GameEventName,
type GameEventWithTurn,
} from '../codesync/GameEvent'
import type { PlayerActionName } from '../codesync/PlayerActionName'
import { never, str } from '../utils'
import { formatString } from './formatString'

const playerActionNameToDisplayMap: {
const gameEventDisplayMap: {
[name in GameEventName]: {
displayedType: string
displayedDetails: string
Expand Down Expand Up @@ -75,13 +74,12 @@ export function getDisplayedKind(event: GameEventWithTurn): string {
}

export function getDisplayedType(event: GameEventWithTurn): string {
return playerActionNameToDisplayMap[event.Type].displayedType
return gameEventDisplayMap[event.Type].displayedType
}

export function getDisplayedDetails(event: GameEventWithTurn): string {
return formatString(
playerActionNameToDisplayMap[event.Type as PlayerActionName]
.displayedDetails,
gameEventDisplayMap[event.Type].displayedDetails,
'Ids' in event ? event.Ids : undefined,
'TargetId' in event ? event.TargetId : undefined,
)
Expand Down

0 comments on commit 3d1d45a

Please sign in to comment.