forked from hyperchessbot/hyperbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbooks2.js
300 lines (220 loc) · 6.16 KB
/
mbooks2.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const fs = require('fs')
//https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
Object.defineProperty(String.prototype, 'hashCode', {
value: function() {
var hash = 0, i, chr;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
});
const PGN_URL = process.env.PGN_URL
const variantName2variantKey = {
"Standard": "standard",
"Three-check": "threeCheck",
"Crazyhouse": "crazyhouse",
"King of the Hill": "kingOfTheHill",
"Horde": "horde",
"Chess960": "chess960",
"Atomic": "atomic",
"From Position": "fromPosition",
"Racing Kings": "racingKings",
"Antichess" : "antichess"
}
const mongoVersion = parseInt(process.env.MONGO_VERSION || "1")
if(mongoVersion != 2) process.exit(0)
const fetch = require('node-fetch')
const MAX_GAMES = parseInt(process.env.MAX_GAMES || "250")
const { parsePgnFull } = require('@easychessanimations/scalachess/lib/outopt.js')
let client
let bookdb
let movecoll
let gamecoll
let result
let i = 0
const MongoClient = require('mongodb').MongoClient
const MONGODB_URI = process.env.MONGODB_URI
const BOT_NAME = process.env.BOT_NAME || "chesshyperbot"
const BOT_TOKEN = process.env.TOKEN
const BOOK_DEPTH = parseInt(process.env.BOOK_DEPTH || "40")
const drop = false
function logFunc(...args){
if(process.env.DISABLE_MONGO2_LOGS == "true") return
console.log(...args)
}
MongoClient.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, setClient) {
if(err){
logFunc("MongoDb connection failed.")
}else{
logFunc("MongoDb connected.")
client = setClient
bookdb = client.db("book2")
movecoll = bookdb.collection("moves")
gamecoll = bookdb.collection("games")
if(drop){
gamecoll.drop()
movecoll.drop()
return
}
loadGames()
}
})
async function processPgn(pgn, resolve){
if(!pgn.match(/[^\s]+/)){
resolve(false)
return
}
i++
let result = parsePgnFull(pgn)
let tags = result.tags
if(tags.variant){
let variantKey = variantName2variantKey[tags.variant]
if(!variantKey){
logFunc(`unknown variant ${tags.variant}`)
resolve(false)
return
}
tags.variant = variantKey
}else{
tags.variant = "standard"
}
if(PGN_URL){
tags.hash = pgn.hashCode().toString()
logFunc(`created hash code ${tags.hash}`)
}
let stored = await gamecoll.findOne(tags)
let rebuild = true
if(stored){
if(stored.bookdepth >= BOOK_DEPTH){
rebuild = false
}
}
if(rebuild){
logFunc(`${i}. processing game ${tags.white} - ${tags.black} ${tags.site}`)
let ok = true
let effFens = result.fens
if(effFens.length > (BOOK_DEPTH+1)) effFens = effFens.slice(0, BOOK_DEPTH+1)
for(let i =1; i < effFens.length; i++){
let { uci, san } = effFens[i]
let fen = effFens[i-1].fen
if(uci){
let parts = fen.split(" ")
if(parts.length >= 4){
let key = parts.slice(0, 4).join(" ")
let doc = {...tags, ...{
key: key,
uci: uci,
san: san
}}
let updateResult = await movecoll.updateOne(doc, {$set: doc}, {upsert: true})
let ok = false
if((typeof updateResult == "object")&&(typeof updateResult.result == "object")){
logFunc(`${san} update n ${updateResult.result.n}`)
if(updateResult.result.n != 1){
logFunc(`move update result n not 1`)
ok = false
}
}else{
logFunc(`no move update result`)
ok = false
}
}else{
logFunc(`to few fen fields`)
}
}
}
if(ok){
let gameDoc = {...tags, ...{
bookdepth: BOOK_DEPTH
}}
logFunc(`updating game ${tags.site}`)
let updateResult = await gamecoll.updateOne(tags, {$set: gameDoc}, {upsert: true})
if((typeof updateResult == "object")&&(typeof updateResult.result == "object")){
logFunc(`game update n ${updateResult.result.n}`)
if(updateResult.result.n != 1){
logFunc(`game update result n not 1`)
}
}else{
logFunc(`no game update result`)
}
}else{
logFunc(`not updating game ${tags.site}`)
}
}else{
logFunc(`${i}. skipping built game ${tags.white} - ${tags.black} ${tags.site}`)
}
resolve(true)
}
function processPgnThen(pgn){
return new Promise(resolve => {
processPgn(pgn, resolve)
})
}
async function processPgns(content){
content = content.replace(/\r/g, "")
let pgns = content.split("\n\n\n")
if(PGN_URL){
content = content.replace(/\n\n\[/g, "\n\n[[")
pgns = content.split("\n\n[")
}
for(let pgn of pgns){
await processPgnThen(pgn)
}
logFunc(`processing pgns done`)
client.close()
logFunc(`client closed`)
}
function loadGames(){
movecoll.countDocuments().then(result=>logFunc("moves", result))
let url = PGN_URL || `https://lichess.org/api/games/user/${BOT_NAME}?max=${MAX_GAMES}`
logFunc(`downloading pgn games from ${url}`)
let headers = {}
if(!PGN_URL) headers.Authorization = `Bearer ${BOT_TOKEN}`
if(PGN_URL && url.match(/\.7z$/)){
logFunc(`7zip url detected`)
fetch(url, {
headers: headers
}).then(response => {
const dest = fs.createWriteStream(`temp.7z`)
response.body.pipe(dest)
response.body.on('end', _ => {
logFunc(`7z file written to disk`)
const Seven = require('node-7z')
const myStream = Seven.extractFull('temp.7z', '.', {
$progress: true
})
let pgnFile = null
myStream.on('data', function (data) {
logFunc(data)
if(data.status == "extracted"){
let file = data.file
if(file.match(/\.pgn$/)){
pgnFile = file
}
}
})
myStream.on('end', function () {
logFunc(`7z file extraced ok ${pgnFile}`)
let content = fs.readFileSync(pgnFile).toString()
processPgns(content)
})
myStream.on('error', err => {
logFunc(`an error occured unzipping 7z file`, err)
client.close()
})
})
dest.on('error', err => {
logFunc(`an error occured writing 7z file`, err)
})
})
}else{
fetch(url, {
headers: headers
}).then(response => response.text().then(content => {
processPgns(content)
}))
}
}