-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmbooks.js
192 lines (133 loc) · 3.92 KB
/
mbooks.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
const mongoVersion = parseInt(process.env.MONGO_VERSION || "1")
if(mongoVersion != 1) process.exit(0)
const MAX_GAMES = parseInt(process.env.MAX_GAMES || "250")
const { makeSanMovesScala } = require('@easychessanimations/scalachess/lib/outopt.js')
let client
let bookdb
let poscoll
let result
let i = 0
const MongoClient = require('mongodb').MongoClient
const MONGODB_URI = process.env.MONGODB_URI
const dbName = 'games'
const { streamNdjson } = require('@easychessanimations/fetchutils')
const BOT_NAME = process.env.BOT_NAME || "bot"
const BOT_TOKEN = process.env.TOKEN
const BOOK_DEPTH = parseInt(process.env.BOOK_DEPTH || "40")
const drop = false
MongoClient.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, setClient) {
if(err){
console.log("MongoDb connection failed.")
}else{
console.log("MongoDb connected.")
client = setClient
bookdb = client.db("book")
poscoll = bookdb.collection("positions")
if(drop) poscoll.drop()
stream()
}
})
function processGameThen(game){
return new Promise(resolve => processGame(game, resolve))
}
async function processGame(game, resolve){
if(game.status == "started"){
console.log(`skipping game ${game.id} in progress`)
resolve(false)
return
}
console.log(`processing game ${game.id}`)
if(game.moves){
let moves = game.moves.split(" ")
if(moves.length > BOOK_DEPTH) moves = moves.slice(0, BOOK_DEPTH)
let [ucis, fens] = makeSanMovesScala(game.variant, game.initialFen, moves)
let variant = game.variant
let gameid = game.id
let score = 0.5
if(game.winner){
score = game.winner == "white" ? 1 : 0
}
const botWhite = game.players.white.user.name == BOT_NAME
for(let j = 1; j < ucis.length; j++){
let san = moves[j-1]
let uci = ucis[j]
let fen = fens[j-1]
let keyparts = fen.split(" ").slice(0, 4)
let key = keyparts.join(" ")
let turnfen = keyparts[1]
if((botWhite && (turnfen == "b")) || ((!botWhite) && (turnfen == "w"))){
console.log("skipping opponent move", turnfen, game.players.white.user.name, game.players.black.user.name, san, uci, key)
continue
}
let index = i++
console.log(index, san, uci, key)
result = await poscoll.findOne({
variant: variant,
key: key,
uci: uci
})
let doc = {
variant: variant,
key: key,
uci: uci,
san: san,
score: score,
plays: 1,
gameids: [gameid]
}
if(!result){
console.log("inserting", doc)
await poscoll.insertOne(doc)
}else{
//console.log("result", index, result)
if(!result.gameids){
result.gameids = [gameid]
console.log("adding gameids")
poscoll.updateOne({variant: variant, key: key, uci: uci}, {$set: doc}, {upsert: true})
}else{
if(result.gameids.includes(gameid)){
console.log("has gameid")
}else{
result.gameids.push(gameid)
let newScore = (result.score || 0.0) + score
result.score = newScore
result.plays++
console.log("updating score", index, newScore, result.plays)
poscoll.updateOne({variant: variant, key: key, uci: uci}, {$set: result}, {upsert: true})
}
}
}
}
}else{
console.log(`game ${game.id} has no moves`)
}
resolve(true)
}
let allgames = []
async function processGames(games){
for(let game of games){
await processGameThen(game)
}
client.close()
}
let r = 0
function stream(){
if(drop){
return
}
console.log("streaming")
//poscoll.find({key: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"}).toArray().then(result => console.log(result)); return
streamNdjson({
url: `https://lichess.org/api/games/user/${BOT_NAME}?max=${MAX_GAMES}`,
token: BOT_TOKEN,
callback: game => {
console.log(`${r++} of ${MAX_GAMES} adding game ${game.id}`)
allgames.push(game)
},
endcallback:_ => {
console.log("received games")
processGames(allgames)
//client.close()
}
})
}