def start_lesson_from(filename, index=None): chessfile = PGNFile(protoopen(addDataPrefix("learn/lessons/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() records, plys = chessfile.get_records() progress = lessons_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) chessfile.loadToModel(rec, -1, gamemodel) if len(gamemodel.moves) > 0: start_lesson_game(gamemodel, filename, chessfile, records, index, rec) else: start_puzzle_game(gamemodel, filename, records, index, rec, from_lesson=True)
def start_puzzle_from(filename, index=None): if filename.lower().endswith(".pgn"): if filename.startswith("lichess_study"): chessfile = PGNFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) else: chessfile = PGNFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() elif filename.lower().endswith(".olv"): chessfile = OLVFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) records, plys = chessfile.get_records() progress = puzzles_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) chessfile.loadToModel(rec, 0, gamemodel) start_puzzle_game(gamemodel, filename, records, index, rec)
def row_activated(self, widget, path, col): if path is None: return filename = addDataPrefix("lectures/%s" % LESSONS[path[0]][0]) chessfile = PGNFile(protoopen(filename)) self.importer = PgnImport(chessfile) chessfile.init_tag_database(self.importer) records, plys = chessfile.get_records() rec = records[random.randrange(0, len(records))] print(rec) timemodel = TimeModel(0, 0) gamemodel = GameModel(timemodel) gamemodel.set_lesson_game() chessfile.loadToModel(rec, -1, gamemodel) name = conf.get("firstName", _("You")) p0 = (LOCAL, Human, (WHITE, name), name) name = "pychessbot" p1 = (LOCAL, Human, (BLACK, name), name) gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio. async (perspective.generalStart(gamemodel, p0, p1))
def get_count(self, filename): subdir = "puzzles" if self.progress_file.endswith("puzzles.json") else "lessons" if filename.lower().endswith(".pgn"): chessfile = PGNFile(protoopen(addDataPrefix("learn/%s/%s" % (subdir, filename)))) chessfile.limit = 1000 chessfile.init_tag_database() elif filename.lower().endswith(".olv"): chessfile = OLVFile(protoopen(addDataPrefix("learn/%s/%s" % (subdir, filename)), encoding="utf-8")) chessfile.close() count = chessfile.count return count
def start_lesson_from(filename, index=None): if filename.startswith("lichess_study"): chessfile = PGNFile( protoopen(addDataPrefix("learn/lessons/%s" % filename), encoding="utf-8")) else: chessfile = PGNFile( protoopen(addDataPrefix("learn/lessons/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() records, plys = chessfile.get_records() progress = lessons_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) gamemodel.set_learn_data(LESSON, filename, index, len(records)) chessfile.loadToModel(rec, -1, gamemodel) color = gamemodel.boards[0].color player_name = conf.get("firstName", _("You")) w_name = player_name if color == WHITE else "PyChess" b_name = "PyChess" if color == WHITE else player_name p0 = (LOCAL, Human, (WHITE, w_name), w_name) p1 = (LOCAL, Human, (BLACK, b_name), b_name) def learn_success(gamemodel): chessfile.loadToModel(rec, -1, gamemodel) progress = lessons_solving_progress[gamemodel.source] progress[gamemodel.current_index] = 1 lessons_solving_progress[gamemodel.source] = progress asyncio. async (gamemodel.restart_analyzer(HINT)) gamemodel.connect("learn_success", learn_success) def on_game_started(gamemodel): perspective.activate_panel("annotationPanel") asyncio. async (gamemodel.start_analyzer( HINT, force_engine=discoverer.getEngineLearn())) gamemodel.connect("game_started", on_game_started) gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio. async (perspective.generalStart(gamemodel, p0, p1))
def start_lesson_from(filename, index=None): chessfile = PGNFile(protoopen(addDataPrefix("learn/lessons/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() records, plys = chessfile.get_records() progress = lessons_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) gamemodel.set_learn_data(LESSON, filename, index, len(records)) chessfile.loadToModel(rec, -1, gamemodel) color = gamemodel.boards[0].color player_name = conf.get("firstName", _("You")) w_name = player_name if color == WHITE else "PyChess" b_name = "PyChess" if color == WHITE else player_name p0 = (LOCAL, Human, (WHITE, w_name), w_name) p1 = (LOCAL, Human, (BLACK, b_name), b_name) def learn_success(gamemodel): progress = lessons_solving_progress[gamemodel.source] progress[gamemodel.current_index] = 1 lessons_solving_progress[gamemodel.source] = progress asyncio.async(gamemodel.restart_analyzer(HINT)) gamemodel.connect("learn_success", learn_success) def start_analyzer(gamemodel): asyncio.async(gamemodel.start_analyzer(HINT, force_engine=discoverer.getEngineLearn())) gamemodel.connect("game_started", start_analyzer) gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio.async(perspective.generalStart(gamemodel, p0, p1))
def start_puzzle_from(filename): chessfile = PGNFile(protoopen(addDataPrefix("lectures/%s" % filename))) chessfile.limit = 1000 importer = PgnImport(chessfile) chessfile.init_tag_database(importer) records, plys = chessfile.get_records() rec = records[random.randrange(0, len(records))] timemodel = TimeModel(0, 0) gamemodel = GameModel(timemodel) gamemodel.set_practice_game() gamemodel.practice = ("puzzle", filename) chessfile.loadToModel(rec, 0, gamemodel) # TODO: change colors according to FEN! name = rec["White"] p0 = (LOCAL, Human, (WHITE, name), name) engine = discoverer.getEngineByName(stockfish_name) name = rec["Black"] ponder_off = True p1 = (ARTIFICIAL, discoverer.initPlayerEngine, (engine, BLACK, 20, variants[NORMALCHESS], 60, 0, 0, ponder_off), name) def fix_name(gamemodel, name): asyncio. async (gamemodel.start_analyzer(HINT, force_engine=stockfish_name)) gamemodel.players[1].name = name gamemodel.emit("players_changed") gamemodel.connect("game_started", fix_name, name) gamemodel.variant.need_initial_board = True gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio. async (perspective.generalStart(gamemodel, p0, p1))
def start_lesson_from(filename, index=None): chessfile = PGNFile(protoopen(addDataPrefix("learn/lessons/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() records, plys = chessfile.get_records() progress = lessons_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) chessfile.loadToModel(rec, -1, gamemodel) if len(gamemodel.moves) > 0: start_lesson_game(gamemodel, filename, chessfile, records, index, rec) else: start_puzzle_game(gamemodel, filename, records, index, rec, from_lesson=True)
def start_puzzle_from(filename, index=None): if filename.lower().endswith(".pgn"): chessfile = PGNFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() elif filename.lower().endswith(".olv"): chessfile = OLVFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) records, plys = chessfile.get_records() progress = puzzles_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) chessfile.loadToModel(rec, 0, gamemodel) start_puzzle_game(gamemodel, filename, records, index, rec)
def opening(): if filename.endswith(".pgn"): GLib.idle_add(self.progressbar1.show) GLib.idle_add(self.progressbar1.set_text, _("Opening chessfile...")) chessfile = PGNFile(protoopen(filename), self.progressbar1) self.importer = PgnImport(chessfile) chessfile.init_tag_database(self.importer) if self.importer.cancel: chessfile.tag_database.close() if os.path.isfile(chessfile.sqlite_path): os.remove(chessfile.sqlite_path) chessfile = None else: chessfile.init_scoutfish() chessfile.init_chess_db() elif filename.endswith(".epd"): self.importer = None chessfile = epd.load(protoopen(filename)) elif filename.endswith(".fen"): self.importer = None chessfile = fen.load(protoopen(filename)) else: self.importer = None chessfile = None GLib.idle_add(self.spinner.stop) GLib.idle_add(self.progress_dialog.hide) if chessfile is not None: self.chessfile = chessfile self.chessfiles.append(chessfile) GLib.idle_add(self.emit, "chessfile_opened0", chessfile) else: if self.chessfile is None: self.close(None)
def opening(): # Redirection of the PGN file nonlocal filename for ext in [".sqlite", ".bin", ".scout"]: if filename.endswith(ext): filename = filename[:len(filename) - len(ext)] + ".pgn" # Processing by file extension if filename.endswith(".pgn"): GLib.idle_add(self.progressbar.show) GLib.idle_add(self.progressbar.set_text, _("Opening chessfile...")) chessfile = PGNFile(protoopen(filename), self.progressbar) self.importer = chessfile.init_tag_database() if self.importer is not None and self.importer.cancel: chessfile.tag_database.close() if os.path.isfile(chessfile.sqlite_path): os.remove(chessfile.sqlite_path) chessfile = None else: chessfile.init_scoutfish() chessfile.init_chess_db() elif filename.endswith(".epd"): self.importer = None chessfile = epd.load(protoopen(filename)) elif filename.endswith(".olv"): self.importer = None chessfile = olv.load(protoopen(filename, encoding="utf-8")) elif filename.endswith(".fen"): self.importer = None chessfile = fen.load(protoopen(filename)) else: self.importer = None chessfile = None GLib.idle_add(self.spinner.stop) GLib.idle_add(self.spinner.hide) GLib.idle_add(self.progress_dialog.hide) if chessfile is not None: self.chessfile = chessfile self.chessfiles.append(chessfile) GLib.idle_add(self.emit, "chessfile_opened0", chessfile) else: if self.chessfile is None: self.close(None)
def opening(): if filename.endswith(".pgn"): GLib.idle_add(self.progressbar.show) GLib.idle_add(self.progressbar.set_text, _("Opening chessfile...")) chessfile = PGNFile(protoopen(filename), self.progressbar) self.importer = chessfile.init_tag_database() if self.importer is not None and self.importer.cancel: chessfile.tag_database.close() if os.path.isfile(chessfile.sqlite_path): os.remove(chessfile.sqlite_path) chessfile = None else: chessfile.init_scoutfish() chessfile.init_chess_db() elif filename.endswith(".epd"): self.importer = None chessfile = epd.load(protoopen(filename)) elif filename.endswith(".olv"): self.importer = None chessfile = olv.load(protoopen(filename, encoding="utf-8")) elif filename.endswith(".fen"): self.importer = None chessfile = fen.load(protoopen(filename)) else: self.importer = None chessfile = None GLib.idle_add(self.spinner.stop) GLib.idle_add(self.spinner.hide) GLib.idle_add(self.progress_dialog.hide) if chessfile is not None: self.chessfile = chessfile self.chessfiles.append(chessfile) GLib.idle_add(self.emit, "chessfile_opened0", chessfile) else: if self.chessfile is None: self.close(None)
DATA_FILES += [("share/pychess/sounds", glob('sounds/*.ogg'))] DATA_FILES += [('share/icons/hicolor/24x24/apps', ['pychess.png'])] DATA_FILES += [('share/gtksourceview-3.0/language-specs', ['gtksourceview-3.0/language-specs/pgn.lang'])] # Piece sets DATA_FILES += [("share/pychess/pieces", glob('pieces/*.png'))] if not isfile(os.path.abspath("learn/puzzles/mate_in_4.sqlite")): from pychess.Savers.pgn import PGNFile from pychess.System.protoopen import protoopen # Lectures, puzzles, lessons for filename in glob('learn/puzzles/*.pgn'): chessfile = PGNFile(protoopen(filename)) chessfile.init_tag_database() for filename in glob('learn/lessons/*.pgn'): chessfile = PGNFile(protoopen(filename)) chessfile.init_tag_database() DATA_FILES += [("share/pychess/learn/puzzles", glob('learn/puzzles/*.olv'))] DATA_FILES += [("share/pychess/learn/puzzles", glob('learn/puzzles/*.pgn'))] DATA_FILES += [("share/pychess/learn/puzzles", glob('learn/puzzles/*.sqlite'))] DATA_FILES += [("share/pychess/learn/lessons", glob('learn/lessons/*.pgn'))] DATA_FILES += [("share/pychess/learn/lessons", glob('learn/lessons/*.sqlite'))] DATA_FILES += [("share/pychess/learn/lectures", glob('learn/lectures/*.txt'))] for dir in [d for d in listdir('pieces') if isdir(os.path.join('pieces', d))]: DATA_FILES += [("share/pychess/pieces/" + dir, glob('pieces/' + dir + '/*.svg'))]
def start_puzzle_from(filename, index=None): if filename.lower().endswith(".pgn"): if filename.startswith("lichess_study"): chessfile = PGNFile( protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) else: chessfile = PGNFile( protoopen(addDataPrefix("learn/puzzles/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() elif filename.lower().endswith(".olv"): chessfile = OLVFile( protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) records, plys = chessfile.get_records() progress = puzzles_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) gamemodel.set_learn_data(PUZZLE, filename, index, len(records)) chessfile.loadToModel(rec, 0, gamemodel) engine = discoverer.getEngineByName(discoverer.getEngineLearn()) ponder_off = True color = gamemodel.boards[0].color w_name = "" if rec["White"] is None else rec["White"] b_name = "" if rec["Black"] is None else rec["Black"] player_name = conf.get("firstName", _("You")) engine_name = discoverer.getName(engine) if rec["Event"].startswith("Lichess Practice"): w_name = player_name if color == WHITE else engine_name b_name = engine_name if color == WHITE else player_name opp_name = engine_name if rec["Event"].startswith( "Lichess Practice") else b_name if color == WHITE: p0 = (LOCAL, Human, (WHITE, w_name), w_name) p1 = (ARTIFICIAL, discoverer.initPlayerEngine, (engine, BLACK, 20, variants[NORMALCHESS], 20, 0, 0, ponder_off), b_name) else: p0 = (ARTIFICIAL, discoverer.initPlayerEngine, (engine, WHITE, 20, variants[NORMALCHESS], 20, 0, 0, ponder_off), w_name) p1 = (LOCAL, Human, (BLACK, b_name), b_name) def on_game_started(gamemodel, name, color): perspective.activate_panel("annotationPanel") asyncio. async (gamemodel.start_analyzer( HINT, force_engine=discoverer.getEngineLearn())) gamemodel.players[1 - color].name = name gamemodel.emit("players_changed") gamemodel.connect("game_started", on_game_started, opp_name, color) def goal_checked(gamemodle): if gamemodel.reason == PRACTICE_GOAL_REACHED: progress = puzzles_solving_progress[gamemodel.source] progress[gamemodel.current_index] = 1 puzzles_solving_progress[gamemodel.source] = progress gamemodel.connect("goal_checked", goal_checked) gamemodel.variant.need_initial_board = True gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio. async (perspective.generalStart(gamemodel, p0, p1))
def start_puzzle_from(filename, index=None): if filename.lower().endswith(".pgn"): chessfile = PGNFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename))) chessfile.limit = 1000 chessfile.init_tag_database() elif filename.lower().endswith(".olv"): chessfile = OLVFile(protoopen(addDataPrefix("learn/puzzles/%s" % filename), encoding="utf-8")) records, plys = chessfile.get_records() progress = puzzles_solving_progress.get(filename, [0] * chessfile.count) if index is None: index = progress.index(0) rec = records[index] timemodel = TimeModel(0, 0) gamemodel = LearnModel(timemodel) chessfile.loadToModel(rec, 0, gamemodel) gamemodel.set_learn_data(PUZZLE, filename, index, len(records)) engine = discoverer.getEngineByName(discoverer.getEngineLearn()) ponder_off = True color = gamemodel.boards[0].color w_name = "" if rec["White"] is None else rec["White"] b_name = "" if rec["Black"] is None else rec["Black"] player_name = conf.get("firstName", _("You")) engine_name = discoverer.getName(engine) if rec["Event"].startswith("Lichess Practice"): w_name = player_name if color == WHITE else engine_name b_name = engine_name if color == WHITE else player_name opp_name = engine_name if rec["Event"].startswith("Lichess Practice") else b_name if color == WHITE: p0 = (LOCAL, Human, (WHITE, w_name), w_name) p1 = (ARTIFICIAL, discoverer.initPlayerEngine, (engine, BLACK, 20, variants[NORMALCHESS], 20, 0, 0, ponder_off), b_name) else: p0 = (ARTIFICIAL, discoverer.initPlayerEngine, (engine, WHITE, 20, variants[NORMALCHESS], 20, 0, 0, ponder_off), w_name) p1 = (LOCAL, Human, (BLACK, b_name), b_name) def start_analyzer(gamemodel, name, color): asyncio.async(gamemodel.start_analyzer(HINT, force_engine=discoverer.getEngineLearn())) gamemodel.players[1 - color].name = name gamemodel.emit("players_changed") gamemodel.connect("game_started", start_analyzer, opp_name, color) def goal_checked(gamemodle): if gamemodel.reason == PRACTICE_GOAL_REACHED: progress = puzzles_solving_progress[gamemodel.source] progress[gamemodel.current_index] = 1 puzzles_solving_progress[gamemodel.source] = progress gamemodel.connect("goal_checked", goal_checked) gamemodel.variant.need_initial_board = True gamemodel.status = WAITING_TO_START perspective = perspective_manager.get_perspective("games") asyncio.async(perspective.generalStart(gamemodel, p0, p1))