forked from katiejiang/1n-werewolf
-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
code improvementwon't necessarily enhance the functionality, but will make for better codewon't necessarily enhance the functionality, but will make for better code
Description
In the server main file it is sometimes hard to differentiate logs from logic:
Example:
const gameID = newPlayer.gameID;
if (debug>=3) console.log(`Player ${newPlayer.name} (${newID}) initially voted for ${newPlayer.vote}`);
const players = Players.find({ gameID: gameID, session: {$ne: null}, alive: true }, { fields: {name:1, vote:1} }).fetch();
if (players.some (p => !p.vote)) return null;
const game = Games.findOne(gameID);
if (debug>=1) {
console.log(`Game ${game.name} ${game.state}: all ${players.length} players voted`);
for (const player of players) {
if (player.vote == "0") {
console.log(` Player ${player.name} (${player._id}) did not vote (${player.vote})`);
} else {
const vote = players.find (p => p._id === player.vote);
if (vote)
console.log(` Player ${player.name} (${player._id}) voted for ${vote.name} (${player.vote})`);
else
console.log(` Player ${player.name} (${player._id}) invalid vote for ${player.vote}`);
}
}
}
if (game.state == "nightTime") {
dawn (game, players);
Players.update({gameID: gameID, session: {$ne: null}}, {$set: {vote: null}}, {multi: true});
}From: https://github.com/timadye/werewolf/blob/master/server/main.js#L107-L133
The interesting code is:
const gameID = newPlayer.gameID;
const players = Players.find({ gameID: gameID, session: {$ne: null}, alive: true }, { fields: {name:1, vote:1} }).fetch();
if (players.some (p => !p.vote)) return null;
const game = Games.findOne(gameID);
if (game.state == "nightTime") {
dawn (game, players);
Players.update({gameID: gameID, session: {$ne: null}}, {$set: {vote: null}}, {multi: true});
}Some potential changes:
- Extract Things like LogDebug, LogInfo which have the debug if checks included
- For larger logs things extract entire methods for them e.g.
function logVotesResult(game, players) {
// omitted for brevity
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
code improvementwon't necessarily enhance the functionality, but will make for better codewon't necessarily enhance the functionality, but will make for better code