Ejemplo n.º 1
0
 def _do_katago_contribute(self):
     if self.contributing and not self.engine.server_error and self.engine.katago_process is not None:
         return
     self.contributing = self.animate_contributing = True  # special mode
     if self.play_analyze_mode == MODE_PLAY:  # switch to analysis view
         self.play_mode.switch_ui_mode()
     self.pondering = False
     self.board_gui.animating_pv = None
     for bw, player_info in self.players_info.items():
         self.update_player(bw, player_type=PLAYER_AI, player_subtype=AI_DEFAULT)
     self.engine.shutdown(finish=False)
     self.engine = KataGoContributeEngine(self)
     self.game = BaseGame(self)
Ejemplo n.º 2
0
 def _read_stdout_thread(self):
     while self.katago_process is not None:
         try:
             line = self.katago_process.stdout.readline()
             if line:
                 line = line.decode(errors="ignore").strip()
                 if line.startswith("{"):
                     try:
                         analysis = json.loads(line)
                         if "gameId" in analysis:
                             game_id = analysis["gameId"]
                             if game_id in self.finished_games:
                                 continue
                             current_game = self.active_games.get(game_id)
                             new_game = current_game is None
                             if new_game:
                                 board_size = [analysis["boardXSize"], analysis["boardYSize"]]
                                 placements = {
                                     f"A{bw}": [
                                         Move.from_gtp(move, pl).sgf(board_size)
                                         for pl, move in analysis["initialStones"]
                                         if pl == bw
                                     ]
                                     for bw in "BW"
                                 }
                                 game_properties = {k: v for k, v in placements.items() if v}
                                 game_properties["SZ"] = f"{board_size[0]}:{board_size[1]}"
                                 game_properties["KM"] = analysis["rules"]["komi"]
                                 game_properties["RU"] = json.dumps(analysis["rules"])
                                 game_properties["PB"] = analysis["blackPlayer"]
                                 game_properties["PW"] = analysis["whitePlayer"]
                                 current_game = BaseGame(self.katrain, game_properties=game_properties)
                                 self.active_games[game_id] = current_game
                             last_node = current_game.sync_branch(
                                 [Move.from_gtp(coord, pl) for pl, coord in analysis["moves"]]
                             )
                             last_node.set_analysis(analysis)
                             if new_game:
                                 current_game.set_current_node(last_node)
                             self.start_time = self.start_time or time.time() - 1
                             self.move_count += 1
                             self.visits_count += analysis["rootInfo"]["visits"]
                             last_move = self.last_move_for_game[game_id]
                             self.last_move_for_game[game_id] = time.time()
                             dt = self.last_move_for_game[game_id] - last_move if last_move else 0
                             self.katrain.log(
                                 f"[{time.time()-self.start_time:.1f}] Game {game_id} Move {analysis['turnNumber']}: {' '.join(analysis['move'])} Visits {analysis['rootInfo']['visits']} Time {dt:.1f}s\t Moves/min {60*self.move_count/(time.time()-self.start_time):.1f} Visits/s {self.visits_count/(time.time()-self.start_time):.1f}",
                                 OUTPUT_DEBUG,
                             )
                             self.katrain("update-state")
                     except Exception as e:
                         traceback.print_exc()
                         self.katrain.log(f"Exception {e} in parsing or processing JSON: {line}", OUTPUT_ERROR)
                 elif "uploaded sgf" in line:
                     self.uploaded_games_count += 1
                 else:
                     self.katrain.log(line, OUTPUT_KATAGO_STDERR)
             elif self.katago_process:
                 self.check_alive(exception_if_dead=False)  # stderr will do this
         except Exception as e:
             self.katrain.log(f"Exception in reading stdout {e}", OUTPUT_DEBUG)
             return