-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOOManager.ts
69 lines (55 loc) · 1.77 KB
/
OOOManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Server, Socket } from "socket.io";
import { Room } from "./Room";
export class OOOManager {
io: Server
// lobby: Room
rooms: Room[]
queued: boolean;
constructor(io: Server) {
this.io = io;
// this.lobby = new Room(io, "OOOLobby");
this.rooms = [];
this.queued = false
}
enter (socket: Socket) {
if(!this.queued) {
const room = new Room(this.io, `ooo:${this.rooms.length}`)
this.rooms.push(room)
socket.emit("ooo:roomnumber", room.id)
room.enter(socket);
room.updateState({
user1: socket.id
})
this.queued = true;
}
else if (this.queued) {
const room = this.rooms[this.rooms.length - 1]
socket.emit("ooo:roomnumber", room.id)
room.enter(socket)
room.updateState({
user2: socket.id
})
this.queued = false
}
socket.on("ooo:send_msg", (msg, roomId) => {
console.log("sndmsg", socket.id, roomId);
let room = this.rooms.find(room => room.id == roomId)
if (room) {
const isMember = room.state.user1 == socket.id || room.state.user2 == socket.id
if (isMember) {
console.log("Sending message", msg, socket.id, room.id)
room.updateState({
events: [
...(room.state.events || []), {
user: socket.id,
message: msg
}
]
})
}
}
})
}
leave (socket: Socket ) {
}
}