-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (64 loc) · 2.75 KB
/
Copy pathscript.js
File metadata and controls
74 lines (64 loc) · 2.75 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
((
/** @type {string} */ streamUptimeString,
/** @type {string} */ streamStartDateString,
/** @type {string} */ urlEncodedGetMmrHistoryResponseJson,
/** @type {string} */ playerName,
) => {
/* streamStartDateString will be a date string even if the channel is not currently live (the date will be the current
date). This may be a Nightbot bug. This is why streamUptimeString is needed to check whether the channel is live */
if (/\bnot live\b/i.test(streamUptimeString)) {
return `${playerName} is not live.`;
}
const streamStartDate = new Date(streamStartDateString);
if (Number.isNaN(streamStartDate.valueOf())) {
return `Failed to parse stream start date: ${streamStartDateString}`.slice(0, 400);
}
const getMmrHistoryResponseJson = decodeURIComponent(urlEncodedGetMmrHistoryResponseJson);
if (/^Error Connecting To Remote Server\b/i.test(getMmrHistoryResponseJson)) {
return getMmrHistoryResponseJson;
}
try {
/** @type {{
readonly data: ReadonlyArray<{
readonly mmr_change_to_last_game: number;
readonly date_raw: number;
}>;
}} */
const getMmrHistoryResponse = JSON.parse(getMmrHistoryResponseJson);
let winCountThisStream = 0;
let lossCountThisStream = 0;
let drawCountThisStream = 0;
let latestMatchThisStream = 0;
let latestRawEloThisStream = null;
let earliestMatchThisStream = Number.POSITIVE_INFINITY;
let earliestRawEloThisStream = null;
for (const {date_raw: dateUnixS, mmr_change_to_last_game: mmrChange, elo: rawElo} of getMmrHistoryResponse.data) {
const date = new Date(dateUnixS * 1000);
if (date >= streamStartDate) {
if (mmrChange > 0) {
winCountThisStream++;
}
else if (mmrChange == 0) {
drawCountThisStream++;
}
else {
lossCountThisStream++;
}
if (latestMatchThisStream < date) {
latestMatchThisStream = date;
latestRawEloThisStream = rawElo;
}
if (earliestMatchThisStream > date) {
earliestMatchThisStream = date;
earliestRawEloThisStream = rawElo - mmrChange;
}
}
}
let fullStreamEloChange = latestRawEloThisStream - earliestRawEloThisStream;
let currentTierPatched = getMmrHistoryResponse.data[0].currenttierpatched;
let rankingInTier = getMmrHistoryResponse.data[0].ranking_in_tier;
return `${playerName} is ${fullStreamEloChange >= 0 ? 'UP' : 'DOWN'} ${fullStreamEloChange}RR this stream. Currently ${winCountThisStream}W - ${lossCountThisStream}L - ${drawCountThisStream}D. | Their current rank is ${currentTierPatched} - ${rankingInTier} RR.`;
} catch (e) {
return `Failed to parse MMR history: ${e.message}: ${getMmrHistoryResponseJson}`.slice(0, 400);
}
})