Skip to content

Commit c7152fd

Browse files
committed
duckguessr: Cleanup event definitions
1 parent 3c6eead commit c7152fd

File tree

10 files changed

+510
-633
lines changed

10 files changed

+510
-633
lines changed

apps/duckguessr/api/index.ts

Lines changed: 9 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@ import "dotenv/config";
33
import * as Sentry from "@sentry/node";
44
import { Server } from "socket.io";
55

6-
import { prismaClient as prismaCoa } from "~prisma-schemas/schemas/coa/client";
7-
86
import { PrismaClient } from "./prisma/client_duckguessr/client";
97
import { createGameSocket } from "./services/game";
108
// import { createMatchmakingSocket } from "./services/game";
11-
import { createPlayerSocket } from "./services/player";
12-
import type {
13-
ClientToServerEvents,
14-
InterServerEvents,
15-
ServerToClientEvents,
16-
SocketData,
17-
} from "./types/socketEvents";
9+
import { server as home } from "./services/home";
10+
import { server as player } from "./services/player";
1811

1912
if (process.env.SENTRY_DSN) {
2013
Sentry.init({
@@ -28,142 +21,12 @@ const cors = {
2821
origin: process.env.FRONTEND_URL,
2922
};
3023

31-
const io = new Server<
32-
ClientToServerEvents,
33-
ServerToClientEvents,
34-
InterServerEvents,
35-
SocketData
36-
>({
24+
const io = new Server({
3725
cors,
3826
});
3927

40-
const convertBlobToBase64 = (blob: Blob) =>
41-
new Promise((resolve, reject) => {
42-
const reader = new FileReader();
43-
reader.onerror = reject;
44-
reader.onload = () => {
45-
resolve(reader.result);
46-
};
47-
reader.readAsDataURL(blob);
48-
});
49-
50-
io.of("/round").on("connection", async (socket) => {
51-
socket.on("getGameRounds", async (gameId, callback) => {
52-
const round = await prisma.round.findFirst({
53-
include: {
54-
roundScores: true,
55-
},
56-
where: {
57-
gameId,
58-
startedAt: { not: null },
59-
finishedAt: null,
60-
},
61-
orderBy: {
62-
roundNumber: "asc",
63-
},
64-
});
65-
66-
if (!round) {
67-
callback({
68-
roundScores: await prisma.roundScore.findMany({
69-
where: {
70-
roundId: {
71-
in: (
72-
await prisma.round.findMany({
73-
where: {
74-
gameId,
75-
},
76-
select: {
77-
id: true,
78-
},
79-
})
80-
).map(({ id }) => id),
81-
},
82-
},
83-
}),
84-
});
85-
return;
86-
}
87-
88-
fetch(`${process.env.CLOUDINARY_URL_ROOT}${round.sitecodeUrl}`)
89-
.then(
90-
async (response) =>
91-
`data:${response.headers.get(
92-
"content-type",
93-
)};base64,${convertBlobToBase64(await response.blob())}`,
94-
)
95-
.then((base64) => {
96-
callback({
97-
gameId,
98-
roundNumber: round.roundNumber,
99-
base64,
100-
});
101-
})
102-
.catch((e) => {
103-
console.error(e);
104-
callback(null);
105-
});
106-
});
107-
});
108-
109-
io.of("/game").on("connection", async (socket) => {
110-
socket.on("getGame", async (id, callback) => {
111-
const game = await prisma.game.findFirst({
112-
include: {
113-
gamePlayers: {
114-
include: {
115-
player: true,
116-
},
117-
},
118-
rounds: {
119-
include: {
120-
roundScores: true,
121-
},
122-
},
123-
dataset: true,
124-
},
125-
where: {
126-
id,
127-
},
128-
});
129-
if (!game) {
130-
callback(null);
131-
return;
132-
}
133-
134-
const personDetails = await prismaCoa.inducks_person.findMany({
135-
select: {
136-
personcode: true,
137-
fullname: true,
138-
nationalitycountrycode: true,
139-
},
140-
where: {
141-
personcode: {
142-
in: game.rounds.map(({ personcode }) => personcode),
143-
},
144-
},
145-
});
146-
147-
callback({
148-
...game,
149-
rounds: game.rounds
150-
.filter(({ roundNumber }) => roundNumber !== null)
151-
.map((round) => {
152-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
153-
const { personcode, sitecodeUrl, ...unfinishedRound } = round;
154-
return round.finishedAt && round.finishedAt.getTime() <= Date.now()
155-
? round
156-
: unfinishedRound;
157-
}),
158-
authors: personDetails.sort(
159-
({ personcode: personcode1 }, { personcode: personcode2 }) =>
160-
personcode1 < personcode2 ? -1 : 1,
161-
),
162-
});
163-
});
164-
});
165-
166-
createPlayerSocket(io);
28+
home(io)
29+
player(io);
16730
// createMatchmakingSocket(io);
16831

16932
(async () => {
@@ -180,7 +43,10 @@ createPlayerSocket(io);
18043
console.debug(
18144
`Creating socket for unfinished game with ID ${pendingGame.id}`,
18245
);
183-
await createGameSocket(io, pendingGame.id);
46+
const socket = await createGameSocket(pendingGame.id);
47+
if (socket) {
48+
socket.server(io);
49+
}
18450
}
18551
})();
18652

apps/duckguessr/api/services/datasets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type DatasetsServices = NamespaceProxyTarget<
1212

1313
const prisma = new PrismaClient();
1414

15-
const listenEvents = ({}: DatasetsServices) => ({
15+
const listenEvents = () => ({
1616
getDatasets: async () => prisma.$queryRaw`
1717
SELECT dataset.id, name, title, description, COUNT(*) AS images, COUNT(DISTINCT personcode) AS authors
1818
FROM dataset

0 commit comments

Comments
 (0)