-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-helpers.js
More file actions
295 lines (253 loc) · 9.45 KB
/
api-helpers.js
File metadata and controls
295 lines (253 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// API Helper Functions for Snake Game Highscores
// This file contains functions to interact with Supabase
// Load config (with fallback if not loaded)
let SUPABASE_URL = '';
let SUPABASE_KEY = '';
// Initialize config when SUPABASE_CONFIG is available
function initSupabaseConfig() {
if (typeof SUPABASE_CONFIG !== 'undefined' && SUPABASE_CONFIG) {
SUPABASE_URL = SUPABASE_CONFIG.url || '';
SUPABASE_KEY = SUPABASE_CONFIG.anonKey || '';
} else {
console.error('SUPABASE_CONFIG is not defined. Make sure config.js is loaded before api-helpers.js');
}
}
// Try to initialize immediately if config is already loaded
// Also handle the case where config.js might not exist (e.g., on GitHub Pages if not deployed)
(function () {
if (typeof SUPABASE_CONFIG !== 'undefined') {
initSupabaseConfig();
} else {
// Wait a bit for config.js to load (if it exists)
setTimeout(() => {
if (typeof SUPABASE_CONFIG !== 'undefined') {
initSupabaseConfig();
} else {
console.warn('config.js not found. Supabase features will be disabled. Make sure config.js exists with your Supabase credentials.');
}
}, 100);
}
})();
// Profanity wordlist - loaded from profanity.txt
let PROFANITY_WORDS = [];
let profanityLoaded = false;
/**
* Load profanity wordlist from file (call this once on page load)
* @returns {Promise<void>}
*/
async function loadProfanityWords() {
if (profanityLoaded) return; // Already loaded
try {
const response = await fetch('profanity.txt');
if (!response.ok) {
throw new Error('Failed to load profanity wordlist');
}
const text = await response.text();
// Split by newlines and filter out empty lines
PROFANITY_WORDS = text.split('\n')
.map(line => line.trim().toLowerCase())
.filter(line => line.length > 0);
profanityLoaded = true;
} catch (error) {
console.error('Error loading profanity wordlist:', error);
// Fallback to empty array if file can't be loaded
PROFANITY_WORDS = [];
}
}
/**
* Check if a username contains profanity (synchronous check against loaded wordlist)
* @param {string} username - Username to check
* @returns {boolean} - True if contains profanity, false otherwise
*/
function containsProfanity(username) {
if (PROFANITY_WORDS.length === 0) {
// Wordlist not loaded yet, skip check
return false;
}
const lowerUsername = username.toLowerCase().trim();
// Check if entire username is a profanity word
for (const word of PROFANITY_WORDS) {
const cleanWord = word.toLowerCase().trim();
if (lowerUsername === cleanWord) {
return true;
}
}
// Check for profanity words as separate words (with word boundaries)
// This prevents "ass" from matching "class" or "pass"
for (const word of PROFANITY_WORDS) {
const cleanWord = word.toLowerCase().trim();
// Skip very short words (less than 3 chars) to reduce false positives
if (cleanWord.length < 3) {
continue;
}
// Escape special regex characters
const escapedWord = cleanWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Check for whole word match with word boundaries
// This ensures the profanity word appears as a complete word, not embedded
const wordRegex = new RegExp('\\b' + escapedWord + '\\b', 'i');
if (wordRegex.test(lowerUsername)) {
return true;
}
}
return false;
}
/**
* Get top scores from the database
* @param {number} limit - Number of scores to return (default: 50)
* @returns {Promise<Array>} Array of score objects with username, score, rank, etc.
*/
async function getTopScores(limit = 50) {
// Ensure config is initialized
if (!SUPABASE_URL || !SUPABASE_KEY) {
initSupabaseConfig();
}
if (!SUPABASE_URL || !SUPABASE_KEY) {
throw new Error('Supabase configuration not available. Please check config.js');
}
try {
// Query the table directly to include speed and skin columns
const response = await fetch(`${SUPABASE_URL}/rest/v1/highscores?select=username,score,speed,skin&order=score.desc&limit=${limit}`, {
method: 'GET',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to fetch scores: ${response.status} ${errorText}`);
}
return await response.json();
} catch (error) {
console.error('Error fetching scores:', error);
throw error;
}
}
/**
* Convert hex color code to color name
* @param {string} hexColor - Hex color code (e.g., '#FF0000')
* @returns {string} Color name (e.g., 'RED') or original value if not recognized
*/
function hexToColorName(hexColor) {
// Handle special values
const colorStr = String(hexColor).trim();
if (colorStr.toUpperCase() === 'RAINBOW') {
return 'RAINBOW';
}
if (colorStr.toUpperCase() === 'HACKER') {
return 'HACKER';
}
if (colorStr.toUpperCase() === 'SPEED') {
return 'SPEED';
}
if (colorStr.toUpperCase() === 'NINJA') {
return 'NINJA';
}
const colorMap = {
'#FF0000': 'RED',
'#FFA500': 'ORANGE',
'#E1FF00': 'YELLOW',
'#00FF00': 'GREEN',
'#0000FF': 'BLUE',
'#FF00FF': 'PURPLE',
'#800080': 'DARK_PURPLE',
'#000000': 'BLACK',
'#C8C8C8': 'GRAY',
'#FFFFFF': 'WHITE',
'#008000': 'DARKGREEN'
};
// If it's already a color name, return as is
const colorNames = Object.values(colorMap);
if (colorNames.includes(colorStr.toUpperCase())) {
return colorStr.toUpperCase();
}
// Convert hex to uppercase for comparison and map to color name
const upperHex = colorStr.toUpperCase();
return colorMap[upperHex] || colorStr; // Return mapped name or original if not found
}
/**
* Submit a score to the database
* @param {string} username - Username (will be validated server-side)
* @param {number} score - Score value (will be validated server-side)
* @param {number} speed - Speed value (optional, will be saved if provided)
* @param {string} skin - Skin/color value (optional, will be saved if provided)
* @returns {Promise<Object>} Response object with success status and updated scores
*/
async function submitScore(username, score, speed = null, skin = null) {
// Ensure config is initialized
if (!SUPABASE_URL || !SUPABASE_KEY) {
initSupabaseConfig();
}
if (!SUPABASE_URL || !SUPABASE_KEY) {
throw new Error('Supabase configuration not available. Please check config.js');
}
try {
// Basic client-side validation (server will also validate)
if (!username || username.trim().length === 0) {
throw new Error('Username is required');
}
if (username.trim().length > 20) {
throw new Error('Username must be 20 characters or less');
}
// Check for profanity (synchronous check)
if (containsProfanity(username)) {
throw new Error('Username contains inappropriate content. Please choose a different username.');
}
if (typeof score !== 'number' || score < 0) {
throw new Error('Score must be a non-negative number');
}
// Prepare the request body
const requestBody = {
username: username.trim(),
score: Math.floor(score) // Ensure integer
};
// Add speed if provided
if (speed !== null && speed !== undefined) {
requestBody.speed = Math.floor(speed);
}
// Add skin if provided (convert hex code to color name)
if (skin !== null && skin !== undefined) {
const skinValue = skin.toString();
requestBody.skin = hexToColorName(skinValue);
}
const response = await fetch(`${SUPABASE_URL}/rest/v1/highscores`, {
method: 'POST',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Content-Type': 'application/json',
'Prefer': 'return=representation'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
const errorText = await response.text();
let errorMessage = `Failed to submit score: ${response.status}`;
try {
const errorJson = JSON.parse(errorText);
if (errorJson.message) {
errorMessage = errorJson.message;
}
} catch (e) {
errorMessage = errorText;
}
throw new Error(errorMessage);
}
const data = await response.json();
// Get updated top scores after submission
const topScores = await getTopScores(50);
return {
success: true,
inserted: data,
topScores: topScores
};
} catch (error) {
console.error('Error submitting score:', error);
throw error;
}
}
// Export functions (if using modules)
if (typeof module !== 'undefined' && module.exports) {
module.exports = { getTopScores, submitScore };
}