-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.js
More file actions
85 lines (74 loc) · 3.11 KB
/
stats.js
File metadata and controls
85 lines (74 loc) · 3.11 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
window.chessHelperStats = {
async addGame(won, eloChange = 0, finalElo = 0) {
try {
const stored = await chrome.storage.local.get(['appConfig']);
const cfg = stored.appConfig || {};
if (typeof cfg.games !== 'number') cfg.games = 0;
if (typeof cfg.wins !== 'number') cfg.wins = 0;
if (!cfg.gameHistory) cfg.gameHistory = [];
let res = 'DRAW';
let isWin = false;
if (won === 'WIN' || won === true) {
res = 'WIN';
isWin = true;
} else if (won === 'LOSS' || won === false) {
res = 'LOSS';
}
cfg.games++;
if (isWin) {
cfg.wins++;
}
const color = window.chessHelperEngine?.myColor() || 'w';
// Динамически считываем текущий ELO пользователя
let activeElo = finalElo || cfg.elo || 1300;
if (!finalElo) {
try {
const eloEl = document.querySelector('.player-bottom .user-tag-rating, .player-bottom-component .user-tag-rating, .player-bottom .player-rating, .board-layout-bottom .user-tag-rating');
if (eloEl) {
const parsedElo = parseInt(eloEl.textContent.replace(/[^\d]/g, ''));
if (!isNaN(parsedElo) && parsedElo > 100) {
activeElo = parsedElo;
}
}
} catch (_) {}
}
// Синхронизируем текущий ELO в конфиг
if (activeElo && activeElo !== cfg.elo) {
cfg.elo = activeElo;
}
cfg.gameHistory.unshift({
color: color === 'w' ? 'White' : 'Black',
result: res,
elo: activeElo,
eloChange: eloChange,
date: new Date().toLocaleDateString(cfg.lang === 'ru' ? 'ru-RU' : 'en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
})
});
if (cfg.gameHistory.length > 8) {
cfg.gameHistory.length = 8;
}
await chrome.storage.local.set({ appConfig: cfg });
if (window.appConfig) {
window.appConfig.elo = cfg.elo;
window.appConfig.games = cfg.games;
window.appConfig.wins = cfg.wins;
window.appConfig.gameHistory = cfg.gameHistory;
}
} catch (e) {
console.error('[ch:stats] Failed to save game outcome:', e);
}
},
async getStats() {
const stored = await chrome.storage.local.get(['appConfig']);
const cfg = stored.appConfig || {};
return {
wins: cfg.wins || 0,
games: cfg.games || 0,
history: cfg.gameHistory || []
};
}
};