-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathpoints.js
666 lines (556 loc) · 24.7 KB
/
points.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
class PointsSystem {
constructor(options = {}) {
this.dbName = options.dbName || 'pointsSystemDB';
this.storeName = options.storeName || 'userPoints';
this.pointsPerEngagement = options.pointsPerEngagement || 1;
this.engagementWindow = options.engagementWindow || 15 * MS_PER_MINUTE;
this.streakWindow = options.streakWindow || MS_PER_HOUR;
this.streakBreakTime = options.streakBreakTime || MS_PER_HOUR;
this.streakMultiplierBase = options.streakMultiplierBase || 0.1;
this.streakCap = options.streakCap || 10;
this.messageStore = options.messageStore;
this.db = null;
this.cache = new Map();
this.initPromise = this.initDatabase();
this.migrationComplete = false;
this.migrationInProgress = false;
}
// Generate a unique key for a user based on username and type
getUserKey(username, type) {
return `${username}:${type || 'default'}`;
}
async initDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, 1);
request.onupgradeneeded = event => {
const db = event.target.result;
if (!db.objectStoreNames.contains(this.storeName)) {
const store = db.createObjectStore(this.storeName, { keyPath: 'userKey' });
store.createIndex('points', 'points');
store.createIndex('lastActive', 'lastActive');
store.createIndex('username', 'username');
store.createIndex('type', 'type');
store.createIndex('username_type', ['username', 'type']);
}
};
request.onsuccess = event => {
this.db = event.target.result;
resolve();
};
request.onerror = () => reject(request.error);
});
}
async ensureDB() {
if (!this.db) await this.initPromise;
return this.db;
}
async getUserPoints(username, type = 'default') {
const userKey = this.getUserKey(username, type);
if (this.cache.has(userKey)) {
return this.cache.get(userKey);
}
const db = await this.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(this.storeName, 'readonly');
const store = tx.objectStore(this.storeName);
const request = store.get(userKey);
request.onsuccess = () => {
const userData = request.result || this.createDefaultUserData(username, type);
this.cache.set(userKey, userData);
resolve(userData);
};
request.onerror = () => {
const userData = this.createDefaultUserData(username, type);
this.cache.set(userKey, userData);
resolve(userData);
};
});
}
createDefaultUserData(username, type = 'default') {
const userKey = this.getUserKey(username, type);
return {
userKey,
username,
type,
points: 0,
pointsSpent: 0,
lastEngagement: 0,
currentStreak: 0,
lastActive: 0,
engagementHistory: []
};
}
async saveUserPoints(userData) {
const db = await this.ensureDB();
const userKey = userData.userKey || this.getUserKey(userData.username, userData.type);
this.cache.set(userKey, userData);
return new Promise((resolve, reject) => {
const tx = db.transaction(this.storeName, 'readwrite');
const store = tx.objectStore(this.storeName);
const request = store.put(userData);
request.onsuccess = () => resolve(userData);
request.onerror = () => reject(request.error);
});
}
async recordEngagement(username, type = 'default', timestamp = Date.now()) {
if (!username) return null;
const userData = await this.getUserPoints(username, type);
const now = timestamp;
// Check if this engagement is in a new window from the last one
const timeSinceLastEngagement = now - userData.lastEngagement;
if (timeSinceLastEngagement >= this.engagementWindow) {
// Award points for a new engagement
userData.points += this.pointsPerEngagement;
// Update streak logic
if (timeSinceLastEngagement <= this.streakBreakTime) {
// Continue streak
userData.currentStreak++;
// Calculate streak bonus capped at streakCap
const streakMultiplier = Math.min(userData.currentStreak * this.streakMultiplierBase, this.streakCap);
const streakBonus = Math.floor(this.pointsPerEngagement * streakMultiplier);
// Add streak bonus if applicable
if (streakBonus > 0) {
userData.points += streakBonus;
}
} else {
// Break streak if too much time has passed
userData.currentStreak = 1;
}
// Keep track of engagement for history
userData.engagementHistory.push(now);
// Trim history to keep only last 100 entries
if (userData.engagementHistory.length > 100) {
userData.engagementHistory = userData.engagementHistory.slice(-100);
}
}
userData.lastEngagement = now;
userData.lastActive = now;
await this.saveUserPoints(userData);
return userData;
}
async spendPoints(username, type = 'default', amount) {
if (amount <= 0) return { success: false, message: "Amount must be positive" };
const userData = await this.getUserPoints(username, type);
// Calculate available points
const availablePoints = userData.points - userData.pointsSpent;
if (availablePoints < amount) {
return {
success: false,
message: "Not enough points",
available: availablePoints,
requested: amount
};
}
userData.pointsSpent += amount;
await this.saveUserPoints(userData);
return {
success: true,
message: "Points spent successfully",
spent: amount,
remaining: userData.points - userData.pointsSpent
};
}
async getLeaderboard(limit = 10, type = null) {
const db = await this.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(this.storeName, 'readonly');
const store = tx.objectStore(this.storeName);
const index = store.index('points');
const users = [];
index.openCursor(null, 'prev').onsuccess = event => {
const cursor = event.target.result;
if (cursor && users.length < limit) {
const userData = cursor.value;
// Filter by type if specified
if (type && userData.type !== type) {
cursor.continue();
return;
}
users.push({
username: userData.username,
type: userData.type,
points: userData.points,
pointsSpent: userData.pointsSpent,
available: userData.points - userData.pointsSpent,
currentStreak: userData.currentStreak
});
cursor.continue();
} else {
resolve(users);
}
};
});
}
// Get users with the same name across different platforms
async getUsersWithSameName(username) {
const db = await this.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(this.storeName, 'readonly');
const store = tx.objectStore(this.storeName);
const index = store.index('username');
const range = IDBKeyRange.only(username);
const results = [];
index.openCursor(range).onsuccess = event => {
const cursor = event.target.result;
if (cursor) {
results.push(cursor.value);
cursor.continue();
} else {
resolve(results);
}
};
});
}
async checkIfMigrationNeeded() {
if (this.migrationComplete || this.migrationInProgress || !this.messageStore) {
return false;
}
// Check if we need to migrate (if the store is empty)
const leaderboard = await this.getLeaderboard(1);
return leaderboard.length === 0;
}
async migrateFromMessageStore() {
if (this.migrationComplete || this.migrationInProgress || !this.messageStore) {
return false;
}
const migrationNeeded = await this.checkIfMigrationNeeded();
if (!migrationNeeded) {
this.migrationComplete = true;
return false;
}
this.migrationInProgress = true;
console.log('Starting points migration from message history...');
try {
// Get all message history - increased limit to ensure we get more historical data
const recentMessages = await this.messageStore.getRecentMessages(50000);
// Group messages by username, type and timestamp
const userEngagements = {};
for (const message of recentMessages) {
if (!message.chatname) continue;
const username = message.chatname;
const type = message.type || 'default';
const timestamp = message.timestamp;
const key = `${username}:${type}`;
if (!userEngagements[key]) {
userEngagements[key] = {
username,
type,
timestamps: []
};
}
userEngagements[key].timestamps.push(timestamp);
}
// Process each user's engagements
let processedCount = 0;
for (const key in userEngagements) {
const { username, type, timestamps } = userEngagements[key];
// Sort timestamps chronologically
timestamps.sort((a, b) => a - b);
let lastEngagementTime = 0;
let currentStreak = 0;
let totalPoints = 0;
let engagementHistory = [];
for (const timestamp of timestamps) {
const timeSince = timestamp - lastEngagementTime;
if (timeSince >= this.engagementWindow || lastEngagementTime === 0) {
// New engagement window
totalPoints += this.pointsPerEngagement;
engagementHistory.push(timestamp);
if (timeSince <= this.streakBreakTime || lastEngagementTime === 0) {
// Continue or start streak
currentStreak++;
// Calculate streak bonus
const streakMultiplier = Math.min(currentStreak * this.streakMultiplierBase, this.streakCap);
const streakBonus = Math.floor(this.pointsPerEngagement * streakMultiplier);
if (streakBonus > 0) {
totalPoints += streakBonus;
}
} else {
// Break streak
currentStreak = 1;
}
lastEngagementTime = timestamp;
}
}
// Only save if user has points
if (totalPoints > 0) {
const userData = this.createDefaultUserData(username, type);
userData.points = totalPoints;
userData.lastEngagement = lastEngagementTime;
userData.lastActive = lastEngagementTime;
userData.currentStreak = currentStreak;
// Keep only the last 100 engagements in history
userData.engagementHistory = engagementHistory.slice(-100);
await this.saveUserPoints(userData);
processedCount++;
// Log progress intermittently
if (processedCount % 50 === 0) {
console.log(`Processed ${processedCount} users so far...`);
}
}
}
console.log(`Points migration complete! Processed ${processedCount} users.`);
this.migrationComplete = true;
this.migrationInProgress = false;
return true;
} catch (error) {
console.error('Points migration failed:', error);
this.migrationInProgress = false;
return false;
}
}
async forceMigration() {
if (this.migrationInProgress) {
console.log('Migration already in progress');
return false;
}
this.migrationComplete = false;
return await this.migrateFromMessageStore();
}
async processNewMessage(message) {
if (!message || !message.chatname) return null;
const type = message.type || 'default';
const username = message.chatname;
const timestamp = message.timestamp || Date.now();
return await this.recordEngagement(username, type, timestamp);
}
}
// Create the points system instance
const pointsSystem = new PointsSystem({
pointsPerEngagement: 1,
engagementWindow: 15 * MS_PER_MINUTE, // 15 minutes
streakWindow: MS_PER_HOUR, // 1 hour window for streaks
streakBreakTime: MS_PER_HOUR, // Break streak if no activity for 1 hour
streakMultiplierBase: 0.1, // 10% bonus per streak hour, so 10 hours = 2x points
streakCap: 10, // Cap multiplier at 10x (100% bonus)
messageStore: messageStoreDB // Link to existing message store
});
async function initializePointsSystem() {
try {
await pointsSystem.ensureDB();
// Attempt migration if needed, but don't block initialization
const migrationNeeded = await pointsSystem.checkIfMigrationNeeded();
if (migrationNeeded) {
console.log('Migration needed, starting in background...');
setTimeout(() => pointsSystem.migrateFromMessageStore(), 3000);
} else {
console.log('No migration needed, points system ready');
}
// Hook into message database to automatically record points
const originalAddMessage = messageStoreDB.addMessage;
messageStoreDB.addMessage = async function(message) {
const result = await originalAddMessage.call(this, message);
await pointsSystem.processNewMessage(message);
return result;
};
console.log('Points system initialized');
} catch (error) {
console.error('Failed to initialize points system:', error);
}
}
// Initialize points system after message store is ready
setTimeout(initializePointsSystem, 2000);
// Command handler functions
async function handlePointsCommand(username, type = 'default') {
const userData = await pointsSystem.getUserPoints(username, type);
const availablePoints = userData.points - userData.pointsSpent;
return {
username,
type,
pointsTotal: userData.points,
pointsSpent: userData.pointsSpent,
pointsAvailable: availablePoints,
currentStreak: userData.currentStreak
};
}
async function checkMigrationStatus() {
return {
migrationComplete: pointsSystem.migrationComplete,
migrationInProgress: pointsSystem.migrationInProgress
};
}
// Diagnostic functions for the points system
// Function to check if migration ran successfully
async function checkMigrationStatus() {
return {
migrationComplete: pointsSystem.migrationComplete,
migrationInProgress: pointsSystem.migrationInProgress
};
}
// Function to search for similar usernames (case insensitive)
async function searchSimilarUsernames(partialName) {
const db = await pointsSystem.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(pointsSystem.storeName, 'readonly');
const store = tx.objectStore(pointsSystem.storeName);
const users = [];
// We need to scan all records since we're doing a partial/case-insensitive match
store.openCursor().onsuccess = event => {
const cursor = event.target.result;
if (cursor) {
const userData = cursor.value;
// Case insensitive search
if (userData.username.toLowerCase().includes(partialName.toLowerCase())) {
users.push({
userKey: userData.userKey,
username: userData.username,
type: userData.type,
points: userData.points,
pointsSpent: userData.pointsSpent,
lastActive: new Date(userData.lastActive),
currentStreak: userData.currentStreak
});
}
cursor.continue();
} else {
resolve(users);
}
};
});
}
// Function to check for recent messages from a specific user
async function checkRecentMessagesForUser(username, type = null, limit = 10) {
if (!messageStoreDB) {
return { error: "Message store not available" };
}
try {
// First check if we can find messages for this exact user
let messages;
if (type) {
messages = await messageStoreDB.getUserMessages(username, type, 0, limit);
} else {
// Try to get any messages from this username regardless of type
messages = await messageStoreDB.getRecentMessages(500);
messages = messages.filter(msg =>
msg.chatname && msg.chatname.toLowerCase() === username.toLowerCase()
).slice(0, limit);
}
return {
messagesFound: messages.length,
sampleMessages: messages.map(msg => ({
id: msg.id,
chatname: msg.chatname,
type: msg.type,
timestamp: new Date(msg.timestamp),
message: msg.chatmessage ? msg.chatmessage.substring(0, 30) + '...' : '[no message]'
}))
};
} catch (error) {
return { error: error.toString() };
}
}
async function searchSimilarUsernames(partialName) {
const db = await pointsSystem.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(pointsSystem.storeName, 'readonly');
const store = tx.objectStore(pointsSystem.storeName);
const users = [];
// We need to scan all records since we're doing a partial/case-insensitive match
store.openCursor().onsuccess = event => {
const cursor = event.target.result;
if (cursor) {
const userData = cursor.value;
// Case insensitive search
if (userData.username.toLowerCase().includes(partialName.toLowerCase())) {
users.push({
userKey: userData.userKey,
username: userData.username,
type: userData.type,
points: userData.points,
pointsSpent: userData.pointsSpent,
lastActive: new Date(userData.lastActive),
currentStreak: userData.currentStreak
});
}
cursor.continue();
} else {
resolve(users);
}
};
});
}
async function handleSearchUsersByName(username) {
return await pointsSystem.getUsersWithSameName(username);
}
async function handleSpendPointsCommand(username, amount, type = 'default') {
return await pointsSystem.spendPoints(username, type, amount);
}
async function handleLeaderboardCommand(limit = 10, type = null) {
return await pointsSystem.getLeaderboard(limit, type);
}
async function handleForceMigration() {
return await pointsSystem.forceMigration();
}
async function listAllUsers(limit = 100) {
const db = await pointsSystem.ensureDB();
return new Promise((resolve) => {
const tx = db.transaction(pointsSystem.storeName, 'readonly');
const store = tx.objectStore(pointsSystem.storeName);
const users = [];
store.openCursor().onsuccess = event => {
const cursor = event.target.result;
if (cursor && users.length < limit) {
const userData = cursor.value;
users.push({
userKey: userData.userKey,
username: userData.username,
type: userData.type,
points: userData.points,
lastActive: new Date(userData.lastActive)
});
cursor.continue();
} else {
resolve(users);
}
};
});
}
// Function to manually award points to a user
async function manuallyAwardPoints(username, type, points) {
const userData = await pointsSystem.getUserPoints(username, type);
userData.points += points;
userData.lastActive = Date.now();
await pointsSystem.saveUserPoints(userData);
return userData;
}
// Function to force a re-process of historical messages for a specific user
async function reprocessUserHistory(username, type = null) {
if (!messageStoreDB) {
return { error: "Message store not available" };
}
try {
// Get messages for this user
let messages = await messageStoreDB.getRecentMessages(5000);
// Filter for the specific user
messages = messages.filter(msg => {
if (!msg.chatname) return false;
const nameMatch = msg.chatname.toLowerCase() === username.toLowerCase();
const typeMatch = type ? msg.type === type : true;
return nameMatch && typeMatch;
});
console.log(`Found ${messages.length} messages for ${username} ${type ? `(${type})` : ''}`);
// Sort by timestamp ascending
messages.sort((a, b) => a.timestamp - b.timestamp);
// Process each message
let processed = 0;
for (const message of messages) {
await pointsSystem.processNewMessage(message);
processed++;
}
return {
processed,
messagesFound: messages.length
};
} catch (error) {
return { error: error.toString() };
}
}
// Add diagnostic functions to window for console use
window.pointsDiagnostic = {
checkMigrationStatus,
searchSimilarUsernames,
checkRecentMessagesForUser,
listAllUsers,
manuallyAwardPoints,
reprocessUserHistory
};