From 62be03d0d754fa0853a5ebbf89786b81cf4dd81c Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Mon, 10 Jun 2024 01:36:09 -0700 Subject: [PATCH] Fix support for MissionSiteExpiredEvent --- src/api/ApplyPlayerActionRoute.cs | 3 ++- src/api/PlayerActionPayload.cs | 2 +- .../Controller/TimeAdvancementController.cs | 5 +++-- src/game-lib/Events/GameEventName.cs | 7 +++++++ src/game-lib/Events/MissionSiteExpiredEvent.cs | 16 ---------------- src/game-lib/Events/WorldEvent.cs | 8 +++++--- web/src/lib/codesync/WorldEvent.ts | 5 ++--- web/src/lib/rendering/renderGameEvent.ts | 2 +- 8 files changed, 21 insertions(+), 27 deletions(-) create mode 100644 src/game-lib/Events/GameEventName.cs delete mode 100644 src/game-lib/Events/MissionSiteExpiredEvent.cs diff --git a/src/api/ApplyPlayerActionRoute.cs b/src/api/ApplyPlayerActionRoute.cs index 914fbd4d..7f8ae62b 100644 --- a/src/api/ApplyPlayerActionRoute.cs +++ b/src/api/ApplyPlayerActionRoute.cs @@ -3,6 +3,7 @@ using Lib.OS; using Microsoft.AspNetCore.Http.HttpResults; using UfoGameLib.Controller; +using UfoGameLib.Events; using UfoGameLib.Lib; using UfoGameLib.State; @@ -46,7 +47,7 @@ private static JsonHttpResult 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 diff --git a/src/api/PlayerActionPayload.cs b/src/api/PlayerActionPayload.cs index 631862e8..2d82df73 100644 --- a/src/api/PlayerActionPayload.cs +++ b/src/api/PlayerActionPayload.cs @@ -41,7 +41,7 @@ private Func 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!), diff --git a/src/game-lib/Controller/TimeAdvancementController.cs b/src/game-lib/Controller/TimeAdvancementController.cs index df289e55..ee49db3c 100644 --- a/src/game-lib/Controller/TimeAdvancementController.cs +++ b/src/game-lib/Controller/TimeAdvancementController.cs @@ -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. @@ -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)); _log.Info($"{missionSite.LogString} expired!"); } } diff --git a/src/game-lib/Events/GameEventName.cs b/src/game-lib/Events/GameEventName.cs new file mode 100644 index 00000000..26c48e7c --- /dev/null +++ b/src/game-lib/Events/GameEventName.cs @@ -0,0 +1,7 @@ +namespace UfoGameLib.Events; + +public static class GameEventName +{ + public const string AdvanceTimePlayerAction = "AdvanceTimePlayerAction"; + public const string WorldEvent = "WorldEvent"; +} diff --git a/src/game-lib/Events/MissionSiteExpiredEvent.cs b/src/game-lib/Events/MissionSiteExpiredEvent.cs deleted file mode 100644 index 227fc017..00000000 --- a/src/game-lib/Events/MissionSiteExpiredEvent.cs +++ /dev/null @@ -1,16 +0,0 @@ -// ReSharper disable MemberCanBePrivate.Global -// Reason: public fields are being serialized to JSON. -namespace UfoGameLib.Events; - -public class MissionSiteExpiredEvent : WorldEvent -{ - public readonly int TargetId; - - public MissionSiteExpiredEvent(int id, int targetId) : base(id, nameof(MissionSiteExpiredEvent)) - { - TargetId = targetId; - } - - public override MissionSiteExpiredEvent Clone() - => new(Id, TargetId); -} \ No newline at end of file diff --git a/src/game-lib/Events/WorldEvent.cs b/src/game-lib/Events/WorldEvent.cs index 540c4e0e..c1cfc629 100644 --- a/src/game-lib/Events/WorldEvent.cs +++ b/src/game-lib/Events/WorldEvent.cs @@ -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); } diff --git a/web/src/lib/codesync/WorldEvent.ts b/web/src/lib/codesync/WorldEvent.ts index cb0e01d4..7a529181 100644 --- a/web/src/lib/codesync/WorldEvent.ts +++ b/web/src/lib/codesync/WorldEvent.ts @@ -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' diff --git a/web/src/lib/rendering/renderGameEvent.ts b/web/src/lib/rendering/renderGameEvent.ts index 539f8baf..a0397e76 100644 --- a/web/src/lib/rendering/renderGameEvent.ts +++ b/web/src/lib/rendering/renderGameEvent.ts @@ -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, ) }