-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.ts
173 lines (151 loc) · 4.77 KB
/
Adapter.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Server } from "socket.io";
import { OOOManager } from "./OOOManager";
import { Room } from "./Room";
import { createUser, User } from "./User";
/*
User in lobby
- See number of members
- Enter anarchy or one on one
User in anarchy chat
- Send message
- Get message history
- Leave to lobby
User in one on one
- Get added to an emmpty room, or a room with one other person
- messages in that room
- Leave to lobby
LOBBY
- room = lobby
- client <- user_counts
- client -> change_mode
ANARCHY
- room = anarchy
- client <- chat_hist
- client -> send_msg
- client -> leave_chat
ONE ON ONE
- room = oneonone:<room_number>
- client <- room_details (either there is another person in there or not)
- client <- chat_hist
- client -> send_msg
- client -> leave_chat
*/
export class Adapter {
io: Server;
lobby: Room;
anarchyChat: Room;
oooManager: OOOManager;
users: { [id: string]: User };
constructor(io: Server) {
this.io = io;
this.lobby = new Room(io, "lobby");
this.anarchyChat = new Room(io, "anarchy");
this.oooManager = new OOOManager(io);
this.users = {};
this.io.on("connection", (socket) => {
this.lobby.enter(socket);
this.lobby.updateState({
userCount: (this.lobby.state.userCount || 0) + 1,
});
this.users[socket.id] = createUser(socket.id);
socket.emit("set ident", this.users[socket.id]);
socket.on("modeChange", (newMode) => {
// Lobby to Anarchy
if (this.users[socket.id].mode == "lobby" && newMode == "anarchy") {
this.lobby.leave(socket);
this.lobby.updateState({
userCount: (this.lobby.state.userCount || 0) - 1,
});
this.users[socket.id].mode = "anarchy";
socket.emit("set ident", this.users[socket.id]);
this.anarchyChat.enter(socket);
this.anarchyChat.updateState({
userCount: (this.anarchyChat.state.userCount || 0) + 1,
events: [
...(this.anarchyChat.state.events || []),
{
type: "user entered",
name: this.users[socket.id].emoji,
},
],
});
}
// Anarchy to Lobby
if (this.users[socket.id].mode == "anarchy" && newMode == "lobby") {
this.anarchyChat.leave(socket);
this.anarchyChat.updateState({
userCount: (this.anarchyChat.state.userCount || 0) - 1,
events: [
...(this.anarchyChat.state.events || []),
{
type: "user left",
name: this.users[socket.id].emoji,
},
],
});
this.lobby.enter(socket);
this.lobby.updateState({
userCount: (this.anarchyChat.state.userCount || 0) + 1,
});
this.users[socket.id].mode = "lobby";
socket.emit("set ident", this.users[socket.id]);
}
// Lobby to OOO
if (this.users[socket.id].mode == "lobby" && newMode == "ooo") {
this.lobby.leave(socket);
this.lobby.updateState({
userCount: (this.lobby.state.userCount || 0) - 1,
});
this.users[socket.id].mode = "ooo";
socket.emit("set ident", this.users[socket.id]);
this.oooManager.enter(socket);
}
// OOO to Lobby
if (this.users[socket.id].mode == "ooo" && newMode == "lobby") {
this.oooManager.leave(socket);
this.users[socket.id].mode = "ooo";
socket.emit("set ident", this.users[socket.id]);
this.lobby.enter(socket);
this.lobby.updateState({
userCount: (this.lobby.state.userCount || 0) - 1,
});
}
});
socket.on("anarchy:send_msg", (msg) => {
if (this.users[socket.id].mode == "anarchy") {
this.anarchyChat.updateState({
events: [
...this.anarchyChat.state.events,
{
type: "message",
user: this.users[socket.id].emoji,
message: msg,
},
],
});
}
});
socket.on("disconnect", () => {
if (this.users[socket.id].mode == "lobby") {
this.lobby.leave(socket);
this.lobby.updateState({
userCount: (this.lobby.state.userCount || 0) - 1,
});
}
if (this.users[socket.id].mode == "anarchy") {
this.anarchyChat.leave(socket);
this.anarchyChat.updateState({
userCount: (this.anarchyChat.state.userCount || 0) - 1,
events: [
...(this.anarchyChat.state.events || []),
{
type: "user left",
name: this.users[socket.id].emoji,
},
],
});
}
});
});
}
}