diff --git a/server/src/events/lobby/create.ts b/server/src/events/lobby/create.ts new file mode 100644 index 0000000..45f9c33 --- /dev/null +++ b/server/src/events/lobby/create.ts @@ -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; \ No newline at end of file