-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-cache-fetcher.js
More file actions
206 lines (179 loc) · 7.57 KB
/
github-cache-fetcher.js
File metadata and controls
206 lines (179 loc) · 7.57 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
// GitHub Cache Fetcher Module
// Fetches world records data from GitHub-hosted cache files
class GitHubCacheFetcher {
constructor() {
this.baseURL = 'https://raw.githubusercontent.com/DarkSnakeGang/FastSnakeStats/refs/heads/main';
this.cacheDir = 'daily';
this.metadataURL = `${this.baseURL}/time-travel-cache/metadata/available-dates.json`;
this.fallbackToAPI = true; // Whether to fall back to API calls
}
// Get the most recent available date from GitHub
async getMostRecentDate() {
try {
const response = await fetch(this.metadataURL);
if (!response.ok) {
console.log('GitHub metadata not available (404), GitHub cache not set up yet');
return null;
}
const metadata = await response.json();
if (metadata.availableDates && metadata.availableDates.length > 0) {
return metadata.availableDates[metadata.availableDates.length - 1];
}
} catch (error) {
console.log('Error fetching GitHub metadata:', error);
}
return null;
}
// Check if a specific date is available in GitHub cache
async isDateAvailable(date) {
try {
const response = await fetch(this.metadataURL);
if (!response.ok) {
console.log('GitHub metadata not available (404), cannot check date availability');
return false;
}
const metadata = await response.json();
return metadata.availableDates && metadata.availableDates.includes(date);
} catch (error) {
console.log('Error checking date availability:', error);
return false;
}
}
// Fetch cache data for a specific date from GitHub
async fetchCacheForDate(date) {
try {
const [year, month] = date.split('-');
const cacheURL = `${this.baseURL}/time-travel-cache/${this.cacheDir}/${year}/${month}/${date}.json`;
console.log(`Fetching GitHub cache for ${date}...`);
const response = await fetch(cacheURL);
if (!response.ok) {
console.log(`GitHub cache not available for ${date}`);
// Update time travel button status if in time travel mode
if (window.isTimeTravelEnabled && window.selectedTimeTravelDate === date) {
if (window.updateTimeTravelButtonStatus) {
window.updateTimeTravelButtonStatus('missing');
}
}
return null;
}
const cacheData = await response.json();
console.log(`Successfully fetched GitHub cache for ${date}`);
return cacheData;
} catch (error) {
console.log(`Error fetching GitHub cache for ${date}:`, error);
// Update time travel button status if in time travel mode
if (window.isTimeTravelEnabled && window.selectedTimeTravelDate === date) {
console.log('GitHub cache fetcher: Setting button to missing (error)');
if (window.updateTimeTravelButtonStatus) {
window.updateTimeTravelButtonStatus('missing');
}
}
return null;
}
}
// Convert GitHub cache format to the format expected by the app
convertCacheFormat(githubCache, targetDate) {
if (!githubCache || !githubCache.records) {
return null;
}
const convertedData = {};
// Convert each record from GitHub format to app format
for (const [key, record] of Object.entries(githubCache.records)) {
if (record.success && record.runs && Array.isArray(record.runs)) {
// The runs are already in the correct format from our script
// Just ensure they have the right structure
const convertedRuns = record.runs.map(run => {
// Check if this is already in the correct format
if (run.players && run.players.data && Array.isArray(run.players.data) && run.players.data.length > 0) {
// Already in correct format, return as is
return {
times: run.times,
date: run.date || targetDate,
id: run.id,
weblink: run.weblink,
players: run.players,
values: run.values || {}
};
} else {
// Handle legacy format (if any)
console.warn(`Legacy format detected for run ${run.id}, skipping`);
return null;
}
}).filter(run => run !== null); // Remove any null runs
convertedData[key] = convertedRuns;
} else {
// Handle empty results
convertedData[key] = [];
}
}
return convertedData;
}
// Fetch world records for current settings (most recent available data)
async fetchCurrentWorldRecords() {
const mostRecentDate = await this.getMostRecentDate();
if (!mostRecentDate) {
console.log('No GitHub cache available');
return null;
}
const cacheData = await this.fetchCacheForDate(mostRecentDate);
if (!cacheData) {
console.log('Failed to fetch GitHub cache');
return null;
}
return this.convertCacheFormat(cacheData, mostRecentDate);
}
// Fetch world records for a specific date
async fetchWorldRecordsForDate(date) {
// Don't check metadata - just try to fetch the cache directly
const cacheData = await this.fetchCacheForDate(date);
if (!cacheData) {
console.log(`Failed to fetch GitHub cache for ${date}`);
return null;
}
return this.convertCacheFormat(cacheData, date);
}
// Get available dates from GitHub
async getAvailableDates() {
try {
const response = await fetch(this.metadataURL);
if (!response.ok) return [];
const metadata = await response.json();
return metadata.availableDates || [];
} catch (error) {
console.log('Error fetching available dates:', error);
return [];
}
}
// Check if GitHub cache is accessible
async isGitHubCacheAvailable() {
try {
const response = await fetch(this.metadataURL);
if (response.ok) {
return true;
} else {
return false;
}
} catch (error) {
console.log('Error checking GitHub cache availability:', error);
return false;
}
}
// Get cache statistics from GitHub
async getCacheStats() {
try {
const response = await fetch(this.metadataURL);
if (!response.ok) return null;
const metadata = await response.json();
return {
totalDates: metadata.totalDates || 0,
dateRange: metadata.dateRange || null,
lastUpdated: metadata.lastUpdated || null
};
} catch (error) {
console.log('Error fetching cache stats:', error);
return null;
}
}
}
// Create global instance
window.githubCacheFetcher = new GitHubCacheFetcher();