Begin working on the lobby creation event

This commit is contained in:
Oliver Akins 2022-03-07 23:41:48 -06:00
parent 3dfb8b877f
commit a6935d9d96
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -0,0 +1,30 @@
import { Status, ICreateLobby, colours, spaceships } from "common";
import { WebsocketEvent } from "@/types/WebsocketEvent";
import { Player } from "@/objects/Player";
import { Game } from "@/objects/Game";
const data: WebsocketEvent = {
name: "req:lobby.create",
handler(_, socket, data: ICreateLobby) {
// Assert that the user's name conforms to a small subset of unicode to
// help prevent weird errors from occuring due to potentially violating
// strings and running code through the name entry.
if (data.name.match(/[^A-Za-z0-9\_\-]/)) {
socket.emit(`res:lobby.create`, {
success: Status.BadRequest,
message: `Can't use special characters in your name, only A-Z, 0-9, underscores, and dashes are allowed.`,
});
};
// Create the game:
let host = new Player(socket, {
name: data.name,
colour: colours[Math.floor(Math.random() * colours.length)],
ship: spaceships[0],
});
let game = new Game(host);
game.log.info(`New game created by ${data.name}`)
},
};
export default data;