1
1
import { createClient } from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'
2
2
export const SUPABASE_URL = "https://fohgyexhzptaxjqrrrfd.supabase.co" ;
3
3
export const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZvaGd5ZXhoenB0YXhqcXJycmZkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDUwMjQxNTcsImV4cCI6MjAyMDYwMDE1N30.fa7XvwiBbWSe2MLIR6Wkh_OC95uV7UXxt7_25PlyAlc"
4
- export const SUPABASE = createClient ( SUPABASE_URL , SUPABASE_ANON_KEY ) ;
4
+ export default SUPABASE = createClient ( SUPABASE_URL , SUPABASE_ANON_KEY ) ;
5
5
window . SUPABASE = SUPABASE ;
6
6
7
+ // #region Type definitions
7
8
/**
8
9
* @typedef {Object } User
9
10
* @property {string } id
10
- * @property {string } join_date
11
11
* @property {string } username
12
12
* @property {AccountState } account_state
13
+ * @property {Role } role
14
+ * @property {string } bio
13
15
*/
14
16
15
17
/**
16
18
* @typedef {Object } Submission
17
19
* @property {string } id
18
20
* @property {string } game_mode
19
- * @property {string } username
20
- * @property {Object } score
21
+ * @property {Object | null } score
21
22
* @property {string } upload_date
22
- * @property {string } replay_date
23
- * @property {string } replay_path
23
+ * @property {string? } replay_date
24
24
* @property {string } submitted_by
25
+ * @property {SubmissionValidity } validity
26
+ * @property {string? } proof
27
+ */
28
+
29
+ /**
30
+ * @typedef {Object } ReplayData
31
+ * @property {string } submission_id
32
+ * @property {string } replay_data - Base64 encoded replay data
25
33
*/
26
34
27
35
/**
28
36
* @readonly
29
37
* @enum {string}
30
38
*/
31
39
export const AccountState = {
32
- NORMAL : "normal" ,
33
- BANNED : "banned" ,
34
- UNVERIFIED : "unverified"
35
- }
40
+ NORMAL : "Normal" ,
41
+ BANNED : "Banned" ,
42
+ UNVERIFIED : "Unverified"
43
+ }
44
+
45
+ /**
46
+ * @readonly
47
+ * @enum {string}
48
+ */
49
+ export const Role = {
50
+ USER : "User" ,
51
+ VERIFIER : "Verifier" ,
52
+ ADMIN : "Administrator"
53
+ }
54
+
55
+ /**
56
+ * @readonly
57
+ * @enum {string}
58
+ */
59
+ export const SubmissionValidity = {
60
+ UNVERIFIED : "Unverified" ,
61
+ VERIFIED : "Verified" ,
62
+ SUSPICIOUS : "Suspicious" ,
63
+ REJECTED : "Rejected"
64
+ }
65
+ // #endregion
66
+
67
+ export const TABLES = {
68
+ PROFILES : "profiles" ,
69
+ SUBMISSIONS : "submissions" ,
70
+ REPLAYS : "replays" ,
71
+ }
72
+
73
+ // #region Profile table functions
74
+ export async function getAllProfiles ( limit = 10 , offset = 0 ) {
75
+ const { data, error } = await SUPABASE . from ( TABLES . PROFILES )
76
+ . select ( '*' )
77
+ . range ( offset , limit ) ;
78
+
79
+ if ( error ) throw new Error ( error . message ) ;
80
+ return data ;
81
+ }
82
+ export async function getProfileById ( id ) {
83
+ const { data, error } = await SUPABASE . from ( TABLES . PROFILES )
84
+ . select ( 1 )
85
+ . eq ( 'id' , id ) ;
86
+
87
+ if ( error ) throw new Error ( error . message ) ;
88
+ return data [ 0 ] ;
89
+ }
90
+ export async function getProfileByUsername ( username ) {
91
+ const { data, error } = await SUPABASE . from ( TABLES . PROFILES )
92
+ . select ( 1 )
93
+ . eq ( 'username' , username ) ;
94
+
95
+ if ( error ) throw new Error ( error . message ) ;
96
+ return data [ 0 ] ;
97
+ }
98
+
99
+ export async function editProfile ( id , newProfileData ) {
100
+ const { data, error } = await SUPABASE . from ( TABLES . PROFILES )
101
+ . update ( newProfileData )
102
+ . eq ( 'id' , id ) ;
103
+
104
+ if ( error ) throw new Error ( error . message ) ;
105
+ return data ;
106
+ }
107
+ // #endregion
108
+
109
+ // #region Submission table functions
110
+ export async function getSubmissionsByGameMode ( gameMode , limit = 10 , offset = 0 ) {
111
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
112
+ . select ( '*' )
113
+ . eq ( 'game_mode' , gameMode )
114
+ . range ( offset , limit ) ;
115
+
116
+ if ( error ) throw new Error ( error . message ) ;
117
+ return data ;
118
+ }
119
+ export async function getSubmissionById ( id ) {
120
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
121
+ . select ( 1 )
122
+ . eq ( 'id' , id ) ;
123
+
124
+ if ( error ) throw new Error ( error . message ) ;
125
+ return data [ 0 ] ;
126
+ }
127
+ export async function getSubmissionByUserId ( userId , limit = 10 , offset = 0 ) {
128
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
129
+ . select ( '*' )
130
+ . eq ( 'submitted_by' , userId )
131
+ . range ( offset , limit ) ;
132
+
133
+ if ( error ) throw new Error ( error . message ) ;
134
+ return data ;
135
+ }
136
+
137
+ export async function createSubmission ( submissionData ) {
138
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
139
+ . insert ( submissionData ) ;
140
+
141
+ if ( error ) throw new Error ( error . message ) ;
142
+ return data ;
143
+ }
144
+ export async function editSubmission ( id , newSubmissionData ) {
145
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
146
+ . update ( newSubmissionData )
147
+ . eq ( 'id' , id ) ;
148
+
149
+ if ( error ) throw new Error ( error . message ) ;
150
+ return data ;
151
+ }
152
+ export async function deleteSubmission ( id ) {
153
+ const { data, error } = await SUPABASE . from ( TABLES . SUBMISSIONS )
154
+ . delete ( )
155
+ . eq ( 'id' , id ) ;
156
+
157
+ if ( error ) throw new Error ( error . message ) ;
158
+ return data ;
159
+ }
160
+ // #endregion
161
+
162
+ // #region Replay table functions
163
+ export async function getReplayDataBySubmissionId ( submissionId ) {
164
+ const { data, error } = await SUPABASE . from ( TABLES . REPLAYS )
165
+ . select ( '*' )
166
+ . eq ( 'submission_id' , submissionId ) ;
167
+
168
+ if ( error ) throw new Error ( error . message ) ;
169
+ return data [ 0 ] ;
170
+ }
171
+
172
+ export async function createReplayData ( replayData ) {
173
+ const { data, error } = await SUPABASE . from ( TABLES . REPLAYS )
174
+ . insert ( replayData ) ;
175
+
176
+ if ( error ) throw new Error ( error . message ) ;
177
+ return data ;
178
+ }
179
+ export async function deleteReplayData ( submissionId ) {
180
+ const { data, error } = await SUPABASE . from ( TABLES . REPLAYS )
181
+ . delete ( )
182
+ . eq ( 'submission_id' , submissionId ) ;
183
+
184
+ if ( error ) throw new Error ( error . message ) ;
185
+ return data ;
186
+ }
187
+ // #endregion
0 commit comments