-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
197 lines (171 loc) · 5.17 KB
/
api.js
File metadata and controls
197 lines (171 loc) · 5.17 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
/**
* FruitMatch - API Client
* Developed by DevCatanzaro
*
* Backend: Node.js + Express
*/
// API URL - Change this to your server URL
const API_URL = window.location.origin + "/api";
/**
* Submit or update user score for a specific mode
* Only updates if the new score is higher
* @param {Object} data - {username, score, mode, level, lines}
* @returns {Object} - {record, ranking: {position, isNewPersonalBest, previousBest, isInTop10, totalPlayers}}
*/
async function submitScorePocketBase({username, score, mode = 'free', level = 0, lines = 0}) {
try {
const res = await fetch(`${API_URL}/scores`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ username, score, mode, level, lines })
});
if (!res.ok) {
console.error('Error saving score:', await res.text());
return null;
}
const data = await res.json();
console.log(`Score saved: ${username} - ${mode} - ${score}`, data);
return data;
} catch (e) {
console.error('Error in submitScorePocketBase:', e);
return null;
}
}
/**
* Get ranking from API
* @param {number} limit - number of results
* @param {string} mode - game mode ('all' for global)
*/
async function getTopScoresPocketBase(limit = 10, mode = 'all') {
try {
const params = new URLSearchParams({ limit });
if (mode && mode !== 'all') {
params.append('mode', mode);
}
const res = await fetch(`${API_URL}/scores?${params}`);
if (!res.ok) {
console.error('Error getting ranking:', await res.text());
return [];
}
const data = await res.json();
return data.scores || [];
} catch (e) {
console.error('Error in getTopScoresPocketBase:', e);
return [];
}
}
/**
* Get user stats
* @param {string} username
*/
async function getUserStatsPocketBase(username) {
try {
const res = await fetch(`${API_URL}/stats/${encodeURIComponent(username)}`);
if (!res.ok) {
return null;
}
return await res.json();
} catch (e) {
console.error('Error in getUserStatsPocketBase:', e);
return null;
}
}
/**
* Update user stats
* @param {string} username
* @param {Object} stats
*/
async function updateUserStatsPocketBase(username, stats) {
try {
const res = await fetch(`${API_URL}/stats/${encodeURIComponent(username)}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(stats)
});
if (!res.ok) {
console.error('Error updating stats:', await res.text());
return null;
}
return await res.json();
} catch (e) {
console.error('Error in updateUserStatsPocketBase:', e);
return null;
}
}
/**
* Get user progress (adventure mode)
* @param {string} username
*/
async function getUserProgressPocketBase(username) {
try {
const res = await fetch(`${API_URL}/progress/${encodeURIComponent(username)}`);
if (!res.ok) {
return null;
}
return await res.json();
} catch (e) {
console.error('Error in getUserProgressPocketBase:', e);
return null;
}
}
/**
* Save user progress (adventure mode)
* @param {string} username
* @param {Object} progress
*/
async function saveUserProgressPocketBase(username, progress) {
try {
const res = await fetch(`${API_URL}/progress/${encodeURIComponent(username)}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(progress)
});
if (!res.ok) {
console.error('Error saving progress:', await res.text());
return null;
}
return await res.json();
} catch (e) {
console.error('Error in saveUserProgressPocketBase:', e);
return null;
}
}
/**
* Log game event
* @param {string} username
* @param {string} eventType
* @param {Object} eventData
*/
async function logGameEvent(username, eventType, eventData) {
try {
const res = await fetch(`${API_URL}/logs/${encodeURIComponent(username)}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ eventType, eventData })
});
if (!res.ok) {
console.error('Error logging event:', await res.text());
return null;
}
return await res.json();
} catch (e) {
console.error('Error in logGameEvent:', e);
return null;
}
}
/**
* Get existing usernames (for suggestions)
*/
async function getExistingUsernames() {
try {
const res = await fetch(`${API_URL}/usernames`);
if (!res.ok) {
return [];
}
const data = await res.json();
return data.usernames || [];
} catch (e) {
console.error('Error in getExistingUsernames:', e);
return [];
}
}