rhythia-api 186.0.0 → 187.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc.json +6 -6
- package/api/addCollectionMap.ts +82 -82
- package/api/approveMap.ts +78 -78
- package/api/chartPublicStats.ts +32 -32
- package/api/createBeatmap.ts +168 -168
- package/api/createBeatmapPage.ts +64 -64
- package/api/createClan.ts +81 -81
- package/api/createCollection.ts +58 -58
- package/api/deleteBeatmapPage.ts +77 -77
- package/api/deleteCollection.ts +59 -59
- package/api/deleteCollectionMap.ts +71 -71
- package/api/editAboutMe.ts +91 -91
- package/api/editClan.ts +90 -90
- package/api/editCollection.ts +77 -77
- package/api/editProfile.ts +123 -123
- package/api/getAvatarUploadUrl.ts +85 -85
- package/api/getBadgedUsers.ts +56 -56
- package/api/getBeatmapComments.ts +57 -57
- package/api/getBeatmapPage.ts +106 -106
- package/api/getBeatmapPageById.ts +99 -99
- package/api/getBeatmapStarRating.ts +53 -53
- package/api/getBeatmaps.ts +159 -159
- package/api/getClan.ts +77 -77
- package/api/getCollection.ts +130 -130
- package/api/getCollections.ts +132 -132
- package/api/getLeaderboard.ts +136 -136
- package/api/getMapUploadUrl.ts +93 -93
- package/api/getPassToken.ts +55 -55
- package/api/getProfile.ts +146 -146
- package/api/getPublicStats.ts +180 -180
- package/api/getRawStarRating.ts +57 -57
- package/api/getScore.ts +85 -85
- package/api/getTimestamp.ts +23 -23
- package/api/getUserScores.ts +175 -175
- package/api/nominateMap.ts +82 -82
- package/api/postBeatmapComment.ts +59 -59
- package/api/rankMapsArchive.ts +64 -64
- package/api/searchUsers.ts +56 -56
- package/api/setPasskey.ts +59 -59
- package/api/submitScore.ts +433 -433
- package/api/updateBeatmapPage.ts +229 -229
- package/handleApi.ts +21 -20
- package/index.html +2 -2
- package/index.ts +867 -867
- package/package.json +1 -1
- package/types/database.ts +800 -800
- package/utils/getUserBySession.ts +48 -48
- package/utils/requestUtils.ts +87 -87
- package/utils/security.ts +20 -20
- package/utils/star-calc/index.ts +72 -72
- package/utils/star-calc/osuUtils.ts +53 -53
- package/utils/star-calc/sspmParser.ts +398 -398
- package/utils/star-calc/sspmv1Parser.ts +165 -165
- package/utils/supabase.ts +13 -13
- package/utils/test +4 -4
- package/utils/validateToken.ts +7 -7
- package/vercel.json +12 -12
package/api/submitScore.ts
CHANGED
|
@@ -1,433 +1,433 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
|
-
import z from "zod";
|
|
3
|
-
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
4
|
-
import { supabase } from "../utils/supabase";
|
|
5
|
-
import { decryptString } from "../utils/security";
|
|
6
|
-
import { isEqual } from "lodash";
|
|
7
|
-
import { getUserBySession } from "../utils/getUserBySession";
|
|
8
|
-
import { User } from "@supabase/supabase-js";
|
|
9
|
-
|
|
10
|
-
export const Schema = {
|
|
11
|
-
input: z.strictObject({
|
|
12
|
-
session: z.string(),
|
|
13
|
-
data: z.strictObject({
|
|
14
|
-
token: z.string(),
|
|
15
|
-
relayHwid: z.string(),
|
|
16
|
-
songId: z.string(),
|
|
17
|
-
misses: z.number(),
|
|
18
|
-
hits: z.number(),
|
|
19
|
-
mapHash: z.string(),
|
|
20
|
-
speed: z.number(),
|
|
21
|
-
mods: z.array(z.string()),
|
|
22
|
-
additionalData: z.any(),
|
|
23
|
-
spin: z.boolean(),
|
|
24
|
-
virtualStars: z.number(),
|
|
25
|
-
}),
|
|
26
|
-
}),
|
|
27
|
-
output: z.object({
|
|
28
|
-
error: z.string().optional(),
|
|
29
|
-
}),
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
function easeInExpoDeqHard(x: number, star: number) {
|
|
33
|
-
let exponent = 100 - 12 * star;
|
|
34
|
-
if (exponent < 5) exponent = 5;
|
|
35
|
-
return x === 0 ? 0 : Math.pow(2, exponent * x - exponent);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function calculatePerformancePoints(
|
|
39
|
-
starRating: number,
|
|
40
|
-
accuracy: number
|
|
41
|
-
) {
|
|
42
|
-
return (
|
|
43
|
-
Math.round(
|
|
44
|
-
Math.pow(
|
|
45
|
-
(starRating * easeInExpoDeqHard(accuracy, starRating) * 100) / 2,
|
|
46
|
-
2
|
|
47
|
-
) / 1000
|
|
48
|
-
) * 2
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function POST(request: Request): Promise<NextResponse> {
|
|
53
|
-
return protectedApi({
|
|
54
|
-
request,
|
|
55
|
-
schema: Schema,
|
|
56
|
-
authorization: validUser,
|
|
57
|
-
activity: handler,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export async function handler({
|
|
62
|
-
data,
|
|
63
|
-
session,
|
|
64
|
-
}: (typeof Schema)["input"]["_type"]): Promise<
|
|
65
|
-
NextResponse<(typeof Schema)["output"]["_type"]>
|
|
66
|
-
> {
|
|
67
|
-
const tokenContents = JSON.parse(decryptString(data.token));
|
|
68
|
-
if (
|
|
69
|
-
!isEqual(tokenContents, {
|
|
70
|
-
relayHwid: data.relayHwid,
|
|
71
|
-
songId: data.songId,
|
|
72
|
-
misses: data.misses,
|
|
73
|
-
hits: data.hits,
|
|
74
|
-
mapHash: data.mapHash,
|
|
75
|
-
speed: data.speed,
|
|
76
|
-
mods: data.mods,
|
|
77
|
-
additionalData: data.additionalData,
|
|
78
|
-
spin: data.spin,
|
|
79
|
-
virtualStars: data.virtualStars,
|
|
80
|
-
})
|
|
81
|
-
) {
|
|
82
|
-
return NextResponse.json(
|
|
83
|
-
{
|
|
84
|
-
error: "Token miscalculation",
|
|
85
|
-
},
|
|
86
|
-
{ status: 500 }
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const user = (await getUserBySession(session)) as User;
|
|
91
|
-
|
|
92
|
-
let { data: leversData } = await supabase
|
|
93
|
-
.from("levers")
|
|
94
|
-
.select("*")
|
|
95
|
-
.eq("id", 1)
|
|
96
|
-
.single();
|
|
97
|
-
|
|
98
|
-
if (leversData && leversData.disable_scores) {
|
|
99
|
-
return NextResponse.json({
|
|
100
|
-
error: "Scores are temporarily disabled",
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let { data: userData, error: userError } = await supabase
|
|
105
|
-
.from("profiles")
|
|
106
|
-
.select("*")
|
|
107
|
-
.eq("uid", user.id)
|
|
108
|
-
.single();
|
|
109
|
-
|
|
110
|
-
if (!userData)
|
|
111
|
-
return NextResponse.json(
|
|
112
|
-
{
|
|
113
|
-
error: "User doesn't exist",
|
|
114
|
-
},
|
|
115
|
-
{ status: 500 }
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
console.log(userData);
|
|
119
|
-
|
|
120
|
-
if (userData.ban == "excluded" || userData.ban == "restricted") {
|
|
121
|
-
return NextResponse.json(
|
|
122
|
-
{
|
|
123
|
-
error: "Silenced, restricted or excluded players can't submit scores.",
|
|
124
|
-
},
|
|
125
|
-
{ status: 400 }
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let { data: beatmaps, error } = await supabase
|
|
130
|
-
.from("beatmaps")
|
|
131
|
-
.select("*")
|
|
132
|
-
.eq("beatmapHash", data.mapHash)
|
|
133
|
-
.single();
|
|
134
|
-
|
|
135
|
-
let { data: beatmapPages, error: bpError } = await supabase
|
|
136
|
-
.from("beatmapPages")
|
|
137
|
-
.select("*")
|
|
138
|
-
.eq("latestBeatmapHash", data.mapHash)
|
|
139
|
-
.single();
|
|
140
|
-
|
|
141
|
-
if (!beatmapPages) {
|
|
142
|
-
return NextResponse.json(
|
|
143
|
-
{
|
|
144
|
-
error: "Map not submitted",
|
|
145
|
-
},
|
|
146
|
-
{ status: 500 }
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (!beatmaps) {
|
|
151
|
-
return NextResponse.json(
|
|
152
|
-
{
|
|
153
|
-
error: "Map not submitted",
|
|
154
|
-
},
|
|
155
|
-
{ status: 500 }
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const noteCount = data.misses + data.hits;
|
|
160
|
-
if (noteCount !== beatmaps.noteCount) {
|
|
161
|
-
return NextResponse.json(
|
|
162
|
-
{
|
|
163
|
-
error: "Wrong map",
|
|
164
|
-
},
|
|
165
|
-
{ status: 500 }
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
await supabase.from("beatmaps").upsert({
|
|
170
|
-
beatmapHash: data.mapHash,
|
|
171
|
-
playcount: (beatmaps.playcount || 1) + 1,
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
let passed = true;
|
|
175
|
-
|
|
176
|
-
// Pass invalidation
|
|
177
|
-
if (data.misses + data.hits !== beatmaps.noteCount) {
|
|
178
|
-
passed = false;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const accurracy = data.hits / noteCount;
|
|
182
|
-
let awarded_sp = 0;
|
|
183
|
-
|
|
184
|
-
console.log(
|
|
185
|
-
data.misses + data.hits == beatmaps.noteCount,
|
|
186
|
-
data.misses + data.hits,
|
|
187
|
-
beatmaps.noteCount
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
let multiplierMod = 1;
|
|
191
|
-
if (data.mods.includes("mod_hardrock")) {
|
|
192
|
-
multiplierMod *= 1.12;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (data.mods.includes("mod_nofail")) {
|
|
196
|
-
multiplierMod *= Math.pow(0.95, data.misses);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
beatmaps.starRating &&
|
|
201
|
-
Math.abs(beatmaps.starRating - data.virtualStars) < 0.01
|
|
202
|
-
) {
|
|
203
|
-
awarded_sp = calculatePerformancePoints(
|
|
204
|
-
data.speed * beatmaps.starRating * multiplierMod,
|
|
205
|
-
accurracy
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (beatmapPages.status == "UNRANKED") {
|
|
210
|
-
awarded_sp = 0;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
console.log("p1");
|
|
214
|
-
|
|
215
|
-
let parsed = [];
|
|
216
|
-
|
|
217
|
-
try {
|
|
218
|
-
parsed = JSON.parse(decryptString(data.additionalData));
|
|
219
|
-
} catch (error) {}
|
|
220
|
-
await supabase.from("scores").upsert({
|
|
221
|
-
beatmapHash: data.mapHash,
|
|
222
|
-
replayHwid: data.relayHwid,
|
|
223
|
-
songId: data.songId,
|
|
224
|
-
userId: userData.id,
|
|
225
|
-
passed,
|
|
226
|
-
misses: data.misses,
|
|
227
|
-
awarded_sp: Math.round(awarded_sp * 100) / 100,
|
|
228
|
-
speed: data.speed,
|
|
229
|
-
mods: data.mods,
|
|
230
|
-
additional_data: parsed,
|
|
231
|
-
spin: data.spin || false,
|
|
232
|
-
});
|
|
233
|
-
console.log("p2");
|
|
234
|
-
|
|
235
|
-
let { data: scores2, error: errorsp } = await supabase
|
|
236
|
-
.from("scores")
|
|
237
|
-
.select(`awarded_sp,beatmapHash,spin`)
|
|
238
|
-
.eq("userId", userData.id)
|
|
239
|
-
.neq("awarded_sp", 0)
|
|
240
|
-
.eq("passed", true)
|
|
241
|
-
.order("awarded_sp", { ascending: false });
|
|
242
|
-
|
|
243
|
-
if (scores2 == null) return NextResponse.json({ error: "No scores" });
|
|
244
|
-
|
|
245
|
-
let allHashMap: Record<string, number> = {};
|
|
246
|
-
let spinHashMap: Record<string, number> = {};
|
|
247
|
-
|
|
248
|
-
for (const score of scores2) {
|
|
249
|
-
const { beatmapHash, awarded_sp } = score;
|
|
250
|
-
|
|
251
|
-
if (!beatmapHash || !awarded_sp) continue;
|
|
252
|
-
|
|
253
|
-
// Normal Scores
|
|
254
|
-
if (!allHashMap[beatmapHash] || allHashMap[beatmapHash] < awarded_sp) {
|
|
255
|
-
allHashMap[beatmapHash] = awarded_sp;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// Spin Scores
|
|
259
|
-
if (score.spin) {
|
|
260
|
-
if (!spinHashMap[beatmapHash] || spinHashMap[beatmapHash] < awarded_sp) {
|
|
261
|
-
spinHashMap[beatmapHash] = awarded_sp;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
// All scores
|
|
266
|
-
const totalSp = weightCalculate(allHashMap);
|
|
267
|
-
|
|
268
|
-
// Only spin scores
|
|
269
|
-
const spinTotalSp = weightCalculate(spinHashMap);
|
|
270
|
-
|
|
271
|
-
await supabase.from("profiles").upsert({
|
|
272
|
-
id: userData.id,
|
|
273
|
-
play_count: (userData.play_count || 0) + 1,
|
|
274
|
-
skill_points: Math.round(totalSp * 100) / 100,
|
|
275
|
-
spin_skill_points: Math.round(spinTotalSp * 100) / 100,
|
|
276
|
-
squares_hit: (userData.squares_hit || 0) + data.hits,
|
|
277
|
-
});
|
|
278
|
-
console.log("p3");
|
|
279
|
-
|
|
280
|
-
if (awarded_sp > 99 && userData.ban == "cool") {
|
|
281
|
-
await postToWebhooks({
|
|
282
|
-
rp: Math.round(awarded_sp * 100) / 100,
|
|
283
|
-
username: userData.username || "",
|
|
284
|
-
userid: userData.id,
|
|
285
|
-
avatar: userData.avatar_url || "",
|
|
286
|
-
mapimage: beatmaps.imageLarge || "",
|
|
287
|
-
spin: data.spin,
|
|
288
|
-
speed: data.speed,
|
|
289
|
-
accuracy: accurracy,
|
|
290
|
-
mapname: beatmaps.title || "",
|
|
291
|
-
mapid: beatmapPages.id || 0,
|
|
292
|
-
misses: data.misses || 0,
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (Math.abs((beatmaps.starRating || 0) - data.virtualStars) > 0.01) {
|
|
297
|
-
return NextResponse.json({
|
|
298
|
-
error: "Map mismatch, no RP points were awarded, please report the bug.",
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return NextResponse.json({});
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export function weightCalculate(hashMap: Record<string, number>) {
|
|
306
|
-
let totalSp = 0;
|
|
307
|
-
let weight = 100;
|
|
308
|
-
|
|
309
|
-
const values = Object.values(hashMap);
|
|
310
|
-
values.sort((a, b) => b - a);
|
|
311
|
-
|
|
312
|
-
for (const score of values) {
|
|
313
|
-
totalSp += ((score || 0) * weight) / 100;
|
|
314
|
-
weight = weight * 0.97;
|
|
315
|
-
|
|
316
|
-
if (weight < 5) {
|
|
317
|
-
break;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return totalSp;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const webHookTemplate = {
|
|
324
|
-
content: null,
|
|
325
|
-
embeds: [
|
|
326
|
-
{
|
|
327
|
-
title: "Captain Lou Albano - Do the Mario",
|
|
328
|
-
url: "https://www.rhythia.com/maps/4469",
|
|
329
|
-
color: 9633967,
|
|
330
|
-
fields: [
|
|
331
|
-
{
|
|
332
|
-
name: "Rhythm Points",
|
|
333
|
-
value: "424 RP",
|
|
334
|
-
inline: true,
|
|
335
|
-
},
|
|
336
|
-
{
|
|
337
|
-
name: "Accuracy",
|
|
338
|
-
value: "100%",
|
|
339
|
-
inline: true,
|
|
340
|
-
},
|
|
341
|
-
{
|
|
342
|
-
name: "Speed",
|
|
343
|
-
value: "1.45x",
|
|
344
|
-
inline: true,
|
|
345
|
-
},
|
|
346
|
-
{
|
|
347
|
-
name: "Playstyle",
|
|
348
|
-
value: "Spin",
|
|
349
|
-
inline: true,
|
|
350
|
-
},
|
|
351
|
-
{
|
|
352
|
-
name: "Misses",
|
|
353
|
-
value: "0",
|
|
354
|
-
inline: true,
|
|
355
|
-
},
|
|
356
|
-
],
|
|
357
|
-
author: {
|
|
358
|
-
name: "cunev",
|
|
359
|
-
url: "https://www.rhythia.com/player/0",
|
|
360
|
-
icon_url:
|
|
361
|
-
"https://static.rhythia.com/user-avatar-1735149648551-a2a8cfbe-af5d-46e8-a19a-be2339c1679a",
|
|
362
|
-
},
|
|
363
|
-
footer: {
|
|
364
|
-
text: "Sun, 22 Dec 2024 22:40:17 GMT",
|
|
365
|
-
},
|
|
366
|
-
thumbnail: {
|
|
367
|
-
url: "https://static.rhythia.com/beatmap-img-1735223264605-eliuka_dj_sharpnel_-_we_luv_lamalarge",
|
|
368
|
-
},
|
|
369
|
-
},
|
|
370
|
-
],
|
|
371
|
-
attachments: [],
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
export async function postToWebhooks({
|
|
375
|
-
rp,
|
|
376
|
-
username,
|
|
377
|
-
userid,
|
|
378
|
-
avatar,
|
|
379
|
-
mapimage,
|
|
380
|
-
spin,
|
|
381
|
-
speed,
|
|
382
|
-
accuracy,
|
|
383
|
-
mapname,
|
|
384
|
-
mapid,
|
|
385
|
-
misses,
|
|
386
|
-
}: {
|
|
387
|
-
rp: number;
|
|
388
|
-
username: string;
|
|
389
|
-
userid: number;
|
|
390
|
-
avatar: string;
|
|
391
|
-
mapimage: string;
|
|
392
|
-
spin: boolean;
|
|
393
|
-
speed: number;
|
|
394
|
-
accuracy: number;
|
|
395
|
-
mapname: string;
|
|
396
|
-
mapid: number;
|
|
397
|
-
misses: number;
|
|
398
|
-
}) {
|
|
399
|
-
const webHooks = await supabase
|
|
400
|
-
.from("discordWebhooks")
|
|
401
|
-
.select("*")
|
|
402
|
-
.eq("type", "scores");
|
|
403
|
-
|
|
404
|
-
if (!webHooks.data) return;
|
|
405
|
-
|
|
406
|
-
for (const webhook of webHooks.data) {
|
|
407
|
-
const webhookUrl = webhook.webhook_link;
|
|
408
|
-
|
|
409
|
-
const embed = webHookTemplate.embeds[0];
|
|
410
|
-
embed.title = mapname;
|
|
411
|
-
embed.url = `https://www.rhythia.com/maps/${mapid}`;
|
|
412
|
-
embed.fields[0].value = `${rp} RP`;
|
|
413
|
-
embed.fields[1].value = `${Math.round(accuracy * 10000) / 100}%`;
|
|
414
|
-
embed.fields[2].value = `${speed}x`;
|
|
415
|
-
embed.fields[3].value = spin ? "Spin" : "Lock";
|
|
416
|
-
embed.fields[4].value = `${misses} misses`;
|
|
417
|
-
embed.author.name = username;
|
|
418
|
-
embed.author.url = `https://www.rhythia.com/player/${userid}`;
|
|
419
|
-
embed.author.icon_url = avatar;
|
|
420
|
-
embed.thumbnail.url = mapimage;
|
|
421
|
-
if (mapimage.includes("backfill")) {
|
|
422
|
-
embed.thumbnail.url = "https://www.rhythia.com/unkimg.png";
|
|
423
|
-
}
|
|
424
|
-
embed.footer.text = new Date().toUTCString();
|
|
425
|
-
await fetch(webhookUrl, {
|
|
426
|
-
method: "POST",
|
|
427
|
-
headers: {
|
|
428
|
-
"Content-Type": "application/json",
|
|
429
|
-
},
|
|
430
|
-
body: JSON.stringify(webHookTemplate),
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
}
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
4
|
+
import { supabase } from "../utils/supabase";
|
|
5
|
+
import { decryptString } from "../utils/security";
|
|
6
|
+
import { isEqual } from "lodash";
|
|
7
|
+
import { getUserBySession } from "../utils/getUserBySession";
|
|
8
|
+
import { User } from "@supabase/supabase-js";
|
|
9
|
+
|
|
10
|
+
export const Schema = {
|
|
11
|
+
input: z.strictObject({
|
|
12
|
+
session: z.string(),
|
|
13
|
+
data: z.strictObject({
|
|
14
|
+
token: z.string(),
|
|
15
|
+
relayHwid: z.string(),
|
|
16
|
+
songId: z.string(),
|
|
17
|
+
misses: z.number(),
|
|
18
|
+
hits: z.number(),
|
|
19
|
+
mapHash: z.string(),
|
|
20
|
+
speed: z.number(),
|
|
21
|
+
mods: z.array(z.string()),
|
|
22
|
+
additionalData: z.any(),
|
|
23
|
+
spin: z.boolean(),
|
|
24
|
+
virtualStars: z.number(),
|
|
25
|
+
}),
|
|
26
|
+
}),
|
|
27
|
+
output: z.object({
|
|
28
|
+
error: z.string().optional(),
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function easeInExpoDeqHard(x: number, star: number) {
|
|
33
|
+
let exponent = 100 - 12 * star;
|
|
34
|
+
if (exponent < 5) exponent = 5;
|
|
35
|
+
return x === 0 ? 0 : Math.pow(2, exponent * x - exponent);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function calculatePerformancePoints(
|
|
39
|
+
starRating: number,
|
|
40
|
+
accuracy: number
|
|
41
|
+
) {
|
|
42
|
+
return (
|
|
43
|
+
Math.round(
|
|
44
|
+
Math.pow(
|
|
45
|
+
(starRating * easeInExpoDeqHard(accuracy, starRating) * 100) / 2,
|
|
46
|
+
2
|
|
47
|
+
) / 1000
|
|
48
|
+
) * 2
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function POST(request: Request): Promise<NextResponse> {
|
|
53
|
+
return protectedApi({
|
|
54
|
+
request,
|
|
55
|
+
schema: Schema,
|
|
56
|
+
authorization: validUser,
|
|
57
|
+
activity: handler,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function handler({
|
|
62
|
+
data,
|
|
63
|
+
session,
|
|
64
|
+
}: (typeof Schema)["input"]["_type"]): Promise<
|
|
65
|
+
NextResponse<(typeof Schema)["output"]["_type"]>
|
|
66
|
+
> {
|
|
67
|
+
const tokenContents = JSON.parse(decryptString(data.token));
|
|
68
|
+
if (
|
|
69
|
+
!isEqual(tokenContents, {
|
|
70
|
+
relayHwid: data.relayHwid,
|
|
71
|
+
songId: data.songId,
|
|
72
|
+
misses: data.misses,
|
|
73
|
+
hits: data.hits,
|
|
74
|
+
mapHash: data.mapHash,
|
|
75
|
+
speed: data.speed,
|
|
76
|
+
mods: data.mods,
|
|
77
|
+
additionalData: data.additionalData,
|
|
78
|
+
spin: data.spin,
|
|
79
|
+
virtualStars: data.virtualStars,
|
|
80
|
+
})
|
|
81
|
+
) {
|
|
82
|
+
return NextResponse.json(
|
|
83
|
+
{
|
|
84
|
+
error: "Token miscalculation",
|
|
85
|
+
},
|
|
86
|
+
{ status: 500 }
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const user = (await getUserBySession(session)) as User;
|
|
91
|
+
|
|
92
|
+
let { data: leversData } = await supabase
|
|
93
|
+
.from("levers")
|
|
94
|
+
.select("*")
|
|
95
|
+
.eq("id", 1)
|
|
96
|
+
.single();
|
|
97
|
+
|
|
98
|
+
if (leversData && leversData.disable_scores) {
|
|
99
|
+
return NextResponse.json({
|
|
100
|
+
error: "Scores are temporarily disabled",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let { data: userData, error: userError } = await supabase
|
|
105
|
+
.from("profiles")
|
|
106
|
+
.select("*")
|
|
107
|
+
.eq("uid", user.id)
|
|
108
|
+
.single();
|
|
109
|
+
|
|
110
|
+
if (!userData)
|
|
111
|
+
return NextResponse.json(
|
|
112
|
+
{
|
|
113
|
+
error: "User doesn't exist",
|
|
114
|
+
},
|
|
115
|
+
{ status: 500 }
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
console.log(userData);
|
|
119
|
+
|
|
120
|
+
if (userData.ban == "excluded" || userData.ban == "restricted") {
|
|
121
|
+
return NextResponse.json(
|
|
122
|
+
{
|
|
123
|
+
error: "Silenced, restricted or excluded players can't submit scores.",
|
|
124
|
+
},
|
|
125
|
+
{ status: 400 }
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let { data: beatmaps, error } = await supabase
|
|
130
|
+
.from("beatmaps")
|
|
131
|
+
.select("*")
|
|
132
|
+
.eq("beatmapHash", data.mapHash)
|
|
133
|
+
.single();
|
|
134
|
+
|
|
135
|
+
let { data: beatmapPages, error: bpError } = await supabase
|
|
136
|
+
.from("beatmapPages")
|
|
137
|
+
.select("*")
|
|
138
|
+
.eq("latestBeatmapHash", data.mapHash)
|
|
139
|
+
.single();
|
|
140
|
+
|
|
141
|
+
if (!beatmapPages) {
|
|
142
|
+
return NextResponse.json(
|
|
143
|
+
{
|
|
144
|
+
error: "Map not submitted",
|
|
145
|
+
},
|
|
146
|
+
{ status: 500 }
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!beatmaps) {
|
|
151
|
+
return NextResponse.json(
|
|
152
|
+
{
|
|
153
|
+
error: "Map not submitted",
|
|
154
|
+
},
|
|
155
|
+
{ status: 500 }
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const noteCount = data.misses + data.hits;
|
|
160
|
+
if (noteCount !== beatmaps.noteCount) {
|
|
161
|
+
return NextResponse.json(
|
|
162
|
+
{
|
|
163
|
+
error: "Wrong map",
|
|
164
|
+
},
|
|
165
|
+
{ status: 500 }
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
await supabase.from("beatmaps").upsert({
|
|
170
|
+
beatmapHash: data.mapHash,
|
|
171
|
+
playcount: (beatmaps.playcount || 1) + 1,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
let passed = true;
|
|
175
|
+
|
|
176
|
+
// Pass invalidation
|
|
177
|
+
if (data.misses + data.hits !== beatmaps.noteCount) {
|
|
178
|
+
passed = false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const accurracy = data.hits / noteCount;
|
|
182
|
+
let awarded_sp = 0;
|
|
183
|
+
|
|
184
|
+
console.log(
|
|
185
|
+
data.misses + data.hits == beatmaps.noteCount,
|
|
186
|
+
data.misses + data.hits,
|
|
187
|
+
beatmaps.noteCount
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
let multiplierMod = 1;
|
|
191
|
+
if (data.mods.includes("mod_hardrock")) {
|
|
192
|
+
multiplierMod *= 1.12;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (data.mods.includes("mod_nofail")) {
|
|
196
|
+
multiplierMod *= Math.pow(0.95, data.misses);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (
|
|
200
|
+
beatmaps.starRating &&
|
|
201
|
+
Math.abs(beatmaps.starRating - data.virtualStars) < 0.01
|
|
202
|
+
) {
|
|
203
|
+
awarded_sp = calculatePerformancePoints(
|
|
204
|
+
data.speed * beatmaps.starRating * multiplierMod,
|
|
205
|
+
accurracy
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (beatmapPages.status == "UNRANKED") {
|
|
210
|
+
awarded_sp = 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
console.log("p1");
|
|
214
|
+
|
|
215
|
+
let parsed = [];
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
parsed = JSON.parse(decryptString(data.additionalData));
|
|
219
|
+
} catch (error) {}
|
|
220
|
+
await supabase.from("scores").upsert({
|
|
221
|
+
beatmapHash: data.mapHash,
|
|
222
|
+
replayHwid: data.relayHwid,
|
|
223
|
+
songId: data.songId,
|
|
224
|
+
userId: userData.id,
|
|
225
|
+
passed,
|
|
226
|
+
misses: data.misses,
|
|
227
|
+
awarded_sp: Math.round(awarded_sp * 100) / 100,
|
|
228
|
+
speed: data.speed,
|
|
229
|
+
mods: data.mods,
|
|
230
|
+
additional_data: parsed,
|
|
231
|
+
spin: data.spin || false,
|
|
232
|
+
});
|
|
233
|
+
console.log("p2");
|
|
234
|
+
|
|
235
|
+
let { data: scores2, error: errorsp } = await supabase
|
|
236
|
+
.from("scores")
|
|
237
|
+
.select(`awarded_sp,beatmapHash,spin`)
|
|
238
|
+
.eq("userId", userData.id)
|
|
239
|
+
.neq("awarded_sp", 0)
|
|
240
|
+
.eq("passed", true)
|
|
241
|
+
.order("awarded_sp", { ascending: false });
|
|
242
|
+
|
|
243
|
+
if (scores2 == null) return NextResponse.json({ error: "No scores" });
|
|
244
|
+
|
|
245
|
+
let allHashMap: Record<string, number> = {};
|
|
246
|
+
let spinHashMap: Record<string, number> = {};
|
|
247
|
+
|
|
248
|
+
for (const score of scores2) {
|
|
249
|
+
const { beatmapHash, awarded_sp } = score;
|
|
250
|
+
|
|
251
|
+
if (!beatmapHash || !awarded_sp) continue;
|
|
252
|
+
|
|
253
|
+
// Normal Scores
|
|
254
|
+
if (!allHashMap[beatmapHash] || allHashMap[beatmapHash] < awarded_sp) {
|
|
255
|
+
allHashMap[beatmapHash] = awarded_sp;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Spin Scores
|
|
259
|
+
if (score.spin) {
|
|
260
|
+
if (!spinHashMap[beatmapHash] || spinHashMap[beatmapHash] < awarded_sp) {
|
|
261
|
+
spinHashMap[beatmapHash] = awarded_sp;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// All scores
|
|
266
|
+
const totalSp = weightCalculate(allHashMap);
|
|
267
|
+
|
|
268
|
+
// Only spin scores
|
|
269
|
+
const spinTotalSp = weightCalculate(spinHashMap);
|
|
270
|
+
|
|
271
|
+
await supabase.from("profiles").upsert({
|
|
272
|
+
id: userData.id,
|
|
273
|
+
play_count: (userData.play_count || 0) + 1,
|
|
274
|
+
skill_points: Math.round(totalSp * 100) / 100,
|
|
275
|
+
spin_skill_points: Math.round(spinTotalSp * 100) / 100,
|
|
276
|
+
squares_hit: (userData.squares_hit || 0) + data.hits,
|
|
277
|
+
});
|
|
278
|
+
console.log("p3");
|
|
279
|
+
|
|
280
|
+
if (awarded_sp > 99 && userData.ban == "cool") {
|
|
281
|
+
await postToWebhooks({
|
|
282
|
+
rp: Math.round(awarded_sp * 100) / 100,
|
|
283
|
+
username: userData.username || "",
|
|
284
|
+
userid: userData.id,
|
|
285
|
+
avatar: userData.avatar_url || "",
|
|
286
|
+
mapimage: beatmaps.imageLarge || "",
|
|
287
|
+
spin: data.spin,
|
|
288
|
+
speed: data.speed,
|
|
289
|
+
accuracy: accurracy,
|
|
290
|
+
mapname: beatmaps.title || "",
|
|
291
|
+
mapid: beatmapPages.id || 0,
|
|
292
|
+
misses: data.misses || 0,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (Math.abs((beatmaps.starRating || 0) - data.virtualStars) > 0.01) {
|
|
297
|
+
return NextResponse.json({
|
|
298
|
+
error: "Map mismatch, no RP points were awarded, please report the bug.",
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return NextResponse.json({});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function weightCalculate(hashMap: Record<string, number>) {
|
|
306
|
+
let totalSp = 0;
|
|
307
|
+
let weight = 100;
|
|
308
|
+
|
|
309
|
+
const values = Object.values(hashMap);
|
|
310
|
+
values.sort((a, b) => b - a);
|
|
311
|
+
|
|
312
|
+
for (const score of values) {
|
|
313
|
+
totalSp += ((score || 0) * weight) / 100;
|
|
314
|
+
weight = weight * 0.97;
|
|
315
|
+
|
|
316
|
+
if (weight < 5) {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return totalSp;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const webHookTemplate = {
|
|
324
|
+
content: null,
|
|
325
|
+
embeds: [
|
|
326
|
+
{
|
|
327
|
+
title: "Captain Lou Albano - Do the Mario",
|
|
328
|
+
url: "https://www.rhythia.com/maps/4469",
|
|
329
|
+
color: 9633967,
|
|
330
|
+
fields: [
|
|
331
|
+
{
|
|
332
|
+
name: "Rhythm Points",
|
|
333
|
+
value: "424 RP",
|
|
334
|
+
inline: true,
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
name: "Accuracy",
|
|
338
|
+
value: "100%",
|
|
339
|
+
inline: true,
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
name: "Speed",
|
|
343
|
+
value: "1.45x",
|
|
344
|
+
inline: true,
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
name: "Playstyle",
|
|
348
|
+
value: "Spin",
|
|
349
|
+
inline: true,
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
name: "Misses",
|
|
353
|
+
value: "0",
|
|
354
|
+
inline: true,
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
author: {
|
|
358
|
+
name: "cunev",
|
|
359
|
+
url: "https://www.rhythia.com/player/0",
|
|
360
|
+
icon_url:
|
|
361
|
+
"https://static.rhythia.com/user-avatar-1735149648551-a2a8cfbe-af5d-46e8-a19a-be2339c1679a",
|
|
362
|
+
},
|
|
363
|
+
footer: {
|
|
364
|
+
text: "Sun, 22 Dec 2024 22:40:17 GMT",
|
|
365
|
+
},
|
|
366
|
+
thumbnail: {
|
|
367
|
+
url: "https://static.rhythia.com/beatmap-img-1735223264605-eliuka_dj_sharpnel_-_we_luv_lamalarge",
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
],
|
|
371
|
+
attachments: [],
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export async function postToWebhooks({
|
|
375
|
+
rp,
|
|
376
|
+
username,
|
|
377
|
+
userid,
|
|
378
|
+
avatar,
|
|
379
|
+
mapimage,
|
|
380
|
+
spin,
|
|
381
|
+
speed,
|
|
382
|
+
accuracy,
|
|
383
|
+
mapname,
|
|
384
|
+
mapid,
|
|
385
|
+
misses,
|
|
386
|
+
}: {
|
|
387
|
+
rp: number;
|
|
388
|
+
username: string;
|
|
389
|
+
userid: number;
|
|
390
|
+
avatar: string;
|
|
391
|
+
mapimage: string;
|
|
392
|
+
spin: boolean;
|
|
393
|
+
speed: number;
|
|
394
|
+
accuracy: number;
|
|
395
|
+
mapname: string;
|
|
396
|
+
mapid: number;
|
|
397
|
+
misses: number;
|
|
398
|
+
}) {
|
|
399
|
+
const webHooks = await supabase
|
|
400
|
+
.from("discordWebhooks")
|
|
401
|
+
.select("*")
|
|
402
|
+
.eq("type", "scores");
|
|
403
|
+
|
|
404
|
+
if (!webHooks.data) return;
|
|
405
|
+
|
|
406
|
+
for (const webhook of webHooks.data) {
|
|
407
|
+
const webhookUrl = webhook.webhook_link;
|
|
408
|
+
|
|
409
|
+
const embed = webHookTemplate.embeds[0];
|
|
410
|
+
embed.title = mapname;
|
|
411
|
+
embed.url = `https://www.rhythia.com/maps/${mapid}`;
|
|
412
|
+
embed.fields[0].value = `${rp} RP`;
|
|
413
|
+
embed.fields[1].value = `${Math.round(accuracy * 10000) / 100}%`;
|
|
414
|
+
embed.fields[2].value = `${speed}x`;
|
|
415
|
+
embed.fields[3].value = spin ? "Spin" : "Lock";
|
|
416
|
+
embed.fields[4].value = `${misses} misses`;
|
|
417
|
+
embed.author.name = username;
|
|
418
|
+
embed.author.url = `https://www.rhythia.com/player/${userid}`;
|
|
419
|
+
embed.author.icon_url = avatar;
|
|
420
|
+
embed.thumbnail.url = mapimage;
|
|
421
|
+
if (mapimage.includes("backfill")) {
|
|
422
|
+
embed.thumbnail.url = "https://www.rhythia.com/unkimg.png";
|
|
423
|
+
}
|
|
424
|
+
embed.footer.text = new Date().toUTCString();
|
|
425
|
+
await fetch(webhookUrl, {
|
|
426
|
+
method: "POST",
|
|
427
|
+
headers: {
|
|
428
|
+
"Content-Type": "application/json",
|
|
429
|
+
},
|
|
430
|
+
body: JSON.stringify(webHookTemplate),
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|