Exemple #1
0
    def pending_command(self, usr, args, tags):
        """Pending
        ======
        This command allows you to view pending games of yourself or a user.
        Also you can use -t tourney_name if you want all games from that tourney
        
        EXAMPLES:
            o pending -> your pending games
            o pending seberg -> seberg's pending games
            o pending -t test -> pending games for tourney test 
        """
        args = parse(args)

        if args[1].has_key("t") and len(args[1]["t"]) > 0:
            tourn = args[1]["t"][0]
        else:
            tourn = None

        if args[0]:
            user = self.icsbot["users"][args[0][0]]
            if not user["rowid"]:
                return self.icsbot.qtell.split(usr, "The user %s is not registered with me." % user["handle"])
        else:
            if not usr["rowid"] and not tourn:
                return self.icsbot.qtell.split(
                    usr, "As you are not registered with me, you must give a handle or a tourney."
                )
            user = usr

        if not tourn:
            self.games.sql.dcursor.execute(
                'SELECT rowid FROM games WHERE (white_id=? or black_id=?) and (result="-" or result="*" or result="ADJ")',
                (user["rowid"], user["rowid"]),
            )
            gs = self.games.sql.dcursor.fetchall()
            if not gs:
                return self.icsbot.qtell.split(usr, "%s has no unifinished games." % user["handle"])
            else:
                to_send = ["Unfinished games of %s:" % user["handle"]]

        else:
            self.games.sql.dcursor.execute("SELECT rowid, * FROM tourneys WHERE name=?", (tourn,))
            tourney = self.games.sql.dcursor.fetchall()
            if len(tourney) == 0:
                return self.icsbot.qtell.split(usr, 'The tourney "%s" does not exist.' % tourn)
            elif len(tourney) > 1:
                return self.icsbot.qtell.split(usr, "More then one tourney matches. This _should_ be impossible.")

            tourney = tourney[0]
            self.games.sql.dcursor.execute(
                'SELECT rowid FROM games WHERE tourney_id=? and (result="-" or result="*")', (tourney["rowid"],)
            )
            gs = self.games.sql.dcursor.fetchall()
            if not gs:
                return self.icsbot.qtell.split(usr, "There are no unifinished games in tourney %s." % tourney["name"])
            else:
                to_send = ["Unfinished in tourney %s:" % tourney["name"]]

        to_send.append("+---------+-------------------+-------------------+-------+---------+")
        to_send.append("| Tourney |             white | black             | Round |  Result |")
        to_send.append("+---------+-------------------+-------------------+-------+---------+")
        for g in gs:
            # Yes, this does do some more SQLs then necessary :).
            g = self.games[g["rowid"]]
            to_send.append(
                "| %7s | %17s | %-17s | %5s | %s |"
                % (g["tourney"], g["white"], g["black"], g["round"], g["result"].center(7))
            )
        to_send.append("+---------+-------------------+-------------------+-------+---------+")
        return self.icsbot.qtell.send_list(usr, to_send)
Exemple #2
0
    def play(self, user, args, tags):
        """play [tourney] [-o opponent] [-r round]
        Ask the bot to issue a match command for you and start looking for the
        game to start. This commands assumes the first game that fits to be
        the right game.
        EXAMPLES:
            o play
            o play test
            o play -o seberg
            o play test -o seberg -r 2
        -t and -r can be given in any order at any point.        
        """
        args = parse(args)
        if args[0]:
            tourney = args[0][0]
        else:
            tourney = None
        if args[1].has_key("o") and len(args[1]["o"]) > 0:
            opp_handle = args[1]["o"][0].lower()
        else:
            opp_handle = None
        if args[1].has_key("r") and len(args[1]["r"]) > 0:
            try:
                round_ = int(args[1]["r"][0])
            except:
                return self.icsbot.qtell.split(usr, "Round must be an integer.")
        else:
            round_ = None

        handle_n = user["handle"]
        handle = handle_n.lower()
        for g_p in self.pending():
            if g_p["white"].lower() == handle or g_p["black"].lower() == handle:
                if tourney and tourney.lower() != g_p["tourney"].lower():
                    continue

                if round_ is not None and round_ != g_p["round"]:
                    continue

                if opp_handle is not None and (
                    g_p["white"].lower() != opp_handle and g_p["black"].lower() != opp_handle
                ):
                    continue

                if g_p["black"].lower() == handle:
                    if not self.icsbot["users"][g_p["white"]]["online"]:
                        return self.icsbot.qtell.split(
                            user,
                            'Your opponent %s for the game in the tourney "%s" appears not to be online.'
                            % (g_p["white"], g_p["tourney"]),
                        )
                    l = [g_p["black"], g_p["white"], g_p["controls"], "black"]
                else:
                    if not self.icsbot["users"][g_p["black"]]["online"]:
                        return self.icsbot.qtell.split(
                            user,
                            'Your opponent %s for the game in the tourney "%s" appears not to be online.'
                            % (g_p["black"], g_p["tourney"]),
                        )
                    l = [g_p["white"], g_p["black"], g_p["controls"], "white"]

                if opp_handle and opp_handle.lower() != l[1].lower():
                    continue

                if not " r" in l[2] or " u" in l[2]:
                    l[2] = l[2] + " r"

                if self.no_td:
                    self.icsbot.send('tell %s Please "match %s %s %s"' % tuple(l))
                else:
                    self.icsbot.send("rmatch %s %s %s %s" % tuple(l))
                opponent = self.icsbot["users"][l[1]]
                self.icsbot.send(
                    self.icsbot.qtell.split(
                        opponent,
                        "You have recieved a match request for your game in tourney %s against %s, please accept or decline it."
                        % (g_p["tourney"], handle_n),
                    )
                )
                self.icsbot.send(
                    self.icsbot.qtell.split(
                        user,
                        "A match request for your game in tourney %s against %s has been sent. If this is the wrong game, please withdraw the match request and use: play tourney_name or play -o opponent"
                        % (g_p["tourney"], handle_n),
                    )
                )
                self.games.to_start.add(g_p)
                return
        return self.icsbot.qtell.split(user, "I have not found a game to start for you.")
