From 4cc96b201e2e717cd3148953ce3f2c2d1c423502 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 10 May 2021 17:31:10 +0100 Subject: [PATCH] removed the turns debug info and done some more logic --- .gitignore | 1 + client/public/scrabble/network.js | 3 +- server/src/game-logic.js | 63 +++-- server/src/socketserver.js | 9 +- turns-debug.json | 403 ------------------------------ 5 files changed, 57 insertions(+), 422 deletions(-) delete mode 100644 turns-debug.json diff --git a/.gitignore b/.gitignore index 0284493..96b57de 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ client/node_modules/ server/node_modules/ *.log *.env +.turns-debug.json diff --git a/client/public/scrabble/network.js b/client/public/scrabble/network.js index 3b96604..1997bba 100644 --- a/client/public/scrabble/network.js +++ b/client/public/scrabble/network.js @@ -271,7 +271,8 @@ function netSkipTurn() function onTurnError(socket, args) { - console.log(args); + console.log('error', args); + alert('Error in your turn: ' + args.error); } function onturnProcessed(socket, args) diff --git a/server/src/game-logic.js b/server/src/game-logic.js index c34c7e4..592eaab 100644 --- a/server/src/game-logic.js +++ b/server/src/game-logic.js @@ -263,13 +263,33 @@ function PlayTurn(gameuid, playeruid, turn) const game = ActiveGames[gameuid]; const turninfo = gameNextTurn(gameuid); + turn.turn = turninfo.newTurn; + turn.oldboardtiles = ActiveGames[gameuid].gamestates[ActiveGames[gameuid].gamestates.length - 1].boardtiles; + + // generate diff between oldboardtiles and newboardtiles + const diff = turnDiff(turn.oldboardtiles, turn.boardtiles); + if (diff.length === 0) + { + const error = { + error: 'error-game-no-change' + }; + return [error, undefined, undefined, undefined] + } + + // process outcome + + // check if user is allowed to make that move + + // process turn and allocate scores + + // give user new tiles + + turn.boardtiles = turn.oldboardtiles.append(turn.boardtiles); ActiveGames[gameuid].gamestates.push(turn); ActiveGames[gameuid].turn = turninfo.newTurn; - ActiveGames[gameuid].turn = turninfo.newTotalTurn; + ActiveGames[gameuid].turntotal = turninfo.newTotalTurn; - // give user new tiles - - return [turn, turninfo]; + return [undefined, turn, turninfo, {}]; } function SkipTurn(gameuid, playeruid) @@ -287,7 +307,7 @@ function SkipTurn(gameuid, playeruid) ActiveGames[gameuid].gamestates.push(turn); ActiveGames[gameuid].turn = turninfo.newTurn; - ActiveGames[gameuid].turn = turninfo.newTotalTurn; + ActiveGames[gameuid].turntotal = turninfo.newTotalTurn; return [turn, turninfo]; } @@ -298,6 +318,7 @@ function gameNextTurn(gameuid) let newTurn = ActiveGames[gameuid].turn += 1; newTurn = ActiveGames[gameuid].turn % playerCount; const newTotalTurn = ActiveGames[gameuid].turntotal += 1; + return { turnplayer: ActiveGames[gameuid].players[newTurn], newTurn: newTurn, @@ -305,17 +326,6 @@ function gameNextTurn(gameuid) }; } -// returns tuple ([newtileset], [newusertiles]) -function ExchangeTiles(tileset, tilesToExchange) -{ - -} - -function UserLeaveGame(useruid) -{ - -} - // same as how the function EndGame() { @@ -323,6 +333,27 @@ function EndGame() } +// verrryyy naive way of doing it but it returns the difference in tiles between args +function turnDiff(turntilesold, turntilesnew) +{ + let ret = []; + if (turntilesold.length === 0) return turntilesnew; + if (turntilesnew.length === 0) return []; // because there's no new tiles ennit + for (const tile1 of turntilesold) + { + for (const tile2 of turntilesnew) + { + if (JSON.stringify(tile1) === JSON.stringify(tile2)) + continue; + if (ret.includes(tile2) || ret.includes(tile1)) + continue; + ret.push(tile2); + } + } + return ret; +} + + module.exports = { // Game validation exports diff --git a/server/src/socketserver.js b/server/src/socketserver.js index 548a7fa..828f69d 100644 --- a/server/src/socketserver.js +++ b/server/src/socketserver.js @@ -420,9 +420,14 @@ function GamePlayTurn(socket, args) } else { // TODO: validate args - const [outcome, turninfo] = Game.Logic.PlayTurn(game.uid, user.uid, args) + const [err, outcome, turninfo, newuserpieces] = Game.Logic.PlayTurn(game.uid, user.uid, args) - // process errorsq + // process errors + if (err) + { + socket.emit('game-turn-error', err); + return; + } io.to(game.uid).emit('game-turn-processed', { outcome: outcome diff --git a/turns-debug.json b/turns-debug.json deleted file mode 100644 index 74bbead..0000000 --- a/turns-debug.json +++ /dev/null @@ -1,403 +0,0 @@ -{ - "uid": "UQWPWN", - "locale": "en", - "players": [ - { - "uid": "4034e8f531d4ad69", - "name": "ben", - "activetiles": [ - "E", - "R", - "I", - "F", - "I", - "E", - "U" - ], - "score": 0 - } - ], - "turn": 1, - "turntotal": 1, - "gamestates": [ - { - "playeruid": -1, - "turn": 0, - "turntype": "", - "outcome": { - "valid": false - }, - "oldboardtiles": [], - "boardtiles": [] - }, - { - "playeruid": "4034e8f531d4ad69", - "turn": -1, - "turntype": "PLACE", - "outcome": {}, - "oldboardtiles": [], - "boardtiles": [ - { - "pos": { - "x": 5, - "y": 14 - }, - "modifier": "NONE", - "letter": "I", - "score": -1 - }, - { - "pos": { - "x": 6, - "y": 14 - }, - "modifier": "NONE", - "letter": "F", - "score": -1 - }, - { - "pos": { - "x": 7, - "y": 14 - }, - "modifier": "TW", - "letter": "I", - "score": -1 - } - ] - } - ], - "tilebag": [ - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "I", - "I", - "I", - "I", - "I", - "I", - "I", - "O", - "O", - "O", - "O", - "O", - "O", - "O", - "O", - "N", - "N", - "N", - "N", - "N", - "N", - "R", - "R", - "R", - "R", - "R", - "T", - "T", - "T", - "T", - "T", - "T", - "L", - "L", - "L", - "L", - "S", - "S", - "S", - "S", - "U", - "U", - "U", - "D", - "D", - "D", - "D", - "G", - "G", - "G", - "B", - "B", - "C", - "C", - "M", - "M", - "P", - "P", - "F", - "H", - "H", - "V", - "V", - "W", - "W", - "Y", - "Y", - "K", - "J", - "X", - "Q", - "Z", - "_", - "_" - ], - "tileset": [ - "A", - "B", - "C", - "D", - "E", - "F", - "G", - "H", - "I", - "J", - "K", - "L", - "M", - "N", - "O", - "P", - "Q", - "R", - "S", - "T", - "U", - "V", - "W", - "X", - "Y", - "Z" - ] -}{ - "uid": "UQWPWN", - "locale": "en", - "players": [ - { - "uid": "4034e8f531d4ad69", - "name": "ben", - "activetiles": [ - "E", - "R", - "I", - "F", - "I", - "E", - "U" - ], - "score": 0 - } - ], - "turn": 2, - "turntotal": 2, - "gamestates": [ - { - "playeruid": -1, - "turn": 0, - "turntype": "", - "outcome": { - "valid": false - }, - "oldboardtiles": [], - "boardtiles": [] - }, - { - "playeruid": "4034e8f531d4ad69", - "turn": -1, - "turntype": "PLACE", - "outcome": {}, - "oldboardtiles": [], - "boardtiles": [ - { - "pos": { - "x": 5, - "y": 14 - }, - "modifier": "NONE", - "letter": "I", - "score": -1 - }, - { - "pos": { - "x": 6, - "y": 14 - }, - "modifier": "NONE", - "letter": "F", - "score": -1 - }, - { - "pos": { - "x": 7, - "y": 14 - }, - "modifier": "TW", - "letter": "I", - "score": -1 - } - ] - }, - { - "playeruid": "4034e8f531d4ad69", - "turn": -1, - "turntype": "PLACE", - "outcome": {}, - "oldboardtiles": [], - "boardtiles": [ - { - "pos": { - "x": 8, - "y": 14 - }, - "modifier": "NONE", - "letter": "U", - "score": -1 - } - ] - } - ], - "tilebag": [ - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "E", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "A", - "I", - "I", - "I", - "I", - "I", - "I", - "I", - "O", - "O", - "O", - "O", - "O", - "O", - "O", - "O", - "N", - "N", - "N", - "N", - "N", - "N", - "R", - "R", - "R", - "R", - "R", - "T", - "T", - "T", - "T", - "T", - "T", - "L", - "L", - "L", - "L", - "S", - "S", - "S", - "S", - "U", - "U", - "U", - "D", - "D", - "D", - "D", - "G", - "G", - "G", - "B", - "B", - "C", - "C", - "M", - "M", - "P", - "P", - "F", - "H", - "H", - "V", - "V", - "W", - "W", - "Y", - "Y", - "K", - "J", - "X", - "Q", - "Z", - "_", - "_" - ], - "tileset": [ - "A", - "B", - "C", - "D", - "E", - "F", - "G", - "H", - "I", - "J", - "K", - "L", - "M", - "N", - "O", - "P", - "Q", - "R", - "S", - "T", - "U", - "V", - "W", - "X", - "Y", - "Z" - ] -} \ No newline at end of file