Repository navigation
Expand file tree
/
Copy pathapp.ts
More file actions
188 lines (175 loc) · 7.17 KB
/
Copy pathapp.ts
File metadata and controls
188 lines (175 loc) · 7.17 KB
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
/* eslint no-console: "off" */
import express, { Router } from "express";
import { Server } from "socket.io";
import { SocketEvents } from "@gamenite/shared";
import { z } from "zod";
import * as http from "node:http";
import * as chat from "./controllers/chat.controller.ts";
import * as game from "./controllers/game.controller.ts";
import * as leaderboard from "./controllers/leaderboard.controller.ts";
import * as matchmaker from "./controllers/matchmaker.controller.ts";
import * as user from "./controllers/user.controller.ts";
import * as model from "./controllers/model.controller.ts";
import * as thread from "./controllers/thread.controller.ts";
import * as profile from "./controllers/profile.controller.ts";
import * as follow from "./controllers/follow.controller.ts";
import * as puzzle from "./controllers/puzzle.controller.ts";
import * as rating from "./controllers/rating.controller.ts";
import * as replay from "./controllers/replay.controller.ts";
import * as annotation from "./controllers/annotation.controller.ts";
import * as broadcast from "./controllers/broadcast.controller.ts";
import * as broadcastChat from "./controllers/broadcastChat.controller.ts";
import * as highlight from "./controllers/highlight.controller.ts";
import * as training from "./controllers/training.controller.ts";
import * as deployment from "./controllers/deployment.controller.ts";
import { inferenceRouter } from "./controllers/inference.controller.ts";
import { type GameServer } from "./types.ts";
export const app = express();
export const httpServer = http.createServer(app);
export const io: GameServer = new Server(httpServer);
app.use(express.json());
app.use(
"/api",
Router()
.use(
"/game",
express
.Router() //
.post("/create", game.postCreate)
.get("/list", game.getList)
.get("/:id", game.getById),
)
.use(
"/thread",
express
.Router() //
.post("/create", thread.postCreate)
.get("/list", thread.getList)
.get("/:id", thread.getById)
.post("/:id/comment", thread.postByIdComment),
)
.use(
"/user",
Router() // Any concrete routes here should be disallowed as usernames
.post("/list", user.postList)
.post("/login", user.postLogin)
.post("/signup", user.postSignup)
.post("/:username", user.postByUsername)
.get("/:username", user.getByUsername),
)
.use(
"/model",
express
.Router()
.post("/upload", model.uploadMiddleware, model.postUpload)
.get("/user/:username", model.getByUsername)
.get("/:id", model.getById)
.post("/:id/deploy", model.postDeploy)
.post("/:modelId/fork", model.postFork)
.patch("/deployment/:id", model.patchDeploymentStatus),
)
.use("/leaderboard", express.Router().get("/:gameKey", leaderboard.getByGame))
.use("/matchmaker", express.Router().get("/queue", matchmaker.getQueueStatus))
.use("/profile", express.Router().get("/:username", profile.getProfileSummary))
.use(
"/follow",
express
.Router()
.post("/feed", follow.postFeed)
.get("/:username/followers", follow.getFollowers)
.get("/:username/following", follow.getFollowingList)
.post("/:username", follow.postFollow)
.post("/:username/unfollow", follow.postUnfollow),
)
.use(
"/puzzle",
express
.Router()
// NOTE: registered before "/:gameKey" or that param route shadows it.
.get("/leaderboard", puzzle.getLeaderboard)
.get("/:gameKey", puzzle.getToday)
.post("/:gameKey/attempt", puzzle.postAttempt)
.post("/:gameKey/attempt/ai", puzzle.postAiAttempt)
.post("/:gameKey/hint", puzzle.postHint)
.get("/:gameKey/training", puzzle.getTraining)
.post("/:gameKey/training/attempt", puzzle.postTrainingAttempt),
)
.use("/rating", rating.ratingRouter())
.use(
"/replay",
express
.Router()
.get("/list", replay.getList)
.get("/:matchId/download", replay.getDownload)
.get("/:matchId", replay.getById)
.post("/:matchId/view", replay.postView)
.post("/:matchId/analysis", replay.postAnalysis),
)
.use(
"/annotation",
express.Router().post("/create", annotation.postCreate).get("/:id", annotation.getById),
)
.use(
"/broadcast",
express
.Router()
.post("/create", broadcast.postCreate)
.get("/list", broadcast.getList)
.get("/:id", broadcast.getById)
.post("/:id/end", broadcast.postEnd)
.post("/:id/slowmode", broadcastChat.postSlowMode),
)
.use(
"/highlight",
express.Router().post("/create", highlight.postCreate).post("/list", highlight.postList),
)
.use("/training", training.trainingRouter())
.use("/deployment", deployment.deploymentRouter())
.use("/inference", inferenceRouter()),
);
io.on("connection", (socket) => {
const socketId = socket.id;
console.log(`CONN [${socketId}] connected`);
socket.on("disconnect", () => {
console.log(`CONN [${socketId}] disconnected`);
});
socket.on("chatJoin", chat.socketJoin(socket, io));
socket.on("chatLeave", chat.socketLeave(socket, io));
socket.on("chatSendMessage", chat.socketSendMessage(socket, io));
socket.on("gameJoinAsPlayer", game.socketJoinAsPlayer(socket, io));
socket.on("gameMakeMove", game.socketMakeMove(socket, io));
socket.on("gameForfeit", game.socketForfeit(socket, io));
socket.on("gameStart", game.socketStart(socket, io));
socket.on("gameWatch", game.socketWatch(socket, io));
socket.on("matchmakingJoin", matchmaker.socketJoinQueue(socket, io));
socket.on("matchmakingLeave", matchmaker.socketLeaveQueue(socket, io));
socket.on("replayWatch", replay.socketReplayWatch(socket, io));
socket.on("replayLeave", replay.socketReplayLeave(socket, io));
// Closed tabs never send replayLeave; broadcast corrected watcher counts
// while the departing socket's rooms are still known.
socket.on("disconnecting", () => replay.handleReplayDisconnecting(socket, io));
socket.on("broadcastWatch", broadcast.socketBroadcastWatch(socket, io));
socket.on("broadcastLeave", broadcast.socketBroadcastLeave(socket, io));
socket.on("broadcastChatSend", broadcastChat.socketBroadcastChatSend(socket, io));
socket.onAny((name, payload) => {
// The training progress bridge's events carry a bare { jobId } payload by
// contract (trainingProgress.types.ts), not the { auth, payload } shape —
// don't log valid bridge traffic as errors.
if (name === SocketEvents.subscribe || name === SocketEvents.unsubscribe) {
console.log(`RECV [${socketId}] got ${name} ${JSON.stringify(payload)}`);
return;
}
const zPayload = z.object({ auth: z.object({ username: z.string() }), payload: z.any() });
const checked = zPayload.safeParse(payload);
if (checked.error) {
console.log(`RECV error: ${checked.error.message}`);
} else {
console.log(
`RECV [${socketId}] got ${name}${checked.data.auth.username} ${JSON.stringify(checked.data.payload)}`,
);
}
});
socket.onAnyOutgoing((name) => {
console.log(`SEND [${socketId}] gets ${name}`);
});
});