From a6d6d6b5702c9e0c81d538ad21301d513e199600 Mon Sep 17 00:00:00 2001 From: MaciejkaG Date: Thu, 29 Feb 2024 21:55:56 +0100 Subject: [PATCH] Added proper room system, need to split menu and game into two separate pages and after that we can proceed to connecting menu and the game view --- index.js | 11 +++++++---- public/assets/css/main.css | 24 ++++++++++++++++++++++++ public/assets/js/socket.js | 11 +++++++---- views/index.handlebars | 15 +++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 7577d94..d24dc99 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,7 @@ app.get("/setup", (req, res) => { }); app.post('/api/setup-profile', function (req, res) { - if (req.session.nickname == null) { + if (req.session.nickname == null && 4 < req.body.nickname.length && req.body.nickname.length < 16) { req.session.nickname = req.body.nickname; } @@ -93,15 +93,18 @@ io.on('connection', (socket) => { }); } else { if (socket.rooms.size === 1) { - io.to(msg).emit("joined"); + io.to(msg).emit("joined", session.nickname); + let opp = io.sockets.sockets.get(io.sockets.adapter.rooms.get(msg).values().next().value); + let oppNickname = opp.request.session.nickname; socket.join(msg); callback({ - status: "ok" + status: "ok", + oppNickname: oppNickname, }); } else { callback({ status: "alreadyInLobby", - gameCode: id + gameCode: id, }); } } diff --git a/public/assets/css/main.css b/public/assets/css/main.css index 1aea6b1..8dd3830 100644 --- a/public/assets/css/main.css +++ b/public/assets/css/main.css @@ -134,4 +134,28 @@ body { #pvpJoinView form input[type=submit]:hover { background: white; color: black; +} + +/* Preparation */ +#preparingGame .modes { + display: flex; + gap: 2rem; +} + +#preparingGame .modes div { + height: 13rem; + width: 11rem; + background: black; + border: solid 1px white; + border-radius: 15px; + user-select: none; + padding: 1rem 3rem; + display: flex; + flex-direction: column; + gap: 0.75rem; +} + +#preparingGame .modes div * { + margin: 0; + width: 100%; } \ No newline at end of file diff --git a/public/assets/js/socket.js b/public/assets/js/socket.js index 9065465..e09639c 100644 --- a/public/assets/js/socket.js +++ b/public/assets/js/socket.js @@ -1,7 +1,10 @@ const socket = io(); -socket.on("joined", () => { - console.log("Someone joined the lobby!"); +socket.on("joined", (nick) => { + lockUI(true); + $("#oppNameField").html(nick); + switchView("preparingGame"); + lockUI(false); }); $("#createGameButton").on("click", function () { @@ -65,8 +68,8 @@ form.addEventListener('submit', (e) => { socket.emit("join lobby", input.value, (response) => { switch (response.status) { case "ok": - alert("Game joined!"); - switchView("mainMenuView"); + $("#oppNameField").html(response.oppNickname); + switchView("preparingGame"); lockUI(false); break; diff --git a/views/index.handlebars b/views/index.handlebars index ddd9800..37a0feb 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -62,6 +62,21 @@ +
+
+

Statki

+

PvP / Wczytywanie

+
+
+

Czekaj...

+

Wkrótce nastąpi przekierowanie

+

Przeciwnik:

+

+
+
+
+
+

Statki