feat(*): Add server classes

This commit is contained in:
Josh Creek
2023-07-09 21:31:42 +00:00
parent 59b9d0214d
commit 664e93ebff
5 changed files with 103 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import { WebSocket } from 'ws';
import { User } from './User';
export class Room {
id: string;
users: Map<WebSocket, User>;
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<string, number>();
this.users.forEach((user) => {
if (user.estimate !== null) {
estimates.set(user.name, user.estimate);
}
});
return estimates;
}
clearEstimates() {
this.users.forEach((user) => {
user.estimate = null;
});
}
}
+11
View File
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}