0
0
Fork 0

Update logs to use Game's logger when possible instead of Global.

This commit is contained in:
Oliver-Akins 2021-01-02 13:32:05 -07:00
parent 93de617d83
commit 5345c97f33
10 changed files with 44 additions and 39 deletions

View file

@ -21,7 +21,7 @@ export default (io: Server, socket: Socket, data: DeleteGame) => {
let player = game.players.find(x => x.isHost); let player = game.players.find(x => x.isHost);
if (player != null && player.socket !== socket) { if (player != null && player.socket !== socket) {
log.warn(`${player.name} attempted to delete game ${game.id}.`); game.log.warn(`${player.name} attempted to delete game.`);
socket.emit(`GameDeleted`, { socket.emit(`GameDeleted`, {
status: 403, status: 403,
message: `Not allowed to delete a game that you are not the host of.`, message: `Not allowed to delete a game that you are not the host of.`,
@ -31,6 +31,7 @@ export default (io: Server, socket: Socket, data: DeleteGame) => {
}; };
// Delete game // Delete game
game.log.debug(`Game deleted.`)
delete games[data.game_code]; delete games[data.game_code];
io.to(game.id).emit(`GameDeleted`, { status: 200 }); io.to(game.id).emit(`GameDeleted`, { status: 200 });
} }

View file

@ -6,7 +6,7 @@ export default (io: Server, socket: Socket, data: GetPastQuestions) => {
// Assert game exists // Assert game exists
if (!games[data.game_code]) { if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`); log.debug(`Can't get questions game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 404, status: 404,
message: `Game with code ${data.game_code} could not be found`, message: `Game with code ${data.game_code} could not be found`,
@ -17,7 +17,7 @@ export default (io: Server, socket: Socket, data: GetPastQuestions) => {
let game = games[data.game_code]; let game = games[data.game_code];
let team = game.teams[data.team - 1]; let team = game.teams[data.team - 1];
log.silly(`Past questions retrieved for team ${data.team} (gID=${game.id})`); game.log.silly(`Past questions retrieved for team ${data.team}`);
socket.emit(`PastQuestions`, { socket.emit(`PastQuestions`, {
questions: team.questions questions: team.questions
}); });

View file

@ -15,7 +15,6 @@ export default (io: Server, socket: Socket, data: JoinGame) => {
}); });
return; return;
}; };
let game = games[data.game_code]; let game = games[data.game_code];
@ -23,6 +22,7 @@ export default (io: Server, socket: Socket, data: JoinGame) => {
let sameName = game.players.find(x => x.name == data.name); let sameName = game.players.find(x => x.name == data.name);
if (sameName != null) { if (sameName != null) {
if (!game.ingame) { if (!game.ingame) {
game.log.info(`Client attempted to connect using name already in use.`);
socket.emit(`GameJoined`, { socket.emit(`GameJoined`, {
status: 400, status: 400,
message: `A player already has that name in the game.`, message: `A player already has that name in the game.`,
@ -34,9 +34,11 @@ export default (io: Server, socket: Socket, data: JoinGame) => {
// Player has the same name but is allowed to rejoin if they // Player has the same name but is allowed to rejoin if they
// disconnect in the middle of the game // disconnect in the middle of the game
if (!sameName.socket.connected) { if (!sameName.socket.connected) {
game.log.info(`Player Reconnected to the game (name=${data.name})`);
socket.emit(`GameRejoined`, { status: 200 }); socket.emit(`GameRejoined`, { status: 200 });
return; return;
} else { } else {
game.log.debug(`${socket.id} attempted to claim ${sameName.socket.id}'s game spot.`);
socket.emit(`GameJoined`, { socket.emit(`GameJoined`, {
status: 403, status: 403,
message: `Can't connect to an already connected client`, message: `Can't connect to an already connected client`,
@ -49,7 +51,7 @@ export default (io: Server, socket: Socket, data: JoinGame) => {
// Assert game is not in-progess // Assert game is not in-progess
if (game.ingame) { if (game.ingame) {
log.debug(`${data.name} tried to connect to gID:${game.id} in the middle of a game.`); game.log.debug(`${data.name} tried to connect in the middle of a game.`);
socket.emit(`GameJoined`, { socket.emit(`GameJoined`, {
status: 403, status: 403,
message: `Cannot connect to a game that's in progress.`, message: `Cannot connect to a game that's in progress.`,
@ -61,7 +63,7 @@ export default (io: Server, socket: Socket, data: JoinGame) => {
let player = new Player(data.name, socket); let player = new Player(data.name, socket);
game.players.push(player); game.players.push(player);
log.debug(`${data.name} joined gID:${game.id}`); game.log.debug(`${data.name} joined the game`);
socket.join(game.id); socket.join(game.id);
socket.emit(`GameJoined`, { socket.emit(`GameJoined`, {
status: 200, status: 200,

View file

@ -19,7 +19,7 @@ export default (io: Server, socket: Socket, data: LeaveGame) => {
// Ensure it's not the host trying to leave so that the game can // Ensure it's not the host trying to leave so that the game can
// actually start // actually start
if (game.host.socket == socket) { if (game.host.socket == socket) {
log.debug(`Host `) game.log.debug(`Host attempted to leave game. (name=${game.host.name})`);
socket.emit(`GameLeft`, { socket.emit(`GameLeft`, {
status: 303, status: 303,
message: `Can't leave the game as the host, use "DeleteGame".`, message: `Can't leave the game as the host, use "DeleteGame".`,
@ -41,11 +41,11 @@ export default (io: Server, socket: Socket, data: LeaveGame) => {
switch (role) { switch (role) {
case "guesser": case "guesser":
log.silly(`Removed ${player.name} from guesser role.`); game.log.silly(`Removed ${player.name} from guesser role.`);
team.guessers = team.guessers.filter(p => p.socket !== socket); team.guessers = team.guessers.filter(p => p.socket !== socket);
break; break;
case "writer": case "writer":
log.silly(`Removed ${player.name} from writer role.`); game.log.silly(`Removed ${player.name} from writer role.`);
team.writer = null; team.writer = null;
break; break;
}; };
@ -54,7 +54,7 @@ export default (io: Server, socket: Socket, data: LeaveGame) => {
game.players = game.players.filter(p => p.socket != socket); game.players = game.players.filter(p => p.socket != socket);
log.debug(`${player.name} left gID:${game.id}`); game.log.debug(`${player.name} left the game.`);
socket.to(game.id).emit(`PlayerUpdate`, { socket.to(game.id).emit(`PlayerUpdate`, {
status: 200, status: 200,
action: `remove`, action: `remove`,

View file

@ -23,12 +23,12 @@ export default (io: Server, socket: Socket, data: NewHand) => {
for (var card of team.hand) { for (var card of team.hand) {
deck.discard(card); deck.discard(card);
team.removeCard(card); team.removeCard(card);
log.silly(`[${game.id}] Removing card: '${card}' from team ${data.team}'s hand.`); game.log.silly(`Removing card: '${card}' from team ${data.team}'s hand.`);
}; };
// Add the questions and then alert the game clients about the changes // Add the questions and then alert the game clients about the changes
team.addCardsToHand(deck.draw(conf.game.hand_size)); team.addCardsToHand(deck.draw(conf.game.hand_size));
log.silly(`[${game.id}] Drew a new hand of cards for team ${data.team}.`); game.log.silly(`Drew a new hand of cards for team ${data.team}.`);
io.to(game.id).emit(`UpdateHand`, { io.to(game.id).emit(`UpdateHand`, {
mode: `replace`, mode: `replace`,
questions: team.hand, questions: team.hand,

View file

@ -6,7 +6,7 @@ export default (io: Server, socket: Socket, data: ObjectList) => {
// Assert game exists // Assert game exists
if (!games[data.game_code]) { if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`); log.debug(`Can't get objects for game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 404, status: 404,
message: `Game with code ${data.game_code} could not be found`, message: `Game with code ${data.game_code} could not be found`,
@ -16,7 +16,7 @@ export default (io: Server, socket: Socket, data: ObjectList) => {
}; };
let game = games[data.game_code]; let game = games[data.game_code];
log.silly(`[${game.id}] Sent client object card.`); game.log.silly(`Sent client object card`);
socket.emit(`ObjectList`, { socket.emit(`ObjectList`, {
objects: game.objects objects: game.objects
}); });

View file

@ -6,7 +6,7 @@ export default (io: Server, socket: Socket, data: SelectObject) => {
// Assert game exists // Assert game exists
if (!games[data.game_code]) { if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`); log.debug(`Can't choose an object for a game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 404, status: 404,
message: `Game with code ${data.game_code} could not be found`, message: `Game with code ${data.game_code} could not be found`,
@ -18,7 +18,7 @@ export default (io: Server, socket: Socket, data: SelectObject) => {
// Assert that the object is actually a valid choice // Assert that the object is actually a valid choice
if (!game.objects.includes(data.object)) { if (!game.objects.includes(data.object)) {
log.warn(`[${game.id}] Someone tried selecting an object that doesn't exist: ${data.object}`); game.log.warn(`Someone tried selecting an object that doesn't exist: ${data.object}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 409, status: 409,
message: `That object isn't on the card.`, message: `That object isn't on the card.`,
@ -27,7 +27,7 @@ export default (io: Server, socket: Socket, data: SelectObject) => {
return; return;
}; };
log.debug(`[${game.id}] Object has been chosen: ${data.object}`); game.log.debug(`Object has been chosen: ${data.object}`);
game.object = data.object; game.object = data.object;
io.to(game.id).emit(`ChosenObject`, { io.to(game.id).emit(`ChosenObject`, {
object: data.object object: data.object

View file

@ -6,7 +6,7 @@ export default (io: Server, socket: Socket, data: SendCard) => {
// Assert game exists // Assert game exists
if (!games[data.game_code]) { if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`); log.debug(`Can't send a card in a game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 404, status: 404,
message: `Game with code ${data.game_code} could not be found`, message: `Game with code ${data.game_code} could not be found`,
@ -20,7 +20,7 @@ export default (io: Server, socket: Socket, data: SendCard) => {
// The writer is answering // The writer is answering
if (data.from === "writer") { if (data.from === "writer") {
log.debug(`[${game.id}] Writer selected question to answer.`); game.log.debug(` Writer selected question to answer.`);
deck.discard(data.text); deck.discard(data.text);
team.selectQuestion(data.text); team.selectQuestion(data.text);
@ -33,7 +33,7 @@ export default (io: Server, socket: Socket, data: SendCard) => {
// The writer is sending the card to the writer // The writer is sending the card to the writer
else if (data.from === "guesser") { else if (data.from === "guesser") {
log.debug(`[${game.id}] Guesser is sending the card to the writer.`); game.log.debug(`Guesser is sending the card to the writer.`);
// Update the team's hand // Update the team's hand
team.removeCard(data.text); team.removeCard(data.text);
@ -54,7 +54,7 @@ export default (io: Server, socket: Socket, data: SendCard) => {
} }
else { else {
log.warn(`[${game.id}] Unknown role in the "from" property: ${data.from}`); game.log.warn(`Unknown role in the "from" property: ${data.from}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 400, status: 400,
message: `Unknown role in the "from" property: ${data.from}`, message: `Unknown role in the "from" property: ${data.from}`,

View file

@ -6,7 +6,7 @@ export default (io: Server, socket: Socket, data: UpdateAnswer) => {
// Assert game exists // Assert game exists
if (!games[data.game_code]) { if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`); log.debug(`Can't update answer in a game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`Error`, {
status: 404, status: 404,
message: `Game with code ${data.game_code} could not be found`, message: `Game with code ${data.game_code} could not be found`,

View file

@ -18,11 +18,9 @@ export default (io: Server, socket: Socket, data: UpdatePlayer) => {
// Execute the corresponding action code // Execute the corresponding action code
switch (data.action) { switch (data.action) {
case "modify": case "modify":
log.debug(`Modifying a player. (gID=${data.game_code},name=${data.name})`);
modifyPlayer(io, socket, data); modifyPlayer(io, socket, data);
break; break;
case "remove": case "remove":
log.debug(`Removing a player. (gID=${data.game_code},name=${data.name})`);
removePlayer(io, socket, data); removePlayer(io, socket, data);
break; break;
default: default:
@ -49,7 +47,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Ensure that the player was found correctly so it is not undefined // Ensure that the player was found correctly so it is not undefined
if (player == null) { if (player == null) {
log.debug(`Can't modify a player that doesn't exist. (name=${data.name},gID=${game.id})`); game.log.debug(`Can't modify a player that doesn't exist. (name=${data.name})`);
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 404, status: 404,
message: `Cannot find player with the name: ${data.name}`, message: `Cannot find player with the name: ${data.name}`,
@ -60,7 +58,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Assert the player is modifying themselves // Assert the player is modifying themselves
if (player.socket !== socket) { if (player.socket !== socket) {
log.debug(`${socket.id} is trying to modify a different player: ${data.name} (gID=${game.id})`); game.log.debug(`${socket.id} is trying to modify a different player: ${data.name}`);
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `Cannot modify other players`, message: `Cannot modify other players`,
@ -70,7 +68,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
}; };
if (!data.to) { if (!data.to) {
log.silly(`Client did not include a "to" object in request.`) game.log.silly(`Client did not include a "to" object in request.`)
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 400, status: 400,
message: `The "to" property must to be specified in the request.`, message: `The "to" property must to be specified in the request.`,
@ -81,26 +79,29 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// The player is joining a team for the first time // The player is joining a team for the first time
if (!data.from) { if (!data.from) {
log.silly(`Client included a "to" but not a "from" property.`); game.log.silly(`Client included a "to" but not a "from" property`);
let team = game.teams[data.to.team - 1]; let team = game.teams[data.to.team - 1];
switch (data.to.role) { switch (data.to.role) {
case "guesser": case "guesser":
if (team.guessers.length >= 7) { if (team.guessers.length >= 7) {
game.log.debug(`Game cannot have more than 7 guessers`);
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `A team can't have 8 or more ${conf.game.guesser_name}`, message: `A team can't have 8 or more ${conf.game.guesser_name}`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
}); });
return; return;
} };
team.guessers.push(player); team.guessers.push(player);
game.log.silly(`${player.name} became a guesser`);
// Move the rooms the player is in // Move the rooms the player is in
player.socket.join(`${game.id}:${data.to.team}:guesser`) player.socket.join(`${game.id}:${data.to.team}:guesser`)
break; break;
case "writer": case "writer":
if (team.writer) { if (team.writer) {
game.log.debug(`Game cannot have more than 1 writer`);
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `Someone on that team is already the ${conf.game.writer_name}`, message: `Someone on that team is already the ${conf.game.writer_name}`,
@ -110,6 +111,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
}; };
// Change team object // Change team object
team.writer = player; team.writer = player;
game.log.silly(`${player.name} became the writer`);
// Move the rooms the player is in // Move the rooms the player is in
player.socket.join(`${game.id}:${data.to.team}:writer`); player.socket.join(`${game.id}:${data.to.team}:writer`);
@ -119,7 +121,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Check if the player is just swapping roles on the same team // Check if the player is just swapping roles on the same team
else if (data.from.team === data.to.team) { else if (data.from.team === data.to.team) {
log.silly(`Client provided "to" and "from" objects for the same team.`) game.log.silly(`Client provided "to" and "from" objects for the same team.`)
let team = game.teams[data.to.team - 1]; let team = game.teams[data.to.team - 1];
switch (data.to.role) { switch (data.to.role) {
case "guesser": case "guesser":
@ -160,7 +162,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// The player is swapping roles and teams // The player is swapping roles and teams
else { else {
log.silly(`Client provided both "to" and "from" for different teams.`); game.log.silly(`Client provided both "to" and "from" for different teams.`);
let oldTeam = game.teams[data.from.team - 1]; let oldTeam = game.teams[data.from.team - 1];
let newTeam = game.teams[data.to.team - 1]; let newTeam = game.teams[data.to.team - 1];
@ -232,7 +234,7 @@ const removePlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Ensure that the player was found correctly so it is not undefined // Ensure that the player was found correctly so it is not undefined
if (player == null) { if (player == null) {
log.debug(`Can't delete a player that doesn't exist. (name=${data.name},gID=${game.id})`); game.log.debug(`Can't delete a player that doesn't exist. (name=${data.name})`);
socket.emit(`PlayerUpdate`, { socket.emit(`PlayerUpdate`, {
status: 404, status: 404,
message: `Cannot find player with the name: ${data.name}`, message: `Cannot find player with the name: ${data.name}`,
@ -256,16 +258,16 @@ const removePlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
for (var team of game.teams) { for (var team of game.teams) {
if (team.writer == player) { if (team.writer == player) {
team.writer = null; team.writer = null;
log.silly(`Removed ${player.name} from the writer role.`); game.log.silly(`Removed ${player.name} from the writer role.`);
} else if (team.guessers.includes(player)) { } else if (team.guessers.includes(player)) {
team.guessers = team.guessers.filter(x => x !== player); team.guessers = team.guessers.filter(x => x !== player);
log.silly(`Removed ${player.name} from the guesser role`); game.log.silly(`Removed ${player.name} from the guesser role`);
}; };
}; };
game.players = game.players.filter(x => x !== player); game.players = game.players.filter(x => x !== player);
log.info(`${host.name} kicked ${player.name} from game ${game.id}`); game.log.info(`${host.name} kicked ${player.name} from game`);
io.to(game.id).emit(`PlayerUpdate`, { io.to(game.id).emit(`PlayerUpdate`, {
action: "remove", action: "remove",
name: player.name, name: player.name,