From c00d7fcc2aa7d480cc1146d4db775aa482e8d174 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 1 Oct 2023 02:29:25 +0200 Subject: [PATCH] Multi movement --- code/server/server.ts | 17 ++++++++++++++--- code/src/Engine/Client/Client.ts | 4 ++-- code/src/Game/PlayerCharacter.ts | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/code/server/server.ts b/code/server/server.ts index b2a8ff9..3644c82 100644 --- a/code/server/server.ts +++ b/code/server/server.ts @@ -31,17 +31,17 @@ wss.on("connection", (ws) => { // Add client to room rooms.set(msg.room_id, new Array()); rooms.get(msg.room_id)!.push(ws); - // Send ACK msg to client + // Send CON msg to client ws.send( JSON.stringify({ - type: "CRE", + type: "CON", msg: "OK", }) ); } else { ws.send( JSON.stringify({ - type: "CRE", + type: "CON", msg: "ERR", }) ); @@ -53,11 +53,22 @@ wss.on("connection", (ws) => { console.log("Joining room: " + msg.room_id); rooms.get(msg.room_id)!.push(ws); clients.get(ws).room = msg.room_id; + ws.send( + JSON.stringify({ + type: "CON", + msg: "OK", + }) + ); for (const element of rooms.get(msg.room_id)) { if (element != ws) { + // Add client to all others in room element.send( JSON.stringify({ type: "JOI", id: clients.get(ws).id }) ); + // Add all others in room to client + ws.send( + JSON.stringify({ type: "JOI", id: clients.get(element).id }) + ); } } } diff --git a/code/src/Engine/Client/Client.ts b/code/src/Engine/Client/Client.ts index f81cfe8..9802a42 100644 --- a/code/src/Engine/Client/Client.ts +++ b/code/src/Engine/Client/Client.ts @@ -29,7 +29,7 @@ export class Client { return false; } - joinRoom(roomId): boolean { + joinRoom(roomId: string): boolean { this.send(JSON.stringify({ type: "JOI", room_id: roomId }), 100); return false; } @@ -40,7 +40,7 @@ export class Client { console.log(message); const msg = JSON.parse(message); switch (msg.type) { - case "CRE": + case "CON": switch (msg.msg) { case "OK": this.connected = true; diff --git a/code/src/Game/PlayerCharacter.ts b/code/src/Game/PlayerCharacter.ts index 7e51fee..5fa7bd2 100644 --- a/code/src/Game/PlayerCharacter.ts +++ b/code/src/Game/PlayerCharacter.ts @@ -184,7 +184,7 @@ export default class PlayerCharacter { } const client = Game.getInstanceNoSa().client; - if (true) { + if (client.connected) { client.send( JSON.stringify({ type: "MOV",