def set_dests(self): dests = {} promotions = [] moves = self.board.legal_moves() # print("self.board.legal_moves()", moves) if self.random_mover: self.random_move = random.choice(moves) if moves else "" # print("RM: %s" % self.random_move) for move in moves: if self.variant[-5:] == "shogi": move = usi2uci(move) elif self.variant == "xiangqi" or self.variant == "grand" or self.variant == "grandhouse" or self.variant == "shako": move = grand2zero(move) source, dest = move[0:2], move[2:4] if source in dests: dests[source].append(dest) else: dests[source] = [dest] if not move[-1].isdigit(): promotions.append(move) self.dests = dests self.promotions = promotions
async def load_game(app, game_id): """ Return Game object from app cache or from database """ db = app["db"] games = app["games"] users = app["users"] if game_id in games: return games[game_id] doc = await db.game.find_one({"_id": game_id}) if doc is None: return None wp, bp = doc["us"] if wp in users: wplayer = users[wp] else: wplayer = User(app, username=wp, anon=True) users[wp] = wplayer if bp in users: bplayer = users[bp] else: bplayer = User(app, username=bp, anon=True) users[bp] = bplayer variant = C2V[doc["v"]] initial_fen = doc.get("if") # Old USI Shogi games saved using usi2uci() need special handling usi_format = variant.endswith("shogi") and doc.get("uci") is None if usi_format: wplayer, bplayer = bplayer, wplayer if initial_fen: # print("load_game() USI SFEN was:", initial_fen) parts = initial_fen.split() if len(parts) > 3 and parts[1] in "wb": pockets = "[%s]" % parts[2] if parts[2] not in "-0" else "" initial_fen = parts[0] + pockets + ( " w" if parts[1] == "b" else " b") + " 0 " + parts[3] else: initial_fen = parts[0] + (" w" if parts[1] == "b" else " b") + " 0" # print(" changed to:", initial_fen) game = Game(app, game_id, variant, initial_fen, wplayer, bplayer, base=doc["b"], inc=doc["i"], byoyomi_period=int(bool(doc.get("bp"))), level=doc.get("x"), rated=bool(doc.get("y")), chess960=bool(doc.get("z")), create=False) mlist = decode_moves(doc["m"], variant) if mlist: game.saved = True if usi_format and variant == "shogi": mirror = mirror9 mlist = map(mirror, mlist) elif usi_format and (variant == "minishogi" or variant == "kyotoshogi"): mirror = mirror5 mlist = map(mirror, mlist) elif variant == "xiangqi" or variant == "grand" or variant == "grandhouse" or variant == "shako" or variant == "janggi": mlist = map(zero2grand, mlist) if "a" in doc: if usi_format and "m" in doc["a"][0]: doc["a"][0]["m"] = mirror(usi2uci(doc["a"][0]["m"])) game.steps[0]["analysis"] = doc["a"][0] if "mct" in doc: print(doc["mct"]) manual_count_toggled = iter(doc["mct"]) count_started = -1 count_ended = -1 for ply, move in enumerate(mlist): try: if "mct" in doc: # print("Ply", ply, "Move", move) if ply + 1 >= count_ended: try: game.board.count_started = -1 count_started, count_ended = next(manual_count_toggled) # print("New count interval", (count_started, count_ended)) except StopIteration: # print("Piece's honour counting started") count_started = 0 count_ended = MAX_PLY + 1 game.board.count_started = 0 if ply + 1 == count_started: # print("Count started", count_started) game.board.count_started = ply san = game.board.get_san(move) game.board.push(move) game.check = game.board.is_checked() turnColor = "black" if game.board.color == BLACK else "white" if usi_format: turnColor = "black" if turnColor == "white" else "white" game.steps.append({ "fen": game.board.fen, "move": move, "san": san, "turnColor": turnColor, "check": game.check }) if "a" in doc: if usi_format and "m" in doc["a"][ply + 1]: doc["a"][ply + 1]["m"] = mirror( usi2uci(doc["a"][ply + 1]["m"])) try: game.steps[-1]["analysis"] = doc["a"][ply + 1] except IndexError: print("IndexError", ply, move, san) except Exception: log.exception("ERROR: Exception in load_game() %s %s %s %s %s" % (game_id, variant, doc.get("if"), move, list(mlist))) break if len(game.steps) > 1: move = game.steps[-1]["move"] game.lastmove = move level = doc.get("x") game.date = doc["d"] game.status = doc["s"] game.level = level if level is not None else 0 game.result = C2R[doc["r"]] try: game.wrating = doc["p0"]["e"] game.wrdiff = doc["p0"]["d"] game.brating = doc["p1"]["e"] game.brdiff = doc["p1"]["d"] except KeyError: game.wrating = "1500?" game.wrdiff = "0" game.brating = "1500?" game.brdiff = "0" return game
async def load_game(app, game_id): """ Return Game object from app cache or from database """ db = app["db"] games = app["games"] users = app["users"] if game_id in games: return games[game_id] doc = await db.game.find_one({"_id": game_id}) if doc is None: return None wp, bp = doc["us"] if wp in users: wplayer = users[wp] else: wplayer = User(app, username=wp, anon=True) users[wp] = wplayer if bp in users: bplayer = users[bp] else: bplayer = User(app, username=bp, anon=True) users[bp] = bplayer variant = C2V[doc["v"]] initial_fen = doc.get("if") # Old USI Shogi games saved using usi2uci() need special handling usi_format = variant.endswith("shogi") and doc.get("uci") is None if usi_format: wplayer, bplayer = bplayer, wplayer if initial_fen: print("load_game() USI SFEN was:", initial_fen) parts = initial_fen.split() pockets = "[%s]" % parts[2] initial_fen = parts[0] + pockets + (" w" if parts[1] == " b" else " w") + " 0 " + parts[3] print(" changed to:", initial_fen) game = Game(app, game_id, variant, initial_fen, wplayer, bplayer, doc["b"], doc["i"], doc.get("x"), bool(doc.get("y")), bool(doc.get("z")), create=False) mlist = decode_moves(doc["m"], variant) if mlist: game.saved = True if usi_format and variant == "shogi": mirror = mirror9 mlist = list(map(mirror, mlist)) elif usi_format and (variant == "minishogi" or variant == "kyotoshogi"): mirror = mirror5 mlist = list(map(mirror, mlist)) elif variant == "xiangqi" or variant == "grand" or variant == "grandhouse" or variant == "shako": mlist = map(zero2grand, mlist) if "a" in doc: if usi_format and "m" in doc["a"][0]: doc["a"][0]["m"] = mirror(usi2uci(doc["a"][0]["m"])) game.steps[0]["analysis"] = doc["a"][0] for ply, move in enumerate(mlist): try: san = game.board.get_san(move) game.board.push(move) game.check = game.board.is_checked() game.steps.append({ "fen": game.board.fen, "move": move, "san": san, "turnColor": "black" if game.board.color == BLACK else "white", "check": game.check} ) if "a" in doc: if usi_format and "m" in doc["a"][ply + 1]: doc["a"][ply + 1]["m"] = mirror(usi2uci(doc["a"][ply + 1]["m"])) game.steps[-1]["analysis"] = doc["a"][ply + 1] except Exception: log.exception("ERROR: Exception in load_game() %s %s %s %s" % (game_id, variant, doc.get("if"), mlist)) break if len(game.steps) > 1: move = game.steps[-1]["move"] game.lastmove = move level = doc.get("x") game.date = doc["d"] game.status = doc["s"] game.level = level if level is not None else 0 game.result = C2R[doc["r"]] game.random_move = "" try: game.wrating = doc["p0"]["e"] game.wrdiff = doc["p0"]["d"] game.brating = doc["p1"]["e"] game.brdiff = doc["p1"]["d"] except KeyError: game.wrating = "1500?" game.wrdiff = "0" game.brating = "1500?" game.brdiff = "0" return game