Skip to content

Commit 2e81f93

Browse files
committed
refactored match making logic to wss
1 parent 232f3a5 commit 2e81f93

File tree

17 files changed

+381
-310
lines changed

17 files changed

+381
-310
lines changed

packages/app/.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"parser": "@typescript-eslint/parser",
99
"parserOptions": {
10-
"project": "./packages/app/tsconfig.json"
10+
"project": "./tsconfig.json"
1111
},
1212

1313
"rules": {

packages/app/src/app/dashboard/_components/snippetsTable.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export function SnippetsTable({
146146
{
147147
id: "language",
148148
title: "Language",
149+
//@ts-expect-error it's ok
149150
options: snippetLanguages,
150151
},
151152
]}

packages/app/src/app/race/(play)/friends/page.tsx

Whitespace-only changes.
+9-142
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"use server";
2-
import { prisma } from "@/lib/prisma";
3-
import type { Race } from "@prisma/client";
4-
import { siteConfig } from "@/config/site";
5-
import { Prisma } from "@prisma/client";
6-
import { Snippet, User } from "@prisma/client";
2+
//please don't alias these paths, it will break wss
3+
import { type Language } from "../../../config/languages";
4+
import { prisma } from "../../../lib/prisma";
5+
import type { Snippet } from "@prisma/client";
76

87
export async function getRandomSnippet(input: {
9-
language: string;
8+
language: Language;
109
reportedSnippets?: string[];
11-
}) {
10+
}): Promise<Snippet> {
1211
const itemCount = await prisma.snippet.count({
1312
where: {
1413
onReview: false,
@@ -40,144 +39,12 @@ export async function getRandomSnippet(input: {
4039
return snippet;
4140
}
4241

43-
export async function getSnippetById(snippetId: string) {
42+
export async function getSnippetById(
43+
snippetId: string,
44+
): Promise<Snippet | null> {
4445
return await prisma.snippet.findUnique({
4546
where: {
4647
id: snippetId,
4748
},
4849
});
4950
}
50-
51-
export async function raceMatchMaking(
52-
snippet: Snippet,
53-
userId?: User["id"],
54-
): Promise<Race> {
55-
if (userId) {
56-
// For logged in user, we choose race if:
57-
// 1. its snippet is the same
58-
// 2. hasn't started or ended yet
59-
// 3. all participants are logged in user
60-
// 4. race's participants has not reached maxiumun capacity
61-
// TODO: 5. participants stats most suitable to current user
62-
63-
let availableRace = await prisma.race.findMany({
64-
where: {
65-
snippet: {
66-
language: snippet.language,
67-
},
68-
AND: [{ startedAt: null }, { endedAt: null }],
69-
participants: {
70-
every: {
71-
user: {
72-
isNot: null,
73-
},
74-
},
75-
},
76-
},
77-
include: {
78-
participants: true,
79-
_count: {
80-
select: {
81-
participants: true,
82-
},
83-
},
84-
},
85-
});
86-
87-
// filter out full race
88-
availableRace = availableRace.filter(
89-
(race) =>
90-
race._count.participants <
91-
siteConfig.multiplayer.maxParticipantsPerRace,
92-
);
93-
94-
// TODO: sort races based on participant stats most suitable to current user
95-
// for now we pick first one, if there isn't any create one instead
96-
const race =
97-
availableRace.length > 0
98-
? availableRace[0]
99-
: await prisma.race.create({
100-
data: {
101-
snippet: {
102-
connect: {
103-
id: snippet.id,
104-
},
105-
},
106-
},
107-
});
108-
console.log("Picked race", race);
109-
return race;
110-
} else {
111-
// For guest user, we choose race if:
112-
// 1. its snippet is the same
113-
// 2. hasn't started or ended yet
114-
// 3. all participants are guest user
115-
// 4. race's participants has not reached maxiumun capacity
116-
117-
let availableRace = await prisma.race.findMany({
118-
where: {
119-
snippet: {
120-
language: snippet.language,
121-
},
122-
AND: [{ startedAt: null }, { endedAt: null }],
123-
participants: {
124-
every: {
125-
user: null,
126-
},
127-
},
128-
},
129-
include: {
130-
_count: {
131-
select: {
132-
participants: true,
133-
},
134-
},
135-
},
136-
});
137-
138-
// filter out full race
139-
availableRace = availableRace.filter(
140-
(race) =>
141-
race._count.participants <
142-
siteConfig.multiplayer.maxParticipantsPerRace,
143-
);
144-
145-
// pick first one, if there isn't any create one instead
146-
const race =
147-
availableRace.length > 0
148-
? availableRace[0]
149-
: await prisma.race.create({
150-
data: {
151-
snippet: {
152-
connect: {
153-
id: snippet.id,
154-
},
155-
},
156-
},
157-
});
158-
console.log("Picked race", race);
159-
return race;
160-
}
161-
}
162-
163-
export async function createRaceParticipant(
164-
raceToJoin: Prisma.RaceGetPayload<Record<string, never>>,
165-
user?: any,
166-
) {
167-
return await prisma.raceParticipant.create({
168-
data: {
169-
user: user
170-
? {
171-
connect: {
172-
id: user.id,
173-
},
174-
}
175-
: undefined,
176-
Race: {
177-
connect: {
178-
id: raceToJoin?.id,
179-
},
180-
},
181-
},
182-
});
183-
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { getCurrentUser } from "@/lib/session";
2-
import NoSnippet from "../../_components/no-snippet";
3-
import Race from "../../_components/race/race-multiplayer";
42
import { redirect } from "next/navigation";
5-
import { raceMatchMaking, createRaceParticipant } from "../loaders";
6-
import { getRandomSnippet } from "../loaders";
3+
import Race from "../../_components/race/race-multiplayer";
4+
import { Language, isValidLanguage } from "@/config/languages";
75

86
export default async function MultiplayerRacePage({
97
searchParams,
@@ -15,30 +13,17 @@ export default async function MultiplayerRacePage({
1513
if (!searchParams.lang) {
1614
redirect("/race");
1715
}
16+
const isValidLang = isValidLanguage(searchParams.lang);
1817

19-
const snippet = await getRandomSnippet({ language: searchParams.lang });
20-
if (!snippet) {
21-
return (
22-
<main className="flex flex-col items-center justify-between py-10 lg:p-24">
23-
<NoSnippet
24-
message={"Looks like there is no snippet available yet. Create one?"}
25-
language={searchParams.lang}
26-
/>
27-
</main>
28-
);
18+
if (!isValidLang) {
19+
redirect("/race");
2920
}
3021

3122
const user = await getCurrentUser();
32-
const raceToJoin = await raceMatchMaking(snippet, user?.id);
33-
const participant = await createRaceParticipant(raceToJoin, user);
23+
3424
return (
3525
<main className="flex flex-col items-center justify-between py-10 lg:p-24">
36-
<Race
37-
snippet={snippet}
38-
user={user}
39-
raceId={raceToJoin?.id}
40-
participantId={participant.id}
41-
/>
26+
<Race user={user} language={searchParams.lang as Language} />
4227
</main>
4328
);
4429
}

packages/app/src/app/race/(play)/practice/page.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import NoSnippet from "../../_components/no-snippet";
44
import Race from "../../_components/race/race-multiplayer";
55
import { getSnippetById } from "../loaders";
66
import { CacheBuster } from "@/components/cache-buster";
7+
import { Language, isValidLanguage } from "@/config/languages";
8+
import { redirect } from "next/navigation";
79

810
async function getSearchParamSnippet(snippetId: string | string[]) {
911
if (typeof snippetId === "string") {
@@ -21,17 +23,22 @@ export default async function PracticeRacePage({
2123
};
2224
}) {
2325
const user = await getCurrentUser();
26+
const language = searchParams.lang as Language;
27+
const isValidLang = isValidLanguage(language);
28+
if (!isValidLang) {
29+
redirect("/race");
30+
}
31+
2432
const snippet =
2533
(await getSearchParamSnippet(searchParams.snippetId)) ??
26-
(await getRandomSnippet({ language: searchParams.lang }));
27-
const language = searchParams.lang;
34+
(await getRandomSnippet({ language: language }));
2835

2936
return (
3037
<main>
3138
<CacheBuster />
3239
{snippet && (
3340
<div className="pt-8">
34-
<Race snippet={snippet} user={user} />
41+
<Race practiceSnippet={snippet} language={language} user={user} />
3542
</div>
3643
)}
3744
{!snippet && (

0 commit comments

Comments
 (0)