def setup_games_repo(): if not os.path.exists(os.path.join(games_path('.git'))): commands = [ "cd {}".format(games_path()), "echo \"{}\" > .gitignore".format(TEMPORARY_GAME_PATH), "git init", "git add .gitignore", "git commit -m \"Initial commit\"" ] os.system(";".join(commands))
def start_new_game(temporary=False): base = 'tmp' if temporary else str(int(time.time())) fn = os.path.extsep.join([base, 'pgn']) path = games_path(fn) _logger().info("Starting new game '{}'...".format(path)) game = Game() game.headers["Date"] = time.strftime("%Y.%m.%d") game.headers["White"] = "Human" game.headers["Black"] = "Raspberry Turk" _save_game(game, path) enter_game(fn)
def _sync(): _logger().info("Syncing games...") b = get_board() commit_message = "new game" if len(b.move_stack) > 0: color = chess.COLOR_NAMES[not b.turn] move = b.move_stack[-1].uci() commit_message = "{} {}".format(color, move) commands = [ "cd {}".format(games_path()), "git add --all", "git commit -m \"{}\"".format(commit_message) ] os.system(";".join(commands)) _logger().info("Sync games successful.")
import os import logging import chess import time from raspberryturk import games_path from chess.pgn import read_game, Game, FileExporter # Python有一个chess library,这里的pgn调用的就是这个库的一些方法 # pgn是一种棋类游戏过程的文件 TEMPORARY_GAME_PATH = os.path.extsep.join(['tmp', 'pgn']) CURRENT_GAME_PATH = games_path(os.path.extsep.join( ['game', 'pgn'])) # 这个path我觉得应该是在游戏过程中的cache def _logger(): return logging.getLogger(__name__) def _game(): with open(CURRENT_GAME_PATH) as pgn: return read_game(pgn) def _save_game(game, path=CURRENT_GAME_PATH): _logger().info("Saving game '{}'...".format(path)) pgn = open(path, 'w') exporter = FileExporter(pgn) game.accept(exporter) pgn.close() _logger().info("Done saving game '{}'.".format(path)) _sync()
import os import logging import chess import time from raspberryturk import games_path from chess.pgn import read_game, Game, FileExporter TEMPORARY_GAME_PATH = os.path.extsep.join(['tmp', 'pgn']) CURRENT_GAME_PATH = games_path(os.path.extsep.join(['game', 'pgn'])) def _logger(): return logging.getLogger(__name__) def _game(): with open(CURRENT_GAME_PATH) as pgn: return read_game(pgn) def _save_game(game, path=CURRENT_GAME_PATH): _logger().info("Saving game '{}'...".format(path)) pgn = open(path, 'w') exporter = FileExporter(pgn) game.accept(exporter) pgn.close() _logger().info("Done saving game '{}'.".format(path)) _sync() def is_temporary():