Implement JoinGame
This commit is contained in:
parent
330c21ab53
commit
089cae940d
1 changed files with 64 additions and 5 deletions
|
|
@ -1,13 +1,72 @@
|
||||||
|
import { games, log } from '../main';
|
||||||
|
import { Player } from '../objects/Player';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
|
||||||
export default (io: Server, socket: Socket, data: JoinGame) => {
|
export default (io: Server, socket: Socket, data: JoinGame) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// Assert game exists
|
||||||
|
if (!games[data.game_code]) {
|
||||||
|
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`);
|
||||||
socket.emit(`Error`, {
|
socket.emit(`Error`, {
|
||||||
status: 501,
|
status: 404,
|
||||||
message: `JoinGame: Not Implemented Yet`,
|
message: `Game with code ${data.game_code} could not be found`,
|
||||||
source: `JoinGame`,
|
source: `JoinGame`
|
||||||
});
|
});
|
||||||
} catch (err) {
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let game = games[data.game_code];
|
||||||
|
|
||||||
|
|
||||||
|
// Ensure no one has the same name as the player that is joining
|
||||||
|
let sameName = game.players.find(x => x.name == data.name);
|
||||||
|
if (sameName != null) {
|
||||||
|
if (!game.ingame) {
|
||||||
|
socket.emit(`Error`, {
|
||||||
|
status: 400,
|
||||||
|
message: `A player already has that name in the game.`,
|
||||||
|
source: `JoinGame`
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player has the same name but is allowed to rejoin if they
|
||||||
|
// disconnect in the middle of the game
|
||||||
|
if (!sameName.socket.connected) {
|
||||||
|
socket.emit(`GameRejoined`, { status: 200 });
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
socket.emit(`Error`, {
|
||||||
|
status: 403,
|
||||||
|
message: `Can't connect to an already connected client`,
|
||||||
|
source: `JoinGame`
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Assert game is not in-progess
|
||||||
|
if (game.ingame) {
|
||||||
|
socket.emit(`Error`, {
|
||||||
|
status: 403,
|
||||||
|
message: `Cannot connect to a game that's in progress.`,
|
||||||
|
source: `JoinGame`
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let player = new Player(data.name, socket);
|
||||||
|
game.players.push(player);
|
||||||
|
|
||||||
|
socket.emit(`GameJoined`, {});
|
||||||
|
socket.to(game.id).emit(`PlayerUpdate`, {
|
||||||
|
action: "new",
|
||||||
|
name: player.name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
socket.emit(`Error`, {
|
socket.emit(`Error`, {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: `${err.name}: ${err.message}`,
|
message: `${err.name}: ${err.message}`,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue