rhythia-api 85.0.0 → 87.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/.env ADDED
@@ -0,0 +1 @@
1
+ ADMIN_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBma2FqbmdibGxjYmR6b3lscnZwIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcxODU3NjA3MCwiZXhwIjoyMDM0MTUyMDcwfQ.XKUlQWvzmcYyirM-Zi4nwhiEKcpx1xLS97QUyuR3MoY"
@@ -6,11 +6,12 @@ import { supabase } from "../utils/supabase";
6
6
  export const Schema = {
7
7
  input: z.strictObject({
8
8
  session: z.string(),
9
- page: z.number().optional(),
9
+ page: z.number().default(1),
10
10
  }),
11
11
  output: z.object({
12
12
  error: z.string().optional(),
13
13
  total: z.number().optional(),
14
+ viewPerPage: z.number(),
14
15
  leaderboard: z
15
16
  .array(
16
17
  z.object({
@@ -26,9 +27,9 @@ export const Schema = {
26
27
  }),
27
28
  };
28
29
 
29
- export async function POST(res: Response): Promise<NextResponse> {
30
+ export async function POST(request: Request): Promise<NextResponse> {
30
31
  return protectedApi({
31
- response: res,
32
+ request,
32
33
  schema: Schema,
33
34
  authorization: validUser,
34
35
  activity: handler,
@@ -38,12 +39,16 @@ export async function POST(res: Response): Promise<NextResponse> {
38
39
  export async function handler(
39
40
  data: (typeof Schema)["input"]["_type"]
40
41
  ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
41
- const range = [0, 100];
42
- if (data.page) {
43
- range[0] = 100 * data.page;
44
- range[1] = range[0] + 100;
45
- }
42
+ const result = await getLeaderboard(data.page);
43
+ return NextResponse.json(result);
44
+ }
45
+
46
+ const VIEW_PER_PAGE = 2;
46
47
 
48
+ export async function getLeaderboard(page = 1) {
49
+ const startPage = (page - 1) * VIEW_PER_PAGE;
50
+ const endPage = startPage + VIEW_PER_PAGE - 1;
51
+ console.log(startPage, endPage);
47
52
  const countQuery = await supabase
48
53
  .from("profiles")
49
54
  .select("*", { count: "exact", head: true });
@@ -52,10 +57,11 @@ export async function handler(
52
57
  .from("profiles")
53
58
  .select("*")
54
59
  .order("skill_points", { ascending: false })
55
- .range(range[0], range[1]);
60
+ .range(startPage, endPage);
56
61
 
57
- return NextResponse.json({
62
+ return {
58
63
  total: countQuery.count || 0,
64
+ viewPerPage: VIEW_PER_PAGE,
59
65
  leaderboard: queryData?.map((user) => ({
60
66
  flag: user.flag,
61
67
  id: user.id,
@@ -64,5 +70,5 @@ export async function handler(
64
70
  total_score: user.total_score,
65
71
  username: user.username,
66
72
  })),
67
- });
73
+ };
68
74
  }
@@ -32,13 +32,10 @@ export async function POST(request: Request) {
32
32
  export async function handler(data: (typeof Schema)["input"]["_type"]) {
33
33
  const { data: searchData, error } = await supabase
34
34
  .from("profiles")
35
- .select()
35
+ .select("id,username")
36
36
  .ilike("username", `%${data.text}%`)
37
37
  .limit(10);
38
38
  return NextResponse.json({
39
- results: (searchData || []).map((data) => ({
40
- id: data.id,
41
- username: data.username,
42
- })),
39
+ results: searchData || [],
43
40
  });
44
41
  }
@@ -44,9 +44,10 @@ export async function handler({
44
44
  let { data: userData, error: userError } = await supabase
45
45
  .from("profiles")
46
46
  .select("*")
47
- .eq("uid", user.id);
47
+ .eq("uid", user.id)
48
+ .single();
48
49
 
49
- if (!userData?.length)
50
+ if (!userData)
50
51
  return NextResponse.json(
51
52
  {
52
53
  error: "User doesn't exist",
@@ -56,51 +57,45 @@ export async function handler({
56
57
 
57
58
  let { data: beatmaps, error } = await supabase
58
59
  .from("beatmaps")
59
- .select("*")
60
- .eq("beatmapHash", data.mapHash);
60
+ .select("playcount")
61
+ .eq("beatmapHash", data.mapHash)
62
+ .single();
61
63
 
62
64
  let newPlaycount = 1;
63
65
 
64
- if (beatmaps?.length) {
65
- newPlaycount = (beatmaps[0].playcount || 1) + 1;
66
+ if (beatmaps) {
67
+ newPlaycount = (beatmaps.playcount || 1) + 1;
66
68
  }
67
69
 
68
- const upsertData = await supabase
69
- .from("beatmaps")
70
- .upsert({
71
- beatmapHash: data.mapHash,
72
- title: data.mapTitle,
73
- playcount: newPlaycount,
74
- difficulty: data.mapDifficulty,
75
- noteCount: data.mapNoteCount,
76
- length: data.mapLength,
77
- })
78
- .select();
70
+ const p1 = supabase.from("beatmaps").upsert({
71
+ beatmapHash: data.mapHash,
72
+ title: data.mapTitle,
73
+ playcount: newPlaycount,
74
+ difficulty: data.mapDifficulty,
75
+ noteCount: data.mapNoteCount,
76
+ length: data.mapLength,
77
+ });
79
78
 
80
- const insertData = await supabase
81
- .from("scores")
82
- .upsert({
83
- beatmapHash: data.mapHash,
84
- noteResults: data.noteResults,
85
- replayHwid: data.relayHwid,
86
- songId: data.songId,
87
- triggers: data.triggers,
88
- userId: userData[0].id,
89
- passed: data.mapNoteCount == Object.keys(data.noteResults).length,
90
- misses: Object.values(data.noteResults).filter((e) => !e).length,
91
- })
92
- .select();
79
+ const p2 = supabase.from("scores").upsert({
80
+ beatmapHash: data.mapHash,
81
+ noteResults: data.noteResults,
82
+ replayHwid: data.relayHwid,
83
+ songId: data.songId,
84
+ triggers: data.triggers,
85
+ userId: userData[0].id,
86
+ passed: data.mapNoteCount == Object.keys(data.noteResults).length,
87
+ misses: Object.values(data.noteResults).filter((e) => !e).length,
88
+ });
93
89
 
94
- const insertUserData = await supabase
95
- .from("profiles")
96
- .upsert({
97
- id: userData[0].id,
98
- play_count: (userData[0].play_count || 0) + 1,
99
- squares_hit:
100
- (userData[0].squares_hit || 0) +
101
- Object.values(data.noteResults).filter((e) => e).length,
102
- })
103
- .select();
90
+ const p3 = supabase.from("profiles").upsert({
91
+ id: userData[0].id,
92
+ play_count: (userData[0].play_count || 0) + 1,
93
+ squares_hit:
94
+ (userData[0].squares_hit || 0) +
95
+ Object.values(data.noteResults).filter((e) => e).length,
96
+ });
97
+
98
+ await Promise.all([p1, p2, p3]);
104
99
 
105
100
  return NextResponse.json({});
106
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhythia-api",
3
- "version": "85.0.0",
3
+ "version": "87.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",
package/test.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { getLeaderboard } from "./api/getLeaderboard";
2
+
3
+ async function main() {
4
+ const result = await getLeaderboard(1);
5
+ console.log(result);
6
+ }
7
+
8
+ main();