From b5f6d678e14a1fef79a093267cade9a39b13f709 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 6 May 2021 03:16:38 +0100 Subject: [PATCH] Started clientside turn communication --- LICENSE | 2 +- TODO | 3 +++ client/public/scrabble/events.js | 1 - client/public/scrabble/network.js | 12 ++++++++++-- server/src/game-logic.js | 8 ++++++++ server/src/socketserver.js | 11 +++++++++-- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/LICENSE b/LICENSE index 22242c6..ae5a0da 100644 --- a/LICENSE +++ b/LICENSE @@ -1 +1 @@ -COPYRIGHT 2021 BENJAMIN KYD ALL RIGHTS RESERVED +COPYRIGHT 2021 BENJAMIN KYD - ALL RIGHTS RESERVED diff --git a/TODO b/TODO index b98d500..6bac72e 100644 --- a/TODO +++ b/TODO @@ -30,6 +30,9 @@ ☐ Game logic ✔ Game creation @done(21-04-26 17:38) + ☐ Multiplayer game logic + ☐ Process turns + ☐ Display turns on client ✘ Singleplayer game logic - OUT OF SCOPE @cancelled(21-04-11 01:04) ✘ AI CPU player @cancelled(21-04-11 01:07) diff --git a/client/public/scrabble/events.js b/client/public/scrabble/events.js index 26ed5b0..6d55dc8 100644 --- a/client/public/scrabble/events.js +++ b/client/public/scrabble/events.js @@ -33,4 +33,3 @@ document.querySelectorAll('piece').forEach(element => { element.addEventListener('mousedown', e => mouseDown(e, element)); element.addEventListener('touchstart', e => mouseDown(e, element)); }); - diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index a2b3b21..c057464 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -24,7 +24,9 @@ function initMultiplayer() socket.on('identify-error', args => onIdentifyError(socket, args)); socket.on('game-begin', args => onGameBegin(socket, args)); + socket.on('game-your-turn', args => onStartTurn(socket, args)); + console.log('multiplayer ready') } @@ -61,7 +63,7 @@ function onIdentify(socket, args) return; } - const lobbyUID = urlParser.get('uid') + const lobbyUID = urlParser.get('uid'); if (!lobbyUID) { @@ -103,7 +105,6 @@ function onDisconnect() function onGameBegin(socket, args) { - if (!args) { ConnectionState.innerHTML = localeString('error-game-begin'); @@ -118,6 +119,13 @@ function onGameBegin(socket, args) console.log(args); + + +} + +function onStartTurn(socket, args) +{ + console.log('my turn') } diff --git a/server/src/game-logic.js b/server/src/game-logic.js index 6defbdd..3dca161 100644 --- a/server/src/game-logic.js +++ b/server/src/game-logic.js @@ -71,6 +71,13 @@ NOTES let ActiveGames = []; +function GetTurnUser(gameuid) +{ + if (!ActiveGames[gameuid]) return false; + return ActiveGames[gameuid].players[ActiveGames[gameuid].turn]; +} + + function BeginGame(lobby) { // game uses the owners language - assumes it's valid @@ -180,6 +187,7 @@ module.exports = { // Game validation exports // Get game exports + GetTurnUser: GetTurnUser, // Change game state exports BeginGame: BeginGame, diff --git a/server/src/socketserver.js b/server/src/socketserver.js index dd0f1bc..eb50d06 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -424,10 +424,17 @@ function EmitGameBegin(game) const gameuserconnection = Game.Registrar.GetConnectionByUser(gameuser.uid); // TODO: consider not sending all users the entire game state - // due to cheating + // due to cheating - a few more considerations and maybe a + // getsafegame function is needed io.to(gameuserconnection).emit('game-begin', { game: game, gameuser: gameuser - }) + }); } + + // Let starting player know it's their turn + const userturnstart = Game.Logic.GetTurnUser(game.uid).uid; + const userturnstartconnection = Game.Registrar.GetConnectionByUser(userturnstart); + + io.to(userturnstartconnection).emit('game-your-turn'); }