From 66edbbf51e1495a56779dc157910df6c1cf398ca Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 5 May 2021 02:38:17 +0100 Subject: [PATCH] proper game start logic --- client/public/scrabble/network.js | 2 +- server/src/game-logic.js | 52 +++++++++++++++++++++++++------ server/src/game-registrar.js | 9 ++++++ server/src/socketserver.js | 22 +++++++------ 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index d78410d..a2b3b21 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -116,7 +116,7 @@ function onGameBegin(socket, args) return; } - + console.log(args); } diff --git a/server/src/game-logic.js b/server/src/game-logic.js index 83b37c5..6defbdd 100644 --- a/server/src/game-logic.js +++ b/server/src/game-logic.js @@ -73,7 +73,7 @@ let ActiveGames = []; function BeginGame(lobby) { - // game uses the owners language + // game uses the owners language - assumes it's valid const gameowner = Registrar.GetUserByUID(lobby.owneruid); let tilebag = Dist.GenerateStartStateDistribution(gameowner.locale); @@ -101,11 +101,22 @@ function BeginGame(lobby) } } + const gamestate = { + playeruid: -1, + turn: 0, + outcome: { + valid: false + }, + oldboardtiles: [], + boardtiles: [] + }; + ActiveGames[lobby.uid] = { uid: lobby.uid, locale: gameowner.locale, players: players, turn: 0, + gamestates: [gamestate], tilebag: tilebag, tileset: Dist.GetTileSet(gameowner.locale) }; @@ -114,9 +125,38 @@ function BeginGame(lobby) } /* -TURN OBJECT +TURN OBJECT - Un-filled in GAMESTATE object { - + // UID of the player that played the turn + playeruid: uid, + turn: int, + // Generated after turn is processed + outcome: { + valid: bool, + points: pointsgained, + words: [{ + word: word, + points: points, + tiles: [{ + pos: {x: x, y: y}, + modifier: modifier, + letter: letter, + score: int + }] + }], + } + oldboardtiles: [{ + pos: {x: x, y: y}, + modifier: modifier, + letter: letter, + score: int + }] + boardtiles: [{ + pos: {x: x, y: y}, + modifier: modifier, + letter: letter, + score: int + }] } NOTES - Turns are handled a little weird, client sends turn on turn end and @@ -135,12 +175,6 @@ function ExchangeTiles(tileset, tilesToExchange) } -// does not alter tileset -function SelectTilesFromBag(tileset, num) -{ - -} - module.exports = { // Game validation exports diff --git a/server/src/game-registrar.js b/server/src/game-registrar.js index 8f52fe7..75806f4 100644 --- a/server/src/game-registrar.js +++ b/server/src/game-registrar.js @@ -160,6 +160,14 @@ function GetUserbyConnection(connectionid) return false; } +function GetConnectionByUser(useruid) +{ + for (const user in OnlineUsers) + if (OnlineUsers[user].uid === useruid && OnlineUsers[user].state === 'CONNECTED') + return OnlineUsers[user].connectionid; + return false; +} + // TODO: User intent function UserConnect(useruid, connectionid, intent) { @@ -209,6 +217,7 @@ module.exports = { // Get user connection exports GetUserbyConnection: GetUserbyConnection, + GetConnectionByUser: GetConnectionByUser, // Change user connection state exports UserConnect: UserConnect, diff --git a/server/src/socketserver.js b/server/src/socketserver.js index ab0d4a4..dd0f1bc 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -416,14 +416,18 @@ function LobbyUpdateCallback(user, lobby, state) // send the client their user as well as the rest of the game function EmitGameBegin(game) { - // TODO: consider not sending all users the entire game state - // due to cheating - io.to(game.uid).emit('game-begin', { - game: game - }); + // Instead of using io.to(room), i'm sending an individual packet to everyone + // in the game so that i can customise the game object that they recieve + for (const user of game.players) + { + const gameuser = game.players.filter(i => i.uid === user.uid)[0]; + const gameuserconnection = Game.Registrar.GetConnectionByUser(gameuser.uid); - // for (const user of game.players) - // { - // const gameuser = game.players.filter(i => i.uid === user.uid)[0]; - // } + // TODO: consider not sending all users the entire game state + // due to cheating + io.to(gameuserconnection).emit('game-begin', { + game: game, + gameuser: gameuser + }) + } }