-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
123 lines (99 loc) · 4.56 KB
/
server.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
const fetch = require('node-fetch')
const express = require('express')
const path = require('path')
const app = express()
const PORT = parseInt(process.env.PORT || "3000")
const APP_NAME = "Express Hello World"
const relayUrl = process.env.RELAY_URL
const maxGames = parseInt(process.env.MAX_GAMES || "1")
const broadcastId = process.env.BROADCAST_ID
let allPgns = Array(maxGames).fill(process.env.DEFAULT_PGN || `[Event "Rated Bullet game"]
[Site "https://lichess.org/roF5tSg1"]
[Date "2021.01.20"]
[White "YoBot_v2"]
[Black "ResoluteBot"]
[Result "1-0"]
[UTCDate "2021.01.20"]
[UTCTime "15:32:53"]
[WhiteElo "2564"]
[BlackElo "2851"]
[WhiteRatingDiff "+10"]
[BlackRatingDiff "-10"]
[WhiteTitle "BOT"]
[BlackTitle "BOT"]
[Variant "Standard"]
[TimeControl "30+0"]
[ECO "A45"]
[Opening "Indian Game"]
[Termination "Time forfeit"]
[Annotator "lichess.org"]
1. d4 Nf6 { A45 Indian Game } 2. e3 e6 3. c4 Be7 4. Nf3 O-O 5. b3 c5 6. dxc5 Na6 7. Be2 Nxc5 8. Nc3 Nfe4 9. Nxe4 Nxe4 10. O-O Bf6 11. Nd4 Nc3 12. Qc2 Nxe2+ 13. Qxe2 d5 14. cxd5 Qxd5 15. Rd1 Bd7 16. Bb2 Qa5 17. Nf3 Bb5 18. Qd2 Qxd2 19. Rxd2 Bxb2 20. Rxb2 Rfd8 21. Nd4 Be8 22.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Nd5 Kc5 110. Ne3 Be6 111. Nf1 Bg8 112. Ng3 Bd5 113. Kc2 Bc4 114. Kb2 Kc6 115. Ne4 Bf1 116. Nd2 Be2 117. Kb3 Kb5 118. Kb2 Bh5 119. Kb3 Bd1+ 120. Kb2 Bh5 121. Kb3 Bd1+ 122. Ka2 Ba4 123. Kb1 Kc5 124. Kb2 Bc6 125. Nb3+ Kd6 126. Na5 Bd5 127. Nb3 Kc7 128. Nc1 Kb6 129. Ne2 Kc5 130. Nf4 Bc4 131. Ng6 Bd3 132. Ne7 Bb5 133. Ng6 Bf1 134. Ne7 { White wins on time. } 1-0
`)
let cachedPgns = []
function fetchOngoing(nowPlaying){
if(broadcastId){
let burl = `https://lichess.org/broadcast/-/${broadcastId}/push`
console.log("pushing to", burl)
fetch(burl, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.TOKEN}`
},
body: allPgns.join("\n\n").replace(/\n\n+/g, "\n\n")
}).then(response => response.text().then(content => console.log("push response", content)))
}
for(let i = 0; i < nowPlaying.length; i++){
let game = nowPlaying[i]
const url = `https://lichess.org/game/export/${game.gameId}`
console.log("loading", url)
setTimeout(_ => fetch(url).then(response => response.text().then(content => {
allPgns[i] = content
console.log("loaded", i)
})), i * 2000)
}
for(let i = nowPlaying.length; i < maxGames; i++){
let index = i - nowPlaying.length
if(index < cachedPgns.length){
allPgns[i] = cachedPgns[index]
}
}
}
function fetchNowPlaying(){
fetch(`https://lichess.org/api/account/playing`, {
headers: {
Authorization: `Bearer ${process.env.TOKEN}`
}
}).then(response => response.text().then(content => {
let blob = JSON.parse(content)
let nowPlaying = blob.nowPlaying
if(nowPlaying.length > maxGames) nowPlaying = nowPlaying.slice(0, maxGames)
console.log("now playing", nowPlaying.length)
fetchOngoing(nowPlaying)
}))
}
if(process.env.TOKEN){
if(relayUrl) fetch(relayUrl).then(response => response.text().then(content => {
cachedPgns = content.split("\n\n\n")
console.log("fetched cache", cachedPgns.length)
}))
setTimeout(_ => fetchNowPlaying(), 10000)
setInterval(_ => {
fetchNowPlaying()
}, maxGames * 3000)
}
app.get('/', (req, res) => {
if(!relayUrl){
res.send("no relay url")
return
}
fetch(relayUrl).then(response => response.text().then(content => {
res.send(content)
}))
})
app.get('/ongoing', (req, res) => {
res.send(allPgns.join("\n\n").replace(/\n\n+/g, "\n\n"))
})
app.use('/', express.static(path.join(__dirname, '/')))
app.listen(PORT, _ => {
console.log(`${APP_NAME} listening on port ${PORT}`)
})