-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.js
102 lines (86 loc) · 2.88 KB
/
stats.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
const fs = require('fs');
const path = require('path');
const filename = path.join(__dirname, 'stats.json');
let stats = {
players: {},
recentWords: []
};
function getStats(player) {
return Object.assign({}, playerStats(player));
}
function writeStats() {
fs.writeFile(filename, JSON.stringify(stats, null, 2), null, err => {
if (err) console.error(err);
});
}
function playerStats(player) {
if (!stats.players[player]) stats.players[player] = {};
const s = stats.players[player];
if (s.totalGuesses === undefined) s.totalGuesses = 0;
if (s.correctGuesses === undefined) s.correctGuesses = 0;
if (s.winningGuesses === undefined) s.winningGuesses = 0;
if (s.wordsSuggested === undefined) s.wordsSuggested = 0;
if (s.suggestionsHung === undefined) s.suggestionsHung = 0;
return s;
}
function getLeaderboard() {
return {
mostTotalGuesses: topForProperty('totalGuesses'),
mostWinningGuesses: topForProperty('winningGuesses'),
bestGuesses: topForPercentage('correctGuesses', 'totalGuesses', 20),
mostWordsSuggested: topForProperty('wordsSuggested'),
deadliestWords: topForPercentage('suggestionsHung', 'wordsSuggested', 5)
};
}
function topForProperty(property) {
return Object.keys(stats.players)
.map(player => ({ player, [property]: stats.players[player][property] || 0 }))
.sort((a, b) => b[property] - a[property])
.filter(val => val[property] !== 0)
.slice(0, 3);
}
function topForPercentage(numerator, denominator, minDenominator) {
return Object.keys(stats.players)
.filter(player => stats.players[player][denominator] >= minDenominator)
.map(player => ({ player, percentage: 100 * (stats.players[player][numerator] || 0) / stats.players[player][denominator]}))
.sort((a, b) => b.percentage - a.percentage)
.filter(val => val.percentage !== 0)
.slice(0, 3);
}
function updateStatsForGuess(player, isCorrect, isFinalLetter) {
const s = playerStats(player);
s.totalGuesses++;
if (isCorrect) {
s.correctGuesses++;
if (isFinalLetter) s.winningGuesses++;
}
writeStats();
}
function updateStatsForNewGame(suggester) {
playerStats(suggester).wordsSuggested++;
writeStats();
}
function updateStatsForCompletedGame(suggester, isSuccessful) {
if (!isSuccessful) playerStats(suggester).suggestionsHung++;
writeStats();
}
function addWord(word) {
stats.recentWords.unshift(word);
if (stats.recentWords.length > 10) stats.recentWords.splice(10);
writeStats();
}
function getRecentWords() {
return stats.recentWords.slice(0);
}
if (fs.existsSync(filename)) {
stats = JSON.parse(fs.readFileSync(filename));
}
module.exports = {
getStats,
getLeaderboard,
updateStatsForGuess,
updateStatsForNewGame,
updateStatsForCompletedGame,
addWord,
getRecentWords
};