/* Returns true if the game is waiting and userid * has not joined, false otherwise */ export function isWaiting(game, userid) { return game.state == "waiting" && game.player1 != userid; } /* Lets userid join the given game, throws an error if * the game cannot be joined. */ export function joinGame(game, userid) { if (!isWaiting(game, userid)) { throw Error("Not waiting!"); } if (game.player1 == undefined) { game.player1 = userid; } else { game.player2 = userid; game.state = "playing"; } } /* Returns the game state ready to be sent to the client. */ export function toJson(game, userid) { // TODO: create a copy of game using structuredClone and // set game.myturn=true if the current player is userid. return game; }