def onStyle12(self, match): style12 = match.groups()[0] gameno = int(style12.split()[15]) if gameno in self.queuedStyle12s: self.queuedStyle12s[gameno].append(style12) return try: self.gamemodelStartedEvents[gameno].wait() except KeyError: pass if gameno in self.castleSigns: castleSigns = self.castleSigns[gameno] else: castleSigns = ("k", "q") gameno, relation, curcol, ply, wname, bname, wms, bms, gain, lastmove, fen = \ self.parseStyle12(style12, castleSigns) # examine starts with a <12> line only if lastmove is None and relation == IC_POS_EXAMINATING: pgnHead = [("Event", "FICS examined game"), ("Site", "freechess.org"), ("White", wname), ("Black", bname), ("Result", "*"), ("SetUp", "1"), ("FEN", fen)] pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n*\n" wplayer = self.connection.players.get(wname) bplayer = self.connection.players.get(bname) # examine from console or got mexamine in observed game if self.connection.examined_game is None: no_smoves = True game = FICSGame(wplayer, bplayer, gameno=int(gameno), game_type=GAME_TYPES["examined"], minutes=0, inc=0, board=FICSBoard(0, 0, pgn=pgn), relation=relation) self.connection.examined_game = game else: # examine an archived game from GUI no_smoves = False game = self.connection.examined_game game.gameno = int(gameno) game.relation = relation # game.game_type = GAME_TYPES["examined"] game = self.connection.games.get(game) # don't start new game in puzzlebot/endgamebot when they just reuse gameno if game.relation == IC_POS_OBSERVING_EXAMINATION or \ (game.board is not None and game.board.pgn == pgn): self.emit("boardUpdate", gameno, ply, curcol, lastmove, fen, wname, bname, wms, bms) return game.relation = relation game.board = FICSBoard(0, 0, pgn=pgn) self.gamesImObserving[game] = wms, bms # start a new game now or after smoves self.gamemodelStartedEvents[game.gameno] = threading.Event() if no_smoves: self.emit("exGameCreated", game) self.gamemodelStartedEvents[game.gameno].wait() else: if isinstance(game, FICSHistoryGame): self.connection.client.run_command( "smoves %s %s" % (self.connection.history_owner, game.history_no)) elif isinstance(game, FICSJournalGame): self.connection.client.run_command( "smoves %s %%%s" % (self.connection.journal_owner, game.journal_no)) elif isinstance(game, FICSAdjournedGame): self.connection.client.run_command( "smoves %s %s" % (self.connection.stored_owner, game.opponent.name)) self.connection.client.run_command("forward 999") else: self.emit("boardUpdate", gameno, ply, curcol, lastmove, fen, wname, bname, wms, bms)
def on_icc_send_moves(self, data): # gamenumber algebraic-move smith-move time clock send_moves = data gameno, san_move, alg_move, time, clock = send_moves.split() gameno = int(gameno) try: game = self.connection.games.get_game_by_gameno(gameno) except KeyError: return fen = "" if game == self.theGameImPlaying: curcol, ply, wms, bms = self.my_game_info else: curcol, ply, wms, bms = self.gamesImObserving[game] if curcol == WHITE: wms = int(clock) * 1000 else: bms = int(clock) * 1000 ply += 1 curcol = 1 - curcol if game == self.theGameImPlaying: self.my_game_info = (curcol, ply, wms, bms) else: self.gamesImObserving[game] = (curcol, ply, wms, bms) if gameno in self.queued_send_moves: self.queued_send_moves[gameno].append(send_moves) if len(self.queued_send_moves[gameno]) < self.moves_to_go: return if self.moves_to_go is None: game.queue.put_nowait((gameno, ply, curcol, san_move, fen, game.wplayer.name, game.bplayer.name, wms, bms)) self.emit("timesUpdate", gameno, wms, bms) else: if game.gameno not in self.gamemodelStartedEvents: return if game.gameno not in self.queuedEmits: return pgnHead = [ ("Event", "ICC %s %s game" % (game.display_rated.lower(), game.game_type.fics_name)), ("Site", "chessclub.com"), ("White", game.wplayer.name), ("Black", game.bplayer.name), ("Result", "*"), ("TimeControl", "%d+%d" % (game.minutes * 60, game.inc)), ] wrating = game.wplayer.ratings[game.game_type.rating_type] brating = game.bplayer.ratings[game.game_type.rating_type] if wrating != 0: pgnHead += [("WhiteElo", wrating)] if brating != 0: pgnHead += [("BlackElo", brating)] pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n" moves = self.queued_send_moves[gameno] ply = 0 for send_moves in moves: gameno_, san_move, alg_move, time, clock = send_moves.split() if ply % 2 == 0: pgn += "%d. " % (ply // 2 + 1) pgn += "%s {[%%emt %s]} " % (san_move, time) ply += 1 pgn += "*\n" del self.queued_send_moves[gameno] self.moves_to_go = None wms = bms = 0 game = FICSGame(game.wplayer, game.bplayer, game_type=game.game_type, result=game.result, rated=game.rated, minutes=game.minutes, inc=game.inc, board=FICSBoard(wms, bms, pgn=pgn)) in_progress = True if in_progress: game.gameno = gameno else: if gameno is not None: game.gameno = gameno # game.reason = reason game = self.connection.games.get(game, emit=False) self.emit("obsGameCreated", game) try: self.gamemodelStartedEvents[game.gameno].wait() except KeyError: pass for emit in self.queuedEmits[game.gameno]: emit() del self.queuedEmits[game.gameno] curcol, ply, wms, bms = self.gamesImObserving[game] self.emit("timesUpdate", game.gameno, wms, bms)
def on_icc_send_moves(self, data): log.debug("DG_SEND_MOVES %s" % data) # gamenumber algebraic-move smith-move time clock send_moves = data gameno, san_move, alg_move, time, clock = send_moves.split() gameno = int(gameno) try: game = self.connection.games.get_game_by_gameno(gameno) except KeyError: log.debug("Game %s is not in self.connection.games" % gameno) return fen = "" if game == self.theGameImPlaying: curcol, ply, wms, bms = self.my_game_info else: curcol, ply, wms, bms = self.gamesImObserving[game] if curcol == WHITE: wms = int(clock) * 1000 else: bms = int(clock) * 1000 ply += 1 curcol = 1 - curcol if game == self.theGameImPlaying: self.my_game_info = (curcol, ply, wms, bms) else: self.gamesImObserving[game] = (curcol, ply, wms, bms) if gameno in self.queued_send_moves: self.queued_send_moves[gameno].append(send_moves) if self.moves_to_go and len( self.queued_send_moves[gameno]) < self.moves_to_go: return if self.moves_to_go == 0 or self.moves_to_go is None: log.debug("put san_move to move_queue %s" % san_move) game.move_queue.put_nowait( (gameno, ply, curcol, san_move, fen, game.wplayer.name, game.bplayer.name, wms, bms)) self.emit("timesUpdate", gameno, wms, bms) else: if game.gameno not in self.gamemodelStartedEvents: return if game.gameno not in self.queuedEmits: return pgnHead = [ ("Event", "ICC %s %s game" % (game.display_rated.lower(), game.game_type.fics_name)), ("Site", "chessclub.com"), ("White", game.wplayer.name), ("Black", game.bplayer.name), ("Result", "*"), ("TimeControl", "%d+%d" % (game.minutes * 60, game.inc)), ] wrating = game.wplayer.ratings[game.game_type.rating_type] brating = game.bplayer.ratings[game.game_type.rating_type] if wrating != 0: pgnHead += [("WhiteElo", wrating)] if brating != 0: pgnHead += [("BlackElo", brating)] pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n" moves = self.queued_send_moves[gameno] ply = 0 for send_moves in moves: gameno_, san_move, alg_move, time, clock = send_moves.split() if ply % 2 == 0: pgn += "%d. " % (ply // 2 + 1) pgn += "%s {[%%emt %s]} " % (san_move, time) ply += 1 pgn += "*\n" del self.queued_send_moves[gameno] self.moves_to_go = None wms = bms = 0 game = FICSGame(game.wplayer, game.bplayer, game_type=game.game_type, result=game.result, rated=game.rated, minutes=game.minutes, inc=game.inc, board=FICSBoard(wms, bms, pgn=pgn)) in_progress = True if in_progress: game.gameno = gameno else: if gameno is not None: game.gameno = gameno # game.reason = reason game = self.connection.games.get(game, emit=False) self.emit("obsGameCreated", game) try: self.gamemodelStartedEvents[game.gameno].wait() except KeyError: pass for emit in self.queuedEmits[game.gameno]: emit() del self.queuedEmits[game.gameno] curcol, ply, wms, bms = self.gamesImObserving[game] self.emit("timesUpdate", game.gameno, wms, bms)
def onStyle12(self, match): style12 = match.groups()[0] gameno = int(style12.split()[15]) if gameno in self.queuedStyle12s: self.queuedStyle12s[gameno].append(style12) return try: self.gamemodelStartedEvents[gameno].wait() except KeyError: pass if gameno in self.castleSigns: castleSigns = self.castleSigns[gameno] else: castleSigns = ("k", "q") gameno, relation, curcol, ply, wname, bname, wms, bms, gain, lastmove, fen = \ self.parseStyle12(style12, castleSigns) # examine starts with a <12> line only if lastmove is None and relation == IC_POS_EXAMINATING: pgnHead = [ ("Event", "FICS examined game"), ("Site", "freechess.org"), ("White", wname), ("Black", bname), ("Result", "*"), ("SetUp", "1"), ("FEN", fen) ] pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n*\n" wplayer = self.connection.players.get(wname) bplayer = self.connection.players.get(bname) # examine from console or got mexamine in observed game if self.connection.examined_game is None: no_smoves = True game = FICSGame(wplayer, bplayer, gameno=int(gameno), game_type=GAME_TYPES["examined"], minutes=0, inc=0, board=FICSBoard(0, 0, pgn=pgn), relation=relation) self.connection.examined_game = game else: # examine an archived game from GUI no_smoves = False game = self.connection.examined_game game.gameno = int(gameno) game.relation = relation # game.game_type = GAME_TYPES["examined"] game = self.connection.games.get(game) # don't start new game in puzzlebot/endgamebot when they just reuse gameno if game.relation == IC_POS_OBSERVING_EXAMINATION or \ (game.board is not None and game.board.pgn == pgn): self.emit("boardUpdate", gameno, ply, curcol, lastmove, fen, wname, bname, wms, bms) return game.relation = relation game.board = FICSBoard(0, 0, pgn=pgn) self.gamesImObserving[game] = wms, bms # start a new game now or after smoves self.gamemodelStartedEvents[game.gameno] = threading.Event() if no_smoves: self.emit("exGameCreated", game) self.gamemodelStartedEvents[game.gameno].wait() else: if isinstance(game, FICSHistoryGame): self.connection.client.run_command("smoves %s %s" % ( self.connection.history_owner, game.history_no)) elif isinstance(game, FICSJournalGame): self.connection.client.run_command("smoves %s %%%s" % ( self.connection.journal_owner, game.journal_no)) elif isinstance(game, FICSAdjournedGame): self.connection.client.run_command("smoves %s %s" % ( self.connection.stored_owner, game.opponent.name)) self.connection.client.run_command("forward 999") else: self.emit("boardUpdate", gameno, ply, curcol, lastmove, fen, wname, bname, wms, bms)