refactor(*): Move server into src

This commit is contained in:
Josh Creek
2023-07-09 22:40:42 +01:00
parent e2ae0573c4
commit f75dcb2ef0
12 changed files with 6 additions and 14 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;
});
}
}