def __init__(self, fen, moves = [], timecontrol = None, multipv = 1, variantkey = "standard"): self.fen = fen self.moves = moves self.timecontrol = timecontrol self.multipv = int(multipv) self.variantkey = variantkey self.board = getvariantboard(self.variantkey) self.board.set_fen(self.fen) self.zobristkeyhex = get_zobrist_key_hex(self.board)
def mergeuciinfo(self, ui): if not ( ui.scorekind is None ): self.scorekind = ui.scorekind if not ( ui.score is None ): self.score = ui.score if not ( ui.pv is None ): self.pv = ui.pv if True: self.pvsan = [] caj = self.parentdepthitem.parentanalysisinfo.analyzejob board = getvariantboard(caj.variantkey) board.set_fen(caj.fen) for uci in self.pv: move = chess.Move.from_uci(uci) san = board.san(move) self.pvsan.append(san) board.push(move) else: pass
def buildplayerbook(player, force=False): defaultbookblob = {"name": player} bookblob = read_json_from_fdb(bookpath(player), defaultbookblob) book = Book(bookblob) if (BOOK_FILTER_VERSION > book.filterversion) or force: book.gameids = {} book.positions = {} book.filterversion = BOOK_FILTER_VERSION playerndjson = PlayerNdjson(player).fromdb() ndjson = playerndjson.ndjson #print("building", player) cnt = 0 found = 0 filtered = [] for gameblob in ndjson: cnt += 1 g = LichessGame(gameblob, player) if bookfilterok(g): filtered.append(g) found += 1 if (cnt % 1000) == 0: #print("filtering", cnt, "found", found) pass #print("filtering done, found", found) if len(filtered) > MAX_BOOK_GAMES: filtered = filtered[:MAX_BOOK_GAMES] cnt = 0 for g in filtered: cnt += 1 #print("building", cnt, "of", len(filtered), g.white.name, g.black.name) if g.id in book.gameids: pass #print("up to date") else: book.gameids[g.id] = True board = getvariantboard("atomic") zkh = get_zobrist_key_hex(board) movecnt = 0 for san in g.moves: move = board.parse_san(san) if movecnt >= MAX_BOOK_PLIES: break movecnt += 1 uci = move.uci() if zkh in book.positions: pos = book.positions[zkh] else: pos = BookPosition({"zobristkeyhex": zkh}) if uci in pos.moves: bookmove = pos.moves[uci] else: bookmove = BookMove({"uci": uci, "san": san}) if board.turn == g.mecolor: bookmove.plays += 1 if g.meresult == 1: bookmove.wins += 1 elif g.meresult == 0: bookmove.losses += 1 else: bookmove.draws += 1 pos.moves[uci] = bookmove pos.addtopgame(g.excerpt()) book.positions[zkh] = pos board.push(move) zkh = get_zobrist_key_hex(board) print("added", movecnt, "moves of", g.white.name, g.black.name) write_json_to_fdb(bookpath(player), book.toblob())
def playgame(self, event): print("playing game", event) self.playing = True self.ponder = None try: game = event["game"] gameid = game["id"] r = requests.get( f"https://lichess.org//api/bot/game/stream/{gameid}", headers={ "Authorization": f"Bearer {self.token}", "Accept": "application/x-ndjson" }, stream=True) self.gameticks[gameid] = time.time() Thread(target=self.monitorplaygametarget, args=(r, gameid)).start() try: for line in r.iter_lines(): try: line = line.decode("utf-8") self.gameticks[gameid] = time.time() self.lasttick = time.time() event = json.loads(line) kind = event["type"] if kind == "gameFull": self.initialfen = event["initialFen"] self.initialboard = getvariantboard(self.variant) if not (self.initialfen == "startpos"): self.initialboard.set_fen(self.initialfen) self.whiteid = event["white"]["id"] self.blackid = event["black"]["id"] if self.whiteid == self.username.lower(): self.color = chess.WHITE elif self.blackid == self.username.lower(): self.color = chess.BLACK else: print("could not find bot color") break print("game started", self.whiteid, self.blackid, self.color, self.initialfen) if (kind == "gameFull") or (kind == "gameState"): try: if kind == "gameFull": state = event["state"] else: state = event movesstr = state["moves"] moves = [] if len(movesstr) > 0: moves = movesstr.split(" ") board = self.initialboard.copy() for uci in moves: move = chess.Move.from_uci(uci) board.push(move) if board.turn == self.color: bookmove = self.getbookmove(board) if bookmove: print("making book move", bookmove) self.makemove(gameid, bookmove) self.engine.stop() self.ponder = None else: timecontrol = TimeControl( state["wtime"], state["winc"], state["btime"], state["binc"]) (enginemove, ponder) = self.getenginemove( board, self.initialboard, moves, timecontrol, self.ponder) self.ponder = ponder if enginemove: print("making engine move", enginemove, "ponder", ponder) self.makemove(gameid, enginemove) else: print("no engine move") break except: print("probblem processing game state event") pe() except: pass except: print("stream game exception") except: print("problem playing game") print("finished playing game") self.engine.stop() self.playing = False