pokerogue/src/test/utils/inputsHandler.ts
NightKev 828897316e
[Test] Replace doAttack() with move.select() in tests (#3567)
* Consolidate `doSelectTarget()` into `doAttack()`

* Fix ternary

* Add error message to aid in debugging tests

* Update docs

* [Test] Change `doAttack()` to `selectMove()`

* Add `select()` to `src/test/utils/helpers/moveHelper.ts`

* Replace instances of `game.selectMove()` with `game.move.select()`

* Fix imports

* Replace `selectMove()` with `move.select()` helper

Fix broken tests for Pastel Veil and Sweet Veil

* Update tsdocs
2024-08-22 06:49:33 -07:00

119 lines
3.4 KiB
TypeScript

import BattleScene from "#app/battle-scene";
import pad_xbox360 from "#app/configs/inputs/pad_xbox360";
import { InputsController } from "#app/inputs-controller";
import TouchControl from "#app/touch-controls";
import { holdOn } from "#test/utils/gameManagerUtils";
import fs from "fs";
import { JSDOM } from "jsdom";
import Phaser from "phaser";
interface LogEntry {
type: string;
button: any;
}
export default class InputsHandler {
private scene: BattleScene;
private events: Phaser.Events.EventEmitter;
private inputController: InputsController;
public log: LogEntry[] = [];
public logUp: LogEntry[] = [];
private fakePad: Fakepad;
private fakeMobile: FakeMobile;
constructor(scene: BattleScene) {
this.scene = scene;
this.inputController = this.scene.inputController;
this.fakePad = new Fakepad(pad_xbox360);
this.fakeMobile = new FakeMobile();
this.scene.input.gamepad?.gamepads.push(this.fakePad);
this.init();
}
pressTouch(button: string, duration: integer): Promise<void> {
return new Promise(async (resolve) => {
this.fakeMobile.touchDown(button);
await holdOn(duration);
this.fakeMobile.touchUp(button);
resolve();
});
}
pressGamepadButton(button: integer, duration: integer): Promise<void> {
return new Promise(async (resolve) => {
this.scene.input.gamepad?.emit("down", this.fakePad, {index: button});
await holdOn(duration);
this.scene.input.gamepad?.emit("up", this.fakePad, {index: button});
resolve();
});
}
pressKeyboardKey(key: integer, duration: integer): Promise<void> {
return new Promise(async (resolve) => {
this.scene.input.keyboard?.emit("keydown", {keyCode: key});
await holdOn(duration);
this.scene.input.keyboard?.emit("keyup", {keyCode: key});
resolve();
});
}
init(): void {
const touchControl = new TouchControl(this.scene);
touchControl.deactivatePressedKey(); //test purpose
this.events = this.inputController.events;
this.scene.input.gamepad?.emit("connected", this.fakePad);
this.listenInputs();
}
listenInputs(): void {
this.events.on("input_down", (event) => {
this.log.push({type: "input_down", button: event.button});
}, this);
this.events.on("input_up", (event) => {
this.logUp.push({type: "input_up", button: event.button});
}, this);
}
}
class Fakepad extends Phaser.Input.Gamepad.Gamepad {
public id: string;
public index: number;
constructor(pad) {
//@ts-ignore
super(undefined, {...pad, buttons: pad.deviceMapping, axes: []}); //TODO: resolve ts-ignore
this.id = "xbox_360_fakepad";
this.index = 0;
}
}
class FakeMobile {
constructor() {
const fakeMobilePage = fs.readFileSync("./src/test/utils/fakeMobile.html", {encoding: "utf8", flag: "r"});
const dom = new JSDOM(fakeMobilePage);
Object.defineProperty(window, "document", {
value: dom.window.document,
configurable: true,
});
}
touchDown(button: string) {
const node = document.querySelector(`[data-key][id='${button}']`);
if (!node) {
return;
}
const event = new Event("touchstart");
node.dispatchEvent(event);
}
touchUp(button: string) {
const node = document.querySelector(`[data-key][id='${button}']`);
if (!node) {
return;
}
const event = new Event("touchend");
node.dispatchEvent(event);
}
}