pokerogue/test/test-utils/phase-interceptor.ts
Bertie690 e50296d14c
Merged upstream/beta into Modifier rework (#6206)
* [Dev] test:silent now passes --silent='passed-only' to Vitest (#6131)

* [Dev] test:silent now passes --silent='passed-only' to Vitest

* Update test shard to actually use `test-silent`

* Removed obselete `project` flag

* [Bug] Unblock priority spread under Psychic Terrain (#6136)

Unblock priority spread under Psychic Terrain

Co-authored-by: Acelynn Zhang <acelynnzhang@Acelynns-MacBook-Pro.local>

* [Dev] Remove `sanitizeOverrides`, consolidate initialization code into 1 file

https://github.com/pagefaultgames/pokerogue/pull/6134

* Removed `sanitizeOverrides`

* Moved initialization code to its own file

* Hopefully fixed test contamination

* Actually listened to people now

* fixed the thingy

* Run stub setup on init because

* Update testFileInitialization.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Misc] Sinistea and Poltchageist line alt forms now available (#4989)

* Sinistea and Poltchageist line alt forms now available

* Unmark Poltchageist line as unobtainable, fix sprite key of alt forms

* Correct forms not being marked as starter selectable

* Reduce wild chance for Antique/Masterpiece forms

Instead of being 1/2 chance to get the Antique or Masterpiece forms, it is now only a 1/16 chance to get them.

---------

Co-authored-by: damocleas <damocleas25@gmail.com>

* [Bug] Fix when variable move power is called (#6126)

* Apply variable power attribute before type boost

* Update test/abilities/normal-move-type-change.test.ts

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Minor test improvements

---------

Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [UI/UX] [Bug] Fix `ModifierSelectPhase` animation delay (#6121)

* Rework promise handling to ensure no races

* Add delay to ensure pokeball opening animation can be seen

* Remove leftover debug statements.

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Add tween bouncing pokeball to tweens that must complete for promise to resolve

* Fix typo in tsdoc

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* [Test] `MoveHelper#changeMoveset` disables moveset overrides (#5915)

Also fix Assist tests and add `expect` for max moveset length

* [Refactor] Minor cleanup of `initExpKeys` (#6127)

* [Bug] Fix Thrash continuing on caught Pokemon (#6144)

Fix Thrash continuing on caught Pokemon

* [UI/UX] Replace 'Neutral' in the Arena Flyout with 'Field' (#6139)

Update arena-flyout.ts for Field > Neutral

* [Dev] Added typedoc deployments for Beta (#6147)

* [Misc] Fix import in decrypt-save.js (#6149)

* [Refactor][Bug] Illusion no longer overwrites data of original Pokemon

https://github.com/pagefaultgames/pokerogue/pull/6140

* [UI/UX] Added "Hide Username" Setting (#6105)

* [UI/UX] Added "Hide Username" Setting

* Mask tid with asterisk instead of hiding completely

---------

Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>

* [Dev] Add `dist/` to `ls-lint` ignore list

* [i18n] Update Locales

* [Beta] [Bug] Fix shiny display issues (#6154)

Fix shininess check

* [Bug][Beta] Make bounce delay use fixed int (#6156)

Make bounce delay use fixed int

* [Refactor] Refactor UI text ts (#5946)

* Add destroy method to pokemon-sprite-sparkle-handler

* Move TextStyle to enums, convert into const object

* Cleanup text.ts file

* Add necessary explicit types for TextStyle let vars

* Fix locales submodule commit

* Fix merge issue

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Test] Added custom equality matchers (#6157)

* Added rudimentary test matchers for `toEqualArrayUnsorted` and `toHaveTypes`

Might port the rest at a later date

* Actually run the effing setup matchers file + fixed ls lint to not be angy

* added dev dep

* Update .ls-lint.yml

* Update .ls-lint.yml

---------

Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
Co-authored-by: Amani H. <109637146+xsn34kzx@users.noreply.github.com>

* [Misc] Fix import in `vitest.d.ts`

* [Dev] Turned on `checkJs` in TSConfig; fixed script type errors (#6115)

* [Misc] Moved + cleaned up string manipulation functions (#6112)

* Added string utility package to replace util functions

* Changed string manipulation functions fully over to `change-case`

* Fixed missing comma in package.json

trailing commas when :(

* fixed lockfile

* Hopefully re-added all the string utils

* fixed package json

* Fixed remaining cases of regex + code dupliation

* Fixed more bugs and errors

* Moved around functions and hopefully fixed the regex issues

* Minor renaming

* Fixed incorrect casing on setting strings

pascal snake case 💀

* ran biome

* [Refactor] Prevent serialization of full species in pokemon summon data

https://github.com/pagefaultgames/pokerogue/pull/6145

* Prevent serialization of entire species form in pokemon summon data

* Apply suggestions from code review

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Apply Kev's suggestions from code review

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

---------

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Docs] Add `@module` modifier tag to `tsdoc.json`

* [Bug] Fix camel case bug in `strings.ts` (#6161)

* [Dev] Change `target` to `ES2023` in `tsconfig.json` (#6160)

* breakup fight and ball commands into their own methods

* Breakup run and pokemon commands

* Breakup commandPhase#start

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Minor touchups

* Add overload for handle command

* Fix improperly named computeMoveId method

* Improve `canUse` computation

* Explicitly check against Moves.NONE

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Update with Bertie's comments

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Fix imports

* Apply kev's suggestions from code review

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Improve documentation

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* [Balance][Challenge] Added expanded Fresh Start options (#6162)

* [Dev] Add `workflow-dispatch` trigger to tests github workflow (#6152)

Add `workflow-dispatch` trigger to github workflow

Co-authored-by: damocleas <damocleas25@gmail.com>

* [Test] Add support for custom boilerplates to `create-test.js` (#6158)

* Added support for custom boilerplates to test:create script

* Added support for custom boilerplates to create-test.js

* Fixed syntax error

* Update create-test.js

Co-authored-by: Amani H. <109637146+xsn34kzx@users.noreply.github.com>

* Fix pluralization error in `create-test.js`

---------

Co-authored-by: Amani H. <109637146+xsn34kzx@users.noreply.github.com>

* [Refactor] Mark nickname in pokemon as optional (#6168)

Mark nickname in pokemon as optional

* [Dev] Update Vite from 6.3.5 to 7.0.6 (#6163)

* [ME] [Bug] Disable trades in GTS ME with only 1 valid party member

https://github.com/pagefaultgames/pokerogue/pull/6167

* [Refactor] Minor refactor of battler tags (#6129)

* Minor refactor battler tags

* Improve documentation

* Update type when loading in pokemon-data constructor for battler tags

* Fix issues in tsdoc comments with Wlowscha's suggestions

Co-authored-by: Wlowscha <54003515+Wlowscha@users.noreply.github.com>

* Apply bertie's suggestions from code review

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Remove unnecessary as const from tagType

* Remove missed `as const`

* Apply kev's suggestions from code review

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Update src/data/battler-tags.ts

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Update src/data/battler-tags.ts

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

---------

Co-authored-by: Wlowscha <54003515+Wlowscha@users.noreply.github.com>
Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Docs] Adjust `@module` doc comment in `battler-tags.ts`

Note that currently Typedoc is not parsing `@module` docs,
but this comment adjustment would be required if and when
it gets fixed

* [Balance] End of turn triggers won't occur when you end a biome (#6169)

* [Balance] End of turn triggers won't occur when you end a biome

* Add tests

* Move phase manipulation logic into `PhaseManager`

* Rename "biome end" to "interlude"

* Rename `TurnEndPhase#endOfBiome` to `upcomingInterlude`

---------

Co-authored-by: Wlowscha <54003515+Wlowscha@users.noreply.github.com>

* [Test] Add missing single battle override to `CheckInterludePhase` test

* [UI/UX] Fix button and input field overlaps (#6013)

* [Fix] Fix button overlap

* [Fix] Fix input field overlaps

* use getWidth to determine if label should be shortened

---------

Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
Co-authored-by: damocleas <damocleas25@gmail.com>

* [Dev] Moved type helpers to separate directory; (#6123)

* [Dev] Moved type helpers to separate directory; renamed `EnumValues` to `ObjectValues` and enforced usage

* Update tsconfig.json

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Fixed import issue

* Updated documentation slightly

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com>

* [Misc] Improve type signatures of serialized arena/battler tags (#6180)

* Improve type signatures of serialized arena/battler tags

* Minor adjustments to tsdocs from code review

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

---------

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* [Balance] Updated SF/Triage interactions for moves (#6179)

* Fixed move flags

* Disabled order up interactionn with sheer force

* Update src/data/moves/move.ts

* Removed order up test that no longer applies

shouldn't have been there in the first place

* [Move Bug] Fully implemented Future Sight, Doom Desire; fixed Wish Double battle oversight  (#5862)

* Mostly implemented Future Sight/Doom Desire

* Fixed a few docs

* Fixed com

* Update magic_guard.test.ts

* Update documentation

* Update documentation on arena-tag.ts

* Update arena-tag.ts docs

* Update arena-tag.ts

* Update turn-end-phase.ts

* Update move.ts documentation

* Fixed tpyo

* Update move.ts documentation

* Add assorted TODO test cases

* Refactored FS to use a positional tag manager

* Added strong typing to the manager, finished save load stufff

* Fixed locales + tests

* Fixed tests and documentation

* sh
Fixed tests for good

* Fixed MEP

* Reverted overrides changse

* Fixed issues with merging

* Fixed locales update & heal block test

* Fixed wish tests

* Fixed test typo

* Fixed wish test flaking out due to speed ties

* Fixed tests fr fr

* actually fixed tests bc i'm stupid

* Fixed tests for real

* Remove locales update

* Update arena-tag.ts

Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com>

* Update move.ts

Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com>

* Update arena-tag.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Applied review suggestions and added a _wee_ bit more docs

* fixed wish condition

* Applied kev's reviews

* Minor nits

* Minor formatting change in `heal-block.test.ts`

---------

Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Refactor] Added `PhaseManager.toTitleScreen` (#6114)

* Added `toTitlePhase`; documented phase manager

* Documented phase methods

* Fixed syntax errors + updated signature

* Reverted all the goodies

* Fixed missing shift phase call

GHAG

* Reverted change

* [UI/UX] Implement Discard Button (#5985)

* [feature]Implemented needed parts for discard function from issue #4780:
-TryDiscardFunction in battlescene;
-Created a party discard mode button;
-Updated Transfer button in modifier-select-ui-handler to Manage items;
-Created tests for the discard function in test/ui;
-Added images for the new discard and transfer buttons to loading-scene;
-Created placeholder messages for discard feature in party-ui-handler;

Co-authored-by: Tiago Rodrigues <tiago.n.rodrigues@tecnico.ulisboa.pt>

* [Fix] Updated icon for dynamic messaging

* [Fix] Corrected legacy mode icons and adjusted double-battle button location

* [Fix]Adjusted button positioning and mapping after review. Mapping requires debugging.

* [Fix] Fixed visible pokeball in legacy mode and key mapping

* [Fix] Background fixes,manage menu is the only one affected by changes now

* Implement i18n keys

* [Fix] implemented most code optimizations and callbacks to the modified locales folder

* [Fix] Implemented 3 suggestions

* [Fix]improved/corrected test structure

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* [Fix] added functionality test for the discard button

* [Fix] added necessary comment

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* [Fix] Implemented suggested changes in test/discard text prompt

* [Fix] Implemented UI suggestions and removed discard text confirmation

* [Fix] added missing imports

* Fix imports in test file

* [Fix] Implemented suggested cursor behavior and reworked test code

* [Fix] Corrected failed test

* [Fix] atempting to fix the test timeout issue

* [Fix] Undoing latest attempt

* [Fix] Implemented suggestions to fix broken tests

* Reviews

* [Fix] replaced icon images

* [Fix] Updated jsons to match new icons and removed pokeball icon from legacy mode

* Optimized new images

* [Fix] Fixed referenced bug and added similar confirmation box to release

* [Fix] Updated tests to handle the corfirmation box

* [Fix] Added back the accidentally removed changes

* [Fix]updated incorrect import path

* [fix] add description for the manageItemMode function

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* Update src/ui/party-ui-handler.ts

Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>

* [Fix] corrected formating issue

---------

Co-authored-by: Mikhail Shueb <mikhail.shueb@tecnico.ulisboa.pt>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Bertie690 <136088738+Bertie690@users.noreply.github.com>
Co-authored-by: damocleas <damocleas25@gmail.com>
Co-authored-by: Bertie690 <taylormw163@gmail.com>
Co-authored-by: Adri1 <adrien.grivel@hotmail.fr>

* [Bug] Fix Truant behavior (#6171)

* Update locales

* [i18n] Add Tagalog language support (#6170)

* [Bug] [Beta] Fix serializable battler tags (#6183)

Fix serializable battler tags

* [Misc] [Beta] Fix serializable battler tags (#6184)

Fix serializable battler tags

* [Misc] Disallow using NonFunctionProperties in loadTag methods (#6185)

Disallow using NonFunctionProperties in loadTag methods

* [Dev] `pnpm biome` will now only display errors by default (#6187)

* [Sprite] Move Clauncher, Clawitzer, Skiddo, Fomantis, Lurantis animations to consistent (#6177)

* Move Clauncher, Clawitzer, Skiddo out of exp

* Move Fomantis, Lurantis out of exp

* [Misc] Fix missing types in battler tags and remove DragonCheerTag (#6188)

* Fix missing types in battler tags

* Fix issues with dragon cheer

* [Misc] Set default for crit stage battler tag (#6192)

Set default for crit stage

* [Bug] [Beta] Remove setting currentPhase to null in clearAllPhases (#6193)

Remove setting currentPhase to null in clearAllPhases

* [Test] `game.move.use` overrides summon data moveset if present (#6174)

* [Test] Update `game.move.use` to override summon data moveset if present

* ran biome & fixed errors

* [Beta] Remove non-Legend Fresh Start option, fix starting movesets

https://github.com/pagefaultgames/pokerogue/pull/6196

* Remove non-Legend fresh start option, fix starting movesets

* Move updateInfo call

* Fix relearns counting for starter moves

* Reduce array allocations and function calls

* [Misc] Move asset initialization from `preload` to `launchBattle`

https://github.com/pagefaultgames/pokerogue/pull/6109

* Move asset initialization into `preload` instead of `launchBattle`

* Fix init problems; remove unused `waitUntil` util function

* Fixed missing `clearAllPhases` call

* [Balance] Adjust Cosmog / Cosmoem Evolutions, Lower Buneary Friendship requirements (#6198)

* Lower Buneary Friendship Requirement and Change Cosmog method

* Update biomes.ts - Cosmog/Cosmoem wild evo levels

* fix ?

* fixed now

* Update Cosmog Evolution + Biome Levels

---------

Co-authored-by: damocleas <damocleas25@gmail.com>

* [Test] Port over + augment remaining test matchers from pkty (#6159)

* Partially ported over pkty matchers (WIP)

* Cleaned up some more matchers

* Fiexd up matchers

* Fixed up remaining matchers

* Removed the word "matcher" from the pkty matcher functions

If we want them back we can always undo this commit and convert the other custom ones

* Added wip spite test

* Added `toHaveUsedPP` matcher

* Fixed up docs and tests

* Fixed spite test

* Ran biome

* Apply Biome

* Reverted biome breaking i18next

* Update src/typings/i18next.d.ts comment

* Fixed log message to not be overly verbose

* Added option to check for all PP used in pp matcher + cleaned up grudge tests

* Fixed up tests

* Fixed tests and such

* Fix various TSDocs + missing TSDoc imports

* [Beta] Further Cosmog Evolution Readjustments (#6203)

Update pokemon-evolutions.ts

* [Dev] Move various functions out of `pokemon-species.ts` (#6155)

`initSpecies` moved to its own file,
other functions moved to `pokemon-utils.ts`

* [Balance] Prevent MEs on X1 Waves (#6204)

* [Balance] Prevent MEs on X1 Waves

* Fix Bug-Type Superfan Encounter Test

* Fix Unit Tests

* [Balance] Update and Change Breeder Trainer Class Teams (#6200)

* Update Breeder Trainer Class

* Update trainer-config.ts

* Update trainer-config.ts

* Update whirlwind.test.ts

* Update src/data/trainers/trainer-config.ts

Co-authored-by: AJ Fontaine <36677462+Fontbane@users.noreply.github.com>

* Adjust Party Templates and move Wynaut

* Update trainer-party-template.ts

---------

Co-authored-by: damocleas <damocleas25@gmail.com>
Co-authored-by: AJ Fontaine <36677462+Fontbane@users.noreply.github.com>

---------

Co-authored-by: Acelynn Zhang <102631387+acelynnzhang@users.noreply.github.com>
Co-authored-by: Acelynn Zhang <acelynnzhang@Acelynns-MacBook-Pro.local>
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
Co-authored-by: Madmadness65 <59298170+Madmadness65@users.noreply.github.com>
Co-authored-by: damocleas <damocleas25@gmail.com>
Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
Co-authored-by: SmhMyHead <191356399+SmhMyHead@users.noreply.github.com>
Co-authored-by: Amani H. <109637146+xsn34kzx@users.noreply.github.com>
Co-authored-by: AJ Fontaine <36677462+Fontbane@users.noreply.github.com>
Co-authored-by: Wlowscha <54003515+Wlowscha@users.noreply.github.com>
Co-authored-by: fabske0 <192151969+fabske0@users.noreply.github.com>
Co-authored-by: Dean <69436131+emdeann@users.noreply.github.com>
Co-authored-by: Tiago Rodrigues <tiago.n.rodrigues@tecnico.ulisboa.pt>
Co-authored-by: Mikhail Shueb <mikhail.shueb@tecnico.ulisboa.pt>
Co-authored-by: Adri1 <adrien.grivel@hotmail.fr>
Co-authored-by: Blitzy <118096277+Blitz425@users.noreply.github.com>
2025-08-04 00:08:37 +02:00

481 lines
17 KiB
TypeScript

import { Phase } from "#app/phase";
import { UiMode } from "#enums/ui-mode";
import { AttemptRunPhase } from "#phases/attempt-run-phase";
import { BattleEndPhase } from "#phases/battle-end-phase";
import { BerryPhase } from "#phases/berry-phase";
import { CheckSwitchPhase } from "#phases/check-switch-phase";
import { CommandPhase } from "#phases/command-phase";
import { DamageAnimPhase } from "#phases/damage-anim-phase";
import { EggLapsePhase } from "#phases/egg-lapse-phase";
import { EncounterPhase } from "#phases/encounter-phase";
import { EndEvolutionPhase } from "#phases/end-evolution-phase";
import { EnemyCommandPhase } from "#phases/enemy-command-phase";
import { EvolutionPhase } from "#phases/evolution-phase";
import { ExpPhase } from "#phases/exp-phase";
import { FaintPhase } from "#phases/faint-phase";
import { FormChangePhase } from "#phases/form-change-phase";
import { GameOverPhase } from "#phases/game-over-phase";
import { GameOverRewardPhase } from "#phases/game-over-reward-phase";
import { LearnMovePhase } from "#phases/learn-move-phase";
import { LevelCapPhase } from "#phases/level-cap-phase";
import { LoginPhase } from "#phases/login-phase";
import { MessagePhase } from "#phases/message-phase";
import { MoveEffectPhase } from "#phases/move-effect-phase";
import { MoveEndPhase } from "#phases/move-end-phase";
import { MovePhase } from "#phases/move-phase";
import {
MysteryEncounterBattlePhase,
MysteryEncounterOptionSelectedPhase,
MysteryEncounterPhase,
MysteryEncounterRewardsPhase,
PostMysteryEncounterPhase,
} from "#phases/mystery-encounter-phases";
import { NewBattlePhase } from "#phases/new-battle-phase";
import { NewBiomeEncounterPhase } from "#phases/new-biome-encounter-phase";
import { NextEncounterPhase } from "#phases/next-encounter-phase";
import { PartyExpPhase } from "#phases/party-exp-phase";
import { PartyHealPhase } from "#phases/party-heal-phase";
import { PokemonTransformPhase } from "#phases/pokemon-transform-phase";
import { PositionalTagPhase } from "#phases/positional-tag-phase";
import { PostGameOverPhase } from "#phases/post-game-over-phase";
import { PostSummonPhase } from "#phases/post-summon-phase";
import { QuietFormChangePhase } from "#phases/quiet-form-change-phase";
import { RevivalBlessingPhase } from "#phases/revival-blessing-phase";
import { RewardPhase } from "#phases/reward-phase";
import { RibbonRewardPhase } from "#phases/ribbon-reward-phase";
import { SelectBiomePhase } from "#phases/select-biome-phase";
import { SelectGenderPhase } from "#phases/select-gender-phase";
import { SelectRewardPhase } from "#phases/select-reward-phase";
import { SelectStarterPhase } from "#phases/select-starter-phase";
import { SelectTargetPhase } from "#phases/select-target-phase";
import { ShinySparklePhase } from "#phases/shiny-sparkle-phase";
import { ShowAbilityPhase } from "#phases/show-ability-phase";
import { StatStageChangePhase } from "#phases/stat-stage-change-phase";
import { SummonPhase } from "#phases/summon-phase";
import { SwitchPhase } from "#phases/switch-phase";
import { SwitchSummonPhase } from "#phases/switch-summon-phase";
import { TitlePhase } from "#phases/title-phase";
import { ToggleDoublePositionPhase } from "#phases/toggle-double-position-phase";
import { TurnEndPhase } from "#phases/turn-end-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import { TurnStartPhase } from "#phases/turn-start-phase";
import { UnavailablePhase } from "#phases/unavailable-phase";
import { UnlockPhase } from "#phases/unlock-phase";
import { VictoryPhase } from "#phases/victory-phase";
import { ErrorInterceptor } from "#test/test-utils/error-interceptor";
import type { PhaseClass, PhaseString } from "#types/phase-types";
import { UI } from "#ui/ui";
export interface PromptHandler {
phaseTarget?: string;
mode?: UiMode;
callback?: () => void;
expireFn?: () => void;
awaitingActionInput?: boolean;
}
type PhaseInterceptorPhase = PhaseClass | PhaseString;
export class PhaseInterceptor {
public scene;
public phases = {};
public log: string[];
private onHold;
private interval;
private promptInterval;
private intervalRun;
private prompts: PromptHandler[];
private phaseFrom;
private inProgress;
private originalSetMode;
private originalSetOverlayMode;
private originalSuperEnd;
/**
* List of phases with their corresponding start methods.
*
* CAUTION: If a phase and its subclasses (if any) both appear in this list,
* make sure that this list contains said phase AFTER all of its subclasses.
* This way, the phase's `prototype.start` is properly preserved during
* `initPhases()` so that its subclasses can use `super.start()` properly.
*/
private PHASES = [
[LoginPhase, this.startPhase],
[TitlePhase, this.startPhase],
[SelectGenderPhase, this.startPhase],
[NewBiomeEncounterPhase, this.startPhase],
[SelectStarterPhase, this.startPhase],
[PostSummonPhase, this.startPhase],
[SummonPhase, this.startPhase],
[ToggleDoublePositionPhase, this.startPhase],
[CheckSwitchPhase, this.startPhase],
[ShowAbilityPhase, this.startPhase],
[MessagePhase, this.startPhase],
[TurnInitPhase, this.startPhase],
[CommandPhase, this.startPhase],
[EnemyCommandPhase, this.startPhase],
[TurnStartPhase, this.startPhase],
[MovePhase, this.startPhase],
[MoveEffectPhase, this.startPhase],
[DamageAnimPhase, this.startPhase],
[FaintPhase, this.startPhase],
[BerryPhase, this.startPhase],
[TurnEndPhase, this.startPhase],
[BattleEndPhase, this.startPhase],
[EggLapsePhase, this.startPhase],
[SelectRewardPhase, this.startPhase],
[NextEncounterPhase, this.startPhase],
[NewBattlePhase, this.startPhase],
[VictoryPhase, this.startPhase],
[LearnMovePhase, this.startPhase],
[MoveEndPhase, this.startPhase],
[StatStageChangePhase, this.startPhase],
[ShinySparklePhase, this.startPhase],
[SelectTargetPhase, this.startPhase],
[UnavailablePhase, this.startPhase],
[QuietFormChangePhase, this.startPhase],
[SwitchPhase, this.startPhase],
[SwitchSummonPhase, this.startPhase],
[PartyHealPhase, this.startPhase],
[FormChangePhase, this.startPhase],
[EvolutionPhase, this.startPhase],
[EndEvolutionPhase, this.startPhase],
[LevelCapPhase, this.startPhase],
[AttemptRunPhase, this.startPhase],
[SelectBiomePhase, this.startPhase],
[PositionalTagPhase, this.startPhase],
[PokemonTransformPhase, this.startPhase],
[MysteryEncounterPhase, this.startPhase],
[MysteryEncounterOptionSelectedPhase, this.startPhase],
[MysteryEncounterBattlePhase, this.startPhase],
[MysteryEncounterRewardsPhase, this.startPhase],
[PostMysteryEncounterPhase, this.startPhase],
[RibbonRewardPhase, this.startPhase],
[GameOverRewardPhase, this.startPhase],
[RewardPhase, this.startPhase],
[PartyExpPhase, this.startPhase],
[ExpPhase, this.startPhase],
[EncounterPhase, this.startPhase],
[GameOverPhase, this.startPhase],
[UnlockPhase, this.startPhase],
[PostGameOverPhase, this.startPhase],
[RevivalBlessingPhase, this.startPhase],
];
private endBySetMode = [
TitlePhase,
SelectGenderPhase,
CommandPhase,
SelectRewardPhase,
MysteryEncounterPhase,
PostMysteryEncounterPhase,
];
/**
* Constructor to initialize the scene and properties, and to start the phase handling.
* @param scene - The scene to be managed.
*/
constructor(scene) {
this.scene = scene;
this.onHold = [];
this.prompts = [];
this.clearLogs();
this.startPromptHandler();
this.initPhases();
}
/**
* Clears phase logs
*/
clearLogs() {
this.log = [];
}
rejectAll(error) {
if (this.inProgress) {
clearInterval(this.promptInterval);
clearInterval(this.interval);
clearInterval(this.intervalRun);
this.inProgress.onError(error);
}
}
/**
* Method to set the starting phase.
* @param phaseFrom - The phase to start from.
* @returns The instance of the PhaseInterceptor.
*/
runFrom(phaseFrom: PhaseInterceptorPhase): PhaseInterceptor {
this.phaseFrom = phaseFrom;
return this;
}
/**
* Method to transition to a target phase.
* @param phaseTo - The phase to transition to.
* @param runTarget - Whether or not to run the target phase; default `true`.
* @returns A promise that resolves when the transition is complete.
*/
async to(phaseTo: PhaseInterceptorPhase, runTarget = true): Promise<void> {
return new Promise(async (resolve, reject) => {
ErrorInterceptor.getInstance().add(this);
if (this.phaseFrom) {
await this.run(this.phaseFrom).catch(e => reject(e));
this.phaseFrom = null;
}
const targetName = typeof phaseTo === "string" ? phaseTo : phaseTo.name;
this.intervalRun = setInterval(async () => {
const currentPhase = this.onHold?.length && this.onHold[0];
if (currentPhase && currentPhase.name === targetName) {
clearInterval(this.intervalRun);
if (!runTarget) {
return resolve();
}
await this.run(currentPhase).catch(e => {
clearInterval(this.intervalRun);
return reject(e);
});
return resolve();
}
if (currentPhase && currentPhase.name !== targetName) {
await this.run(currentPhase).catch(e => {
clearInterval(this.intervalRun);
return reject(e);
});
}
});
});
}
/**
* Method to run a phase with an optional skip function.
* @param phaseTarget - The phase to run.
* @param skipFn - Optional skip function.
* @returns A promise that resolves when the phase is run.
*/
run(phaseTarget: PhaseInterceptorPhase, skipFn?: (className: PhaseClass) => boolean): Promise<void> {
const targetName = typeof phaseTarget === "string" ? phaseTarget : phaseTarget.name;
this.scene.moveAnimations = null; // Mandatory to avoid crash
return new Promise(async (resolve, reject) => {
ErrorInterceptor.getInstance().add(this);
const interval = setInterval(async () => {
const currentPhase = this.onHold.shift();
if (currentPhase) {
if (currentPhase.name !== targetName) {
clearInterval(interval);
const skip = skipFn?.(currentPhase.name);
if (skip) {
this.onHold.unshift(currentPhase);
ErrorInterceptor.getInstance().remove(this);
return resolve();
}
clearInterval(interval);
return reject(`Wrong phase: this is ${currentPhase.name} and not ${targetName}`);
}
clearInterval(interval);
this.inProgress = {
name: currentPhase.name,
callback: () => {
ErrorInterceptor.getInstance().remove(this);
resolve();
},
onError: error => reject(error),
};
currentPhase.call();
}
});
});
}
whenAboutToRun(phaseTarget: PhaseInterceptorPhase, _skipFn?: (className: PhaseClass) => boolean): Promise<void> {
const targetName = typeof phaseTarget === "string" ? phaseTarget : phaseTarget.name;
this.scene.moveAnimations = null; // Mandatory to avoid crash
return new Promise(async (resolve, _reject) => {
ErrorInterceptor.getInstance().add(this);
const interval = setInterval(async () => {
const currentPhase = this.onHold[0];
if (currentPhase?.name === targetName) {
clearInterval(interval);
resolve();
}
});
});
}
pop() {
this.onHold.pop();
this.scene.phaseManager.shiftPhase();
}
/**
* Remove the current phase from the phase interceptor.
*
* Do not call this unless absolutely necessary. This function is intended
* for cleaning up the phase interceptor when, for whatever reason, a phase
* is manually ended without using the phase interceptor.
*
* @param shouldRun Whether or not the current scene should also be run.
*/
shift(shouldRun = false): void {
this.onHold.shift();
if (shouldRun) {
this.scene.phaseManager.shiftPhase();
}
}
/**
* Method to initialize phases and their corresponding methods.
*/
initPhases() {
this.originalSetMode = UI.prototype.setMode;
this.originalSetOverlayMode = UI.prototype.setOverlayMode;
this.originalSuperEnd = Phase.prototype.end;
UI.prototype.setMode = (mode, ...args) => this.setMode.call(this, mode, ...args);
Phase.prototype.end = () => this.superEndPhase.call(this);
for (const [phase, methodStart] of this.PHASES) {
const originalStart = phase.prototype.start;
this.phases[phase.name] = {
start: originalStart,
endBySetMode: this.endBySetMode.some(elm => elm.name === phase.name),
};
phase.prototype.start = () => methodStart.call(this, phase);
}
}
/**
* Method to start a phase and log it.
* @param phase - The phase to start.
*/
startPhase(phase: PhaseClass) {
this.log.push(phase.name);
const instance = this.scene.phaseManager.getCurrentPhase();
this.onHold.push({
name: phase.name,
call: () => {
this.phases[phase.name].start.apply(instance);
},
});
}
unlock() {
this.inProgress?.callback();
this.inProgress = undefined;
}
/**
* Method to end a phase and log it.
* @param phase - The phase to start.
*/
superEndPhase() {
const instance = this.scene.phaseManager.getCurrentPhase();
this.originalSuperEnd.apply(instance);
this.inProgress?.callback();
this.inProgress = undefined;
}
/**
* m2m to set mode.
* @param mode - The {@linkcode UiMode} to set.
* @param args - Additional arguments to pass to the original method.
*/
setMode(mode: UiMode, ...args: unknown[]): Promise<void> {
const currentPhase = this.scene.phaseManager.getCurrentPhase();
const instance = this.scene.ui;
console.log("setMode", `${UiMode[mode]} (=${mode})`, args);
const ret = this.originalSetMode.apply(instance, [mode, ...args]);
if (!this.phases[currentPhase.constructor.name]) {
throw new Error(
`missing ${currentPhase.constructor.name} in phaseInterceptor PHASES list --- Add it to PHASES inside of /test/utils/phaseInterceptor.ts`,
);
}
if (this.phases[currentPhase.constructor.name].endBySetMode) {
this.inProgress?.callback();
this.inProgress = undefined;
}
return ret;
}
/**
* mock to set overlay mode
* @param mode - The {@linkcode Mode} to set.
* @param args - Additional arguments to pass to the original method.
*/
setOverlayMode(mode: UiMode, ...args: unknown[]): Promise<void> {
const instance = this.scene.ui;
console.log("setOverlayMode", `${UiMode[mode]} (=${mode})`, args);
const ret = this.originalSetOverlayMode.apply(instance, [mode, ...args]);
return ret;
}
/**
* Method to start the prompt handler.
*/
startPromptHandler() {
this.promptInterval = setInterval(() => {
if (this.prompts.length) {
const actionForNextPrompt = this.prompts[0];
const expireFn = actionForNextPrompt.expireFn?.();
const currentMode = this.scene.ui.getMode();
const currentPhase = this.scene.phaseManager.getCurrentPhase()?.constructor.name;
const currentHandler = this.scene.ui.getHandler();
if (expireFn) {
this.prompts.shift();
} else if (
currentMode === actionForNextPrompt.mode &&
currentPhase === actionForNextPrompt.phaseTarget &&
currentHandler.active &&
(!actionForNextPrompt.awaitingActionInput ||
(actionForNextPrompt.awaitingActionInput && currentHandler.awaitingActionInput))
) {
const prompt = this.prompts.shift();
if (prompt?.callback) {
prompt.callback();
}
}
}
});
}
/**
* Method to add an action to the next prompt.
* @param phaseTarget - The target phase for the prompt.
* @param mode - The mode of the UI.
* @param callback - The callback function to execute.
* @param expireFn - The function to determine if the prompt has expired.
* @param awaitingActionInput - ???; default `false`
*/
addToNextPrompt(
phaseTarget: string,
mode: UiMode,
callback: () => void,
expireFn?: () => void,
awaitingActionInput = false,
) {
this.prompts.push({
phaseTarget,
mode,
callback,
expireFn,
awaitingActionInput,
});
}
/**
* Restores the original state of phases and clears intervals.
*
* This function iterates through all phases and resets their `start` method to the original
* function stored in `this.phases`. Additionally, it clears the `promptInterval` and `interval`.
*/
restoreOg() {
for (const [phase] of this.PHASES) {
phase.prototype.start = this.phases[phase.name].start;
}
UI.prototype.setMode = this.originalSetMode;
UI.prototype.setOverlayMode = this.originalSetOverlayMode;
Phase.prototype.end = this.originalSuperEnd;
clearInterval(this.promptInterval);
clearInterval(this.interval);
clearInterval(this.intervalRun);
}
}