-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtourney.js
More file actions
91 lines (79 loc) · 2.46 KB
/
Copy pathtourney.js
File metadata and controls
91 lines (79 loc) · 2.46 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
const fetch = require("node-fetch");
const fs = require("fs");
const colors = require("colors");
const API_BASE_URL = "https://lichess.org/api";
function api(endpoint, opts) {
return new Promise((resolve) => {
fetch(`${API_BASE_URL}/${endpoint}`, opts || {})
.then((resp) => {
resp
.json()
.then((json) => resolve(json))
.catch((err) => {
console.error("could not parse response json", err);
resp
.text()
.then((text) => {
resolve(text);
})
.catch((err) => {
console.error("could not resolve response text", err);
resolve(undefined);
});
});
})
.catch((err) => {
console.error(err);
resolve(undefined);
});
});
}
function getTourneys() {
return new Promise((resolve) => {
api("tournament").then((tourneys) => {
fs.writeFileSync("tourneys.json", JSON.stringify(tourneys, null, 2));
resolve(tourneys);
});
});
}
async function signUp(variant) {
const tourneys = await getTourneys();
if (!tourneys) {
console.error("could not get tourneys");
return;
}
const created = tourneys.created.filter((t) => t.variant.key === variant);
const started = tourneys.started.filter((t) => t.variant.key === variant);
let effTourneys = started.concat(created);
if (process.env.REVERSE_TOURNEY_ORDER === "true") effTourneys.reverse();
for (let t of effTourneys) {
const startingIn = t.secondsToStart
? `starting in ${colors.cyan(Math.floor(t.secondsToStart / 60))} min(s)`
: `finishes in ${colors.green(
Math.floor((t.finishesAt - new Date().getTime()) / 60000)
)} min(s)`;
const tourneyName = t.secondsToStart
? `${colors.cyan(t.fullName)}`
: `${colors.green(t.fullName)}`;
console.log(
`joining ${tourneyName} , ${colors.red.bold(
t.clock.limit / 60
)} + ${colors.blue.bold(t.clock.increment)} , ${colors.magenta(
t.minutes
)} min(s) , ${startingIn} , ${colors.yellow(t.nbPlayers)} player(s)`
);
const resp = await api(`tournament/${t.id}/join`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.LICHESS_TOURNEY_TOKEN}`,
},
body: "",
});
if (resp.ok) {
console.log("done");
} else {
console.error("failed");
}
}
}
signUp(process.env.TOURNEY_VARIANT || "atomic");