Begin working on the lobby creation event
This commit is contained in:
parent
3dfb8b877f
commit
a6935d9d96
1 changed files with 30 additions and 0 deletions
30
server/src/events/lobby/create.ts
Normal file
30
server/src/events/lobby/create.ts
Normal 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;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue