Beispiel #1
0
    def on_game_remove(self, match):
        gameno, wname, bname, comment, result = match.groups()
        result, reason = parse_reason(reprResult.index(result),
                                      comment,
                                      wname=wname)

        wplayer = FICSPlayer(wname)
        try:
            wplayer = self.connection.players.get(wplayer, create=False)
            wplayer.restore_previous_status(
            )  # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            pass
        bplayer = FICSPlayer(bname)
        try:
            bplayer = self.connection.players.get(bplayer, create=False)
            bplayer.restore_previous_status()
        except KeyError:
            pass

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)
        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
Beispiel #2
0
    def on_icc_my_game_result(self, data):
        log.debug("DG_MY_GAME_RESULT %s" % data)
        # gamenumber become-examined game_result_code score_string2 description-string ECO
        # 1242 1 Res 1-0 {Black resigns} {D89}
        parts = data.split(" ", 4)
        gameno, ex, result_code, result, rest = parts
        gameno = int(gameno)
        comment, rest = rest[2:].split("}", 1)

        try:
            game = self.connection.games.get_game_by_gameno(gameno)
        except KeyError:
            return
        wname = game.wplayer.name
        bname = game.bplayer.name

        result, reason = parse_reason(reprResult.index(result),
                                      comment,
                                      wname=wname)

        try:
            wplayer = self.connection.players.get(wname)
            wplayer.restore_previous_status()
            # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            log.debug("%s not in self.connections.players - creating" % wname)
            wplayer = FICSPlayer(wname)

        try:
            bplayer = self.connection.players.get(bname)
            bplayer.restore_previous_status()
        except KeyError:
            log.debug("%s not in self.connections.players - creating" % bname)
            bplayer = FICSPlayer(bname)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)
        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
Beispiel #3
0
    def on_icc_my_game_result(self, data):
        # gamenumber become-examined game_result_code score_string2 description-string ECO
        # 1242 1 Res 1-0 {Black resigns} {D89}
        parts = data.split(" ", 4)
        gameno, ex, result_code, result, rest = parts
        gameno = int(gameno)
        comment, rest = rest[2:].split("}", 1)

        try:
            game = self.connection.games.get_game_by_gameno(gameno)
        except KeyError:
            return
        wname = game.wplayer.name
        bname = game.bplayer.name

        result, reason = parse_reason(
            reprResult.index(result),
            comment,
            wname=wname)

        try:
            wplayer = self.connection.players.get(wname)
            wplayer.restore_previous_status()
            # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            print("%s not in self.connections.players - creating" % wname)
            wplayer = FICSPlayer(wname)

        try:
            bplayer = self.connection.players.get(bname)
            bplayer.restore_previous_status()
        except KeyError:
            print("%s not in self.connections.players - creating" % bname)
            bplayer = FICSPlayer(bname)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)
        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
Beispiel #4
0
    def test1(self):
        """ From gbtami (player accepting a challenge) point of view """
        lines = [
            "Challenge: ggbtami (----) gbtami (1708) unrated blitz 5 0.",
            'You can "accept" or "decline", or propose different parameters.',
            "fics% ",
            "<pf> 7 w=ggbtami t=match p=ggbtami (----) gbtami (1708) unrated blitz 5 0",
            "fics% ", BLOCK_START + '52' + BLOCK_SEPARATOR + '11' +
            BLOCK_SEPARATOR, "You accept the match offer from ggbtami."
            "", "", "<pr> 7", "fics% ",
            "Creating: gbtami (1708) ggbtami (++++) unrated blitz 5 0",
            "{Game 107 (gbtami vs. ggbtami) Creating unrated blitz match.}"
            "", "",
            "<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 107 gbtami ggbtami 1 5 0 39 39 300000 300000 1 none (0:00.000) none 0 0 0",
            ""
            ""
            "Game 107: A disconnection will be considered a forfeit.",
            BLOCK_END, "fics% '"
        ]

        me = self.connection.players.get('gbtami')
        me.ratings[TYPE_BLITZ] = 1708
        opponent = self.connection.players.get('ggbtami')
        opponent.ratings[TYPE_BLITZ] = 0
        game = FICSGame(me,
                        opponent,
                        gameno=107,
                        rated=False,
                        game_type=GAME_TYPES['blitz'],
                        private=False,
                        minutes=5,
                        inc=0,
                        board=FICSBoard(300000, 300000, fen=FEN_START))
        me.game = game
        opponent.game = game
        self.runAndAssertEquals("playGameCreated", lines, (game, ))

        lines = [
            BLOCK_START + '58' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            "<12> rnbqkbnr pppppppp -------- -------- -------- -----P-- PPPPP-PP RNBQKBNR B -1 1 1 1 1 0 107 gbtami ggbtami -1 5 0 39 39 300000 300000 1 P/f2-f3 (0:00.000) f3 0 0 0",
            BLOCK_END, "fics% ",
            "<12> rnbqkbnr pppp-ppp -------- ----p--- -------- -----P-- PPPPP-PP RNBQKBNR W 4 1 1 1 1 0 107 gbtami ggbtami 1 5 0 39 39 300000 300000 2 P/e7-e5 (0:00.000) e5 0 1 0",
            "fics% ",
            BLOCK_START + '61' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            "<12> rnbqkbnr pppp-ppp -------- ----p--- ------P- -----P-- PPPPP--P RNBQKBNR B 6 1 1 1 1 0 107 gbtami ggbtami -1 5 0 39 39 297338 300000 2 P/g2-g4 (0:02.662) g4 0 1 296",
            BLOCK_END, "fics% ",
            "<12> rnb-kbnr pppp-ppp -------- ----p--- ------Pq -----P-- PPPPP--P RNBQKBNR W -1 1 1 1 1 1 107 gbtami ggbtami 1 5 0 39 39 297338 295763 3 Q/d8-h4 (0:04.237) Qh4# 0 1 304",
            "fics% ", "{Game 107 (gbtami vs. ggbtami) gbtami checkmated} 0-1",
            "No ratings adjustment done.", "fics% "
        ]

        game = self.connection.games[game]

        def coro():
            yield from self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 4)
        self.assertEqual(game.reason, WON_MATE)
Beispiel #5
0
    def on_game_list(self, match):
        gameno, wrating, wname, brating, bname, private, shorttype, rated, min, \
            inc, whour, wmin, wsec, bhour, bmin, bsec, wmat, bmat, color, movno = match.groups()
        try:
            gametype = GAME_TYPES_BY_SHORT_FICS_NAME[shorttype]
        except KeyError:
            return

        wplayer = self.connection.players.get(FICSPlayer(wname))
        bplayer = self.connection.players.get(FICSPlayer(bname))
        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        rated=(rated == "r"),
                        private=(private == "p"),
                        minutes=int(min),
                        inc=int(inc),
                        game_type=gametype)

        for player, rating in ((wplayer, wrating), (bplayer, brating)):
            if player.status != IC_STATUS_PLAYING:
                player.status = IC_STATUS_PLAYING
            if player.game != game:
                player.game = game
            rating = self.parseRating(rating)
            if gametype.rating_type in player.ratings and \
                    player.ratings[gametype.rating_type].elo != rating:
                player.ratings[gametype.rating_type].elo = rating

        self.games.append(self.connection.games.get(game, emit=False))
Beispiel #6
0
    def on_icc_started_observing(self, data):
        gameno, wname, bname, wild, rtype, rated, wmin, winc, bmin, binc, played_game, rest = data.split(" ", 11)

        parts = rest.split("}", 1)[1].split()
        wrating = int(parts[0])
        brating = int(parts[1])

        gameno = int(gameno)
        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)
        game_type = GAME_TYPES[rtype.lower()]

        for player, rating in ((wplayer, wrating), (bplayer, brating)):
            if player.ratings[game_type.rating_type] != rating:
                player.ratings[game_type.rating_type] = rating
                player.emit("ratings_changed", game_type.rating_type, player)

        relation = IC_POS_OBSERVING
        wms = bms = int(wmin) * 60 * 1000 + int(winc) * 1000

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=gameno,
                        rated=rated == "1",
                        game_type=game_type,
                        minutes=int(wmin),
                        inc=int(winc),
                        relation=relation)

        game = self.connection.games.get(game, emit=False)

        self.gamesImObserving[game] = (WHITE, 0, wms, bms)
        self.queued_send_moves[game.gameno] = []
        self.queuedEmits[game.gameno] = []
        self.gamemodelStartedEvents[game.gameno] = asyncio.Event()
Beispiel #7
0
    def on_game_remove(self, match):
        gameno, wname, bname, comment, result = match.groups()
        result, reason = parse_reason(
            reprResult.index(result),
            comment,
            wname=wname)

        try:
            wplayer = self.connection.players.get(wname)
            wplayer.restore_previous_status()
            # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            print("%s not in self.connections.players - creating" % wname)
            wplayer = FICSPlayer(wname)

        try:
            bplayer = self.connection.players.get(bname)
            bplayer.restore_previous_status()
        except KeyError:
            print("%s not in self.connections.players - creating" % bname)
            bplayer = FICSPlayer(bname)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)

        # Our played/observed game ends are handled in main connection to prevent
        # removing them by helper connection before latest move(style12) comes from server
        if game == self.connection.bm.theGameImPlaying or \
           game in self.connection.bm.gamesImObserving:
            return

        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
Beispiel #8
0
    def on_game_remove(self, match):
        gameno, wname, bname, comment, result = match.groups()
        result, reason = parse_reason(
            reprResult.index(result),
            comment,
            wname=wname)

        try:
            wplayer = self.connection.players.get(wname)
            wplayer.restore_previous_status()
            # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            print("%s not in self.connections.players - creating" % wname)
            wplayer = FICSPlayer(wname)

        try:
            bplayer = self.connection.players.get(bname)
            bplayer.restore_previous_status()
        except KeyError:
            print("%s not in self.connections.players - creating" % bname)
            bplayer = FICSPlayer(bname)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)

        # Our played/observed game ends are handled in main connection to prevent
        # removing them by helper connection before latest move(style12) comes from server
        if game == self.connection.bm.theGameImPlaying or \
           game in self.connection.bm.gamesImObserving:
            return

        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
Beispiel #9
0
    def onPlayGameCreated(self, matchlist):
        log.debug(
            "'%s' '%s' '%s'" %
            (matchlist[0].string, matchlist[1].string, matchlist[-1].string),
            extra={"task": (self.connection.username, "BM.onPlayGameCreated")})
        wname, wrating, bname, brating, rated, match_type, minutes, inc = matchlist[
            0].groups()
        item = 2 if self.connection.USCN else 1
        gameno, wname, bname, rated, match_type = matchlist[item].groups()
        gameno = int(gameno)
        wrating = parseRating(wrating)
        brating = parseRating(brating)
        rated = rated == "rated"
        game_type = GAME_TYPES[match_type]

        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)
        for player, rating in ((wplayer, wrating), (bplayer, brating)):
            if game_type.rating_type in player.ratings and \
                    player.ratings[game_type.rating_type] != rating:
                player.ratings[game_type.rating_type] = rating
                player.emit("ratings_changed", game_type.rating_type, player)

        style12 = matchlist[-1].groups()[0]
        castleSigns = self.generateCastleSigns(style12, game_type)
        self.castleSigns[gameno] = castleSigns
        gameno, relation, curcol, ply, wname, bname, wms, bms, gain, lastmove, fen = \
            self.parseStyle12(style12, castleSigns)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=gameno,
                        rated=rated,
                        game_type=game_type,
                        minutes=int(minutes),
                        inc=int(inc),
                        board=FICSBoard(wms, bms, fen=fen))

        game = self.connection.games.get(game)

        for player in (wplayer, bplayer):
            if player.status != IC_STATUS_PLAYING:
                player.status = IC_STATUS_PLAYING
            if player.game != game:
                player.game = game

        self.theGameImPlaying = game
        self.gamemodelStartedEvents[gameno] = threading.Event()
        self.connection.client.run_command("follow")
        self.emit("playGameCreated", game)
Beispiel #10
0
    def on_game_remove(self, match):
        gameno, wname, bname, comment, result = match.groups()
        result, reason = parse_reason(
            reprResult.index(result),
            comment,
            wname=wname)

        try:
            wplayer = self.connection.players.get(wname)
            wplayer.restore_previous_status()
            # no status update will be sent by
            # FICS if the player doesn't become available, so we restore
            # previous status first (not necessarily true, but the best guess)
        except KeyError:
            print("%s not in self.connections.players - creating" % wname)
            wplayer = FICSPlayer(wname)

        try:
            bplayer = self.connection.players.get(bname)
            bplayer.restore_previous_status()
        except KeyError:
            print("%s not in self.connections.players - creating" % bname)
            bplayer = FICSPlayer(bname)

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        result=result,
                        reason=reason)
        if wplayer.game is not None:
            game.rated = wplayer.game.rated
        game = self.connection.games.get(game, emit=False)
        self.connection.games.game_ended(game)
        # Do this last to give anybody connected to the game's signals a chance
        # to disconnect from them first
        wplayer.game = None
        bplayer.game = None
    def on_game_list(self, matchlist):
        games = []
        for match in matchlist[:-1]:
            if isinstance(match, str):
                if match:
                    parts0, parts1 = match.split("[")
                    gameno, wrating, wname, brating, bname = parts0.split()
                    private = parts1[0]
                    shorttype = parts1[1]
                    rated = parts1[2]
                    min = parts1[3:6]
                    inc = parts1[7:10]
                else:
                    continue
            else:
                gameno, wrating, wname, brating, bname, private, shorttype, rated, min, inc, whour, wmin, wsec, bhour, bmin, bsec, wmat, bmat, color, movno = (
                    match.groups())
            try:
                gametype = GAME_TYPES_BY_SHORT_FICS_NAME[shorttype]
            except KeyError:
                return

            wplayer = self.connection.players.get(wname)
            bplayer = self.connection.players.get(bname)
            game = FICSGame(
                wplayer,
                bplayer,
                gameno=int(gameno),
                rated=(rated == "r"),
                private=(private == "p"),
                minutes=int(min),
                inc=int(inc),
                game_type=gametype,
            )

            for player, rating in ((wplayer, wrating), (bplayer, brating)):
                if player.status != IC_STATUS_PLAYING:
                    player.status = IC_STATUS_PLAYING
                if player.game != game:
                    player.game = game
                rating = parseRating(rating)
                if player.ratings[gametype.rating_type] != rating:
                    player.ratings[gametype.rating_type] = rating
                    player.emit("ratings_changed", gametype.rating_type,
                                player)
            game = self.connection.games.get(game, emit=False)
            games.append(game)
        self.connection.games.emit("FICSGameCreated", games)
Beispiel #12
0
    def on_game_add(self, match):
        gameno, wname, bname, rated, game_type = match.groups()
        if game_type not in GAME_TYPES:
            return
        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)
        game = FICSGame(wplayer,
                        bplayer,
                        gameno=int(gameno),
                        rated=(rated == "rated"),
                        game_type=GAME_TYPES[game_type])

        for player in (wplayer, bplayer):
            if player.status != IC_STATUS_PLAYING:
                player.status = IC_STATUS_PLAYING
            if player.game != game:
                player.game = game

        self.connection.games.get(game)
Beispiel #13
0
    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)
Beispiel #14
0
    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)
Beispiel #15
0
    def on_icc_my_game_started(self, data):
        log.debug("DG_MY_GAME_STARTED %s" % data)
        # gamenumber whitename blackname wild-number rating-type rated
        # white-initial white-increment black-initial black-increment
        # played-game {ex-string} white-rating black-rating game-id
        # white-titles black-titles irregular-legality irregular-semantics
        # uses-plunkers fancy-timecontrol promote-to-king
        # 685 Salsicha MaxiBomb 0 Blitz 1 3 0 3 0 1 {} 2147 2197 1729752694 {} {} 0 0 0 {} 0
        # 259 Rikikilord ARMH 0 Blitz 1 2 12 2 12 0 {Ex: Rikikilord 0} 1532 1406 1729752286 {} {} 0 0 0 {} 0
        gameno, wname, bname, wild, rtype, rated, wmin, winc, bmin, binc, played_game, rest = data.split(
            " ", 11)

        parts = rest.split("}", 1)[1].split()
        wrating = int(parts[0])
        brating = int(parts[1])

        gameno = int(gameno)
        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)
        if int(wild) > 0:
            game_type = GAME_TYPES["w%s" % wild]
        else:
            game_type = GAME_TYPES[rtype.lower()]
        log.debug("DG_MY_GAME_STARTED game type is %s" % game_type)
        for player, rating in ((wplayer, wrating), (bplayer, brating)):
            try:
                if player.ratings[game_type.rating_type] != rating:
                    player.ratings[game_type.rating_type] = rating
                    player.emit("ratings_changed", game_type.rating_type,
                                player)
            except IndexError:
                log.debug(
                    "!!! game_type.rating_type %s is out of range in player.ratings %s"
                    % (game_type.rating_type, player.ratings))

        wms = bms = int(wmin) * 60 * 1000 + int(winc) * 1000
        # TODO: maybe use DG_POSITION_BEGIN2 and DG_PAST_MOVE ?
        fen = FEN_START

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=gameno,
                        rated=rated == "1",
                        game_type=game_type,
                        minutes=int(wmin),
                        inc=int(winc),
                        board=FICSBoard(wms, bms, fen=fen))

        if self.connection.examined_game is not None:
            pgnHead = [("Event", "ICC examined game"),
                       ("Site", "chessclub.com"), ("White", wname),
                       ("Black", bname), ("Result", "*"), ("SetUp", "1"),
                       ("FEN", fen)]
            pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n*\n"

            game.relation = IC_POS_EXAMINATING
            game.game_type = GAME_TYPES["examined"]
            game.board.pgn = pgn

        game = self.connection.games.get(game)

        for player in (wplayer, bplayer):
            if player.status != IC_STATUS_PLAYING:
                player.status = IC_STATUS_PLAYING
            if player.game != game:
                player.game = game

        self.theGameImPlaying = game
        self.my_game_info = (WHITE, 0, wms, bms)
        self.gamemodelStartedEvents[gameno] = asyncio.Event()
        self.connection.client.run_command("follow")

        if self.connection.examined_game is not None:
            self.emit("exGameCreated", game)
        else:
            if int(wild) in (1, 2, 3, 4, 20, 21, 22):
                # several wild variant (including loadfen/loadgame) need
                # a starting FEN coming in a DG_POSITION_BEGIN datgram
                log.debug("wild20 is waiting for a starting FEN...")
            else:
                self.emit("playGameCreated", game)
Beispiel #16
0
    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)
Beispiel #17
0
    def test1(self):
        """ Test puzzlebot starting a new mate in 2 puzzle """

        # tell puzzlebot gm2
        lines = [
            BLOCK_START + '92' + BLOCK_SEPARATOR + '132' + BLOCK_SEPARATOR,
            '(told puzzlebot, who is examining a game)',
            BLOCK_END,
            'fics% ',
            BLOCK_POSE_START + '\n' + BLOCK_START + '0' + BLOCK_SEPARATOR +
            '80' + BLOCK_SEPARATOR,
            'You are now observing game 127.',
            'Game 127: gbtami (0) puzzlebot (0) unrated untimed 0 0',
            '',
            '<12> --kr---r pp--bppp ----bn-- -Npp---q -----B-Q ---B---- PPP--PPP -K-R---R W -1 0 0 0 0 0 127 gbtami puzzlebot -2 0 0 0 0 0 0 1 none (0:00.000) none 0 0 0',
            BLOCK_END,
            'fics% ',
            BLOCK_POSE_END,
        ]

        signal = 'obsGameCreated'

        game = FICSGame(FICSPlayer("gbtami"),
                        FICSPlayer("puzzlebot"),
                        gameno=127,
                        relation=IC_POS_OBSERVING_EXAMINATION)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            'Removing game 127 from observation list.'
            '',
            'puzzlebot has made you an examiner of game 127.',
            '',
            '<12> --kr---r pp--bppp ----bn-- -Npp---q -----B-Q ---B---- PPP--PPP -K-R---R W -1 0 0 0 0 0 127 gbtami puzzlebot 2 0 0 0 0 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: This is mate problem number [00655]',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: White moves and mates in 2 moves.',
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        # check that our relation has changed as expected
        self.assertEqual(game.relation, IC_POS_EXAMINATING)

        # try with a wrong move first: h4h5
        lines = [
            'puzzlebot stopped examining game 127.',
            'fics% ',
            BLOCK_START + '109' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> --kr---r pp--bppp ----bn-- -Npp---Q -----B-- ---B---- PPP--PPP -K-R---R B -1 0 0 0 0 0 127 gbtami puzzlebot 2 0 0 0 247 0 0 1 Q/h4-h5 (0:00.000) Qxh5 0 0 0',
            '',
            'Game 127: gbtami moves: Qxh5',
            BLOCK_END,
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        game = self.connection.games.get(game)
        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 1)

        # puzzlebot just backs up our move and sends the puzzle starting position again
        lines = [
            BLOCK_POSE_START + '\n' + BLOCK_START + '0' + BLOCK_SEPARATOR +
            '80' + BLOCK_SEPARATOR,
            'puzzlebot is now an examiner of game 127.',
            BLOCK_END,
            'fics% ',
            BLOCK_POSE_END,
            'Game 127: puzzlebot backs up 1 move.',
            '',
            '<12> --kr---r pp--bppp ----bn-- -Npp---q -----B-Q ---B---- PPP--PPP -K-R---R W -1 0 0 0 0 0 127 gbtami puzzlebot 2 0 0 34 35 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            'Game 127: Still in progress *',
            '',
            'puzzlebot(TD)(----)[127] kibitzes: There is a better move.',
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 0)

        # now we take the good move: b5a7
        lines = [
            'puzzlebot stopped examining game 127.',
            'fics% ',
            BLOCK_START + '114' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> --kr---r Np--bppp ----bn-- --pp---q -----B-Q ---B---- PPP--PPP -K-R---R B -1 0 0 0 0 0 127 gbtami puzzlebot 2 0 0 34 34 0 0 1 N/b5-a7 (0:00.000) Nxa7+ 0 0 0',
            '',
            'Game 127: gbtami moves: Nxa7+',
            BLOCK_END,
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 1)

        # puzzlebot moves
        lines = [
            BLOCK_POSE_START + '\n' + BLOCK_START + '0' + BLOCK_SEPARATOR +
            '75' + BLOCK_SEPARATOR,
            'puzzlebot is now an examiner of game 127.', BLOCK_END, 'fics% ',
            BLOCK_POSE_END,
            '<12> ---r---r Np-kbppp ----bn-- --pp---q -----B-Q ---B---- PPP--PPP -K-R---R W -1 0 0 0 0 1 127 gbtami puzzlebot 2 0 0 34 34 0 0 2 K/c8-d7 (0:00.000) Kd7 0 0 0',
            'fics% ', 'Game 127: puzzlebot moves: Kd7', 'fics% '
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])
        print(self.games_persp.cur_gmwidg().gamemodel.moves)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 2)

        # we make the mating move: d3b5
        lines = [
            'puzzlebot stopped examining game 127.',
            'fics% ',
            BLOCK_START + '117' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> ---r---r Np-kbppp ----bn-- -Bpp---q -----B-Q -------- PPP--PPP -K-R---R B -1 0 0 0 0 2 127 gbtami puzzlebot 2 0 0 34 34 0 0 2 B/d3-b5 (0:00.000) Bb5# 0 0 0',
            ''
            'Game 127: gbtami moves: Bb5#',
            '',
            'Game 127: Black checkmated 1-0',
            BLOCK_END,
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: You solved problem number [00655] in 02m01s',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: You made 1 wrong moves and needed 0 hints',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: To solve the next problem type "tell puzzlebot next". To solve the previous problem type "tell puzzlebot previous" To automatically load problems type "tell puzzlebot auto" Or type "tell puzzlebot getmate" or "tell puzzlebot gettactics" or "tell puzzlebot getstudy"',
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 3)

        ########################################################################
        # now start another mate in 2 puzzle
        # tell puzzlebot next
        lines = [
            BLOCK_START + '125' + BLOCK_SEPARATOR + '132' + BLOCK_SEPARATOR,
            '(told puzzlebot, who is examining a game)',
            BLOCK_END,
            'fics% ',
            BLOCK_POSE_START + '\n' + BLOCK_START + '0' + BLOCK_SEPARATOR +
            '80' + BLOCK_SEPARATOR,
            'You are now observing game 127.',
            'Game 127: gbtami (0) puzzlebot (0) unrated untimed 0 0',
            '',
            '<12> -----k-- -R------ --p--n-- -------- P-P--pr- -P---RNK -----PP- ----r--- B -1 0 0 0 0 0 127 puzzlebot gbtami -2 0 0 0 0 0 0 1 none (0:00.000) none 0 0 0',
        ]

        signal = 'obsGameCreated'

        game = FICSGame(FICSPlayer("puzzlebot"),
                        FICSPlayer("gbtami"),
                        gameno=127,
                        relation=IC_POS_OBSERVING_EXAMINATION)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            'Removing game 127 from observation list.'
            '',
            'puzzlebot has made you an examiner of game 127.',
            '',
            '<12> -----k-- -R------ --p--n-- -------- P-P--pr- -P---RNK -----PP- ----r--- B -1 0 0 0 0 0 127 puzzlebot gbtami 2 0 0 0 0 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: This is mate problem number [00656]',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: Black moves and mates in 2 moves.',
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        # check that our relation has changed as expected
        self.assertEqual(game.relation, IC_POS_EXAMINATING)

        # g4g3
        lines = [
            'puzzlebot stopped examining game 127.',
            'fics% ',
            BLOCK_START + '147' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> -----k-- -R------ --p--n-- -------- P-P--p-- -P---RrK -----PP- ----r--- W -1 0 0 0 0 0 127 puzzlebot gbtami 2 0 0 253 0 0 0 2 R/g4-g3 (0:00.000) Rxg3+ 0 0 0',
            '',
            'Game 127: gbtami moves: Rxg3+',
            BLOCK_END,
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 2)

        # puzzlebot moves
        lines = [
            BLOCK_POSE_START + '\n' + BLOCK_START + '0' + BLOCK_SEPARATOR +
            '75' + BLOCK_SEPARATOR,
            'puzzlebot is now an examiner of game 127.', BLOCK_END, 'fics% ',
            BLOCK_POSE_END, 'fics% ',
            '<12> -----k-- -R------ --p--n-- -------- P-P--p-- -P---RPK ------P- ----r--- B -1 0 0 0 0 0 127 puzzlebot gbtami 2 0 0 253 251 0 0 2 P/f2-g3 (0:00.000) fxg3 0 0 0',
            'Game 127: puzzlebot moves: fxg3', 'fics% '
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 3)

        # we make the mating move: e1h1
        lines = [
            'puzzlebot stopped examining game 127.',
            'fics% ',
            BLOCK_START + '150' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> -----k-- -R------ --p--n-- -------- P-P--p-- -P---RPK ------P- -------r W -1 0 0 0 0 1 127 puzzlebot gbtami 2 0 0 253 251 0 0 3 R/e1-h1 (0:00.000) Rh1# 0 0 0',
            ''
            'Game 127: gbtami moves: Rh1#',
            '',
            'Game 127: White checkmated 0-1',
            BLOCK_END,
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: You solved problem number [00656] in 00m45s',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: You made 1 wrong moves and needed 0 hints',
            'fics% ',
            'puzzlebot(TD)(----)[127] kibitzes: To solve the next problem type "tell puzzlebot next". To solve the previous problem type "tell puzzlebot previous" To automatically load problems type "tell puzzlebot auto" Or type "tell puzzlebot getmate" or "tell puzzlebot gettactics" or "tell puzzlebot getstudy"',
            'fics% ',
        ]

        async def coro():
            await self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 4)
Beispiel #18
0
    def test2(self):
        """ Test observing lecturebot """

        # ♜ ♛ . . ♚ . ♞ ♜
        # ♟ ♟ . ♝ . ♟ ♟ ♟
        # . . . ♝ ♟ . . .
        # . . . ♘ . . . .
        # . . . . . . ♕ .
        # . . . ♗ . . . .
        # ♙ ♙ . . . ♙ ♙ .
        # ♖ . ♗ . ♖ . . ♔

        lines = [
            BLOCK_START + '53' + BLOCK_SEPARATOR + '80' + BLOCK_SEPARATOR,
            'You are now observing game 1.',
            'Game 1: LectureBot (0) LectureBot (0) unrated untimed 0 0',
            '',
            '<12> rq--k-nr pp-b-ppp ---bp--- ---N---- ------Q- ---B---- PP---PP- R-B-R--K W -1 0 0 1 1 2 1 LectureBot LectureBot -2 0 0 32 34 0 0 15 B/h2-d6 (0:00.000) Bd6 0 0 0',
            BLOCK_END
        ]

        signal = 'obsGameCreated'

        game = FICSGame(
            FICSPlayer("LectureBot"),
            FICSPlayer("LectureBot"),
            gameno=1)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            "<12> rq--k-nr pp-b-pQp ---bp--- ---N---- -------- ---B---- PP---PP- R-B-R--K B -1 0 0 1 1 0 1 LectureBot LectureBot -2 0 0 32 33 0 0 15 Q/g4-g7 (0:00.000) Qxg7 0 0 0",
            "fics% ",
            "Game 1: LectureBot moves: Qxg7",
            "nfics% ",
            "LectureBot(TD)(----)[1] kibitzes: Black is now going to drop a rook.  Therefore, Black must find an alternate to 13...Bxh2+.",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: Let\'s back up and find the right 13th move.",
            "fics% "
        ]

        def coro():
            yield from self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 29)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            "Game 1: LectureBot backs up 4 moves.",
            "<12> rq--k-nr pp-b-ppp ---bp--- ---N---- ------Q- ---B---- PP---PPP R-B-R-K- B -1 0 0 1 1 2 1 LectureBot LectureBot -2 0 0 33 34 0 0 13 Q/d1-g4 (0:00.000) Qg4 0 0 0",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: Can you find Black\'s best move?",
            "fics% "
        ]

        def coro():
            yield from self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 25)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            "<12> rq---knr pp-b-ppp ---bp--- ---N---- ------Q- ---B---- PP---PPP R-B-R-K- W -1 0 0 0 0 3 1 LectureBot LectureBot -2 0 0 33 34 0 0 14 K/e8-f8 (0:00.000) Kf8 0 0 0",
            "fics% ",
            "Game 1: LectureBot moves: Kf8",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: Black needs to guard the g-pawn.  This also gets the king out of the pin.",
            "fics% ",
            "<12> rq---knr pp-b-ppp ---bp--- ---N---- ------Q- ---B---- PP-B-PPP R---R-K- B -1 0 0 0 0 4 1 LectureBot LectureBot -2 0 0 33 34 0 0 14 B/c1-d2 (0:00.000) Bd2 0 0 0",
            "fics% ",
            "Game 1: LectureBot moves: Bd2",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: White doesn\'t have to move the attacked knight because if 14. exd5, 15. Qxd7 is a trade.",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: Not to mention, it would be a trade favorable to White.",
            "fics% ",
            "<12> rq---knr pp-b-pp- ---bp--- ---N---p ------Q- ---B---- PP-B-PPP R---R-K- W 7 0 0 0 0 0 1 LectureBot LectureBot -2 0 0 33 34 0 0 15 P/h7-h5 (0:00.000) h5 0 0 0",
            "fics% ",
            "Game 1: LectureBot moves: h5",
            "fics% ",
            "LectureBot(TD)(----)[1] kibitzes: Here, Black attacks White\'s queen, and will try to open the h-file for his rook.",
            "fics% "
        ]

        def coro():
            yield from self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 28)

        print(self.games_persp.cur_gmwidg().gamemodel)
Beispiel #19
0
    def test3(self):
        """ Accepting a seek """

        loop = asyncio.get_event_loop()
        loop.set_debug(enabled=True)

        widgets = uistuff.GladeWidgets("PyChess.glade")
        gamewidget.setWidgets(widgets)
        perspective_manager.set_widgets(widgets)

        self.welcome_persp = Welcome()
        perspective_manager.add_perspective(self.welcome_persp)

        self.games_persp = Games()
        perspective_manager.add_perspective(self.games_persp)

        self.fics_persp = FICS()
        perspective_manager.add_perspective(self.fics_persp)
        self.fics_persp.create_toolbuttons()

        self.lounge = perspective_manager.get_perspective("fics")
        self.lounge.open_lounge(self.connection, self.connection,
                                "freechess.org")

        lines = [
            "<s> 11 w=WLTL ti=00 rt=2030  t=1 i=0 r=r tp=lightning c=? rr=0-9999 a=t f=f",
            "fics% ",
            BLOCK_START + "52" + BLOCK_SEPARATOR + "158" + BLOCK_SEPARATOR,
            "<sr> 11 16", "fics% ",
            "Creating: WLTL (2030) gbtami (1771) rated lightning 1 0",
            "{Game 85 (WLTL vs. gbtami) Creating rated lightning match.}", "",
            "<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 85 WLTL gbtami -1 1 0 39 39 60000 60000 1 none (0:00.000) none 1 0 0\n\nGame 85: A disconnection will be considered a forfeit.",
            BLOCK_END, "fics% "
        ]

        me = self.connection.players.get('gbtami')
        me.ratings[TYPE_LIGHTNING] = 1771
        opponent = self.connection.players.get('WLTL')
        opponent.ratings[TYPE_LIGHTNING] = 2030
        game = FICSGame(opponent,
                        me,
                        gameno=85,
                        rated=True,
                        game_type=GAME_TYPES['lightning'],
                        private=False,
                        minutes=1,
                        inc=0,
                        board=FICSBoard(60000, 60000, fen=FEN_START))
        me.game = game
        opponent.game = game
        self.runAndAssertEquals("playGameCreated", lines, (game, ))

        gamemodel = self.games_persp.cur_gmwidg().gamemodel

        def on_game_started(game):
            p1 = gamemodel.players[1]
            p1.move_queue.put_nowait(Move(newMove(G8, F6)))

        gamemodel.connect("game_started", on_game_started)

        lines = [
            "<12> rnbqkbnr pppppppp -------- -------- -------- -P------ P-PPPPPP RNBQKBNR B -1 1 1 1 1 0 85 WLTL gbtami 1 1 0 39 39 60000 60000 1 P/b2-b3 (0:00.000) b3 1 0 0",
            "fics% "
        ]

        game = self.connection.games[game]

        def coro():
            yield from self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        lines = [
            BLOCK_START + "59" + BLOCK_SEPARATOR + "1" + BLOCK_SEPARATOR,
            "<12> rnbqkb-r pppppppp -----n-- -------- -------- -P------ P-PPPPPP RNBQKBNR W -1 1 1 1 1 1 85 WLTL gbtami -1 1 0 39 39 60000 60000 2 N/g8-f6 (0:00.000) Nf6 1 1 0",
            BLOCK_END, "fics% ",
            "<12> rnbqkb-r pppppppp -----n-- -------- -------- -P----P- P-PPPP-P RNBQKBNR B -1 1 1 1 1 0 85 WLTL gbtami 1 1 0 39 39 59900 60000 2 P/g2-g3 (0:00.100) g3 1 1 285",
            "fics% "
        ]

        def coro():
            yield from self.connection.process_lines(lines)

        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)
        self.assertEqual(gamemodel.ply, 3)
        print(gamemodel.boards[-1])
Beispiel #20
0
    def test1(self):
        """ Test following lecturebot starting new lecture after finishing previous one """

        lines = [
            'LectureBot(TD)(----)[1] kibitzes: That concludes this lecture. If this lecture inspired you to write your own lecture, "finger LectureBot" and read note 8. toddmf would really like to hear from you about LectureBot. Please send him comments! Hope to see you all again soon!',
            'fics% ',
            'LectureBot stopped examining game 1.',
            '',
            'Game 1 (which you were observing) has no examiners.',
            'Removing game 1 from observation list.',
            'fics% ',
            'LectureBot, whom you are following, has started examining a game.',
            'You are now observing game 1.',
            'Game 1: LectureBot (0) LectureBot (0) unrated untimed 0 0',
            '',
            '<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 1 LectureBot LectureBot -2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
        ]

        signal = 'obsGameCreated'

        game = FICSGame(
            FICSPlayer("LectureBot"),
            FICSPlayer("LectureBot"),
            gameno=1)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            '<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 1 Henley LectureBot -2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            '<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 1 Henley Browne -2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            '<12> rnbqkbnr pppppppp -------- -------- -------- -----N-- PPPPPPPP RNBQKB-R B -1 1 1 1 1 1 1 Henley Browne -2 0 0 39 39 0 0 1 N/g1-f3 (0:00.000) Nf3 0 0 0',
            'fics% ',
            'Game 1: LectureBot moves: Nf3',
        ]

        async def coro():
            await self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 1)

        lines = [
            '<12> rnbqkbnr pp-ppppp -------- --p----- -------- -----N-- PPPPPPPP RNBQKB-R W 2 1 1 1 1 0 1 Henley Browne -2 0 0 39 39 0 0 2 P/c7-c5 (0:00.000) c5 0 0 0',
            'fics% ',
            'Game 1: LectureBot moves: c5',
            '<12> rnbqkbnr pp-ppppp -------- --p----- -------- -----NP- PPPPPP-P RNBQKB-R B -1 1 1 1 1 0 1 Henley Browne -2 0 0 39 39 0 0 2 P/g2-g3 (0:00.000) g3 0 0 0',
            'fics% '
            'Game 1: LectureBot moves: g3',
            '<12> rnbqkbnr p--ppppp -p------ --p----- -------- -----NP- PPPPPP-P RNBQKB-R W -1 1 1 1 1 0 1 Henley Browne -2 0 0 39 39 0 0 3 P/b7-b6 (0:00.000) b6 0 0 0',
            'fics% '
            'Game 1: LectureBot moves: b6',
        ]

        async def coro():
            await self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 4)
Beispiel #21
0
    def test2(self):
        """ Test manual examine lecturebot lec2 """

        lines = [
            # examine
            BLOCK_START + '62' + BLOCK_SEPARATOR + '36' + BLOCK_SEPARATOR,
            'Starting a game in examine (scratch) mode.',
            '',
            '<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
        ]

        signal = 'exGameCreated'

        game = FICSGame(
            FICSPlayer("gbtami"),
            FICSPlayer("gbtami"),
            gameno=77)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            # bsetup
            BLOCK_START + '63' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            "Entering setup mode.",
            'fics% ',
            BLOCK_END,
            # bsetup fen r1bq2k1/2p2p1p/p1pp2pB/2n4r/8/2N2Q2/PPP2PPP/4RRK1
            BLOCK_START + '64' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 1 1 1 1 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup wcastle none
            BLOCK_START + '65' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 1 1 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup bcastle none
            BLOCK_START + '66' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # tomove white
            BLOCK_START + '67' + BLOCK_SEPARATOR + '134' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup done
            BLOCK_START + '68' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            'Game is validated - entering examine mode.',
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 gbtami gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # wname Honfi
            BLOCK_START + '69' + BLOCK_SEPARATOR + '148' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 Honfi gbtami 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bname Sebestyen
            BLOCK_START + '70' + BLOCK_SEPARATOR + '19' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp--pB --n----r -------- --N--Q-- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 39 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # f3f6
            BLOCK_START + '71' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> r-bq--k- --p--p-p p-pp-QpB --n----r -------- --N----- PPP--PPP ----RRK- B -1 0 0 0 0 1 77 Honfi Sebestyen 2 0 0 39 39 0 0 1 Q/f3-f6 (0:00.000) Qf6 0 0 0',
            'Game 77: gbtami moves: Qf6',
            'fics% ',
            BLOCK_END,
            # d8f6
            BLOCK_START + '72' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> r-b---k- --p--p-p p-pp-qpB --n----r -------- --N----- PPP--PPP ----RRK- W -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 2 Q/d8-f6 (0:00.000) Qxf6 0 0 0',
            'Game 119: gbtami moves: Qxf6',
            'fics% ',
            BLOCK_END,
            # e1e8
            BLOCK_START + '73' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> r-b-R-k- --p--p-p p-pp-qpB --n----r -------- --N----- PPP--PPP -----RK- B -1 0 0 0 0 1 77 Honfi Sebestyen 2 0 0 30 39 0 0 2 R/e1-e8 (0:00.000) Re8# 0 0 0',
            'Game 119: gbtami moves: Re8#',
            'Game 119: Black checkmated 1-0',
            'fics% ',
            BLOCK_END,
        ]

        async def coro():
            await self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[-1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 3)

        lines = [
            # kibitz Example 2:...
            BLOCK_START + '74' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            'gbtami(1719)[77] kibitzes: Example 2: Dementyev vs Karpov, Riga 1971',
            BLOCK_END,
            # bsetup
            BLOCK_START + '75' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            "Entering setup mode.",
            'fics% ',
            BLOCK_END,
            # bsetup fen q3r1k1/4Rp1p/6p1/2Q1B3/P2P4/1r1b3P/5PP1/4R1K1
            BLOCK_START + '76' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- B -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup wcastle none
            BLOCK_START + '77' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- B -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup bcastle none
            BLOCK_START + '78' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- B -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # tomove white
            BLOCK_START + '79' + BLOCK_SEPARATOR + '134' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- W -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bsetup done
            BLOCK_START + '80' + BLOCK_SEPARATOR + '21' + BLOCK_SEPARATOR,
            'Game is validated - entering examine mode.',
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- W -1 0 0 0 0 0 77 Honfi Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # wname Dementyev
            BLOCK_START + '81' + BLOCK_SEPARATOR + '148' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- W -1 0 0 0 0 0 77 Dementyev Sebestyen 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # bname Karpov
            BLOCK_START + '82' + BLOCK_SEPARATOR + '19' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- --Q-B--- P--P---- -r-b---P -----PP- ----R-K- W -1 0 0 0 0 0 77 Dementyev Karpov 2 0 0 30 39 0 0 1 none (0:00.000) none 0 0 0',
            'fics% ',
            BLOCK_END,
            # c5d5
            BLOCK_START + '83' + BLOCK_SEPARATOR + '1' + BLOCK_SEPARATOR,
            '<12> q---r-k- ----Rp-p ------p- ---QB--- P--P---- -r-b---P -----PP- ----R-K- B -1 0 0 0 0 1 77 Dementyev Karpov 2 0 0 30 39 0 0 1 Q/c5-d5 (0:00.000) Qd5 0 0 0',
            'Game 119: gbtami moves: Qd5',
            'fics% ',
            BLOCK_END,
        ]

        async def coro():
            await self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        print(self.games_persp.cur_gmwidg().gamemodel.boards[1])

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 1)
Beispiel #22
0
    def test1(self):
        """ Test observing game """

        lines = [
            "{Game 463 (schachbjm vs. Maras) Creating rated standard match.}",
            BLOCK_START + '34' + BLOCK_SEPARATOR + '80' + BLOCK_SEPARATOR,
            'You are now observing game 463.',
            'Game 463: schachbjm (2243) Maras (2158E) rated standard 45 45',
            '',
            '<12> -r------ --k----- ----p--- n-ppPb-p -----P-P -PP-K-P- PR------ --R----- W -1 0 0 0 0 11 463 schachbjm Maras 0 45 45 17 15 557871 274070 37 R/f8-b8 (0:10.025) Rb8 0 1 0',
            BLOCK_END
        ]

        def coro():
            yield from self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(self.connection.client.commands[-1], "moves 463")

        signal = 'obsGameCreated'
        lines = [
            'Movelist for game 463:',
            '',
            'schachbjm (2243) vs. Maras (2158) --- Sat Jan 23, 14:34 EST 2016',
            'Rated standard match, initial time: 45 minutes, increment: 45 seconds.',
            '',
            'Move  schachbjm               Maras',
            '----  ---------------------   ---------------------',
            '1.  e4      (0:00.000)      e6      (0:00.000)',
            '2.  d4      (0:01.617)      d5      (0:02.220)',
            '3.  Nc3     (0:00.442)      Nc6     (0:54.807)',
            '4.  e5      (0:40.427)      Nge7    (0:28.205)',
            '5.  Nf3     (0:21.570)      Nf5     (0:28.818)',
            '6.  h4      (1:17.369)      h5      (4:58.315)',
            '7.  Bg5     (0:55.946)      Be7     (4:01.555)',
            '8.  Qd2     (0:02.434)      b6      (5:12.110)',
            '9.  O-O-O   (0:59.124)      Bb7     (0:08.796)',
            '10.  Kb1     (0:01.900)      Qd7     (4:39.500)',
            '11.  Bxe7    (19:59.514)     Qxe7    (2:42.462)',
            '12.  g3      (0:58.847)      O-O-O   (0:36.468)',
            '13.  Bh3     (0:12.284)      Nh6     (4:06.076)',
            '14.  Ne2     (0:02.387)      g6      (5:02.695)',
            '15.  Nf4     (0:02.976)      Kb8     (5:26.776)',
            '16.  Rhe1    (2:33.781)      Na5     (2:23.956)',
            '17.  b3      (0:28.817)      Rc8     (1:09.281)',
            '18.  Ng5     (8:15.515)      c5      (5:17.139)',
            '19.  Bxe6    (12:26.052)     fxe6    (1:14.670)',
            '20.  Nxg6    (0:02.168)      Qd7     (1:23.832)',
            '21.  Nxh8    (0:02.249)      Rxh8    (0:04.212)',
            '22.  dxc5    (0:14.456)      Nf5     (0:24.046)',
            '23.  cxb6    (0:07.092)      axb6    (0:03.296)',
            '24.  Qb4     (0:42.800)      Qc6     (2:48.991)',
            '25.  Nf7     (2:09.657)      Rc8     (0:37.030)',
            '26.  Rd2     (0:01.602)      Qc5     (5:03.082)',
            '27.  Qxc5    (0:09.672)      bxc5    (0:00.100)',
            '28.  Nd6     (0:00.849)      Rf8     (0:04.101)',
            '29.  c3      (0:57.437)      Kc7     (3:05.263)',
            '30.  Nxf5    (1:51.872)      Rxf5    (0:00.100)',
            '31.  f4      (0:00.603)      Bc6     (1:06.696)',
            '32.  Kc2     (0:01.613)      Be8     (0:07.670)',
            '33.  Kd3     (1:39.823)      Rf8     (1:28.227)',
            '34.  Ke3     (0:06.207)      Bg6     (0:08.648)',
            '35.  Rc1     (3:24.100)      Bf5     (1:11.762)',
            '36.  Rb2     (0:13.173)      Rb8     (0:10.025)',
            '{Still in progress} *',
        ]

        game = FICSGame(
            FICSPlayer("schachbjm"),
            FICSPlayer("Maras"),
            gameno=463)
        game = self.connection.games.get(game)
        expectedResults = (game, )
        self.runAndAssertEquals(signal, lines, expectedResults)

        print(self.games_persp.cur_gmwidg().gamemodel)

        lines = [
            '<12> -r------ --k----- ----p--- n-ppPb-p -----P-P -PP-K-P- PR------ ------R- B -1 0 0 0 0 11 463 schachbjm Maras 0 45 45 17 15 557871 274070 37 R/b2-g2 (0:10.025) Rg2 0 1 0',
            '<12> ------r- --k----- ----p--- n-ppPb-p -----P-P -PP-K-P- PR------ ------R- W -1 0 0 0 0 11 463 schachbjm Maras 0 45 45 17 15 557871 274070 38 R/b8-g8 (0:10.025) Rg8 0 1 0',
        ]

        def coro():
            yield from self.connection.process_lines(lines)
        self.loop.run_until_complete(coro())

        self.assertEqual(game.move_queue.qsize(), 0)

        self.assertEqual(self.games_persp.cur_gmwidg().gamemodel.ply, 74)

        print(self.games_persp.cur_gmwidg().gamemodel)
Beispiel #23
0
    def onObserveGameCreated(self, matchlist):
        log.debug("'%s'" % (matchlist[1].string),
                  extra={
                      "task":
                      (self.connection.username, "BM.onObserveGameCreated")
                  })
        if self.connection.USCN:
            # TODO? is this ok?
            game_type = GAME_TYPES["blitz"]
            castleSigns = ("k", "q")
        else:
            gameno, wname, wrating, bname, brating, rated, gametype, minutes, inc = matchlist[
                1].groups()
            wrating = parseRating(wrating)
            brating = parseRating(brating)
            game_type = GAME_TYPES[gametype]

        style12 = matchlist[-1].groups()[0]

        castleSigns = self.generateCastleSigns(style12, game_type)
        gameno, relation, curcol, ply, wname, bname, wms, bms, gain, lastmove, fen = \
            self.parseStyle12(style12, castleSigns)
        gameno = int(gameno)
        self.castleSigns[gameno] = castleSigns

        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)

        if relation == IC_POS_OBSERVING_EXAMINATION:
            pgnHead = [("Event",
                        "FICS %s %s game" % (rated, game_type.fics_name)),
                       ("Site", "freechess.org"), ("White", wname),
                       ("Black", bname), ("Result", "*"), ("SetUp", "1"),
                       ("FEN", fen)]
            pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n*\n"
            game = FICSGame(wplayer,
                            bplayer,
                            gameno=gameno,
                            rated=rated == "rated",
                            game_type=game_type,
                            minutes=int(minutes),
                            inc=int(inc),
                            board=FICSBoard(wms, bms, pgn=pgn),
                            relation=relation)
            game = self.connection.games.get(game)
            self.gamesImObserving[game] = wms, bms

            self.gamemodelStartedEvents[game.gameno] = threading.Event()
            self.emit("obsGameCreated", game)
            self.gamemodelStartedEvents[game.gameno].wait()
        else:
            game = FICSGame(wplayer,
                            bplayer,
                            gameno=gameno,
                            rated=rated == "rated",
                            game_type=game_type,
                            minutes=int(minutes),
                            inc=int(inc),
                            relation=relation)
            game = self.connection.games.get(game, emit=False)

            if not game.supported:
                log.warning("Trying to follow an unsupported type game %s" %
                            game.game_type)
                return

            if game.gameno in self.gamemodelStartedEvents:
                log.warning("%s already in gamemodelstartedevents" %
                            game.gameno)
                return

            self.gamesImObserving[game] = wms, bms
            self.queuedStyle12s[game.gameno] = []
            self.queuedEmits[game.gameno] = []
            self.gamemodelStartedEvents[game.gameno] = threading.Event()

            # FICS doesn't send the move list after 'observe' and 'follow' commands
            self.connection.client.run_command("moves %d" % game.gameno)
Beispiel #24
0
    def on_icc_my_game_started(self, data):
        # gamenumber whitename blackname wild-number rating-type rated
        # white-initial white-increment black-initial black-increment
        # played-game {ex-string} white-rating black-rating game-id
        # white-titles black-titles irregular-legality irregular-semantics
        # uses-plunkers fancy-timecontrol promote-to-king
        # 685 Salsicha MaxiBomb 0 Blitz 1 3 0 3 0 1 {} 2147 2197 1729752694 {} {} 0 0 0 {} 0
        # 259 Rikikilord ARMH 0 Blitz 1 2 12 2 12 0 {Ex: Rikikilord 0} 1532 1406 1729752286 {} {} 0 0 0 {} 0
        gameno, wname, bname, wild, rtype, rated, wmin, winc, bmin, binc, played_game, rest = data.split(" ", 11)

        parts = rest.split("}", 1)[1].split()
        wrating = int(parts[0])
        brating = int(parts[1])

        gameno = int(gameno)
        wplayer = self.connection.players.get(wname)
        bplayer = self.connection.players.get(bname)
        game_type = GAME_TYPES[rtype.lower()]

        for player, rating in ((wplayer, wrating), (bplayer, brating)):
            if player.ratings[game_type.rating_type] != rating:
                player.ratings[game_type.rating_type] = rating
                player.emit("ratings_changed", game_type.rating_type, player)

        wms = bms = int(wmin) * 60 * 1000 + int(winc) * 1000
        # TODO: maybe use DG_POSITION_BEGIN2 and DG_PAST_MOVE ?
        fen = FEN_START

        game = FICSGame(wplayer,
                        bplayer,
                        gameno=gameno,
                        rated=rated == "1",
                        game_type=game_type,
                        minutes=int(wmin),
                        inc=int(winc),
                        board=FICSBoard(wms,
                                        bms,
                                        fen=fen))

        if self.connection.examined_game is not None:
            pgnHead = [
                ("Event", "ICC examined game"), ("Site", "chessclub.com"),
                ("White", wname), ("Black", bname), ("Result", "*"),
                ("SetUp", "1"), ("FEN", fen)
            ]
            pgn = "\n".join(['[%s "%s"]' % line for line in pgnHead]) + "\n*\n"

            game.relation = IC_POS_EXAMINATING
            game.game_type = GAME_TYPES["examined"]
            game.board.pgn = pgn

        game = self.connection.games.get(game)

        for player in (wplayer, bplayer):
            if player.status != IC_STATUS_PLAYING:
                player.status = IC_STATUS_PLAYING
            if player.game != game:
                player.game = game

        self.theGameImPlaying = game
        self.my_game_info = (WHITE, 0, wms, bms)
        self.gamemodelStartedEvents[gameno] = asyncio.Event()
        self.connection.client.run_command("follow")

        if self.connection.examined_game is not None:
            self.emit("exGameCreated", game)
        else:
            self.emit("playGameCreated", game)
Beispiel #25
0
    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_games(self, data):
        # 1267      guest2504            1400 KQkr(C)              20u  5  12       W:  1
        # 1060      guest7400            1489 DeadGuyKai            bu  3   0       W: 21
        # 791 1506 PlotinusRedux             guest3090             bu  2  12       W: 21
        # 47 2357 *IM_Danchevski       2683 *GM_Morozevich       Ex: scratch      W: 35
        # 101      Replayer2                 Replayer2            Ex: scratch      W:  1
        # 117 2760 *GM_Topalov          2823 *GM_Caruana          Ex: StLouis16 %0 W: 29
        # 119 1919 stansai              2068 Agrimont             Ex: continuation W: 53
        # 456 games displayed (282 played, 174 examined).
        previous_games = list(self.connection.games.values())
        games = []
        games_got = []
        lines = data.split("\n")
        for line in lines:
            # print(line)
            try:
                parts = line.split()
                index = 0

                gameno = parts[index]
                index += 1

                if parts[index].isdigit():
                    wrating = parts[index]
                    index += 1
                else:
                    wrating = "----"

                wname = parts[index]
                index += 1

                if parts[index].isdigit():
                    brating = parts[index]
                    index += 1
                else:
                    brating = "----"

                bname = parts[index]
                index += 1

                if parts[index] == "Ex:":
                    shorttype = "e"
                    rated = ""
                    min = "0"
                    inc = "0"
                else:
                    rated = parts[index][-1]
                    shorttype = parts[index][:-1]
                    index += 1
                    min = parts[index]
                    index += 1
                    inc = parts[index]
                private = ""
            except IndexError:
                continue

            try:
                gametype = GAME_TYPES_BY_SHORT_FICS_NAME[shorttype]
            except KeyError:
                # TODO: handle ICC wild types
                # print("key error in GAME_TYPES_BY_SHORT_FICS_NAME: %s" % shorttype)
                continue

            wplayer = self.connection.players.get(wname)
            bplayer = self.connection.players.get(bname)
            game = FICSGame(
                wplayer,
                bplayer,
                gameno=int(gameno),
                rated=(rated == "r"),
                private=(private == "p"),
                minutes=int(min),
                inc=int(inc),
                game_type=gametype,
            )

            for player, rating in ((wplayer, wrating), (bplayer, brating)):
                if player.status != IC_STATUS_PLAYING:
                    player.status = IC_STATUS_PLAYING
                if player.game != game:
                    player.game = game
                rating = parseRating(rating)
                if player.ratings[gametype.rating_type] != rating:
                    player.ratings[gametype.rating_type] = rating
                    player.emit("ratings_changed", gametype.rating_type,
                                player)
            if game not in previous_games:
                game = self.connection.games.get(game, emit=False)
                games.append(game)
            games_got.append(game)

        for game in previous_games:
            if (game not in games_got
                    and game not in self.connection.bm.gamesImObserving
                    and game is not self.connection.bm.theGameImPlaying):
                self.connection.games.game_ended(game)
                game.wplayer.game = None
                game.bplayer.game = None

        self.connection.games.emit("FICSGameCreated", games)