def initialize(self): self.db_handle = self.chessfile.handle self.engine = self.chessfile.engine self.conn = self.engine.connect() self.CHUNK = 1000 self.count_source = select([func.count()]).select_from(source) self.ins_event = event.insert() self.ins_site = site.insert() self.ins_player = player.insert() self.ins_annotator = annotator.insert() self.ins_source = source.insert() self.ins_game = game.insert() self.ins_tag_game = tag_game.insert() self.event_dict = {} self.site_dict = {} self.player_dict = {} self.annotator_dict = {} self.source_dict = {} self.next_id = [0, 0, 0, 0, 0, 0] self.next_id[GAME] = self.ini_names(game, GAME) self.next_id[EVENT] = self.ini_names(event, EVENT) self.next_id[SITE] = self.ini_names(site, SITE) self.next_id[PLAYER] = self.ini_names(player, PLAYER) self.next_id[ANNOTATOR] = self.ini_names(annotator, ANNOTATOR) self.next_id[SOURCE] = self.ini_names(source, SOURCE)
def __init__(self, chessfile, append_pgn=False): self.chessfile = chessfile self.append_pgn = append_pgn self.db_handle = chessfile.handle self.engine = chessfile.engine self.conn = self.engine.connect() self.CHUNK = 1000 self.cancel = False self.count_source = select([func.count()]).select_from(source) self.ins_event = event.insert() self.ins_site = site.insert() self.ins_player = player.insert() self.ins_annotator = annotator.insert() self.ins_source = source.insert() self.ins_game = game.insert() self.ins_tag_game = tag_game.insert() self.event_dict = {} self.site_dict = {} self.player_dict = {} self.annotator_dict = {} self.source_dict = {} self.next_id = [0, 0, 0, 0, 0, 0] self.next_id[GAME] = self.ini_names(game, GAME) self.next_id[EVENT] = self.ini_names(event, EVENT) self.next_id[SITE] = self.ini_names(site, SITE) self.next_id[PLAYER] = self.ini_names(player, PLAYER) self.next_id[ANNOTATOR] = self.ini_names(annotator, ANNOTATOR) self.next_id[SOURCE] = self.ini_names(source, SOURCE)
def __init__(self, engine): self.engine = engine self.conn = self.engine.connect() self.CHUNK = 1000 self.cancel = False self.ins_event = event.insert() self.ins_site = site.insert() self.ins_player = player.insert() self.ins_annotator = annotator.insert() self.ins_source = source.insert() self.ins_game = game.insert() self.ins_bitboard = bitboard.insert() self.ins_tag_game = tag_game.insert() self.event_dict = {} self.site_dict = {} self.player_dict = {} self.annotator_dict = {} self.source_dict = {} self.next_id = [0, 0, 0, 0, 0, 0] self.next_id[GAME] = self.ini_names(game, GAME) self.next_id[EVENT] = self.ini_names(event, EVENT) self.next_id[SITE] = self.ini_names(site, SITE) self.next_id[PLAYER] = self.ini_names(player, PLAYER) self.next_id[ANNOTATOR] = self.ini_names(annotator, ANNOTATOR) self.next_id[SOURCE] = self.ini_names(source, SOURCE) s = select([player.c.fideid, player.c.id]) self.fideid_dict = dict([(p[0], p[1]) for p in self.conn.execute(s)]) self.perfix_stmt = select([player.c.id]).where( player.c.name.startswith(bindparam('name')))
def __init__(self): self.conn = engine.connect() self.ins_collection = collection.insert() self.ins_event = event.insert() self.ins_site = site.insert() self.ins_player = player.insert() self.ins_annotator = annotator.insert() self.ins_game = game.insert() self.collection_dict = {} self.event_dict = {} self.site_dict = {} self.player_dict = {} self.annotator_dict = {} self.next_id = [0, 0, 0, 0, 0] self.next_id[COLLECTION] = self.ini_names(collection, COLLECTION) self.next_id[EVENT] = self.ini_names(event, EVENT) self.next_id[SITE] = self.ini_names(site, SITE) self.next_id[PLAYER] = self.ini_names(player, PLAYER) self.next_id[ANNOTATOR] = self.ini_names(annotator, ANNOTATOR)
def save(path, model, offset): game_event = model.tags["Event"] game_site = model.tags["Site"] year, month, day = int(model.tags["Year"]), int(model.tags["Month"]), int( model.tags["Day"]) game_round = model.tags.get("Round") white = repr(model.players[WHITE]) black = repr(model.players[BLACK]) result = model.status eco = model.tags.get("ECO") time_control = model.tags.get("TimeControl") board = int(model.tags.get("Board")) if model.tags.get("Board") else None white_elo = int( model.tags.get("WhiteElo")) if model.tags.get("WhiteElo") else None black_elo = int( model.tags.get("BlackElo")) if model.tags.get("BlackElo") else None variant = model.variant.variant fen = model.boards[0].board.asFen() fen = fen if fen != FEN_START else None game_annotator = model.tags.get("Annotator") ply_count = model.ply - model.lowply def get_id(table, name): if not name: return None selection = select([table.c.id], table.c.name == unicode(name)) result = conn.execute(selection) id_ = result.scalar() if id_ is None: result = conn.execute(table.insert().values(name=unicode(name))) id_ = result.inserted_primary_key[0] return id_ engine = dbmodel.get_engine(path) conn = engine.connect() trans = conn.begin() try: event_id = get_id(event, game_event) site_id = get_id(site, game_site) white_id = get_id(player, white) black_id = get_id(player, black) annotator_id = get_id(annotator, game_annotator) new_values = { 'offset': offset, 'offset8': (offset >> 3) << 3, 'event_id': event_id, 'site_id': site_id, 'date_year': year, 'date_month': month, 'date_day': day, 'round': game_round, 'white_id': white_id, 'black_id': black_id, 'result': result, 'white_elo': white_elo, 'black_elo': black_elo, 'ply_count': ply_count, 'eco': eco, 'time_control': time_control, 'board': board, 'fen': fen, 'variant': variant, 'annotator_id': annotator_id, } if hasattr(model, "game_id") and model.game_id is not None: result = conn.execute(game.update().where( game.c.id == model.game_id).values(new_values)) else: result = conn.execute(game.insert().values(new_values)) model.game_id = result.inserted_primary_key[0] trans.commit() except: trans.rollback() raise conn.close()
def save(path, model, offset): game_event = model.tags["Event"] game_site = model.tags["Site"] date = model.tags["Date"] game_round = model.tags["Round"] white = repr(model.players[WHITE]) black = repr(model.players[BLACK]) result = model.status eco = model.tags["ECO"] time_control = model.tags["TimeControl"] board = int(model.tags["Board"]) if "Board" in model.tags else 0 white_elo = model.tags["WhiteElo"] black_elo = model.tags["BlackElo"] variant = model.variant.variant fen = model.boards[0].board.asFen() fen = fen if fen != FEN_START else "" game_annotator = model.tags["Annotator"] if "Annotator" in model.tags else "" ply_count = model.ply - model.lowply def get_id(table, name): if not name: return None selection = select([table.c.id], table.c.name == name) result = conn.execute(selection) id_ = result.scalar() if id_ is None: result = conn.execute(table.insert().values(name=name)) id_ = result.inserted_primary_key[0] return id_ engine = dbmodel.get_engine(path) conn = engine.connect() trans = conn.begin() try: event_id = get_id(event, game_event) site_id = get_id(site, game_site) white_id = get_id(player, white) black_id = get_id(player, black) annotator_id = get_id(annotator, game_annotator) new_values = { 'offset': offset, 'offset8': (offset >> 3) << 3, 'event_id': event_id, 'site_id': site_id, 'date': date, 'round': game_round, 'white_id': white_id, 'black_id': black_id, 'result': result, 'white_elo': white_elo, 'black_elo': black_elo, 'ply_count': ply_count, 'eco': eco, 'time_control': time_control, 'board': board, 'fen': fen, 'variant': variant, 'annotator_id': annotator_id, } if hasattr(model, "game_id") and model.game_id is not None: result = conn.execute(game.update().where( game.c.id == model.game_id).values(new_values)) else: result = conn.execute(game.insert().values(new_values)) model.game_id = result.inserted_primary_key[0] trans.commit() except Exception: trans.rollback() raise conn.close()
def save (file, model, position=None): movelist = array("H") comments = [] walk(model.boards[0].board, movelist, comments) game_event = model.tags["Event"] game_site = model.tags["Site"] year, month, day = int(model.tags["Year"]), int(model.tags["Month"]), int(model.tags["Day"]) game_round = model.tags.get("Round") white = repr(model.players[WHITE]) black = repr(model.players[BLACK]) result = model.status eco = model.tags.get("ECO") board = int(model.tags.get("Board")) if model.tags.get("Board") else None white_elo = int(model.tags.get("WhiteElo")) if model.tags.get("WhiteElo") else None black_elo = int(model.tags.get("BlackElo")) if model.tags.get("BlackElo") else None variant = 1 if issubclass(model.variant, FischerandomBoard) else None fen = model.boards[0].board.asFen() fen = fen if fen != FEN_START else None game_annotator = model.tags.get("Annotator") ply_count = model.ply-model.lowply def get_id(table, name): if not name: return None s = select([table.c.id], table.c.name==name) result = conn.execute(s) id_ = result.scalar() if id_ is None: result = conn.execute(table.insert().values(name=name)) id_ = result.inserted_primary_key[0] return id_ conn = dbmodel.engine.connect() trans = conn.begin() try: event_id = get_id(event, game_event) site_id = get_id(site, game_site) white_id = get_id(player, white) black_id = get_id(player, black) annotator_id = get_id(annotator, game_annotator) wt = model.players[WHITE].__type__ bt = model.players[BLACK].__type__ if REMOTE in (wt, bt): collection_id = REMOTE elif ARTIFICIAL in (wt, bt): collection_id = ARTIFICIAL else: collection_id = LOCAL new_values = { 'event_id': event_id, 'site_id': site_id, 'date_year': year, 'date_month': month, 'date_day': day, 'round': game_round, 'white_id': white_id, 'black_id': black_id, 'result': result, 'white_elo': white_elo, 'black_elo': black_elo, 'ply_count': ply_count, 'eco': eco, 'board': board, 'fen': fen, 'variant': variant, 'annotator_id': annotator_id, 'collection_id': collection_id, 'movelist': movelist.tostring(), 'comments': "|".join(comments), } if hasattr(model, "game_id") and model.game_id is not None: result = conn.execute(game.update().where(game.c.id==model.game_id).values(new_values)) else: result = conn.execute(game.insert().values(new_values)) model.game_id = result.inserted_primary_key[0] trans.commit() except: trans.rollback() raise
def save(path, model, offset, flip=False): game_event = model.tags["Event"] game_site = model.tags["Site"] date = model.tags["Date"] game_round = model.tags["Round"] white = repr(model.players[WHITE]) black = repr(model.players[BLACK]) result = model.status eco = model.tags["ECO"] time_control = model.tags["TimeControl"] board = int(model.tags["Board"]) if "Board" in model.tags else 0 white_elo = model.tags["WhiteElo"] black_elo = model.tags["BlackElo"] variant = model.variant.variant fen = model.boards[0].board.asFen() fen = fen if fen != FEN_START else "" game_annotator = model.tags[ "Annotator"] if "Annotator" in model.tags else "" ply_count = model.ply - model.lowply def get_id(table, name): if not name: return None selection = select([table.c.id], table.c.name == name) result = conn.execute(selection) id_ = result.scalar() if id_ is None: result = conn.execute(table.insert().values(name=name)) id_ = result.inserted_primary_key[0] return id_ engine = dbmodel.get_engine(path) conn = engine.connect() trans = conn.begin() try: event_id = get_id(event, game_event) site_id = get_id(site, game_site) white_id = get_id(player, white) black_id = get_id(player, black) annotator_id = get_id(annotator, game_annotator) new_values = { "offset": offset, "offset8": (offset >> 3) << 3, "event_id": event_id, "site_id": site_id, "date": date, "round": game_round, "white_id": white_id, "black_id": black_id, "result": result, "white_elo": white_elo, "black_elo": black_elo, "ply_count": ply_count, "eco": eco, "time_control": time_control, "board": board, "fen": fen, "variant": variant, "annotator_id": annotator_id, } if hasattr(model, "game_id") and model.game_id is not None: result = conn.execute(game.update().where( game.c.id == model.game_id).values(new_values)) else: result = conn.execute(game.insert().values(new_values)) model.game_id = result.inserted_primary_key[0] trans.commit() except Exception: trans.rollback() raise conn.close()
def save(path, model, position=None): movelist = array("H") comments = [] walk(model.boards[0].board, movelist, comments) game_event = model.tags["Event"] game_site = model.tags["Site"] year, month, day = int(model.tags["Year"]), int(model.tags["Month"]), int( model.tags["Day"]) game_round = model.tags.get("Round") white = repr(model.players[WHITE]) black = repr(model.players[BLACK]) result = model.status eco = model.tags.get("ECO") time_control = model.tags.get("TimeControl") board = int(model.tags.get("Board")) if model.tags.get("Board") else None white_elo = int( model.tags.get("WhiteElo")) if model.tags.get("WhiteElo") else None black_elo = int( model.tags.get("BlackElo")) if model.tags.get("BlackElo") else None variant = model.variant.variant fen = model.boards[0].board.asFen() fen = fen if fen != FEN_START else None game_annotator = model.tags.get("Annotator") ply_count = model.ply - model.lowply def get_id(table, name): if not name: return None selection = select([table.c.id], table.c.name == unicode(name)) result = conn.execute(selection) id_ = result.scalar() if id_ is None: result = conn.execute(table.insert().values(name=unicode(name))) id_ = result.inserted_primary_key[0] return id_ engine = dbmodel.get_engine(path) DB_MAXINT_SHIFT = get_maxint_shift(engine) conn = engine.connect() trans = conn.begin() try: event_id = get_id(event, game_event) site_id = get_id(site, game_site) white_id = get_id(player, white) black_id = get_id(player, black) annotator_id = get_id(annotator, game_annotator) new_values = { 'event_id': event_id, 'site_id': site_id, 'date_year': year, 'date_month': month, 'date_day': day, 'round': game_round, 'white_id': white_id, 'black_id': black_id, 'result': result, 'white_elo': white_elo, 'black_elo': black_elo, 'ply_count': ply_count, 'eco': eco, 'time_control': time_control, 'board': board, 'fen': fen, 'variant': variant, 'annotator_id': annotator_id, 'movelist': movelist.tostring(), 'comments': "|".join(comments), } if hasattr(model, "game_id") and model.game_id is not None: result = conn.execute(game.update().where( game.c.id == model.game_id).values(new_values)) # TODO: ? result = conn.execute( bitboard.delete().where(bitboard.c.game_id == model.game_id)) else: result = conn.execute(game.insert().values(new_values)) game_id = model.game_id = result.inserted_primary_key[0] if not fen: bitboard_data = [] stat_ins_data = [] stat_upd_data = [] result = model.status for ply, board in enumerate(model.boards): if ply == 0: continue bb = board.board.friends[0] | board.board.friends[1] bitboard_data.append({ 'game_id': game_id, 'ply': ply, 'bitboard': bb - DB_MAXINT_SHIFT, }) if ply <= STAT_PLY_MAX: stat_ins_data.append({ 'ply': ply, 'bitboard': bb - DB_MAXINT_SHIFT, 'count': 0, 'whitewon': 0, 'blackwon': 0, 'draw': 0, 'white_elo_count': 0, 'black_elo_count': 0, 'white_elo': 0, 'black_elo': 0, }) stat_upd_data.append({ '_ply': ply, '_bitboard': bb - DB_MAXINT_SHIFT, '_count': 1, '_whitewon': 1 if result == WHITEWON else 0, '_blackwon': 1 if result == BLACKWON else 0, '_draw': 1 if result == DRAW else 0, '_white_elo_count': 1 if white_elo is not None else 0, '_black_elo_count': 1 if black_elo is not None else 0, '_white_elo': white_elo if white_elo is not None else 0, '_black_elo': black_elo if black_elo is not None else 0, }) result = conn.execute(bitboard.insert(), bitboard_data) conn.execute(insert_or_ignore(engine, stat.insert()), stat_ins_data) conn.execute(upd_stat, stat_upd_data) trans.commit() except: trans.rollback() raise conn.close()