0
0
Fork 0

Update error events

This commit is contained in:
Oliver-Akins 2021-01-01 16:01:02 -07:00
parent 958d8a1382
commit 06aabcf74e

View file

@ -6,8 +6,8 @@ export default (io: Server, socket: Socket, data: UpdatePlayer) => {
// 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 modify player in a game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
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`,
source: `UpdatePlayer` source: `UpdatePlayer`
@ -26,7 +26,7 @@ export default (io: Server, socket: Socket, data: UpdatePlayer) => {
removePlayer(io, socket, data); removePlayer(io, socket, data);
break; break;
default: default:
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
status: 400, status: 400,
message: `Unknown player action: ${data.action}`, message: `Unknown player action: ${data.action}`,
source: `UpdatePlayer`, source: `UpdatePlayer`,
@ -34,7 +34,7 @@ export default (io: Server, socket: Socket, data: UpdatePlayer) => {
}; };
} }
catch (err) { catch (err) {
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
status: 500, status: 500,
message: `${err.name}: ${err.message}`, message: `${err.name}: ${err.message}`,
source: `UpdatePlayer`, source: `UpdatePlayer`,
@ -50,7 +50,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})`); log.debug(`Can't modify a player that doesn't exist. (name=${data.name},gID=${game.id})`);
socket.emit(`Error`, { 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}`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
@ -61,7 +61,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})`); log.debug(`${socket.id} is trying to modify a different player: ${data.name} (gID=${game.id})`);
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `Cannot modify other players`, message: `Cannot modify other players`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
@ -124,7 +124,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
switch (data.to.role) { switch (data.to.role) {
case "guesser": case "guesser":
if (team.guessers.length >= 7) { if (team.guessers.length >= 7) {
socket.emit(`Error`, { 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`
@ -140,7 +140,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
break; break;
case "writer": case "writer":
if (team.writer) { if (team.writer) {
socket.emit(`Error`, { 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}`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
@ -170,7 +170,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Ensure we don't get 8 guessers // Ensure we don't get 8 guessers
if (newTeam.guessers.length >= 7) { if (newTeam.guessers.length >= 7) {
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `Cannot have 8 players as ${conf.game.guesser_name}s on a single team.`, message: `Cannot have 8 players as ${conf.game.guesser_name}s on a single team.`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
@ -186,7 +186,7 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Ensure we don't already have a writer // Ensure we don't already have a writer
if (newTeam.writer) { if (newTeam.writer) {
socket.emit(`Error`, { 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}`,
source: `UpdatePlayer.Modify` source: `UpdatePlayer.Modify`
@ -212,8 +212,12 @@ const modifyPlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
}; };
}; };
io.to(game.id).emit(`UpdatePlayer`, { player.role = data.to.role;
player.team = data.to.team;
io.to(game.id).emit(`PlayerUpdate`, {
status: 200, status: 200,
mode: `modify`,
name: data.name, name: data.name,
role: data.to.role, role: data.to.role,
team: data.to.team, team: data.to.team,
@ -229,6 +233,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})`); log.debug(`Can't delete a player that doesn't exist. (name=${data.name},gID=${game.id})`);
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}`,
source: `UpdatePlayer.Remove` source: `UpdatePlayer.Remove`
@ -238,7 +243,7 @@ const removePlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
// Ensure that the player who is removing the player is the host // Ensure that the player who is removing the player is the host
if (host.socket !== socket) { if (host.socket !== socket) {
socket.emit(`Error`, { socket.emit(`PlayerUpdate`, {
status: 403, status: 403,
message: `Cannot kick a player when you are not the host`, message: `Cannot kick a player when you are not the host`,
source: `UpdatePlayer.Remove` source: `UpdatePlayer.Remove`
@ -261,7 +266,7 @@ const removePlayer = (io: Server, socket: Socket, data: UpdatePlayer): void => {
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}`); log.info(`${host.name} kicked ${player.name} from game ${game.id}`);
io.to(game.id).emit(`UpdatePlayer`, { io.to(game.id).emit(`PlayerUpdate`, {
action: "remove", action: "remove",
name: player.name, name: player.name,
}); });