Exemple #3
0
 def create_pgn(usr, args, tags):
     """createpgn tourney [round] [-c <clock_type>]
     Prompts bot to create a tourney/round pgn with the name tourney_name-round.pgn
     or just tourney_name if no round was given, and dump it into the pgns folder.
     -c sets the clock type that is used. Currently can have 1. no option
     -> no time stamps, 2. clk -> clock reading (default) 3. emt -> (elapsed move
     time) time stamps.
     """
     import icsbot.misc.pgn as pgn
     import os
     
     clocks = {None: None, 'clk': pgn.clk, 'emt': pgn.emt}
     
     args = parse(args)
     if len(args[0]) > 0:
         tourn = args[0][0]
     else:
         return bot.qtell.split(usr, 'You must give a tourney.')
     
     if len(args[0]) > 1:
         try:
             r = int(args[0][1])
         except:
             return bot.qtell.split(usr, 'Round must be an integer.')
     else:
         r = None
     
     to_send = []
     if args[1].has_key('c') and len(args[1]['c']) > 0:
         if args[1]['c'][0] in clocks:
             clock = clocks[args[1]['c'][0]]
         else:
             to_send = ['Invalid clock setting "%s", defaulting to no clock info.' % args[1]['c']]
             clock = clocks[None]
     else:
         clock = clocks[None]
     
     if r is not None:
         insert = ' and round=%s' % r
     else:
         insert = ''
     
     dcursor.execute('SELECT rowid, * FROM tourneys WHERE name=?', (tourn,))
     tourney = dcursor.fetchall()
     if len(tourney) == 0:
         return bot.qtell.split(usr, 'The tourney "%s" does not exist.' % tourn)
     elif len(tourney) > 1:
         return bot.qtell.split(usr, 'More then one tourney matches. This _should_ be impossible.')
 
     tourney = tourney[0]
     dcursor.execute('SELECT rowid FROM games WHERE tourney_id=?%s and (result!="-" or result!="*")' % insert, (tourney['rowid'],))
     gs = dcursor.fetchall()
     if not gs:
         return bot.qtell.split(usr, 'There are no finished games in tourney %s for round %s.' % (tourney['name'], r))
     
     pgns = []
     for g in gs:
         g = games[g['rowid']]
         g.load('rowid')
         g = g.copy()
         tc = g['controls']
         tc = tc.split()
         g['time'] = int(tc[0])
         g['inc'] = int(tc[1])
         if g['tourney_description'] is not None:
             g['tourney'] = g['tourney_description']
         pgns.append(pgn.make_pgn(g, format_time=clock))
     
     pgns = filter(lambda x: x is not False, pgns)
     
     try:
         if r is None:
             f = file('pgns' + os.path.sep + tourney['name'] + '.pgn', 'w')
         else:
             f = file('pgns' + os.path.sep + tourney['name'] + '_%s' % str(r).zfill(2) + '.pgn', 'w')
         
         f.write('\n\n'.join(pgns))
         
     except IOError:
         return bot.qtell.split(usr, 'There was an error saving the pgn, maybe the pgns folder does not exist.')
     
     return bot.qtell.send_list(usr, to_send + ['The pgn was created successfully.'])