|
1 | 1 | "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"; |
7 | 6 |
|
8 | 7 | export async function getRandomSnippet(input: {
|
9 |
| - language: string; |
| 8 | + language: Language; |
10 | 9 | reportedSnippets?: string[];
|
11 |
| -}) { |
| 10 | +}): Promise<Snippet> { |
12 | 11 | const itemCount = await prisma.snippet.count({
|
13 | 12 | where: {
|
14 | 13 | onReview: false,
|
@@ -40,144 +39,12 @@ export async function getRandomSnippet(input: {
|
40 | 39 | return snippet;
|
41 | 40 | }
|
42 | 41 |
|
43 |
| -export async function getSnippetById(snippetId: string) { |
| 42 | +export async function getSnippetById( |
| 43 | + snippetId: string, |
| 44 | +): Promise<Snippet | null> { |
44 | 45 | return await prisma.snippet.findUnique({
|
45 | 46 | where: {
|
46 | 47 | id: snippetId,
|
47 | 48 | },
|
48 | 49 | });
|
49 | 50 | }
|
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 |
| -} |
0 commit comments