Skip to content

Commit

Permalink
Add InvestIntelPlayerAction stub on backend and make frontend call it…
Browse files Browse the repository at this point in the history
… and get the response
  • Loading branch information
Konrad Jamrozik committed Jun 29, 2024
1 parent 09dc10e commit f2c5826
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/api/PlayerActionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private Func<PlayerActionEvent> TranslatePlayerActionToControllerAction(GameSess
{
GameEventName.AdvanceTimePlayerAction => () => controller.AdvanceTime().advaceTimeEvent,
nameof(BuyTransportCapacityPlayerAction) => () => controller.CurrentTurnController.BuyTransportCapacity(TargetId!.Value),
nameof(InvestIntelPlayerAction) => () => controller.CurrentTurnController.InvestIntel(Ids![0], TargetId!.Value),
nameof(HireAgentsPlayerAction) => () => controller.CurrentTurnController.HireAgents(TargetId!.Value),
nameof(SackAgentsPlayerAction) => () => controller.CurrentTurnController.SackAgents(Ids!),
nameof(SendAgentsToGenerateIncomePlayerAction) => () => controller.CurrentTurnController.SendAgentsToGenerateIncome(Ids!),
Expand Down
9 changes: 9 additions & 0 deletions src/game-lib/Controller/GameTurnController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public GameTurnController(

public int CurrentTurn => GameState.Timeline.CurrentTurn;

public PlayerActionEvent InvestIntel(int factionId, int intel)
=> InvestIntel(GetFactionById(factionId), intel);

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

public PlayerActionEvent SendAgentsToTraining(int[] agentsIds)
Expand Down Expand Up @@ -69,6 +72,9 @@ public void LaunchMission(MissionSite site, int agentCount)
LaunchMission(site, agents);
}

public PlayerActionEvent InvestIntel(Faction faction, int intel)
=> ExecuteAndRecordAction(new InvestIntelPlayerAction(_log, faction, intel));

public PlayerActionEvent HireAgents(int count)
=> ExecuteAndRecordAction(new HireAgentsPlayerAction(_log, _agentIdGen, count));

Expand Down Expand Up @@ -103,6 +109,9 @@ public List<PlayerActionEvent> GetAndDeleteRecordedPlayerActionEvents()
private MissionSite GetMissionSiteById(int siteId) =>
GameState.MissionSites.Single(site => site.Id == siteId);

private Faction GetFactionById(int factionId) =>
GameState.Factions.GetById(factionId);

private Agents GetAgentsByIds(int[] agentsIds) =>
GameState.Assets.Agents.GetByIds(agentsIds);

Expand Down
1 change: 0 additions & 1 deletion src/game-lib/Controller/HireAgentsPlayerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public HireAgentsPlayerAction(ILog log, AgentIdGen agentIdGen, int count)
_log = log;
_agentIdGen = agentIdGen;
_count = count;

}

protected override (List<int>? ids, int? targetId) ApplyImpl(GameState state)
Expand Down
31 changes: 31 additions & 0 deletions src/game-lib/Controller/InvestIntelPlayerAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Lib.Contracts;
using UfoGameLib.Lib;
using UfoGameLib.Model;
using UfoGameLib.State;

namespace UfoGameLib.Controller;

public class InvestIntelPlayerAction : PlayerAction
{
private readonly ILog _log;
private readonly Faction _faction;
private readonly int _intel;

public InvestIntelPlayerAction(ILog log, Faction faction, int intel)
{
Contract.Assert(intel >= 1);
_log = log;
_faction = faction;
_intel = intel;
}

protected override (List<int>? ids, int? targetId) ApplyImpl(GameState state)
{
// kja WIP InvestIntelPlayerAction

Console.Out.WriteLine($"Invest intel. Faction: '{_faction.Name}', Faction ID: {_faction.Id}, Intel: {_intel}");
_log.Info($"Invest intel. Faction: '{_faction.Name}', Faction ID: {_faction.Id}, Intel: {_intel}");

return (ids: [_faction.Id], targetId: _intel);
}
}
1 change: 0 additions & 1 deletion src/game-lib/Model/Agents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void AssertCanBeSacked()

public Agent AgentAtPercentile(int percentile, Func<Agent, int> orderBy)
=> this.OrderBy(orderBy).TakePercent(percentile).Last();


// kja2-assert: add assertions that all searched agents were found
public Agents GetByIds(int[] ids) =>
Expand Down
3 changes: 3 additions & 0 deletions src/game-lib/Model/Factions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public Factions(IEnumerable<Faction>? factions = null)
public Factions DeepClone()
=> new Factions(this.Select(faction => faction.DeepClone()));

public Faction GetById(int id) =>
this.Single(faction => faction.Id == id);

public List<MissionSite> CreateMissionSites(
ILog log,
IRandomGen randomGen,
Expand Down
16 changes: 6 additions & 10 deletions web/src/components/FactionsDataGrid/FactionActions.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { Stack } from '@mui/material'
import type { GameState } from '../../lib/codesync/GameState'
import type { GameSession } from '../../lib/gameSession/GameSession'
import SliderWithButton from '../utilities/SliderWithButton'
import type { ManageFactionDialogProps } from './ManageFactionDialog'

export function FactionActions(
props: ManageFactionDialogProps & { gs: GameState },
props: ManageFactionDialogProps & { gameSession: GameSession; gs: GameState },
): React.JSX.Element {
// eslint-disable-next-line unicorn/consistent-function-scoping
function investIntel(amount: number): void {
console.log(`investIntel(${amount}) NOT IMPLEMENTED`)
async function investIntel(amount: number): Promise<void> {
await props.gameSession.investIntel(props.faction.Id, amount)
}

return (
<Stack direction="column" spacing={1} display="flex" alignItems="center">
<SliderWithButton
defaultValue={Math.floor(props.gs.Assets.Intel * 0.2)}
onClick={async (intel: number) => {
await Promise.resolve()
investIntel(intel)
}}
onClick={investIntel}
minValue={0}
maxValue={props.gs.Assets.Intel}
iconName="Intel"
Expand All @@ -27,8 +24,7 @@ export function FactionActions(
<SliderWithButton
defaultValue={Math.floor(props.gs.Assets.Intel * 0.5)}
onClick={async (intel: number) => {
await Promise.resolve()
investIntel(intel)
await investIntel(intel)
}}
minValue={0}
maxValue={props.gs.Assets.Intel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function DeployMissionDialog(
>
<Stack direction="row" spacing={2} alignItems="flex-start">
<FactionDetails {...props} />
<FactionActions {...{ ...props, gs }} />
<FactionActions {...{ ...props, gameSession, gs }} />
</Stack>
</DialogContent>
<DialogActions>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/utilities/SliderWithButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function SliderWithButton(
<Stack direction="row" spacing={2} alignItems="center">
<Icon iconName={props.iconName} />
<Slider
sx={{ width: '120px' }}
sx={{ width: 120 }}
value={value}
onChange={handleSliderChange}
min={props.minValue}
Expand Down
7 changes: 7 additions & 0 deletions web/src/lib/api/playerActionsPayloadsProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { PlayerActionNameInTurn } from '../codesync/PlayerActionEvent'
import type { PlayerActionPayload } from '../codesync/PlayerActionPayload'

// kja this map could be consolidated with PlayerActionEvent.ts and renderGameEvent.ts
export const playerActionsPayloadsProviders: {
[actionName in PlayerActionNameInTurn]: PayloadProviderMap[actionName]
} = {
Expand Down Expand Up @@ -42,6 +43,11 @@ export const playerActionsPayloadsProviders: {
Ids,
TargetId,
}),
InvestIntelPlayerAction: (Ids: number[], TargetId: number) => ({
ActionName: 'InvestIntelPlayerAction' as PlayerActionNameInTurn,
Ids,
TargetId,
}),
}

export type PayloadProvider =
Expand Down Expand Up @@ -74,4 +80,5 @@ type PayloadProviderMap = {
SendAgentsToTrainingPlayerAction: PayloadFromIds
RecallAgentsPlayerAction: PayloadFromIds
LaunchMissionPlayerAction: PayloadFromIdsAndTargetId
InvestIntelPlayerAction: PayloadFromIdsAndTargetId
}
2 changes: 2 additions & 0 deletions web/src/lib/codesync/PlayerActionEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type PlayerActionName =
| 'BuyTransportCapacityPlayerAction'
| 'HireAgentsPlayerAction'
| 'LaunchMissionPlayerAction'
| 'InvestIntelPlayerAction'
| AgentPlayerActionName

export type AgentPlayerActionName =
Expand All @@ -24,6 +25,7 @@ export const PlayerActionNameVal = [
'AdvanceTimePlayerAction',
'BuyTransportCapacityPlayerAction',
'HireAgentsPlayerAction',
'InvestIntelPlayerAction',
'LaunchMissionPlayerAction',
'SackAgentsPlayerAction',
'SendAgentsToGenerateIncomePlayerAction',
Expand Down
10 changes: 10 additions & 0 deletions web/src/lib/gameSession/GameSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ export class GameSession {
return this.applyPlayerAction(payload)
}

public async investIntel(
factionId: number,
amount: number,
): Promise<boolean> {
const payloadProvider =
playerActionsPayloadsProviders.InvestIntelPlayerAction
const payload = payloadProvider([factionId], amount)
return this.applyPlayerAction(payload)
}

public async buyTransportCap(count: number): Promise<boolean> {
/* c8 ignore start */
if (count !== 1) {
Expand Down
4 changes: 4 additions & 0 deletions web/src/lib/rendering/renderGameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const playerActionNameToDisplayMap: {
displayedType: 'Hire agents',
displayedDetails: `Count: +$TargetID = $IDs[0]`,
},
InvestIntelPlayerAction: {
displayedType: 'Invest intel',
displayedDetails: `Faction ID: $IDs[0], Amount: $TargetID`,
},
LaunchMissionPlayerAction: {
displayedType: 'Launch mission',
displayedDetails: `Site ID: $TargetID, Mission ID: $IDs[0], Agent IDs: $IDs[1..]`,
Expand Down

0 comments on commit f2c5826

Please sign in to comment.