-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwarlog.js
More file actions
247 lines (222 loc) · 9.67 KB
/
warlog.js
File metadata and controls
247 lines (222 loc) · 9.67 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
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
const cloudinary = require('cloudinary');
const fetch = require('node-fetch');
const moment = require('moment-timezone');
const FormData = require('form-data');
const envalid = require('envalid');
const table = require('text-table');
const { str } = envalid;
exports.handler = async (event, context) => {
console.log("Starting job");
const env = init();
const battles = await fetch('https://api.royaleapi.com/clan/' + event.clan_id + '/battles?type=war', {
headers: {
auth: env.ROYALE_API_KEY,
},
});
const json = await battles.json();
console.log("Read clan war battles: " + json.length);
const jobs = json
.filter(battle => {
return battle.type === 'clanWarWarDay';
})
.filter(battle => moment.utc(battle.utcTime).isAfter(moment().subtract(event.minutes || 15, 'minutes')))
.map(async battle => {
const warBattleTag = battle.team[0].tag
console.log("Checking tag: " + warBattleTag);
let playerBattles = fetch('https://api-v2.royaleapi.com/player/' + warBattleTag + '/battles', {
headers: {
auth: env.ROYALE_API_KEY,
},
});
const warBattleDeck = battle.team[0].deck
const deckUrl = await buildDeckUrl(warBattleDeck);
let shortDeckUrl = shortenUrl(deckUrl);
let shortDeckLink = shortenUrl(`${battle.team[0].deckLink}&war=1`);
let shortProfileLink = shortenUrl(`https://royaleapi.com/player/${warBattleTag}`);
[playerBattles, shortDeckUrl, shortDeckLink, shortProfileLink] = await Promise.all([
playerBattles,
shortDeckUrl,
shortDeckLink,
shortProfileLink,
]);
const playerBattlesJson = await playerBattles.json();
if (!playerBattlesJson || !Array.isArray(playerBattlesJson)) {
console.log('Not array', playerBattlesJson);
return '';
}
const countTable = createCountTable(
deckMatchCounts(
warBattleDeck,
warBattleTag,
playerBattlesJson
)
);
const trainingMatches = playerBattlesJson.filter(
playerBattle =>
(equalDeck(warBattleDeck, playerBattle.team[0].deck) &&
warBattleTag === playerBattle.team[0].tag) ||
(equalDeck(warBattleDeck, playerBattle.opponent[0].deck) &&
warBattleTag === playerBattle.opponent[0].tag)
);
const groupedMatches = groupBy(trainingMatches, 'type');
const totalTrainingCount =
(groupedMatches['clanMate'] ? groupedMatches['clanMate'].length : 0) +
(groupedMatches['challenge'] ? groupedMatches['challenge'].length : 0) +
(groupedMatches['PvP'] ? groupedMatches['PvP'].length : 0) +
(groupedMatches['tournament'] ? groupedMatches['tournament'].length : 0);
const allFriendlies = playerBattlesJson.filter(battle => battle.type === 'clanMate').length;
const text =
`${battle.winner >= 1 ? 'Victory! :raised_hands:' : 'Loss :crying_cat_face:'}\n` +
`${battle.team[0].name} (${battle.team[0].startTrophies}) vs ${battle.opponent[0].name} (${battle.opponent[0].startTrophies}) at ` +
`${moment
.utc(battle.utcTime)
.locale(env.MOMENT_LOCALE)
.tz(env.TIME_ZONE)
.format(env.MOMENT_DATETIME_FORMAT)}.\n` +
`${battle.team[0].name} (<${shortProfileLink}>) trained a total of ${totalTrainingCount} times with the war deck ` +
`(${shortDeckUrl} ${shortDeckLink}). ` +
`A total of ${allFriendlies} friendlies during the last 25 battles.\n` +
`${countTable}`;
console.log('Returning text: ' + text);
return text;
})
.map(async text => {
text = await text;
const responseText = JSON.stringify({ content: text });
return await fetch('https://discordapp.com/api/webhooks/' + event.discord_key + '?wait=true', {
method: 'POST',
body: responseText,
headers: { 'Content-Type': 'application/json' },
});
});
const results = await Promise.all(jobs);
results.forEach(promise => console.log(promise.status, promise.statusText));
return results;
};
const init = () => {
const env = envalid.cleanEnv(process.env, {
CLOUDINARY_NAME: str(),
CLOUDINARY_KEY: str(),
CLOUDINARY_SECRET_KEY: str(),
ROYALE_API_KEY: str(),
MOMENT_LOCALE: str({ default: 'nb' }),
TIME_ZONE: str({ default: 'Europe/Oslo' }),
MOMENT_DATETIME_FORMAT: str({ default: 'lll' }),
});
require('moment/locale/' + env.MOMENT_LOCALE);
cloudinary.config({
cloud_name: env.CLOUDINARY_NAME,
api_key: env.CLOUDINARY_KEY,
api_secret: env.CLOUDINARY_SECRET_KEY,
});
return env;
};
const numEqual = (warDeck, otherDeck) => {
const warSet = new Set(warDeck.map(card => card.key));
const otherSet = new Set(otherDeck.map(card => card.key));
const intersection = new Set([...warSet].filter(key => otherSet.has(key)));
return intersection.size;
};
const deckMatchCounts = (warDeck, warBattleTag, playerBattles) =>
playerBattles.map(playerBattle => {
const playerBattleDeck =
playerBattle.team
.concat(playerBattle.opponent)
.find(p => p.tag === warBattleTag)
.deck;
return {
type: playerBattle.type,
equalCards: numEqual(warDeck, playerBattleDeck)
};
});
const countArr = countGroup => countGroup.reduce(
(arr, {equalCards}) => {
const countIdx = 8 - equalCards;
if(countIdx < 5) {
arr[countIdx] = arr[countIdx] + 1;
}
return arr;
},
[0, 0, 0, 0, 0]
);
const createCountTable = matchCounts => {
const grouped = groupBy(matchCounts, 'type');
const tableRows = [
["", "WD", "-1", "-2", "-3", "-4"],
[]
];
const totalCounts = ["Total", 0, 0, 0, 0, 0];
Object.entries(grouped).forEach(
([type, counts]) => {
const typeCounts = countArr(counts);
tableRows.push([type, ...typeCounts]);
for (let i = 0; i < 5; i++) {
totalCounts[i + 1] += typeCounts[i];
}
}
);
tableRows.push([]);
tableRows.push(totalCounts);
return "```\n" + table(
tableRows,
{align: ['l', 'r', 'r', 'r', 'r', 'r']}
) + "\n```";
}
const equalDeck = (warDeck, otherDecks) => {
const otherDeckString = otherDecks
.map(card => card.key)
.sort()
.join();
const warDeckString = warDeck
.map(card => card.key)
.sort()
.join();
return otherDeckString === warDeckString;
};
const groupBy = (xs, key) => {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
const shortenUrl = async deckUrl => {
const urlEncoded = encodeURIComponent(deckUrl);
console.log(`https://is.gd/create.php?format=simple&url=${urlEncoded}`);
const response = await fetch(`https://is.gd/create.php?format=simple&url=${urlEncoded}`, {
method: 'GET',
});
return await response.text();
};
const buildDeckUrl = async deck => {
const card1 = { key: deck[0].key, level: deck[0].displayLevel };
const card2 = { key: deck[1].key, level: deck[1].displayLevel };
const card3 = { key: deck[2].key, level: deck[2].displayLevel };
const card4 = { key: deck[3].key, level: deck[3].displayLevel };
const card5 = { key: deck[4].key, level: deck[4].displayLevel };
const card6 = { key: deck[5].key, level: deck[5].displayLevel };
const card7 = { key: deck[6].key, level: deck[6].displayLevel };
const card8 = { key: deck[7].key, level: deck[7].displayLevel };
return cloudinary.url(`CR/${card1.key}`, {
secure: true,
transformation: [
{ width: 100, height: 100, crop: 'scale', x: 0, y: 0 },
{ overlay: `text:Arial_20_bold:Level%20${card1.level},co_white`, gravity: 'south' },
{ height: 100, overlay: `CR:${card2.key}`, width: 100, x: 100, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card2.level},co_white`, gravity: 'south', x: 50 },
{ height: 100, overlay: `CR:${card3.key}`, width: 100, x: 150, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card3.level},co_white`, gravity: 'south', x: 100 },
{ height: 100, overlay: `CR:${card4.key}`, width: 100, x: 200, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card4.level},co_white`, gravity: 'south', x: 150 },
{ height: 100, overlay: `CR:${card5.key}`, width: 100, x: -150, y: 100, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card5.level},co_white`, gravity: 'south', x: -150 },
{ height: 100, overlay: `CR:${card6.key}`, width: 100, x: -50, y: 50, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card6.level},co_white`, gravity: 'south', x: -50 },
{ height: 100, overlay: `CR:${card7.key}`, width: 100, x: 50, y: 50, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card7.level},co_white`, gravity: 'south', x: 50 },
{ height: 100, overlay: `CR:${card8.key}`, width: 100, x: 150, y: 50, crop: 'scale' },
{ overlay: `text:Arial_20_bold:Level%20${card8.level},co_white`, gravity: 'south', x: 150 },
],
});
};
exports.deckMatchCounts = deckMatchCounts
exports.createCountTable = createCountTable