-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketHandlers.ts
186 lines (163 loc) · 4.18 KB
/
WebSocketHandlers.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
174
175
176
177
178
179
180
181
182
183
184
185
186
import { BoardPosition } from "./BoardPosition.ts";
import { amqp, amqpHandlers, wsi } from "./deps_int.ts";
import { Game } from "./Game.ts";
import { getGameById, removeGame, selfInGame } from "./serverstate.ts";
import { safeParseJson } from "./Utils.ts";
import { sendMessageToId } from "./WebSocketInterface.ts";
export function handleMessage(ws: WebSocket, data: any) {
const message = safeParseJson(data);
if (!message) return;
const id = wsi.getId(ws);
if (!id) {
console.error("Received message from unregistered client");
return;
}
switch (message.type) {
case "accept-attendee-request":
case "decline-attendee-request":
handleReceiveConnectResponse(id, message);
break;
case "send-move":
handleReceiveMoveMessage(id, message);
break;
case "get-board":
handleGetBoard(id);
break;
case "disconnect":
handleReceiveDisconnect(id);
break;
default:
console.log("Unexpected WS message: " + message);
}
}
export function handleSendConnectRequest(
ownId: string,
clientId: string,
code: string,
) {
wsi.sendMessageToId(ownId, {
type: "verify-attendee-request",
clientId: clientId,
code: code,
});
}
export async function handleReceiveConnectResponse(ownId: string, data: any) {
if (selfInGame(ownId)) {
console.error("Received connect response while in game");
handleAlreadyInGameError(ownId);
return;
}
const opponentId = data.clientId;
if (!await amqp.queueExists(opponentId)) {
sendMessageToId(ownId, {
type: "error",
error: "Opponent not found",
});
return;
}
let accepted: boolean;
if (data.type === "accept-attendee-request") {
accepted = true;
} else if (data.type === "decline-attendee-request") {
accepted = false;
} else return;
await amqpHandlers.handleSendConnectResponse(ownId, opponentId, accepted);
}
function handleAlreadyInGameError(id: string) {
wsi.sendMessageToId(id, {
type: "error",
error: "Already in game",
});
}
export function handleConnectRequestOpponentNotFoundError(ws: WebSocket) {
ws.send(JSON.stringify({
type: "error",
error: "Opponent not found",
}));
ws.close();
}
export function handleGetBoard(id: string) {
const game = getGameById(id)!;
if (!game) return;
sendMessageToId(id, {
type: "board",
fen: game.getFEN(),
});
}
export function handleSendMoveMessage(
id: string,
from: BoardPosition,
to: BoardPosition,
game: Game,
) {
sendMessageToId(id, {
type: "receive-move",
from: from.toString(),
to: to.toString(),
fen: game.getFEN(),
});
}
function handleReceiveMoveMessage(id: string, message: any) {
const parsed = parseMoveMessage(id, message);
if (!parsed) return;
const { from, to, game } = parsed;
if (
!game.validateMove(from, to) ||
!game.validateCorrectPlayerMoved(from, id)
) {
handleRejectMove(id, message, game);
return;
}
handleAcceptMove(id, from, to, game);
}
function parseMoveMessage(id: string, message: any) {
let game;
try {
const from = new BoardPosition(message.from);
const to = new BoardPosition(message.to);
const game = getGameById(id)!;
if (!game || !from || !to) return;
return { from, to, game };
} catch {
handleRejectMove(id, message, game);
return;
}
}
function handleRejectMove(id: string, message: any, game: Game | undefined) {
sendMessageToId(id, {
type: "reject-move",
from: message.from.toString(),
to: message.to.toString(),
fen: game?.getFEN(),
});
}
function handleAcceptMove(
id: string,
from: BoardPosition,
to: BoardPosition,
game: Game,
) {
game.makeMove(from, to);
amqpHandlers.handleSendMoveMessage(from, to, game);
sendMessageToId(id, {
type: "accept-move",
from: from.toString(),
to: to.toString(),
fen: game.getFEN(),
});
}
function handleReceiveDisconnect(id: string) {
amqpHandlers.handleSendGameClosedMessage(id);
removeGame(id);
wsi.close(id);
}
export function handleSendGameClosedMessage(id: string) {
wsi.sendMessageToId(id, {
type: "opponent-disconnected",
});
}
export function handleSendTimeoutMessage(id: string) {
wsi.sendMessageToId(id, {
type: "timeout",
});
}