-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatting.js
114 lines (105 loc) · 4.21 KB
/
formatting.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
var ALL_PLAYLISTS = ["Duel", "Doubles", "Solo", "Standard"];
var FORMAT_START = '-----------------------------\n';
const tierNames = [
'<:unranked:360259883443945472>',
'<:bronze1:360258827293032450>', '<:bronze2:360258828647792640>', '<:bronze3:360258827838423040>',
'<:silver1:360258826882252800>', '<:silver2:360258827716788225>', '<:silver3:360258827855200256>',
'<:gold1:360258826697441281>', '<:gold2:360258827804737566>', ': <:gold3:360258827125260291>',
'<:plat1:360258827863457792>', '<:plat2:360258827859394560>', '<:plat3:360258827851137024>',
'<:diamond1:360258827481776129>', '<:diamond2:360258827427250183>', '<:diamond3:360258827750342658>',
'<:champ1:360258826642915328>', '<:champ2:360258826928259074>', '<:champ3:360258826127147009>',
'<:grand_chump:360258827930697729>'
];
function emojiForTier(tier) {
tier = tier | 0;
return tierNames[tier];
}
function rankOrEmpty(name, obj) {
if (!obj) {
return '';
}
return name + emojiForTier(obj.tier) + ' , div ' + (obj.division + 1) + " (" + obj.rankPoints + " mmr) " + '\n';
}
// If provided, let actions restrict the playlist by mentioning "1s" or "Doubles" etc in args.
function parsePlaylistArgs(argsLeft) {
argsLeft = argsLeft.map(function(s) { return s.toLowerCase(); });
var playlists = [];
if (argsLeft.indexOf("duel") > -1 || argsLeft.indexOf("1s") > -1) {
playlists.push("Duel");
}
if (argsLeft.indexOf("doubles") > -1 || argsLeft.indexOf("2s") > -1) {
playlists.push("Doubles");
}
if (argsLeft.indexOf("solo") > -1) {
playlists.push("Solo");
}
if (argsLeft.indexOf("standard") > -1 || argsLeft.indexOf("3s") > -1) {
playlists.push("Standard");
}
// If none provided, use all playlists.
return playlists.length === 0 ? ALL_PLAYLISTS : playlists;
}
// Format a collection of current season MMR ranks into the correct rating tier and division.
function seasonRankToText(currentSeasonData, argsLeft) {
var formatted = FORMAT_START;
if (currentSeasonData) {
var playlists = parsePlaylistArgs(argsLeft);
if (playlists.indexOf("Duel") > -1) {
formatted += rankOrEmpty("Duel: ", currentSeasonData["Duel"]);
}
if (playlists.indexOf("Doubles") > -1) {
formatted += rankOrEmpty("Doubles: ", currentSeasonData["Doubles"]);
}
if (playlists.indexOf("Solo") > -1) {
formatted += rankOrEmpty("Solo: ", currentSeasonData["Solo"]);
}
if (playlists.indexOf("Standard") > -1) {
formatted += rankOrEmpty("Standard: ", currentSeasonData["Standard"]);
}
}
if (formatted == FORMAT_START) {
formatted += 'No ranks for current season';
}
return formatted;
}
// Given previous and current stats, format the different for a collection of playlists.
function old2newText(oldStats, newStats, argsLeft) {
var playlists = parsePlaylistArgs(argsLeft);
var formatted = FORMAT_START;
for (var playlist in oldStats) {
if (playlists.indexOf(playlist) === -1) {
continue;
}
var oldStat = oldStats[playlist];
var newStat = newStats[playlist];
var matchesPlayedSince = newStat["matchesPlayed"] - oldStat["matchesPlayed"];
var pointsChange = newStat["rankPoints"] - oldStat["rankPoints"];
if (matchesPlayedSince > 0) {
formatted += playlist + ": "
+ 'You have ' + ((pointsChange > 0) ? 'gained' : 'lost') + ' ' + Math.abs(pointsChange) + ' points '
+ ' in ' + matchesPlayedSince + ' matches'
+ '\n';
}
}
if (formatted === FORMAT_START) {
formatted += 'No updates since last time';
}
return formatted;
}
// Given sorted list of {id, user name, playlist rank}, format them as a table
function ladderToText(ladder) {
if (ladder.length === 0) {
return "No players in rank";
}
var formatted = FORMAT_START;
for (rank of ladder) {
formatted += rankOrEmpty(rank.user.name, rank.data);
}
return formatted;
}
module.exports = {
ladderToText: ladderToText,
old2newText: old2newText,
parsePlaylistArgs: parsePlaylistArgs,
seasonRankToText: seasonRankToText,
}