-
Notifications
You must be signed in to change notification settings - Fork 3
/
dota2_new.js
89 lines (82 loc) · 2.71 KB
/
dota2_new.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
const { gql, GraphQLClient } = require('graphql-request');
const _ = require('lodash');
const friendList = require('./steamFriendList');
const dota2PlayerIds = friendList
.filter((player) => player.dota)
.map((player) => player.steamShortId);
const [firstChunk, secondChunk, thirdChunk] = _.chunk(dota2PlayerIds, 5);
const endpoint = `https://api.stratz.com/graphql?jwt=${process.env.STRATZ}`;
const graphQLClient = new GraphQLClient(endpoint, {});
const summaryQuery = (steamIds) => gql`
{
players(steamAccountIds: [${steamIds}]) {
winCount,
matchCount,
steamAccount {
name,
isDotaPlusSubscriber,
isAnonymous,
smurfFlag,
id,
}
}
}
`;
const getPlayerSummaries = async (ctx) => {
const data = await Promise.all([
graphQLClient.request(summaryQuery(firstChunk)),
graphQLClient.request(summaryQuery(secondChunk)),
graphQLClient.request(summaryQuery(thirdChunk)),
]);
const [first, second, third] = data;
const playerData = [...first.players, ...second.players, ...third.players].flat();
ctx.reply(`${playerData.map((player) =>
`${player?.steamAccount?.name}
Anonyymi: ${player.steamAccount.isAnonymous ? 'Kyllä' : 'Ei'} ${!player.steamAccount.isAnonymous ? `
Pelejä: ${player.matchCount}
Voitot: ${player.winCount}
Voittoprosentti: ${((player.winCount / player.matchCount) * 100).toFixed(2)}%
Dotaplus tilaaja: ${player?.steamAccount?.isDotaPlusSubscriber ? 'Kyllä' : 'Ei'}` : ''}
`).join('')}`);
};
const recentMatchesQuery = (steamId) => gql`
{
player(steamAccountId: ${steamId}) {
steamAccount {
name,
isAnonymous,
id,
},
matches(request: {isParsed: true, take: 5}) {
didRadiantWin,
durationSeconds,
gameMode,
players(steamAccountId: ${steamId}) {
isVictory,
isRadiant,
heroId,
hero {
displayName,
}
kills,
deaths,
assists,
networth,
role,
}
},
}
}
`;
const getRecentMatches = async (ctx) => {
const data = await Promise.all(dota2PlayerIds.map(async (playerId) => graphQLClient.request(recentMatchesQuery(playerId))));
// const data = await Promise.all([graphQLClient.request(recentMatchesQuery('15452480')), graphQLClient.request(recentMatchesQuery('11180593'))]);
ctx.reply(data.map((gamer) => `
Pelaaja: ${gamer.player.steamAccount.name} ${gamer.player.steamAccount.id === 188479752 ? '(Smurffi)' : ''}
${!gamer.player.steamAccount.isAnonymous ? `Ottelut: ${gamer.player.matches.map((match) => `${match.players[0].isVictory ? '✅' : '❌'}`).join('')}` : 'Anonyymi'}
`).join(''));
};
module.exports = {
getPlayerSummaries,
getRecentMatches,
};