diff --git a/package.json b/package.json index 93a6636..faa6fd5 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "sisteransi": "^1.0.5", "typescript": "^4.3.2", "uuid": "^8.3.2", + "ws": "^7.4.6", "yarn": "^1.22.10" }, "scripts": { diff --git a/src/Menu.ts b/src/Menu.ts index 3fa562b..3bb591b 100644 --- a/src/Menu.ts +++ b/src/Menu.ts @@ -30,13 +30,8 @@ export class Menu implements Renderable { constructor() { menuPanel.on('keypress', (evt, key) => { log.info('keypress', key); - if (key.full === 'delete') { - Game.current.removePawn(Game.current.selected); - } else if (key.full === 'up') { - Game.current.advanceSelection(-1); - } else if (key.full === 'down') { - Game.current.advanceSelection(1); - } else if (key.full === 'left') { + + if (key.full === 'left') { this.view = View[Object.keys(View)[Math.min(Math.max(Object.values(View).indexOf(this.view) - 1, 0), Object.keys(View).length - 1)]] } else if (key.full === 'right') { this.view = View[Object.keys(View)[Math.min(Math.max(Object.values(View).indexOf(this.view) + 1, 0), Object.keys(View).length - 1)]] @@ -50,6 +45,26 @@ export class Menu implements Renderable { this.subMenu = SubMenu.NONE; } + if(this.view === View.PAWNS) { + if (key.full === 'delete') { + Game.current.removePawn(Game.current.selected); + } else if (key.full === 'up') { + Game.current.advanceSelection(-1); + } else if (key.full === 'down') { + Game.current.advanceSelection(1); + } + } + + if(this.view === View.MULTIPLAYER) { + if (key.full === 'enter') { + mdns.players[this.multiplayerSelected].sendItem(null); + } else if (key.full === 'up') { + this.multiplayerSelected --; + } else if (key.full === 'down') { + this.multiplayerSelected ++; + } + } + if(this.subMenu === SubMenu.TREES) { if (key.full === '=') { this.trees ++; @@ -148,9 +163,14 @@ export class Menu implements Renderable { })()}` } + multiplayerSelected = 0; + renderMultiplayer() { if(mdns.players.length === 0) return `{center}${tags.bright}${tags.black.fg}No friends online{/center}`; - return mdns.players.map(player => player.toString()).join('\n'); + return mdns.players.map((player, i) => { + if(i === this.multiplayerSelected) return '> ' + player.toString(); + else return player.toString(); + }).join('\n'); } renderInv() { diff --git a/src/mDNS.ts b/src/mDNS.ts index 95cadca..986a0c1 100644 --- a/src/mDNS.ts +++ b/src/mDNS.ts @@ -4,10 +4,15 @@ import getPort from 'get-port'; import os from 'os' import * as uuid from 'uuid'; import faker from 'faker'; +import chalk from 'chalk'; +import { Item, ItemState } from './Item.js'; +import WebSocket from 'ws'; +import { Popup } from './Popup.js'; +import { inspect } from 'util' const mdns = bonjour(); const ID = uuid.v4(); -let devices = []; +let devices: Player[] = []; class Player { name: string; @@ -15,7 +20,24 @@ class Player { port: number; toString() { - return ` ${this.name}`; + return ` ${this.name} ${chalk.bold.black(`${this.host}:${this.port}`)}`; + } + + sendItem(item: ItemState) { + return new Promise((res, rej) => { + const log = new ItemState(Item.LOG, 1, {}); + const json = log.toJson(); + const socket = new WebSocket(`ws://${this.host}:${this.port}`); + // new Popup(`opening ws://${this.host}:${this.port}`); + socket.on('open', () => { + socket.send(json); + socket.close(); + res(undefined); + }); + socket.on('error', () => { + rej(log); + }); + }); } } @@ -34,6 +56,12 @@ export async function ready(name, onThing?) { name, port: port }); + const wss = new WebSocket.Server({ port }); + wss.on('connection', function connection(ws) { + ws.on('message', function incoming(message) { + new Popup(inspect(message)); + }); + }); } mdns.find({ diff --git a/yarn.lock b/yarn.lock index 5a95b8f..5fb6ab5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -235,6 +235,11 @@ uuid@^8.3.2: version "8.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" +ws@^7.4.6: + version "7.4.6" + resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c" + integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A== + yarn@^1.22.10: version "1.22.10" resolved "https://registry.yarnpkg.com/yarn/-/yarn-1.22.10.tgz#c99daa06257c80f8fa2c3f1490724e394c26b18c"