pokerogue/test/test-utils/game-manager.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

556 lines
21 KiB
TypeScript

import { updateUserInfo } from "#app/account";
import { BattleScene } from "#app/battle-scene";
import { getGameMode } from "#app/game-mode";
import { globalScene } from "#app/global-scene";
import overrides from "#app/overrides";
import { allRewards } from "#data/data-lists";
import { BattlerIndex } from "#enums/battler-index";
import { Button } from "#enums/buttons";
import { ExpGainsSpeed } from "#enums/exp-gains-speed";
import { ExpNotification } from "#enums/exp-notification";
import { GameModes } from "#enums/game-modes";
import type { MysteryEncounterType } from "#enums/mystery-encounter-type";
import { PlayerGender } from "#enums/player-gender";
import type { PokeballType } from "#enums/pokeball";
import type { SpeciesId } from "#enums/species-id";
import { UiMode } from "#enums/ui-mode";
import type { EnemyPokemon, PlayerPokemon } from "#field/pokemon";
import { Trainer } from "#field/trainer";
import { CheckSwitchPhase } from "#phases/check-switch-phase";
import { CommandPhase } from "#phases/command-phase";
import { EncounterPhase } from "#phases/encounter-phase";
import { LoginPhase } from "#phases/login-phase";
import { MovePhase } from "#phases/move-phase";
import { MysteryEncounterPhase } from "#phases/mystery-encounter-phases";
import { NewBattlePhase } from "#phases/new-battle-phase";
import { SelectStarterPhase } from "#phases/select-starter-phase";
import type { SelectTargetPhase } from "#phases/select-target-phase";
import { TitlePhase } from "#phases/title-phase";
import { TurnEndPhase } from "#phases/turn-end-phase";
import { TurnInitPhase } from "#phases/turn-init-phase";
import { TurnStartPhase } from "#phases/turn-start-phase";
import { ErrorInterceptor } from "#test/test-utils/error-interceptor";
import { generateStarter } from "#test/test-utils/game-manager-utils";
import { GameWrapper } from "#test/test-utils/game-wrapper";
import { ChallengeModeHelper } from "#test/test-utils/helpers/challenge-mode-helper";
import { ClassicModeHelper } from "#test/test-utils/helpers/classic-mode-helper";
import { DailyModeHelper } from "#test/test-utils/helpers/daily-mode-helper";
import { FieldHelper } from "#test/test-utils/helpers/field-helper";
import { ModifierHelper } from "#test/test-utils/helpers/modifiers-helper";
import { MoveHelper } from "#test/test-utils/helpers/move-helper";
import { OverridesHelper } from "#test/test-utils/helpers/overrides-helper";
import { ReloadHelper } from "#test/test-utils/helpers/reload-helper";
import { SettingsHelper } from "#test/test-utils/helpers/settings-helper";
import type { InputsHandler } from "#test/test-utils/inputs-handler";
import { MockFetch } from "#test/test-utils/mocks/mock-fetch";
import { PhaseInterceptor } from "#test/test-utils/phase-interceptor";
import { TextInterceptor } from "#test/test-utils/text-interceptor";
import type { BallUiHandler } from "#ui/ball-ui-handler";
import type { BattleMessageUiHandler } from "#ui/battle-message-ui-handler";
import type { CommandUiHandler } from "#ui/command-ui-handler";
import type { PartyUiHandler } from "#ui/party-ui-handler";
import type { RewardSelectUiHandler } from "#ui/reward-select-ui-handler";
import type { StarterSelectUiHandler } from "#ui/starter-select-ui-handler";
import type { TargetSelectUiHandler } from "#ui/target-select-ui-handler";
import { isNullOrUndefined } from "#utils/common";
import fs from "node:fs";
import { AES, enc } from "crypto-js";
import { expect, vi } from "vitest";
/**
* Class to manage the game state and transitions between phases.
*/
export class GameManager {
public gameWrapper: GameWrapper;
public scene: BattleScene;
public phaseInterceptor: PhaseInterceptor;
public textInterceptor: TextInterceptor;
public inputsHandler: InputsHandler;
public readonly override: OverridesHelper;
public readonly move: MoveHelper;
public readonly classicMode: ClassicModeHelper;
public readonly dailyMode: DailyModeHelper;
public readonly challengeMode: ChallengeModeHelper;
public readonly settings: SettingsHelper;
public readonly reload: ReloadHelper;
public readonly modifiers: ModifierHelper;
public readonly field: FieldHelper;
/**
* Creates an instance of GameManager.
* @param phaserGame - The Phaser game instance.
* @param bypassLogin - Whether to bypass the login phase.
*/
constructor(phaserGame: Phaser.Game, bypassLogin = true) {
localStorage.clear();
ErrorInterceptor.getInstance().clear();
// Simulate max rolls on RNG functions
// TODO: Create helpers for disabling/enabling battle RNG
BattleScene.prototype.randBattleSeedInt = (range, min = 0) => min + range - 1;
this.gameWrapper = new GameWrapper(phaserGame, bypassLogin);
// TODO: Figure out a way to optimize and re-use the same game manager for each test
// Re-use an existing `globalScene` if present, or else create a new scene from scratch.
if (globalScene) {
this.scene = globalScene;
this.phaseInterceptor = new PhaseInterceptor(this.scene);
this.resetScene();
} else {
this.scene = new BattleScene();
this.phaseInterceptor = new PhaseInterceptor(this.scene);
this.gameWrapper.setScene(this.scene);
}
this.textInterceptor = new TextInterceptor(this.scene);
this.override = new OverridesHelper(this);
this.move = new MoveHelper(this);
this.classicMode = new ClassicModeHelper(this);
this.dailyMode = new DailyModeHelper(this);
this.challengeMode = new ChallengeModeHelper(this);
this.settings = new SettingsHelper(this);
this.reload = new ReloadHelper(this);
this.modifiers = new ModifierHelper(this);
this.field = new FieldHelper(this);
this.initDefaultOverrides();
// TODO: remove `any` assertion
global.fetch = vi.fn(MockFetch) as any;
}
/** Reset a prior `BattleScene` instance to the proper initial state. */
private resetScene(): void {
this.scene.reset(false, true);
(this.scene.ui.handlers[UiMode.STARTER_SELECT] as StarterSelectUiHandler).clearStarterPreferences();
this.gameWrapper.scene = this.scene;
this.scene.phaseManager.toTitleScreen(true);
this.scene.phaseManager.shiftPhase();
}
/**
* Initialize various default overrides for starting tests, typically to alleviate randomness.
*/
// TODO: This should not be here
private initDefaultOverrides(): void {
// Disables Mystery Encounters on all tests (can be overridden at test level)
this.override.mysteryEncounterChance(0);
}
/**
* Sets the game mode.
* @param mode - The mode to set.
*/
setMode(mode: UiMode) {
this.scene.ui?.setMode(mode);
}
/**
* Waits until the specified mode is set.
* @param mode - The mode to wait for.
* @returns A promise that resolves when the mode is set.
*/
// TODO: This is unused
async waitMode(mode: UiMode): Promise<void> {
await vi.waitUntil(() => this.scene.ui?.getMode() === mode);
}
/**
* End the currently running phase immediately.
*/
endPhase() {
this.scene.phaseManager.getCurrentPhase()?.end();
}
/**
* Adds an action to be executed on the next prompt.
* This can be used to (among other things) simulate inputs or run functions mid-phase.
* @param phaseTarget - The target phase.
* @param mode - The mode to wait for.
* @param callback - The callback function to execute on next prompt.
* @param expireFn - Optional function to determine if the prompt has expired.
*/
onNextPrompt(
phaseTarget: string,
mode: UiMode,
callback: () => void,
expireFn?: () => void,
awaitingActionInput = false,
) {
this.phaseInterceptor.addToNextPrompt(phaseTarget, mode, callback, expireFn, awaitingActionInput);
}
/**
* Runs the game to the title phase.
* @returns A promise that resolves when the title phase is reached.
*/
async runToTitle(): Promise<void> {
await this.phaseInterceptor.whenAboutToRun(LoginPhase);
this.phaseInterceptor.pop();
await this.phaseInterceptor.run(TitlePhase);
this.scene.gameSpeed = 5;
this.scene.moveAnimations = false;
this.scene.showLevelUpStats = false;
this.scene.expGainsSpeed = ExpGainsSpeed.SKIP;
this.scene.expParty = ExpNotification.SKIP;
this.scene.hpBarSpeed = 3;
this.scene.enableTutorials = false;
this.scene.gameData.gender = PlayerGender.MALE; // set initial player gender
this.scene.battleStyle = this.settings.battleStyle;
this.scene.fieldVolume = 0;
}
/**
* Helper function to run to the final boss encounter as it's a bit tricky due to extra dialogue
* Also handles Major/Minor bosses from endless modes
* @param species - Array of {@linkcode SpeciesId}s to start the final battle with.
* @param mode - The {@linkcode GameModes} to spawn the final boss encounter in.
*/
async runToFinalBossEncounter(species: SpeciesId[], mode: GameModes) {
await this.runToTitle();
this.onNextPrompt("TitlePhase", UiMode.TITLE, () => {
this.scene.gameMode = getGameMode(mode);
const starters = generateStarter(this.scene, species);
const selectStarterPhase = new SelectStarterPhase();
this.scene.phaseManager.pushPhase(new EncounterPhase(false));
selectStarterPhase.initBattle(starters);
});
// This will consider all battle entry dialog as seens and skip them
vi.spyOn(this.scene.ui, "shouldSkipDialogue").mockReturnValue(true);
if (overrides.OPP_HELD_ITEMS_OVERRIDE.length === 0) {
this.removeEnemyHeldItems();
}
await this.phaseInterceptor.to("CommandPhase", false);
console.log("==================[Final Boss Encounter]==================");
}
/**
* Runs the game to a mystery encounter phase.
* @param encounterType - If specified, will expect encounter to be the given type.
* @param species - Optional array of species for party to start with.
* @returns A Promise that resolves when the EncounterPhase ends.
*/
async runToMysteryEncounter(encounterType?: MysteryEncounterType, species?: SpeciesId[]) {
if (!isNullOrUndefined(encounterType)) {
this.override.disableTrainerWaves();
this.override.mysteryEncounter(encounterType);
}
await this.runToTitle();
this.onNextPrompt(
"TitlePhase",
UiMode.TITLE,
() => {
this.scene.gameMode = getGameMode(GameModes.CLASSIC);
const starters = generateStarter(this.scene, species);
const selectStarterPhase = new SelectStarterPhase();
this.scene.phaseManager.pushPhase(new EncounterPhase(false));
selectStarterPhase.initBattle(starters);
},
() => this.isCurrentPhase(EncounterPhase),
);
this.onNextPrompt(
"EncounterPhase",
UiMode.MESSAGE,
() => {
const handler = this.scene.ui.getHandler() as BattleMessageUiHandler;
handler.processInput(Button.ACTION);
},
() => this.isCurrentPhase(MysteryEncounterPhase),
true,
);
await this.phaseInterceptor.run(EncounterPhase);
if (!isNullOrUndefined(encounterType)) {
expect(this.scene.currentBattle?.mysteryEncounter?.encounterType).toBe(encounterType);
}
}
/**
* Emulate a player's target selection after a move is chosen, usually called automatically by {@linkcode MoveHelper.select}.
* Will trigger during the next {@linkcode SelectTargetPhase}
* @param targetIndex - The {@linkcode BattlerIndex} of the attack target, or `undefined` for multi-target attacks
* @param movePosition - The 0-indexed position of the move in the pokemon's moveset array
* @throws Immediately fails tests
*/
selectTarget(movePosition: number, targetIndex?: BattlerIndex) {
this.onNextPrompt(
"SelectTargetPhase",
UiMode.TARGET_SELECT,
() => {
const handler = this.scene.ui.getHandler() as TargetSelectUiHandler;
const move = (this.scene.phaseManager.getCurrentPhase() as SelectTargetPhase)
.getPokemon()
.getMoveset()
[movePosition].getMove();
// Multi target attacks do not select a target
if (move.isMultiTarget()) {
if (targetIndex !== undefined) {
expect.fail(`targetIndex was passed to selectMove() but move ("${move.name}") is not targeted`);
}
} else {
handler.setCursor(targetIndex ?? BattlerIndex.ENEMY);
}
handler.processInput(Button.ACTION);
},
() =>
this.isCurrentPhase(CommandPhase) ||
this.isCurrentPhase(MovePhase) ||
this.isCurrentPhase(TurnStartPhase) ||
this.isCurrentPhase(TurnEndPhase),
);
}
/** Faint all opponents currently on the field */
async doKillOpponents() {
await this.killPokemon(this.scene.currentBattle.enemyParty[0]);
if (this.scene.currentBattle.double) {
await this.killPokemon(this.scene.currentBattle.enemyParty[1]);
}
}
/** Queue up button presses to skip taking an item on the next {@linkcode SelectRewardPhase} */
doSelectModifier() {
this.onNextPrompt(
"SelectRewardPhase",
UiMode.REWARD_SELECT,
() => {
const handler = this.scene.ui.getHandler() as RewardSelectUiHandler;
handler.processInput(Button.CANCEL);
},
() =>
this.isCurrentPhase(CommandPhase) ||
this.isCurrentPhase(NewBattlePhase) ||
this.isCurrentPhase(CheckSwitchPhase),
true,
);
this.onNextPrompt(
"SelectRewardPhase",
UiMode.CONFIRM,
() => {
const handler = this.scene.ui.getHandler() as RewardSelectUiHandler;
handler.processInput(Button.ACTION);
},
() =>
this.isCurrentPhase(CommandPhase) ||
this.isCurrentPhase(NewBattlePhase) ||
this.isCurrentPhase(CheckSwitchPhase),
);
}
forceEnemyToSwitch() {
const originalMatchupScore = Trainer.prototype.getPartyMemberMatchupScores;
Trainer.prototype.getPartyMemberMatchupScores = () => {
Trainer.prototype.getPartyMemberMatchupScores = originalMatchupScore;
return [
[1, 100],
[1, 100],
];
};
}
/**
* Transition to the first {@linkcode CommandPhase} of the next turn.
* @returns A promise that resolves once the next {@linkcode CommandPhase} has been reached.
*/
async toNextTurn() {
await this.phaseInterceptor.to("TurnInitPhase");
await this.phaseInterceptor.to("CommandPhase");
console.log("==================[New Turn]==================");
}
/** Transition to the {@linkcode TurnEndPhase | end of the current turn}. */
async toEndOfTurn() {
await this.phaseInterceptor.to("TurnEndPhase");
console.log("==================[End of Turn]==================");
}
/**
* Queue up button presses to skip taking an item on the next {@linkcode SelectRewardPhase},
* and then transition to the next {@linkcode CommandPhase}.
*/
async toNextWave() {
this.doSelectModifier();
// forcibly end the message box for switching pokemon
this.onNextPrompt(
"CheckSwitchPhase",
UiMode.CONFIRM,
() => {
this.setMode(UiMode.MESSAGE);
this.endPhase();
},
() => this.isCurrentPhase(TurnInitPhase),
);
await this.phaseInterceptor.to("TurnInitPhase");
await this.phaseInterceptor.to("CommandPhase");
console.log("==================[New Wave]==================");
}
/**
* Check if the player has won the battle.
* @returns whether the player has won the battle (all opposing Pokemon have been fainted)
*/
isVictory() {
return this.scene.currentBattle.enemyParty.every(pokemon => pokemon.isFainted());
}
/**
* Checks if the current phase matches the target phase.
* @param phaseTarget - The target phase.
* @returns Whether the current phase matches the target phase
*/
isCurrentPhase(phaseTarget) {
const targetName = typeof phaseTarget === "string" ? phaseTarget : phaseTarget.name;
return this.scene.phaseManager.getCurrentPhase()?.constructor.name === targetName;
}
/**
* Checks if the current mode matches the target mode.
* @param mode - The target {@linkcode UiMode} to check.
* @returns Whether the current mode matches the target mode.
*/
isCurrentMode(mode: UiMode) {
return this.scene.ui?.getMode() === mode;
}
/**
* Exports the save data to import it in a test game.
* @returns A promise that resolves with the exported save data.
*/
exportSaveToTest(): Promise<string> {
const saveKey = "x0i2O7WRiANTqPmZ";
return new Promise(async resolve => {
const sessionSaveData = this.scene.gameData.getSessionSaveData();
const encryptedSaveData = AES.encrypt(JSON.stringify(sessionSaveData), saveKey).toString();
resolve(encryptedSaveData);
});
}
/**
* Imports game data from a file.
* @param path - The path to the data file.
* @returns A promise that resolves with a tuple containing a boolean indicating success and an integer status code.
*/
async importData(path): Promise<[boolean, number]> {
const saveKey = "x0i2O7WRiANTqPmZ";
const dataRaw = fs.readFileSync(path, { encoding: "utf8", flag: "r" });
let dataStr = AES.decrypt(dataRaw, saveKey).toString(enc.Utf8);
dataStr = this.scene.gameData.convertSystemDataStr(dataStr);
const systemData = this.scene.gameData.parseSystemData(dataStr);
const valid = !!systemData.dexData && !!systemData.timestamp;
if (valid) {
await updateUserInfo();
await this.scene.gameData.initSystem(dataStr);
}
return updateUserInfo();
}
/**
* Faint a player or enemy pokemon instantly by setting their HP to 0.
* @param pokemon - The player/enemy pokemon being fainted
* @returns A Promise that resolves once the fainted pokemon's FaintPhase finishes running.
*/
async killPokemon(pokemon: PlayerPokemon | EnemyPokemon) {
pokemon.hp = 0;
this.scene.phaseManager.pushNew("FaintPhase", pokemon.getBattlerIndex(), true);
await this.phaseInterceptor.to("FaintPhase");
}
/**
* Command an in-battle switch to another {@linkcode Pokemon} via the main battle menu.
* @param pokemonIndex - The 0-indexed position of the party pokemon to switch to.
* Should never be called with 0 as that will select the currently active pokemon and freeze.
*/
doSwitchPokemon(pokemonIndex: number) {
this.onNextPrompt("CommandPhase", UiMode.COMMAND, () => {
(this.scene.ui.getHandler() as CommandUiHandler).setCursor(2);
(this.scene.ui.getHandler() as CommandUiHandler).processInput(Button.ACTION);
});
this.doSelectPartyPokemon(pokemonIndex, "CommandPhase");
}
/**
* Revive pokemon, currently players only.
* @param pokemonIndex - The 0-indexed position of the pokemon in your party to revive
*/
doRevivePokemon(pokemonIndex: number) {
const party = this.scene.getPlayerParty();
const reward = allRewards.MAX_REVIVE();
reward.apply({ pokemon: party[pokemonIndex] });
}
/**
* Select a pokemon from the party menu during the given phase.
* Only really handles the basic case of "navigate to party slot and press Action twice" -
* any menus that come up afterwards are ignored and must be handled separately by the caller.
* @param slot - The 0-indexed position of the pokemon in your party to switch to
* @param inPhase - Which phase to expect the selection to occur in. Defaults to `SwitchPhase`
* (which is where the majority of non-command switch operations occur).
*/
doSelectPartyPokemon(slot: number, inPhase = "SwitchPhase") {
this.onNextPrompt(inPhase, UiMode.PARTY, () => {
const partyHandler = this.scene.ui.getHandler() as PartyUiHandler;
partyHandler.setCursor(slot);
partyHandler.processInput(Button.ACTION); // select party slot
partyHandler.processInput(Button.ACTION); // send out (or whatever option is at the top)
});
}
/**
* Select the BALL option from the command menu, then press Action; in the BALL
* menu, select a pokéball type and press Action again to throw it.
* @param ballIndex - The {@linkcode PokeballType} to throw
*/
public doThrowPokeball(ballIndex: PokeballType) {
this.onNextPrompt("CommandPhase", UiMode.COMMAND, () => {
(this.scene.ui.getHandler() as CommandUiHandler).setCursor(1);
(this.scene.ui.getHandler() as CommandUiHandler).processInput(Button.ACTION);
});
this.onNextPrompt("CommandPhase", UiMode.BALL, () => {
const ballHandler = this.scene.ui.getHandler() as BallUiHandler;
ballHandler.setCursor(ballIndex);
ballHandler.processInput(Button.ACTION); // select ball and throw
});
}
/**
* Intercepts `TurnStartPhase` and mocks {@linkcode TurnStartPhase.getSpeedOrder}'s return value.
* Used to manually modify Pokemon turn order.
* Note: This *DOES NOT* account for priority.
* @param order - The turn order to set as an array of {@linkcode BattlerIndex}es.
* @example
* ```ts
* await game.setTurnOrder([BattlerIndex.PLAYER, BattlerIndex.ENEMY, BattlerIndex.ENEMY_2, BattlerIndex.PLAYER_2]);
* ```
*/
async setTurnOrder(order: BattlerIndex[]): Promise<void> {
await this.phaseInterceptor.to(TurnStartPhase, false);
vi.spyOn(this.scene.phaseManager.getCurrentPhase() as TurnStartPhase, "getSpeedOrder").mockReturnValue(order);
}
/**
* Removes all held items from enemy pokemon.
*/
removeEnemyHeldItems(): void {
this.scene.clearEnemyItems();
console.log("Enemy held items removed");
}
}