Skip to content

Instantly share code, notes, and snippets.

@franknmungai
Created October 25, 2020 11:30
Show Gist options
  • Save franknmungai/f830e4fa946178f0e447bcde1c8c279c to your computer and use it in GitHub Desktop.
Save franknmungai/f830e4fa946178f0e447bcde1c8c279c to your computer and use it in GitHub Desktop.
The complete code snippet for game.js, section 11 on https://stack-chess-tutorial.netlify.app/11-socket.io-server#managing-our-games
const games = {};
class Player {
constructor(name, color, playerID, gameID) {
this.name = name;
this.color = color;
this.playerID = playerID;
this.gameID = gameID;
}
}
const addPlayer = ({ gameID, name, playerID }) => {
if (!games[gameID]) {
const color = Math.random() <= 0.5 ? 'w' : 'b';
const player = new Player(name, color, playerID, gameID);
games[gameID] = [player];
return {
message: 'Joined successfully',
opponent: null,
player,
};
}
if (games[gameID].length >= 2) {
return { error: 'This game is full' };
}
const opponent = games[gameID][0];
const color = opponent.color === 'w' ? 'b' : 'w';
const player = new Player(name, color, playerID, gameID);
games[gameID].push(player);
return {
message: 'Added successfully',
opponent,
player,
};
};
const removePlayer = (playerID) => {
for (const game in games) {
let players = games[game];
const index = players.findIndex((pl) => pl.playerID === playerID);
console.log({ players, index });
if (index !== -1) {
return players.splice(index, 1)[0];
}
}
};
const game = (id) => games[id];
module.exports = {
addPlayer,
game,
removePlayer,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment