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.
}
letter = rack[cmd.indexFrom];
- operations.push(() => {rack[cmd.indexFrom] = ""});
+ operations.push(() => {rack[cmd.indexFrom] = "";});
break;
case "board":
return false;
}
- operations.push(() => {letter = game.bagPopLetter(playerName)});
+ operations.push(() => {letter = game.bagPopLetter(playerName);});
break;
default:
return false;
}
- operations.push(() => {rack[cmd.indexTo] = letter});
+ operations.push(() => {rack[cmd.indexTo] = letter;});
break;
case "board":
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++) {
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]
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();
}
}
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);
}
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);
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) {