return {gameNumber, game};
}
+function reply(message, response, cmdNumber, r) {
+ response.write(
+ JSON.stringify({...r, id: message.id || message.cmds[cmdNumber].id})
+ );
+}
+
function handleCommand(cmdNumber, message, response) {
const {gameNumber, game} = joinGame(message.gameNumber);
switch (cmd.cmd) {
case "sync": // DEPRECATED. Here for old clients.
case "joinGame": {
- response.write(
- JSON.stringify({
- "error": 0,
- "gameNumber": gameNumber,
- "playerName": playerName,
- "currentPlayer": game.currentPlayer,
- "rack": game.getPlayerRack(playerName),
- "board": game.board,
- "remainingLetters": game.bag.length,
- "letterValues": frValues
- })
- );
+ reply(message, response, cmdNumber, {
+ error: 0,
+ gameNumber: gameNumber,
+ playerName: playerName,
+ currentPlayer: game.currentPlayer,
+ rack: game.getPlayerRack(playerName),
+ board: game.board,
+ remainingLetters: game.bag.length,
+ letterValues: frValues
+ });
break;
}
case "score": {
game.setPlayerScore(cmd.player, cmd.score);
- response.write('{"error":0}');
+ reply(message, response, cmdNumber, {error: 0});
break;
}
case "currentPlayer": {
game.setCurrentPlayer(cmd.player);
- response.write('{"error":0}');
+ reply(message, response, cmdNumber, {error: 0});
break;
}
rack = game.getPlayerRack(playerName);
if (cmd.indexFrom > 6 || cmd.indexFrom < 0) {
- response.write("{\"error\":1, \"reason\":\"Wrong indexFrom\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexFrom"});
return false;
}
case "board":
if (cmd.indexFrom < 0 || cmd.indexFrom >= 15 * 15) {
- response.write("{\"error\":1, \"reason\":\"Wrong indexFrom\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexFrom"});
return false;
}
case "bag":
if (!game.bag.length) {
- response.write("{\"error\":1, \"reason\":\"Empty bag\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Empty bag"});
return false;
}
break;
default:
- response.write("{\"error\":1, \"reason\":\"Moving letter from an unknown place\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Moving letter from an unknown place"});
return false;
}
switch (cmd.to) {
case "rack":
if (cmd.indexTo < 0 || cmd.indexTo > 6) {
- response.write("{\"error\":1, \"reason\":\"Wrong indexTo\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexTo"});
return false;
}
rack = rack || game.getPlayerRack(playerName);
if (rack[cmd.indexTo]) {
- response.write("{\"error\":1, \"reason\":\"Moving a tile to a non-empty location\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Moving a tile to a non-empty location"});
return false;
}
case "board":
if (cmd.indexTo < 0 || cmd.indexTo >= 15 * 15) {
- response.write("{\"error\":1, \"reason\":\"Wrong indexTo\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Wrong indexTo"});
return false;
}
operation();
}
- response.write(
- JSON.stringify({
- error: 0,
- rack: (cmd.from === "bag" && cmd.to === "rack") ? rack : undefined, // eslint-disable-line no-undefined
- remainingLetters: game.bag.length
- })
- );
+ reply(message, response, cmdNumber, {
+ error: 0,
+ rack: (
+ (cmd.from === "bag" && cmd.to === "rack")
+ ? rack
+ : undefined // eslint-disable-line no-undefined
+ ),
+ remainingLetters: game.bag.length
+ });
if (rack) {
game.pendingEvents.push({
case "setRack": {
if (cmd.rack.length > 7) {
- response.write(
- JSON.stringify({
- error: 1,
- rack: rack,
- reason: "the new rack is not at the right size"
- })
- );
+ reply(message, response, cmdNumber, {
+ error: 1,
+ rack: rack,
+ reason: "the new rack is not at the right size"
+ });
return false;
}
for (let i = 0; i < 7; i++) {
if ((oldRackSorted[i] !== newRackSorted[i]) && (oldRackSorted[i] || newRackSorted[i])) {
- response.write(
- JSON.stringify({
- error: 1,
- rack: rack,
- reason: "the new rack doesn't contain the same number of letters"
- })
- );
+ reply(message, response, cmdNumber, {
+ error: 1,
+ rack: rack,
+ reason: "the new rack doesn't contain the same number of letters"
+ });
return false;
}
}
rack[i] = cmd.rack[i];
}
- response.write('{"error": 0}');
+ reply(message, response, cmdNumber, {error: 0});
break;
}
case "resetGame": {
game.reset();
- response.write("{\"error\":0}");
+ reply(message, response, cmdNumber, {error: 0});
break;
}
}
});
- response.write("{\"error\":0}");
+ reply(message, response, cmdNumber, {error: 0});
break;
}
default: {
- response.write("{\"error\":1,\"reason\":\"Unknown command\"}");
+ reply(message, response, cmdNumber, {error: 1, reason: "Unknown command"});
return false;
}
}
writeMessage(responseAndType,
JSON.stringify({
- "playerName": message.playerName,
- "currentPlayer": game.currentPlayer,
- "gameNumber": gameNumber,
- "letterValues": game.letterValues,
- "rack": game.getPlayerRack(message.playerName),
- "board": game.board,
- "remainingLetters": game.bag.length
+ playerName: message.playerName,
+ currentPlayer: game.currentPlayer,
+ gameNumber: gameNumber,
+ letterValues: game.letterValues,
+ rack: game.getPlayerRack(message.playerName),
+ board: game.board,
+ remainingLetters: game.bag.length
})
);