mirror of
https://github.com/jcreek/EstimationPoker.git
synced 2026-07-12 18:43:47 +00:00
feat(*): Add server classes
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user