-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
128 lines (115 loc) · 3.03 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
const fs = require('fs');
const fetch = require("node-fetch");
const { GraphQLClient } = require('graphql-request');
const Mode = Object.freeze({
NAMES_ONLY: Symbol('NAMES_ONLY'),
TWITTER_OR_NAME: Symbol('TWITTER_OR_NAME'),
NAME_AND_TWITTER: Symbol('NAME_AND_TWITTER'),
})
const STANDINGS_QUERY = `
query StandingsQuery($slug: String) {
tournament(slug: $slug){
name
slug
events {
id
name
slug
numEntrants
standings(query: {
page: 1
perPage: 8
sortBy: "standing"
}){
nodes{
standing
entrant{
name
participants {
user {
authorizations(types: [TWITTER]) {
externalUsername
}
}
}
}
}
}
}
}
}
`;
const ENDPOINT = 'https://api.smash.gg/gql/alpha';
const TOKEN = fs.readFileSync('SMASHGG_TOKEN', 'utf8').trim();
const SLUG = process.argv[2];
async function main() {
const graphQLClient = new GraphQLClient(ENDPOINT, {
headers: { authorization: `Bearer ${TOKEN}` },
});
const tournamentData = await graphQLClient.request(STANDINGS_QUERY, {
slug: SLUG
});
const t = tournamentData.tournament;
const messages = [];
for (const e of t.events) {
const numPlacings = e.numEntrants > 16 ? 8 : 3;
// TODO(Adrian): Try different modes until it fits in a tweet
const mode = numPlacings > 4 ? Mode.TWITTER_OR_NAME : Mode.NAME_AND_TWITTER;
const intro = `${t.name} - ${e.name} top ${numPlacings}/${e.numEntrants}`;
let placings = e.standings.nodes
.slice(0, numPlacings)
.map(placingString.bind(this, mode));
messages.push(`\
${intro}
${placings.join('\n')}
Full standings: https://smash.gg/${e.slug.replace('/event/', '/events/')}/standings`);
}
console.log(messages.join('\n---------\n'));
}
function placingString(nameMode, standing) {
const name = standing.entrant.name;
const twitter = standing.entrant.participants
.map(p => p.user)
.filter(t => t != null)
.map(user => user.authorizations && user.authorizations[0])
.filter(t => t != null)
.map(authorization => authorization.externalUsername)
.map(t => '@' + t)
.join(', ');
const placing = ordinal(standing.standing);
let nameString;
switch (nameMode) {
case Mode.NAMES_ONLY:
nameString = name;
break;
case Mode.TWITTER_OR_NAME:
nameString = twitter ? `${twitter}` : name;
break;
case Mode.NAME_AND_TWITTER:
nameString = `${name}${twitter ? ` (${twitter})` : ''}`;
break;
}
return `${placing}: ${nameString}`;
}
function ordinal(i) {
const abs = Math.abs(i);
const rem = abs % 10;
const isTeen = Math.floor(abs % 100 / 10) == 1;
let suffix = 'th';
if (!isTeen) {
switch (rem) {
case 1:
suffix = 'st';
break;
case 2:
suffix = 'nd';
break;
case 3:
suffix = 'rd';
break;
}
}
return i + suffix;
}
main().catch(error => console.error(error));