Added websocket to enable "Chat Plays PokeRogue"

pull/760/head
hexblit 2024-05-09 13:35:44 -06:00
parent bb28d3599e
commit f78187fe96
3 changed files with 54 additions and 6 deletions

10
package-lock.json generated
View File

@ -14,7 +14,8 @@
"i18next-browser-languagedetector": "^7.2.1",
"json-stable-stringify": "^1.1.0",
"phaser": "^3.70.0",
"phaser3-rex-plugins": "^1.1.84"
"phaser3-rex-plugins": "^1.1.84",
"ws": "^8.17.0"
},
"devDependencies": {
"@vitest/coverage-istanbul": "^1.4.0",
@ -5860,10 +5861,9 @@
"dev": true
},
"node_modules/ws": {
"version": "8.16.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz",
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
"dev": true,
"version": "8.17.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz",
"integrity": "sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==",
"engines": {
"node": ">=10.0.0"
},

View File

@ -34,7 +34,8 @@
"i18next-browser-languagedetector": "^7.2.1",
"json-stable-stringify": "^1.1.0",
"phaser": "^3.70.0",
"phaser3-rex-plugins": "^1.1.84"
"phaser3-rex-plugins": "^1.1.84",
"ws": "^8.17.0"
},
"engines": {
"node": ">=18.0.0"

View File

@ -1,4 +1,5 @@
import Phaser, {Time} from "phaser";
import WebSocket, {WebSocketServer} from 'ws';
import * as Utils from "./utils";
import {initTouchControls} from './touch-controls';
import pad_generic from "./configs/pad_generic";
@ -27,6 +28,7 @@ export class InputsController {
private buttonKeys: Phaser.Input.Keyboard.Key[][];
private gamepads: Array<string> = new Array();
private scene: Phaser.Scene;
private wss = new WebSocketServer({ port: 9876});
// buttonLock ensures only a single movement key is firing repeated inputs
// (i.e. by holding down a button) at a time
@ -81,6 +83,15 @@ export class InputsController {
// Keyboard
this.setupKeyboardControls();
// setup WebSocket
this.wss.on('connection', function connection(ws: WebSocket) {
ws.on('message', function incoming(message: string) {
console.log('received: %s', message);
});
});
}
loseFocus(): void {
@ -168,6 +179,10 @@ export class InputsController {
}
}
setupWebSocketControls(): void {
}
setupKeyboardControls(): void {
const keyCodes = Phaser.Input.Keyboard.KeyCodes;
const keyConfig = {
@ -224,6 +239,36 @@ export class InputsController {
}
});
}
processInputCommad(input: string): void {
const command = input.toLowerCase();
const commandMapping = {
'up' : Button.UP,
'down' : Button.DOWN,
'left' : Button.LEFT,
'right' : Button.RIGHT,
'submit' : Button.SUBMIT,
'cancel' : Button.CANCEL,
'menu' : Button.MENU,
'stats' : Button.STATS
};
if (commandMapping[command]) {
this.triggerButtonPress(commandMapping[command]);
}
}
triggerButtonPress(button: Button): void {
this.events.emit('input_down', {
controller_type: 'command',
button: button,
});
this.events.emit('input_up', {
controller_type: 'command',
button: button,
});
this.setLastProcessedMovementTime(button);
this.delLastProcessedMovementTime(button);
}
mapGamepad(id: string): GamepadConfig {
id = id.toLowerCase();
@ -291,4 +336,6 @@ export class InputsController {
if (this.buttonLock === button) this.buttonLock = null;
else if (this.buttonLock2 === button) this.buttonLock2 = null;
}
}