From edda3fc6d84e32ee427aac0850650c2a192e41de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Jakse?= Date: Sat, 25 Apr 2020 23:16:34 +0200 Subject: [PATCH] Linting --- trivabble-server.js | 74 +++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/trivabble-server.js b/trivabble-server.js index 85e2370..6a56e60 100644 --- a/trivabble-server.js +++ b/trivabble-server.js @@ -492,13 +492,15 @@ function joinGame(gameNumber) { return {gameNumber, game}; } -function handleCommand(cmd, gameNumber, playerName, response) { - let game; +function handleCommand(cmdNumber, message, response) { + const {gameNumber, game} = joinGame(message.gameNumber); - ({gameNumber, game} = joinGame(gameNumber)); game.lastUpdated = new Date(); + const playerName = message.playerName; + let rack = null; + const cmd = message.cmds[cmdNumber]; switch (cmd.cmd) { case "sync": // DEPRECATED. Here for old clients. @@ -553,7 +555,7 @@ function handleCommand(cmd, gameNumber, playerName, response) { } letter = rack[cmd.indexFrom]; - operations.push(() => {rack[cmd.indexFrom] = ""}); + operations.push(() => {rack[cmd.indexFrom] = "";}); break; case "board": @@ -572,7 +574,7 @@ function handleCommand(cmd, gameNumber, playerName, response) { return false; } - operations.push(() => {letter = game.bagPopLetter(playerName)}); + operations.push(() => {letter = game.bagPopLetter(playerName);}); break; default: @@ -594,7 +596,7 @@ function handleCommand(cmd, gameNumber, playerName, response) { return false; } - operations.push(() => {rack[cmd.indexTo] = letter}); + operations.push(() => {rack[cmd.indexTo] = letter;}); break; case "board": @@ -652,10 +654,10 @@ function handleCommand(cmd, gameNumber, playerName, response) { rack = game.getPlayerRack(playerName); - let oldRackSorted = rack.filter((l) => l); + const oldRackSorted = rack.filter((l) => l); oldRackSorted.sort(); - let newRackSorted = cmd.rack.filter((l) => l); + const newRackSorted = cmd.rack.filter((l) => l); newRackSorted.sort(); for (let i = 0; i < 7; i++) { @@ -708,40 +710,40 @@ function handleCommand(cmd, gameNumber, playerName, response) { return true; } -function handleCommands(cmds, responseAndType) { - if (!cmds.cmds || !cmds.cmds.length) { - const {gameNumber, game} = joinGame(cmds.gameNumber); +function handleCommands(message, responseAndType) { + if (!message.cmds || !message.cmds.length) { + const {gameNumber, game} = joinGame(message.gameNumber); writeMessage(responseAndType, JSON.stringify({ - "playerName": cmds.playerName, + "playerName": message.playerName, "currentPlayer": game.currentPlayer, "gameNumber": gameNumber, "letterValues": game.letterValues, - "rack": game.getPlayerRack(cmds.playerName), + "rack": game.getPlayerRack(message.playerName), "board": game.board, "remainingLetters": game.bag.length }) ); - game.addListeningPlayer(cmds.playerName, responseAndType); + game.addListeningPlayer(message.playerName, responseAndType); return; } - let message = ""; + let wsMessage = ""; const response = ( responseAndType[1] === REQUEST_TYPE_WEBSOCKET ? { write: function (s) { - message += s; + wsMessage += s; }, end: function (s) { if (s) { - message += s; + wsMessage += s; } - webSocketWrite(responseAndType[0], message); + webSocketWrite(responseAndType[0], wsMessage); } } : responseAndType[0] @@ -749,20 +751,20 @@ function handleCommands(cmds, responseAndType) { response.write("["); - for (let i = 0; i < cmds.cmds.length; i++) { + for (let i = 0; i < message.cmds.length; i++) { if (i) { response.write(","); } - if (!handleCommand(cmds.cmds[i], cmds.gameNumber, cmds.playerName, response)) { + if (!handleCommand(i, message, response)) { break; } } response.end("]"); - if (games[cmds.gameNumber]) { - games[cmds.gameNumber].commit(); + if (games[message.gameNumber]) { + games[message.gameNumber].commit(); } } @@ -1066,11 +1068,11 @@ fs.readFile("games.backup.json", function (err, data) { return; } - let cmds; + let message; if (webSocketMatch) { try { - cmds = JSON.parse(decodedData); + message = JSON.parse(decodedData); } catch (e) { socket.end("HTTP/1.1 400 Bad Request"); console.error("WS: FAILED - Could not parse the given data " + decodedData); @@ -1119,21 +1121,21 @@ fs.readFile("games.backup.json", function (err, data) { } while (true) { - const message = webSocketGetMessage(buffer); - if (message && message[1]) { - if (message[0]) { - debuglog("WS message: " + message[0]); + const wsMessage = webSocketGetMessage(buffer); + if (wsMessage && wsMessage[1]) { + if (wsMessage[0]) { + debuglog("WS message: " + wsMessage[0]); try { - cmds.cmds = JSON.parse(message[0]); + message.cmds = JSON.parse(wsMessage[0]); } catch (e) { writeMessage(responseAndType, '{"error":1, "reason": "Unable to parse your JSON data"}'); return; } - handleCommands(cmds, responseAndType); - cmds.cmds = null; + handleCommands(message, responseAndType); + message.cmds = null; } - } else if (message === null) { + } else if (wsMessage === null) { if (responseAndType[0] && !socket.isDestroyed) { responseAndType[0] = null; socket.end(webSocketCloseBuffer); @@ -1142,20 +1144,20 @@ fs.readFile("games.backup.json", function (err, data) { return; } - if (message[1] === buffer.length) { + if (wsMessage[1] === buffer.length) { break; } - if (message[1] === 0) { + if (wsMessage[1] === 0) { receivedBytes = buffer; return; } - buffer = buffer.slice(message[1]); + buffer = buffer.slice(wsMessage[1]); } }); - handleCommands(cmds, responseAndType); + handleCommands(message, responseAndType); }); server.on("error", function (error) { -- 2.30.2