rhythia-api 82.0.0 → 83.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.
@@ -1,6 +1,7 @@
1
1
  import { NextResponse } from "next/server";
2
2
  import z from "zod";
3
3
  import { protectedApi } from "../utils/requestUtils";
4
+ import { supabase } from "../utils/supabase";
4
5
 
5
6
  export const Schema = {
6
7
  input: z.strictObject({
@@ -13,6 +14,8 @@ export const Schema = {
13
14
  triggers: z.array(z.array(z.number())),
14
15
  mapHash: z.string(),
15
16
  mapTitle: z.string(),
17
+ mapDifficulty: z.number(),
18
+ noteCount: z.number(),
16
19
  }),
17
20
  }),
18
21
  output: z.object({
@@ -29,8 +32,61 @@ export async function POST(res: Response): Promise<NextResponse> {
29
32
  });
30
33
  }
31
34
 
32
- export async function handler(
33
- data: (typeof Schema)["input"]["_type"]
34
- ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
35
+ export async function handler({
36
+ data,
37
+ session,
38
+ }: (typeof Schema)["input"]["_type"]): Promise<
39
+ NextResponse<(typeof Schema)["output"]["_type"]>
40
+ > {
41
+ const user = (await supabase.auth.getUser(session)).data.user!;
42
+
43
+ let { data: userData, error: userError } = await supabase
44
+ .from("profiles")
45
+ .select("*")
46
+ .eq("uid", user.id);
47
+
48
+ if (!userData?.length)
49
+ return NextResponse.json(
50
+ {
51
+ error: "User doesn't exist",
52
+ },
53
+ { status: 500 }
54
+ );
55
+
56
+ let { data: beatmaps, error } = await supabase
57
+ .from("beatmaps")
58
+ .select("*")
59
+ .eq("beatmapHash", data.mapHash);
60
+
61
+ let newPlaycount = 1;
62
+
63
+ if (beatmaps?.length) {
64
+ newPlaycount = (beatmaps[0].playcount || 1) + 1;
65
+ }
66
+
67
+ const upsertData = await supabase
68
+ .from("beatmaps")
69
+ .upsert({
70
+ beatmapHash: data.mapHash,
71
+ beatmapTitle: data.mapTitle,
72
+ playcount: newPlaycount,
73
+ difficulty: data.mapDifficulty,
74
+ noteCount: data.noteCount,
75
+ })
76
+ .select();
77
+
78
+ const insertData = await supabase
79
+ .from("scores")
80
+ .upsert({
81
+ beatmapHash: data.mapHash,
82
+ noteResults: data.noteResults,
83
+ replayHwid: data.relayHwid,
84
+ songId: data.songId,
85
+ triggers: data.triggers,
86
+ userId: userData[0].id,
87
+ passed: data.noteCount == Object.keys(data.noteResults).length,
88
+ })
89
+ .select();
90
+
35
91
  return NextResponse.json({});
36
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhythia-api",
3
- "version": "82.0.0",
3
+ "version": "83.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",
package/types/database.ts CHANGED
@@ -9,6 +9,33 @@ export type Json =
9
9
  export type Database = {
10
10
  public: {
11
11
  Tables: {
12
+ beatmaps: {
13
+ Row: {
14
+ beatmapHash: string
15
+ beatmapTitle: string | null
16
+ created_at: string
17
+ difficulty: number | null
18
+ noteCount: number | null
19
+ playcount: number | null
20
+ }
21
+ Insert: {
22
+ beatmapHash: string
23
+ beatmapTitle?: string | null
24
+ created_at?: string
25
+ difficulty?: number | null
26
+ noteCount?: number | null
27
+ playcount?: number | null
28
+ }
29
+ Update: {
30
+ beatmapHash?: string
31
+ beatmapTitle?: string | null
32
+ created_at?: string
33
+ difficulty?: number | null
34
+ noteCount?: number | null
35
+ playcount?: number | null
36
+ }
37
+ Relationships: []
38
+ }
12
39
  profiles: {
13
40
  Row: {
14
41
  about_me: string | null
@@ -65,6 +92,60 @@ export type Database = {
65
92
  },
66
93
  ]
67
94
  }
95
+ scores: {
96
+ Row: {
97
+ beatmapHash: string | null
98
+ created_at: string
99
+ id: number
100
+ noteResults: Json | null
101
+ passed: boolean | null
102
+ playedAt: number | null
103
+ replayHwid: string | null
104
+ songId: string | null
105
+ triggers: Json | null
106
+ userId: number | null
107
+ }
108
+ Insert: {
109
+ beatmapHash?: string | null
110
+ created_at?: string
111
+ id?: number
112
+ noteResults?: Json | null
113
+ passed?: boolean | null
114
+ playedAt?: number | null
115
+ replayHwid?: string | null
116
+ songId?: string | null
117
+ triggers?: Json | null
118
+ userId?: number | null
119
+ }
120
+ Update: {
121
+ beatmapHash?: string | null
122
+ created_at?: string
123
+ id?: number
124
+ noteResults?: Json | null
125
+ passed?: boolean | null
126
+ playedAt?: number | null
127
+ replayHwid?: string | null
128
+ songId?: string | null
129
+ triggers?: Json | null
130
+ userId?: number | null
131
+ }
132
+ Relationships: [
133
+ {
134
+ foreignKeyName: "scores_beatmapHash_fkey"
135
+ columns: ["beatmapHash"]
136
+ isOneToOne: false
137
+ referencedRelation: "beatmaps"
138
+ referencedColumns: ["beatmapHash"]
139
+ },
140
+ {
141
+ foreignKeyName: "scores_userId_fkey"
142
+ columns: ["userId"]
143
+ isOneToOne: false
144
+ referencedRelation: "profiles"
145
+ referencedColumns: ["id"]
146
+ },
147
+ ]
148
+ }
68
149
  }
69
150
  Views: {
70
151
  [_ in never]: never