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