Commit aaa95811 authored by Jeremy Pallats's avatar Jeremy Pallats 💬
Browse files

FIX #103: Snipe Command Fixes

parent 633a2362
Loading
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -148,7 +148,8 @@ class Admin(Action):
    """
    def check_cmd(self):
        """ Sanity check that cmd exists. """
        cmd_set = sorted([cls.__name__ for cls in cog.actions.Action.__subclasses__()])
        cmd_set = sorted([cls.__name__ for cls in cog.actions.Action.__subclasses__()]
                         + ['Snipe', 'SnipeHold'])
        cmd_set.remove('Admin')  # Admin cannot be restricted even by admins
        if not self.args.rule_cmd or self.args.rule_cmd not in cmd_set:
            raise cog.exc.InvalidCommandArgs("Rules require a command in following set: \n\n%s"
@@ -1103,6 +1104,13 @@ class Hold(Action):
        await self.bot.send_message(self.msg.channel, response)


class SnipeHold(Hold):
    """
    SnipeHold, same as Hold but for snipe sheet.
    """
    pass


class KOS(Action):
    """
    Handle the KOS command.
@@ -1690,6 +1698,13 @@ class UM(Action):
        await self.bot.send_message(self.msg.channel, response)


class Snipe(UM):
    """
    Snipe, same as UM but for snipe sheet.
    """
    pass


class User(Action):
    """
    Manage your user settings.
+1 −1
Original line number Diff line number Diff line
@@ -183,7 +183,7 @@ class CogBot(discord.Client):
                self.sched.register('hudson_undermine', scanners['hudson_undermine'],
                                    ('Hold', 'UM', 'User'))
                self.sched.register('hudson_snipe', scanners['hudson_snipe'],
                                    ('Hold', 'UM', 'User'))
                                    ('SnipeHold', 'Snipe'))
                self.sched.register('hudson_kos', scanners['hudson_kos'], ('KOS'))
                self.sched.register('hudson_ocr', scanners['hudson_ocr'], ('OCR'))
                self.sched.schedule_all(delay=1)
+2 −2
Original line number Diff line number Diff line
@@ -364,7 +364,7 @@ Examples:
and 130% opposition.
    """.format(prefix=prefix)
    sub = subs.add_parser(prefix + 'shold', description=desc, formatter_class=RawHelp)
    sub.set_defaults(cmd='Hold', sheet_src=EUMSheet.snipe)
    sub.set_defaults(cmd='SnipeHold', sheet_src=EUMSheet.snipe)
    sub.add_argument('amount', nargs='?', type=int, help='The amount of merits held.')
    sub.add_argument('system', nargs='*', help='The system merits are held in.')
    sub.add_argument('-r', '--redeem', action='store_true', help='Redeem all held merits.')
@@ -680,7 +680,7 @@ Examples:
        List powerplay NPC ships by alligence.
    """.format(prefix=prefix)
    sub = subs.add_parser(prefix + 'snipe', description=desc, formatter_class=RawHelp)
    sub.set_defaults(cmd='UM', sheet_src=EUMSheet.snipe)
    sub.set_defaults(cmd='Snipe', sheet_src=EUMSheet.snipe)
    sub.add_argument('system', nargs='*', help='The system to update or show.')
    sub.add_argument('-s', '--set',
                     help='Set the status of the system, us:them. Example-> --set 3500:200')
+21 −18
Original line number Diff line number Diff line
@@ -104,19 +104,24 @@ class FortScanner():

        self.flush_to_db(session, (users, systems, drops))

    # TODO: Change to updating objects instead of deleting.
    def drop_db_entries(self, session):
        """
        Drop the objects in the database that this scanner is responsible for.
        """
        for cls in self.db_classes:
            session.query(cls).delete()
        session.commit()

    def flush_to_db(self, session, new_objs):
        """
        Flush the parsed values directly into the database.
        This method will purge old entries first.
        Old values will be dropped first as no guarantee same objects.

        Args:
            session: A valid session for db.
            new_objs: A list of list of db objects to put in database.
        """
        for cls in self.db_classes:
            session.query(cls).delete()
            session.commit()
        self.drop_db_entries(session)

        for objs in new_objs:
            session.add_all(objs)
@@ -342,22 +347,12 @@ class UMScanner(FortScanner):

        self.flush_to_db(session, (users, systems, holds))

    def flush_to_db(self, session, new_objs):
    def drop_db_entries(self, session):
        """
        Flush the parsed values directly into the database.
        Ensure ONLY the correct sheet_src dropped.

        Args:
            session: A valid session for db.
            new_objs: A list of list of db objects to put in database.
        Drop the main um entries in the um part of the database.
        """
        for cls in self.db_classes:
            session.query(cls).filter(cls.sheet_src == self.sheet_src).delete()
            session.commit()

        for objs in new_objs:
            session.add_all(objs)
            session.flush()
            session.query(cls).filter(cls.id < SNIPE_FIRST_ID).delete()
            session.commit()

    def users(self, *, row_cnt=None, first_id=1, cls=UMUser):
@@ -642,6 +637,14 @@ class SnipeScanner(UMScanner):
    def __repr__(self):
        return super().__repr__().replace('FortScanner', 'SnipeScanner')

    def drop_db_entries(self, session):
        """
        Drop the snipe entries in the um part of the database.
        """
        for cls in self.db_classes:
            session.query(cls).filter(cls.id >= SNIPE_FIRST_ID).delete()
            session.commit()

    def users(self, *, row_cnt=None, first_id=SNIPE_FIRST_ID, cls=UMUser):
        """
        Scan the users in the sheet and return sheet user objects.