Spaces:
Running
Running
File size: 4,528 Bytes
af86b7d |
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 |
/**
* HF Leaderboard API Client
* Communicates with FastAPI backend for HF Hub leaderboard persistence
*/
export class HFLeaderboardAPI {
constructor(baseUrl = '') {
// If no baseUrl provided, use current origin (works for both dev and production)
this.baseUrl = baseUrl || window.location.origin;
}
/**
* Get leaderboard from HF Hub
* @returns {Promise<Array>} Array of leaderboard entries
*/
async getLeaderboard() {
try {
const response = await fetch(`${this.baseUrl}/api/leaderboard`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
if (data.success) {
console.log('π₯ HF API: Retrieved leaderboard', {
entries: data.leaderboard.length,
message: data.message
});
return data.leaderboard;
} else {
throw new Error(data.message || 'Failed to retrieve leaderboard');
}
} catch (error) {
console.error('β HF API: Error fetching leaderboard:', error);
throw error;
}
}
/**
* Add new entry to leaderboard
* @param {Object} entry - Leaderboard entry {initials, level, round, passagesPassed, date}
* @returns {Promise<Object>} Response object
*/
async addEntry(entry) {
try {
const response = await fetch(`${this.baseUrl}/api/leaderboard/add`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(entry)
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
throw new Error(`HTTP ${response.status}: ${errorData.detail || response.statusText}`);
}
const data = await response.json();
console.log('β
HF API: Added entry', {
initials: entry.initials,
level: entry.level,
message: data.message
});
return data;
} catch (error) {
console.error('β HF API: Error adding entry:', error);
throw error;
}
}
/**
* Update entire leaderboard
* @param {Array} entries - Array of leaderboard entries
* @returns {Promise<Object>} Response object
*/
async updateLeaderboard(entries) {
try {
const response = await fetch(`${this.baseUrl}/api/leaderboard/update`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(entries)
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
throw new Error(`HTTP ${response.status}: ${errorData.detail || response.statusText}`);
}
const data = await response.json();
console.log('β
HF API: Updated leaderboard', {
entries: entries.length,
message: data.message
});
return data;
} catch (error) {
console.error('β HF API: Error updating leaderboard:', error);
throw error;
}
}
/**
* Clear all leaderboard data (admin function)
* @returns {Promise<Object>} Response object
*/
async clearLeaderboard() {
try {
const response = await fetch(`${this.baseUrl}/api/leaderboard/clear`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
throw new Error(`HTTP ${response.status}: ${errorData.detail || response.statusText}`);
}
const data = await response.json();
console.log('β
HF API: Cleared leaderboard', {
message: data.message
});
return data;
} catch (error) {
console.error('β HF API: Error clearing leaderboard:', error);
throw error;
}
}
/**
* Check if HF backend is available
* @returns {Promise<boolean>} True if backend is reachable
*/
async isAvailable() {
try {
const response = await fetch(`${this.baseUrl}/api/leaderboard`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
return response.ok;
} catch (error) {
console.warn('β οΈ HF API: Backend not available, will use localStorage fallback');
return false;
}
}
}
|