-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
240 lines (188 loc) · 5.15 KB
/
server.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { Match, PrismaClient, Team } from "@prisma/client";
import "dotenv/config";
import { WebSocketServer } from "ws";
import { Server } from "socket.io";
import axios from "axios";
import { MixedMatch } from "./types/match";
import { updateTeam } from "./util/team/updateTeam";
import { uploadMatch } from "./util/match/uploadMatch";
import { Clients } from "./util/clients";
const port = 8998;
export const TBA_KEY = process.env.TBA_KEY || "";
const MOCKMESSAGE =
"blue:108:red:78:b_t:0:r_t:0:b_h:0:r_h:0:r_m:0:b_m:0:r_l:0:b_l:0:tel_r:62:tel_b:0:auto_b:0:auto_r:16:pen_b:0:pen_r:0:blue_alliance:[179?20;364?6000;]:red_alliance:[233?130;118?5000;]";
export const prisma = new PrismaClient();
export const server = new WebSocketServer({
port,
});
export const clients = new Clients();
export const DATA_MAPPING = {
BLUE: "blue",
RED: "red",
RED_TRAVERSE: "r_t",
BLUE_TRAVERSE: "b_t",
BLUE_HIGH: "b_h",
RED_HIGH: "r_h",
BLUE_MID: "b_m",
RED_MID: "r_m",
BLUE_LOW: "b_l",
RED_LOW: "r_l",
TELEOP_BLUE: "tel_b",
TELEOP_RED: "tel_r",
AUTO_BLUE: "auto_b",
AUTO_RED: "auto_r",
BLUE_ALLIANCE: "blue_alliance",
RED_ALLIANCE: "red_alliance",
PENALTIES_BLUE: "pen_b",
PENALTIES_RED: "pen_r",
};
export const SERVER_EVENTS = {
UPDATE_MATCHES: "update_matches",
UPDATE_RANKS: "update_ranks",
};
export const RANKING_POINTS = {
WIN: 2,
TIE: 1,
/**
* 20 or more ALLIANCE colored CARGO scored in the HUB.
* If at least 5 ALLIANCE colored CARGO are scored in
* AUTO, called a QUINTET, this threshold drops to 18.
*/
CARGO_BONUS: 1,
// ALLIANCE is credited with at least 16 HANGAR points
HANGAR_BONUS: 1,
};
export const parse = (msg: string): MixedMatch => {
const data = msg.split(":");
var mix: MixedMatch = {
awardedRankingPoints: [],
match: {
blueAlliance: [],
blueAutoScore: 0,
blueEndScore: 0,
blueTeleScore: 0,
blueHangHigh: false,
blueHangMid: false,
blueHangLow: false,
blueHangTraverse: false,
blueScore: 0,
createdAt: new Date(),
redAlliance: [],
id: 0,
redAutoScore: 0,
redEndScore: 0,
redTeleScore: 0,
redHangHigh: false,
redHangMid: false,
redHangLow: false,
redHangTraverse: false,
redScore: 0,
penalties_blue: 0,
penalties_red: 0,
},
};
for (var e = 0; e < data.length; e++) {
const id = data[e];
const value = data[e + 1];
switch (id) {
case DATA_MAPPING.BLUE:
mix.match.blueScore = Number(value);
break;
case DATA_MAPPING.RED:
mix.match.redScore = Number(value);
break;
case DATA_MAPPING.BLUE_TRAVERSE:
mix.match.blueHangTraverse = Number(value) > 0;
break;
case DATA_MAPPING.RED_TRAVERSE:
mix.match.redHangTraverse = Number(value) > 0;
break;
case DATA_MAPPING.BLUE_HIGH:
mix.match.blueHangHigh = Number(value) > 0;
break;
case DATA_MAPPING.RED_HIGH:
mix.match.redHangHigh = Number(value) > 0;
break;
case DATA_MAPPING.BLUE_MID:
mix.match.blueHangMid = Number(value) > 0;
break;
case DATA_MAPPING.RED_MID:
mix.match.redHangMid = Number(value) > 0;
break;
case DATA_MAPPING.BLUE_LOW:
mix.match.blueHangLow = Number(value) > 0;
break;
case DATA_MAPPING.RED_LOW:
mix.match.redHangLow = Number(value) > 0;
break;
case DATA_MAPPING.TELEOP_BLUE:
mix.match.blueTeleScore = Number(value);
break;
case DATA_MAPPING.TELEOP_RED:
mix.match.redTeleScore = Number(value);
break;
case DATA_MAPPING.PENALTIES_BLUE:
mix.match.penalties_blue = Number(value);
break;
case DATA_MAPPING.PENALTIES_RED:
mix.match.penalties_red = Number(value);
break;
case DATA_MAPPING.BLUE_ALLIANCE:
var teams: string[] = [];
const raw = msg
.split(DATA_MAPPING.BLUE_ALLIANCE + ":" + "[")[1]
.split("]")[0]
.split(";");
for (var i = 0; i < raw.length; i++) {
var element = raw[i];
var team = element.split("?")[0];
var opr = element.split("?")[1];
if (opr != null && opr != "") {
mix.awardedRankingPoints.push({
team: team,
points: Number(opr),
});
}
if (i != raw.length && team != null && team != "") {
teams = [...teams, team];
}
}
mix.match.blueAlliance = [...mix.match.blueAlliance, ...teams];
break;
case DATA_MAPPING.RED_ALLIANCE:
var teams: string[] = [];
const raw_ = msg
.split(DATA_MAPPING.RED_ALLIANCE + ":" + "[")[1]
.split("]")[0]
.split(";");
for (var i = 0; i < raw_.length; i++) {
var element = raw_[i];
var team = element.split("?")[0];
var opr = element.split("?")[1];
if (opr != null && opr != "") {
mix.awardedRankingPoints.push({
team: team,
points: Number(opr),
});
}
if (i != raw_.length && team != null && team != "") {
teams = [...teams, team];
}
}
mix.match.redAlliance = [...mix.match.redAlliance, ...teams];
break;
}
}
return mix;
};
var lastMessage = "";
server.addListener("connection", (ws) => {
ws.onmessage = async (event) => {
const message = String(event.data);
console.log("MATCH: " + message);
if (message.toLowerCase() != lastMessage.toLowerCase()) {
lastMessage = message;
await uploadMatch(message);
}
};
});