From b20e3369f11d3f877202db534452f6ceb4d83cb1 Mon Sep 17 00:00:00 2001 From: NKY5223 <76890154+NKY5223@users.noreply.github.com> Date: Sun, 14 Apr 2024 09:10:42 +0800 Subject: [PATCH] Add game leave to Game (for real this time) --- index.js | 12 +++++++++--- js/Game.js | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d14c97f..629de9b 100644 --- a/index.js +++ b/index.js @@ -622,15 +622,21 @@ createGameBtn.addEventListener("click", _ => { // #region Join const gameDiv = document.getElementById("gameDiv"); -game.once("join", _ => { +game.on("join", _ => { document.activeElement?.blur(); changeScreen(gameDiv, 0); -}); -game.on("join", _ => { chatAsClient("Joined game"); }); // #endregion +// #region Leave +game.on("leave", _ => { + document.activeElement?.blur(); + changeScreen(titleScreen, 1); + chatAsClient("Left game"); +}); +// #endregion + // #region Player List const playerList = document.getElementById("playerList"); game.on("updateState", state => { diff --git a/js/Game.js b/js/Game.js index 2b64554..08778c0 100644 --- a/js/Game.js +++ b/js/Game.js @@ -51,6 +51,7 @@ export default class Game { this.socket.on("initMap", msg => this.initMap(msg.m)); this.socket.on("updateMap", msg => this.updateMap(msg.m)); + this.socket.on("leave", msg => this.emit("leave")); this.socket.on("join", msg => { this.initMap(msg.i.map); this.updateState(msg.i.states); @@ -332,6 +333,10 @@ export default class Game { }); }); } + leave() { + this.socket.send({ e: "leave" }); + this.emit("leave"); + } // #endregion // #region Map, State