Repository navigation
Expand file tree
/
Copy pathgame-playtime-tracker.js
More file actions
362 lines (313 loc) · 11.6 KB
/
Copy pathgame-playtime-tracker.js
File metadata and controls
362 lines (313 loc) · 11.6 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* GlitchRealm Game Playtime Tracker
*
* This module tracks gameplay time for all GlitchRealm games and saves it to Firebase.
* It handles:
* - Starting/stopping playtime tracking
* - Tracking when games are active vs. hidden/inactive
* - Periodically saving playtime data to Firebase
* - Handling session tracking across page reloads
*/
class GamePlaytimeTracker {
constructor() {
this.gameId = null; // Unique ID for the game (e.g., "bytesurge")
this.gameName = null; // Display name for the game (e.g., "ByteSurge")
this.startTime = null; // When the current tracking session started
this.totalMinutes = 0; // Total minutes in current session
this.isTracking = false; // Whether tracking is currently active
this.saveInterval = null; // Interval for periodic saves
this.db = null; // Firestore reference
this.userId = null; // Current user ID
this.sessionId = null; // Unique ID for this play session
this.DEBUG = false; // Enable debug logging
}
/**
* Initialize the playtime tracker
* @param {string} gameId - Unique identifier for the game (lowercase, no spaces)
* @param {string} gameName - Display name of the game
* @param {boolean} debug - Enable debug logging
* @returns {Promise<boolean>} - Success status
*/
async init(gameId, gameName, debug = false) {
this.gameId = gameId;
this.gameName = gameName;
this.DEBUG = debug;
this.sessionId = this._generateSessionId();
this.log(`Initializing playtime tracker for ${gameName} (${gameId})`);
// Check for Firebase
if (typeof firebase === 'undefined') {
console.error('Firebase not available. Playtime tracking disabled.');
return false;
}
// Get Firestore instance
this.db = firebase.firestore();
// Set up auth listener
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.userId = user.uid;
this.log(`User signed in: ${this.userId}`);
// If we're already tracking, save current session
if (this.isTracking) {
this._savePlaytime();
}
} else {
this.userId = null;
this.log('User signed out');
}
});
// Set up page visibility listeners
document.addEventListener('visibilitychange', this._handleVisibilityChange.bind(this));
window.addEventListener('blur', this._handleWindowBlur.bind(this));
window.addEventListener('focus', this._handleWindowFocus.bind(this));
// Set up interval for periodic saves
this.saveInterval = setInterval(this._savePlaytime.bind(this), 5000); // Save every 5 seconds
// Set up beforeunload handler to save on exit
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
return true;
}
/**
* Start tracking playtime
* @returns {boolean} - Success status
*/
startTracking() {
if (this.isTracking) {
this.log('Playtime tracking already active');
return false;
}
this.startTime = new Date();
this.isTracking = true;
this.totalMinutes = 0;
this.log(`Started playtime tracking at ${this.startTime.toISOString()}`);
return true;
}
/**
* Stop tracking playtime
* @param {boolean} save - Whether to save the current session
* @returns {number} - Minutes tracked in this session
*/
stopTracking(save = true) {
if (!this.isTracking) {
this.log('Playtime tracking not active');
return 0;
}
const endTime = new Date();
const durationMinutes = this._calculateMinutes(this.startTime, endTime);
this.totalMinutes += durationMinutes;
this.log(`Stopped playtime tracking. Session duration: ${durationMinutes.toFixed(2)} minutes`);
if (save) {
this._savePlaytime();
}
this.isTracking = false;
return durationMinutes;
}
/**
* Get the total minutes tracked in the current session
* @returns {number} - Total minutes
*/
getTotalMinutes() {
if (!this.isTracking) {
return this.totalMinutes;
}
// Calculate current duration and add to total
const now = new Date();
const currentDuration = this._calculateMinutes(this.startTime, now);
return this.totalMinutes + currentDuration;
}
/**
* Calculate minutes between two dates
* @private
* @param {Date} start - Start time
* @param {Date} end - End time
* @returns {number} - Minutes between dates
*/
_calculateMinutes(start, end) {
const diffMs = end - start;
return diffMs / (1000 * 60); // Convert ms to minutes
}
/**
* Generate a unique session ID
* @private
* @returns {string} - Session ID
*/
_generateSessionId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
}
/**
* Save playtime to Firebase
* @private
* @returns {Promise<boolean>} - Success status
*/
async _savePlaytime() {
if (!this.db || !this.gameId) {
return false;
}
try {
// Calculate current playtime
const minutesPlayed = this.getTotalMinutes();
if (minutesPlayed <= 0) {
this.log('No playtime to save');
return false;
}
this.log(`Saving playtime: ${minutesPlayed.toFixed(2)} minutes`);
// Reset tracking but keep active
if (this.isTracking) {
this.totalMinutes = 0;
this.startTime = new Date();
}
// If user is not signed in, store in local storage
if (!this.userId) {
this._saveLocalPlaytime(minutesPlayed);
return true;
}
// Add to total playtime collection
const userGameRef = this.db.collection('playtime')
.doc(this.userId)
.collection('games')
.doc(this.gameId);
// Get current data
const doc = await userGameRef.get();
if (doc.exists) {
// Update existing document
await userGameRef.update({
totalMinutes: firebase.firestore.FieldValue.increment(minutesPlayed),
lastPlayed: firebase.firestore.FieldValue.serverTimestamp(),
sessions: firebase.firestore.FieldValue.arrayUnion({
sessionId: this.sessionId,
minutes: minutesPlayed,
timestamp: new Date() // Using regular Date instead of serverTimestamp
})
});
} else {
// Create new document
await userGameRef.set({
gameId: this.gameId,
gameName: this.gameName,
totalMinutes: minutesPlayed,
firstPlayed: firebase.firestore.FieldValue.serverTimestamp(),
lastPlayed: firebase.firestore.FieldValue.serverTimestamp(),
sessions: [{
sessionId: this.sessionId,
minutes: minutesPlayed,
timestamp: new Date() // Using regular Date instead of serverTimestamp
}]
});
}
return true;
} catch (error) {
console.error('Error saving playtime:', error);
return false;
}
}
/**
* Save playtime to local storage (for non-authenticated users)
* @private
* @param {number} minutes - Minutes to save
*/
_saveLocalPlaytime(minutes) {
try {
const storageKey = `playtime_${this.gameId}`;
const storedData = localStorage.getItem(storageKey);
let playtimeData = storedData ? JSON.parse(storedData) : {
gameId: this.gameId,
gameName: this.gameName,
totalMinutes: 0,
firstPlayed: new Date().toISOString(),
sessions: []
};
// Update data
playtimeData.totalMinutes += minutes;
playtimeData.lastPlayed = new Date().toISOString();
playtimeData.sessions.push({
sessionId: this.sessionId,
minutes: minutes,
timestamp: new Date().toISOString()
});
// Limit the number of sessions stored to prevent storage issues
if (playtimeData.sessions.length > 10) {
playtimeData.sessions = playtimeData.sessions.slice(-10);
}
localStorage.setItem(storageKey, JSON.stringify(playtimeData));
this.log(`Saved ${minutes.toFixed(2)} minutes to local storage`);
} catch (error) {
console.error('Error saving to local storage:', error);
}
}
/**
* Handle visibility change events
* @private
* @param {Event} event - Visibility change event
*/
_handleVisibilityChange(event) {
if (document.hidden) {
this.log('Page hidden, pausing tracking');
this._pauseTracking();
} else {
this.log('Page visible, resuming tracking');
this._resumeTracking();
}
}
/**
* Handle window blur events
* @private
* @param {Event} event - Blur event
*/
_handleWindowBlur(event) {
this.log('Window lost focus, pausing tracking');
this._pauseTracking();
}
/**
* Handle window focus events
* @private
* @param {Event} event - Focus event
*/
_handleWindowFocus(event) {
this.log('Window gained focus, resuming tracking');
this._resumeTracking();
}
/**
* Handle beforeunload events
* @private
* @param {Event} event - Beforeunload event
*/
_handleBeforeUnload(event) {
this.log('Page unloading, saving final playtime');
this.stopTracking(true);
}
/**
* Pause tracking without stopping completely
* @private
*/
_pauseTracking() {
if (!this.isTracking) return;
// Calculate current session time and add to total
const now = new Date();
const currentDuration = this._calculateMinutes(this.startTime, now);
this.totalMinutes += currentDuration;
// Stop tracking but don't save yet
this.isTracking = false;
}
/**
* Resume tracking after pause
* @private
*/
_resumeTracking() {
if (this.isTracking) return;
// Start a new tracking period
this.startTime = new Date();
this.isTracking = true;
}
/**
* Log message if debug is enabled
* @private
* @param {string} message - Message to log
*/
log(message) {
if (this.DEBUG) {
console.log(`[PlaytimeTracker] ${message}`);
}
}
}
// Make available in browser global scope
window.GamePlaytimeTracker = GamePlaytimeTracker;
// Also export as default for ES modules
export default GamePlaytimeTracker;