diff --git a/package-lock.json b/package-lock.json index 7db944a45..cdcf19ffa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" }, diff --git a/package.json b/package.json index d5ffb651f..d439c3343 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 5f5615358..f1c236855 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -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 = 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; } + + } \ No newline at end of file