Add error messages in landing page for create lobby error
parent
e982f782cb
commit
1d777f74ff
|
|
@ -14,10 +14,10 @@ import {
|
||||||
CreateLobbyEvent,
|
CreateLobbyEvent,
|
||||||
emitUpdateLobbyEvent,
|
emitUpdateLobbyEvent,
|
||||||
JoinLobbyEvent,
|
JoinLobbyEvent,
|
||||||
ServerEvent,
|
|
||||||
} from 'interface';
|
} from 'interface';
|
||||||
import { createWsExceptionFilter } from './websocket-exception-filter';
|
import { createWsExceptionFilter } from './websocket-exception-filter';
|
||||||
import {
|
import {
|
||||||
|
InvalidPlayerNameException,
|
||||||
MissingPlayerNameException,
|
MissingPlayerNameException,
|
||||||
PlayerNotFoundException,
|
PlayerNotFoundException,
|
||||||
} from './exceptions';
|
} from './exceptions';
|
||||||
|
|
@ -39,6 +39,7 @@ export class AppService implements OnGatewayConnection {
|
||||||
createWsExceptionFilter([
|
createWsExceptionFilter([
|
||||||
PlayerNotFoundException,
|
PlayerNotFoundException,
|
||||||
MissingPlayerNameException,
|
MissingPlayerNameException,
|
||||||
|
InvalidPlayerNameException,
|
||||||
]),
|
]),
|
||||||
)
|
)
|
||||||
@SubscribeMessage(ClientEvent.CREATE_LOBBY)
|
@SubscribeMessage(ClientEvent.CREATE_LOBBY)
|
||||||
|
|
@ -60,6 +61,7 @@ export class AppService implements OnGatewayConnection {
|
||||||
createWsExceptionFilter([
|
createWsExceptionFilter([
|
||||||
PlayerNotFoundException,
|
PlayerNotFoundException,
|
||||||
MissingPlayerNameException,
|
MissingPlayerNameException,
|
||||||
|
InvalidPlayerNameException,
|
||||||
]),
|
]),
|
||||||
)
|
)
|
||||||
@SubscribeMessage(ClientEvent.JOIN_LOBBY)
|
@SubscribeMessage(ClientEvent.JOIN_LOBBY)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,12 @@ export class MissingPlayerNameException extends WebSocketException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class InvalidPlayerNameException extends WebSocketException {
|
||||||
|
constructor(userName: string) {
|
||||||
|
super('Invalid player name: ' + userName, ErrorCode.INVALID_USER_NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class PlayerNotFoundException extends WebSocketException {
|
export class PlayerNotFoundException extends WebSocketException {
|
||||||
constructor(playerId: string) {
|
constructor(playerId: string) {
|
||||||
super(
|
super(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { Player } from './player';
|
import { Player } from './player';
|
||||||
import {
|
import {
|
||||||
|
InvalidPlayerNameException,
|
||||||
MissingPlayerNameException,
|
MissingPlayerNameException,
|
||||||
PlayerNotFoundException,
|
PlayerNotFoundException,
|
||||||
} from 'src/exceptions';
|
} from 'src/exceptions';
|
||||||
|
|
@ -10,6 +11,7 @@ import { Socket } from 'socket.io/dist/socket';
|
||||||
export class PlayerService {
|
export class PlayerService {
|
||||||
private readonly logger = new Logger(PlayerService.name);
|
private readonly logger = new Logger(PlayerService.name);
|
||||||
private readonly players: Map<string, Player> = new Map();
|
private readonly players: Map<string, Player> = new Map();
|
||||||
|
private readonly userNameValidator: RegExp = /(^[a-zA-Z]){1,20}$/;
|
||||||
|
|
||||||
createPlayer(socket: Socket) {
|
createPlayer(socket: Socket) {
|
||||||
const player: Player = new Player(socket);
|
const player: Player = new Player(socket);
|
||||||
|
|
@ -20,6 +22,9 @@ export class PlayerService {
|
||||||
if (!userName) {
|
if (!userName) {
|
||||||
throw new MissingPlayerNameException();
|
throw new MissingPlayerNameException();
|
||||||
}
|
}
|
||||||
|
if (!this.userNameValidator.test(userName)) {
|
||||||
|
throw new InvalidPlayerNameException(userName);
|
||||||
|
}
|
||||||
this.players.get(socketId).userName = userName;
|
this.players.get(socketId).userName = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Socket } from "socket.io";
|
import { Socket } from 'socket.io';
|
||||||
|
|
||||||
export class Player {
|
export class Player {
|
||||||
socketId: string;
|
socketId: string;
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ export enum ServerError {
|
||||||
export enum ErrorCode {
|
export enum ErrorCode {
|
||||||
PLAYER_NOT_FOUND = "player-not-found",
|
PLAYER_NOT_FOUND = "player-not-found",
|
||||||
GAME_NOT_FOUND = "game-not-found",
|
GAME_NOT_FOUND = "game-not-found",
|
||||||
MISSING_USER_NAME = "player-user-name-not-found",
|
MISSING_USER_NAME = "missing-player-name",
|
||||||
|
INVALID_USER_NAME = "invalid-player-name",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,10 @@
|
||||||
margin: auto;
|
margin: auto;
|
||||||
width: 20%;
|
width: 20%;
|
||||||
min-width: 300px;
|
min-width: 300px;
|
||||||
|
|
||||||
|
#error-message {
|
||||||
|
color: #222;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
> div {
|
> div {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
import {
|
import {
|
||||||
|
attachHandlerToCreateLobbyError,
|
||||||
CreateLobbyEvent,
|
CreateLobbyEvent,
|
||||||
|
ErrorCode,
|
||||||
handleCreateLobby,
|
handleCreateLobby,
|
||||||
handleJoinLobby,
|
handleJoinLobby,
|
||||||
JoinLobbyEvent,
|
JoinLobbyEvent,
|
||||||
} from "interface";
|
} from "interface";
|
||||||
import React, { ChangeEvent } from "react";
|
import React, { ChangeEvent, useState } from "react";
|
||||||
import { Socket } from "socket.io-client";
|
import { Socket } from "socket.io-client";
|
||||||
import "./LandingPage.scss";
|
import "./LandingPage.scss";
|
||||||
|
|
||||||
|
|
@ -24,6 +26,21 @@ const LandingPage = (props: LandingPageProps) => {
|
||||||
const registerLobbyId = (event: ChangeEvent<HTMLInputElement>) =>
|
const registerLobbyId = (event: ChangeEvent<HTMLInputElement>) =>
|
||||||
(joinLobbyPayload.lobbyId = event.target.value);
|
(joinLobbyPayload.lobbyId = event.target.value);
|
||||||
|
|
||||||
|
const [receivedError, setReceivedError] = useState("" as ErrorCode);
|
||||||
|
const userNameErrorCodesToMessageMap = new Map([
|
||||||
|
[ErrorCode.MISSING_USER_NAME, "Introduce your player name"],
|
||||||
|
[
|
||||||
|
ErrorCode.INVALID_USER_NAME,
|
||||||
|
"Player name must be letters or spaces only and up to 25 characters",
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
const userNameErrorMessage =
|
||||||
|
userNameErrorCodesToMessageMap.get(receivedError);
|
||||||
|
|
||||||
|
attachHandlerToCreateLobbyError(socket, (event) =>
|
||||||
|
setReceivedError(event.error),
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="landing-page">
|
<div className="landing-page">
|
||||||
<div>
|
<div>
|
||||||
|
|
@ -34,7 +51,11 @@ const LandingPage = (props: LandingPageProps) => {
|
||||||
<input
|
<input
|
||||||
placeholder="Enter username"
|
placeholder="Enter username"
|
||||||
onChange={registerUsername}
|
onChange={registerUsername}
|
||||||
|
maxLength={20}
|
||||||
></input>
|
></input>
|
||||||
|
{userNameErrorMessage && (
|
||||||
|
<small id="error-message">{userNameErrorMessage}</small>
|
||||||
|
)}
|
||||||
<button onClick={() => handleCreateLobby(socket, createLobbyPayload)}>
|
<button onClick={() => handleCreateLobby(socket, createLobbyPayload)}>
|
||||||
Create Lobby
|
Create Lobby
|
||||||
</button>
|
</button>
|
||||||
|
|
@ -43,6 +64,7 @@ const LandingPage = (props: LandingPageProps) => {
|
||||||
<input
|
<input
|
||||||
placeholder="Enter Lobby Id"
|
placeholder="Enter Lobby Id"
|
||||||
onChange={registerLobbyId}
|
onChange={registerLobbyId}
|
||||||
|
maxLength={5}
|
||||||
></input>
|
></input>
|
||||||
<button
|
<button
|
||||||
className="secondary"
|
className="secondary"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue