Skip to content

Commit

Permalink
Fix support for MissionSiteExpiredEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jun 10, 2024
1 parent dd2e3be commit 62be03d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/api/ApplyPlayerActionRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Lib.OS;
using Microsoft.AspNetCore.Http.HttpResults;
using UfoGameLib.Controller;
using UfoGameLib.Events;
using UfoGameLib.Lib;
using UfoGameLib.State;

Expand Down Expand Up @@ -46,7 +47,7 @@ private static JsonHttpResult<GameSessionTurn> ApplyPlayerActionInternal(
var controller = new GameSessionController(config, log, gameSession);

// kja2-assert: make this check stronger, for membership in valid action name. See https://chatgpt.com/c/fb0a4197-4397-4f3f-bc13-2e0468141b0b
Contract.Assert(playerActionPayload.ActionName != "AdvanceTimePlayerAction");
Contract.Assert(playerActionPayload.ActionName != GameEventName.AdvanceTimePlayerAction);

gameSession.CurrentPlayerActionEvents.Add(playerActionPayload.Apply(controller));
// See analogous line in UfoGameLib.Controller.GameSessionController.PlayGameUntilOver
Expand Down
2 changes: 1 addition & 1 deletion src/api/PlayerActionPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Func<PlayerActionEvent> TranslatePlayerActionToControllerAction(GameSess
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#positional-pattern
=> ActionName switch
{
"AdvanceTimePlayerAction" => controller.AdvanceTimeNoWorldEvents,
EventNames.AdvanceTimePlayerAction => controller.AdvanceTimeNoWorldEvents,
nameof(BuyTransportCapacityPlayerAction) => () => controller.CurrentTurnController.BuyTransportCapacity(TargetId!.Value),
nameof(HireAgentsPlayerAction) => () => controller.CurrentTurnController.HireAgents(TargetId!.Value),
nameof(SackAgentsPlayerAction) => () => controller.CurrentTurnController.SackAgents(Ids!),
Expand Down
5 changes: 3 additions & 2 deletions src/game-lib/Controller/TimeAdvancementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TimeAdvancementController(ILog log, RandomGen randomGen, EventIdGen event

PlayerActionEvent advanceTimeEvent = new PlayerActionEvent(
_eventIdGen.Generate,
"AdvanceTimePlayerAction",
GameEventName.AdvanceTimePlayerAction,
targetId: state.Timeline.CurrentTurn);

// Agents cost upkeep. Note we compute upkeep before evaluating missions.
Expand Down Expand Up @@ -200,7 +200,8 @@ private int UpdateActiveMissionSites(GameState state)
if (expired)
{
expiredMissions++;
_worldEvents.Add(new MissionSiteExpiredEvent(_eventIdGen.Generate, missionSite.Id));
_worldEvents.Add(
new WorldEvent(_eventIdGen.Generate, GameEventName.MissionSiteExpiredEvent, missionSite.Id));

Check failure on line 204 in src/game-lib/Controller/TimeAdvancementController.cs

View workflow job for this annotation

GitHub Actions / build

'GameEventName' does not contain a definition for 'MissionSiteExpiredEvent'

Check failure on line 204 in src/game-lib/Controller/TimeAdvancementController.cs

View workflow job for this annotation

GitHub Actions / build

'GameEventName' does not contain a definition for 'MissionSiteExpiredEvent'
_log.Info($"{missionSite.LogString} expired!");
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/game-lib/Events/GameEventName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace UfoGameLib.Events;

public static class GameEventName
{
public const string AdvanceTimePlayerAction = "AdvanceTimePlayerAction";
public const string WorldEvent = "WorldEvent";
}
16 changes: 0 additions & 16 deletions src/game-lib/Events/MissionSiteExpiredEvent.cs

This file was deleted.

8 changes: 5 additions & 3 deletions src/game-lib/Events/WorldEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace UfoGameLib.Events;

[JsonDerivedType(typeof(MissionSiteExpiredEvent))]
public class WorldEvent : GameEvent
{
public WorldEvent(int id, string type) : base(id, type)
public readonly int TargetId;

public WorldEvent(int id, string type, int targetId) : base(id, type)
{
TargetId = targetId;
}

public override WorldEvent Clone()
=> new(Id, Type);
=> new(Id, Type, TargetId);
}
5 changes: 2 additions & 3 deletions web/src/lib/codesync/WorldEvent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// codesync: UfoGameLib.Events.WorldEvent
import type { GameEventBase } from './GameEvent'

export type WorldEvent = MissionSiteExpiredEvent

export type MissionSiteExpiredEvent = GameEventBase & {
export type WorldEvent = GameEventBase & {
readonly TargetId: number
}

export type WorldEventName = 'MissionSiteExpiredEvent'
2 changes: 1 addition & 1 deletion web/src/lib/rendering/renderGameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getDisplayedDetails(event: GameEventWithTurn): string {
return formatString(
playerActionNameToDisplayMap[event.Type as PlayerActionName]
.displayedDetails,
event.Ids,
'Ids' in event ? event.Ids : undefined,
event.TargetId,
)
}
Expand Down

0 comments on commit 62be03d

Please sign in to comment.