From 664e93ebffe2b58a1e650e91c21a81fa038d05ab Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:31:42 +0000 Subject: [PATCH] feat(*): Add server classes --- server/classes/Room.ts | 52 +++++++++++++++++++ server/classes/User.ts | 11 ++++ .../classes/messages/ChangeEstimateMessage.ts | 13 +++++ server/classes/messages/JoinRoomMessage.ts | 14 +++++ .../classes/messages/SelectEstimateMessage.ts | 13 +++++ 5 files changed, 103 insertions(+) create mode 100644 server/classes/Room.ts create mode 100644 server/classes/User.ts create mode 100644 server/classes/messages/ChangeEstimateMessage.ts create mode 100644 server/classes/messages/JoinRoomMessage.ts create mode 100644 server/classes/messages/SelectEstimateMessage.ts diff --git a/server/classes/Room.ts b/server/classes/Room.ts new file mode 100644 index 0000000..cd69d88 --- /dev/null +++ b/server/classes/Room.ts @@ -0,0 +1,52 @@ +import { WebSocket } from 'ws'; +import { User } from './User'; + +export class Room { + id: string; + users: Map; + + constructor(id: string) { + this.id = id; + this.users = new Map(); + } + + addUser(ws: WebSocket, user: User) { + this.users.set(ws, user); + } + + removeUser(ws: WebSocket) { + this.users.delete(ws); + } + + getUsers() { + return Array.from(this.users.values()); + } + + getUserByWebSocket(ws: WebSocket) { + return this.users.get(ws); + } + + broadcast(message: any) { + this.users.forEach((user, client) => { + if (client.readyState === WebSocket.OPEN) { + client.send(JSON.stringify(message)); + } + }); + } + + getAllEstimates() { + const estimates = new Map(); + this.users.forEach((user) => { + if (user.estimate !== null) { + estimates.set(user.name, user.estimate); + } + }); + return estimates; + } + + clearEstimates() { + this.users.forEach((user) => { + user.estimate = null; + }); + } +} diff --git a/server/classes/User.ts b/server/classes/User.ts new file mode 100644 index 0000000..7fe91b9 --- /dev/null +++ b/server/classes/User.ts @@ -0,0 +1,11 @@ +export class User { + userId: string; + name: string; + estimate: number | null; + + constructor(userId: string, name: string) { + this.userId = userId; + this.name = name; + this.estimate = null; + } +} diff --git a/server/classes/messages/ChangeEstimateMessage.ts b/server/classes/messages/ChangeEstimateMessage.ts new file mode 100644 index 0000000..09540c3 --- /dev/null +++ b/server/classes/messages/ChangeEstimateMessage.ts @@ -0,0 +1,13 @@ +export class ChangeEstimateMessage { + type: string; + roomId: string; + userId: string; + estimate: number; + + constructor(roomId: string, userId: string, estimate: number) { + this.type = 'change-estimate'; + this.roomId = roomId; + this.userId = userId; + this.estimate = estimate; + } +} diff --git a/server/classes/messages/JoinRoomMessage.ts b/server/classes/messages/JoinRoomMessage.ts new file mode 100644 index 0000000..1b51a06 --- /dev/null +++ b/server/classes/messages/JoinRoomMessage.ts @@ -0,0 +1,14 @@ +export class JoinRoomMessage { + type: string; + roomId: string; + userId: string; + name: string; + + constructor(roomId: string, userId: string, name: string) { + this.type = 'join-room'; + this.roomId = roomId; + this.userId = userId; + this.name = name; + } + } + \ No newline at end of file diff --git a/server/classes/messages/SelectEstimateMessage.ts b/server/classes/messages/SelectEstimateMessage.ts new file mode 100644 index 0000000..d05e7be --- /dev/null +++ b/server/classes/messages/SelectEstimateMessage.ts @@ -0,0 +1,13 @@ +export class SelectEstimateMessage { + type: string; + roomId: string; + userId: string; + estimate: number; + + constructor(roomId: string, userId: string, estimate: number) { + this.type = 'select-estimate'; + this.roomId = roomId; + this.userId = userId; + this.estimate = estimate; + } +}