From 9db1084b16fc2a5416e21a1d64504a6217ad666e Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Thu, 15 Aug 2024 12:20:17 +0700 Subject: [PATCH] MISC: Cancel infiltration when player is hospitalized (#1579) --- src/Infiltration/ui/Game.tsx | 15 ++++++++++++++- src/PersonObjects/Player/PlayerEvents.ts | 7 +++++++ .../Player/PlayerObjectGeneralMethods.ts | 4 +++- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/PersonObjects/Player/PlayerEvents.ts diff --git a/src/Infiltration/ui/Game.tsx b/src/Infiltration/ui/Game.tsx index 2d3bf2da0..d53612793 100644 --- a/src/Infiltration/ui/Game.tsx +++ b/src/Infiltration/ui/Game.tsx @@ -1,5 +1,5 @@ import { Button, Container, Paper, Typography } from "@mui/material"; -import React, { useCallback, useState } from "react"; +import React, { useCallback, useEffect, useState } from "react"; import { FactionName, ToastVariant } from "@enums"; import { Router } from "../../ui/GameRoot"; import { Page } from "../../ui/Router"; @@ -16,6 +16,8 @@ import { Victory } from "./Victory"; import { WireCuttingGame } from "./WireCuttingGame"; import { calculateDamageAfterFailingInfiltration } from "../utils"; import { SnackbarEvents } from "../../ui/React/Snackbar"; +import { PlayerEventType, PlayerEvents } from "../../PersonObjects/Player/PlayerEvents"; +import { dialogBoxCreate } from "../../ui/React/DialogBox"; type GameProps = { StartingDifficulty: number; @@ -151,6 +153,17 @@ export function Game(props: GameProps): React.ReactElement { ); } + useEffect(() => { + const clearSubscription = PlayerEvents.subscribe((eventType) => { + if (eventType !== PlayerEventType.Hospitalized) { + return; + } + cancel(); + dialogBoxCreate("Infiltration was cancelled because you were hospitalized"); + }); + return clearSubscription; + }, []); + return ( diff --git a/src/PersonObjects/Player/PlayerEvents.ts b/src/PersonObjects/Player/PlayerEvents.ts new file mode 100644 index 000000000..94170e551 --- /dev/null +++ b/src/PersonObjects/Player/PlayerEvents.ts @@ -0,0 +1,7 @@ +import { EventEmitter } from "../../utils/EventEmitter"; + +export enum PlayerEventType { + Hospitalized, +} + +export const PlayerEvents = new EventEmitter<[PlayerEventType]>(); diff --git a/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts b/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts index 725a1d581..341491946 100644 --- a/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectGeneralMethods.ts @@ -52,6 +52,7 @@ import { isMember } from "../../utils/EnumHelper"; import { canAccessBitNodeFeature } from "../../BitNode/BitNodeUtils"; import { AlertEvents } from "../../ui/React/AlertManager"; import { Augmentations } from "../../Augmentation/Augmentations"; +import { PlayerEventType, PlayerEvents } from "./PlayerEvents"; export function init(this: PlayerObject): void { /* Initialize Player's home computer */ @@ -269,8 +270,9 @@ export function hospitalize(this: PlayerObject, suppressNotification: boolean): this.loseMoney(cost, "hospitalization"); this.hp.current = this.hp.max; if (!suppressNotification) { - SnackbarEvents.emit(`You've been Hospitalized for ${formatMoney(cost)}`, ToastVariant.SUCCESS, 2000); + SnackbarEvents.emit(`You've been hospitalized for ${formatMoney(cost)}`, ToastVariant.SUCCESS, 2000); } + PlayerEvents.emit(PlayerEventType.Hospitalized); return cost; }