Exemple #1
0
    def auto_hatch_eggs(self):
        try:
            connection = mysql.connector.connect(host=self.host,
                                                 user=self.user, port=self.port, passwd=self.password,
                                                 db=self.database)
        except:
            log.error("Could not connect to the SQL database")
            return False

        mon_id = args.auto_hatch_number

        if mon_id == 0:
            log.warn('You have enabled auto hatch but not the mon_id '
                     'so it will mark them as zero so they will remain unhatched...')

        cursor = connection.cursor()

        query_for_count = "SELECT id, fort_id,time_battle,time_end from raids " \
                          "WHERE time_battle <= {0} AND time_end >= {0} AND level = 5 AND IFNULL(pokemon_id,0) = 0" \
            .format(int(time.time()))
        log.debug(query_for_count)

        cursor.execute(query_for_count)
        result = cursor.fetchall()
        rows_that_need_hatch_count = cursor.rowcount
        log.debug("Rows that need updating: {0}".format(rows_that_need_hatch_count))
        if rows_that_need_hatch_count > 0:
            counter = 0
            for row in result:
                log.debug(row)
                query = "UPDATE raids SET pokemon_id = {0} WHERE id = {1}" \
                    .format(mon_id, row[0])

                log.debug(query)
                cursor.execute(query)
                affected_rows = cursor.rowcount
                connection.commit()
                if affected_rows == 1:
                    counter = counter + 1
                    if args.webhook:
                        log.debug('Sending auto hatched raid for raid id {0}'.format(row[0]))
                        send_raid_webhook(row[1], 'MON', row[2], row[3], 5, mon_id)
                    else:
                        log.debug('Sending Webhook is disabled')
                elif affected_rows > 1:
                    log.error(
                        'Something is wrong with the indexing on your table you raids on this id {0}'.format(row['id']))
                else:
                    log.error('The row we wanted to update did not get updated that had id {0}'.format(row['id']))

            if counter == rows_that_need_hatch_count:
                log.info("{0} gym(s) were updated as part of the regular level 5 egg hatching checks".format(counter))
            else:
                log.warn('There was an issue and the number expected the hatch did not match the successful updates. '
                         'Expected {0} Actual {1}'.format(rows_that_need_hatch_count, counter))

            cursor.close()
        else:
            log.info('No Eggs due for hatching')
Exemple #2
0
    def submitRaid(self, gym, pkm, lvl, start, end, type, raidNo, captureTime, MonWithNoEgg=False):
        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) + ') ] ' + 'submitRaid: Submitting raid')

        if self.raidExist(gym, type, raidNo, pkm):
            self.refreshTimes(gym, raidNo, captureTime)
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(
                self.uniqueHash) + ') ] ' + 'submitRaid: %s already submitted - ignoring' % str(type))
            return False

        try:
            connection = mysql.connector.connect(host=self.host,
                                                 user=self.user, port=self.port, passwd=self.password,
                                                 db=self.database)
        except:
            log.error("Could not connect to the SQL database")
            return False

        if start is not None:
            start -= self.timezone * 60 * 60

        if end is not None:
            end -= self.timezone * 60 * 60

        wh_send = False
        wh_start = 0
        wh_end = 0
        eggHatched = False

        cursor = connection.cursor()
        log.debug('[Crop: ' + str(raidNo) + ' (' + str(
            self.uniqueHash) + ') ] ' + 'submitRaid: Submitting something of type %s' % type)

        log.info("Submitting. Gym: %s, Lv: %s, Start and Spawn: %s, End: %s, Mon: %s" % (gym, lvl, start, end, pkm))

        # always insert timestamp to last_scanned to have rows change if raid has been reported before
        updateStr = 'UPDATE raid '
        whereStr = 'WHERE gym_id = \'%s\' ' % str(gym)
        if MonWithNoEgg:
            # submit mon without egginfo -> we have an endtime
            start = end - (int(args.raid_time) * 60)
            log.info("Updating mon without egg")
            setStr = 'SET level = %s, spawn = FROM_UNIXTIME(%s), start = FROM_UNIXTIME(%s), end = FROM_UNIXTIME(%s), ' \
                     'pokemon_id = %s, last_scanned = FROM_UNIXTIME(%s), cp = %s, move_1 = %s, move_2 = %s '
            data = (lvl, captureTime, start, end, pkm, int(time.time()), '999', '1', '1')

            # send out a webhook - this case should only occur once...
            wh_send = True
            wh_start = start
            wh_end = end
        elif end is None or start is None:
            # no end or start time given, just update anything there is
            log.info("Updating without end- or starttime - we should've seen the egg before")
            setStr = 'SET level = %s, pokemon_id = %s, last_scanned = FROM_UNIXTIME(%s), cp = %s, move_1 = %s, move_2 = %s'
            data = (lvl, pkm, int(time.time()), '999', '1', '1')

            foundEndTime, EndTime = self.getRaidEndtime(gym, raidNo)
            if foundEndTime:
                wh_send = True
                wh_start = int(EndTime) - 2700
                wh_end = EndTime
                eggHatched = True
            else:
                wh_send = False
        else:
            log.info("Updating everything")
            # we have start and end, mon is either with egg or we're submitting an egg
            setStr = 'SET level = %s, spawn = FROM_UNIXTIME(%s), start = FROM_UNIXTIME(%s), end = FROM_UNIXTIME(%s), ' \
                     'pokemon_id = %s, ' \
                     'last_scanned = FROM_UNIXTIME(%s), cp = %s, move_1 = %s, move_2 = %s '
            data = (lvl, captureTime, start, end, pkm, int(time.time()), '999', '1', '1')

            wh_send = True
            wh_start = start
            wh_end = end

        query = updateStr + setStr + whereStr
        log.debug(query % data)
        cursor.execute(query, data)
        affectedRows = cursor.rowcount
        connection.commit()
        cursor.close()
        if affectedRows == 0 and not eggHatched:
            # we need to insert the raid...
            log.info("Got to insert")
            if MonWithNoEgg:
                # submit mon without egg info -> we have an endtime
                log.info("Inserting mon without egg")
                start = end - (int(args.raid_time) * 60)
                query = (
                    'INSERT INTO raid (gym_id, level, spawn, start, end, pokemon_id, last_scanned, cp, move_1, move_2) '
                    'VALUES (%s, %s, FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), %s, FROM_UNIXTIME(%s), 999, 1, 1)')
                data = (gym, lvl, captureTime, start, end, pkm, int(time.time()))
            elif end is None or start is None:
                log.info("Inserting without end or start")
                # no end or start time given, just inserting won't help much...
                log.warning("Useless to insert without endtime...")
                return False
            else:
                # we have start and end, mon is either with egg or we're submitting an egg
                log.info("Inserting everything")
                query = (
                    'INSERT INTO raid (gym_id, level, spawn, start, end, pokemon_id, last_scanned, cp, move_1, move_2) '
                    'VALUES (%s, %s, FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), %s, FROM_UNIXTIME(%s), 999, 1, 1)')
                data = (gym, lvl, captureTime, start, end, pkm, int(time.time()))

            cursorIns = connection.cursor()
            log.debug(query % data)
            cursorIns.execute(query, data)
            connection.commit()
            cursorIns.close()

            wh_send = True
            if MonWithNoEgg:
                wh_start = int(end) - 2700
            else:
                wh_start = start
            wh_end = end
            if pkm is None:
                pkm = 0

        connection.close()
        log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) + ') ] ' + 'submitRaid: Submit finished')
        self.refreshTimes(gym, raidNo, captureTime)

        if args.webhook and wh_send:
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) + ') ] ' + 'submitRaid: Send webhook')
            if wh_start:
                wh_start += (self.timezone * 60 * 60)
            if wh_end:
                wh_end += (self.timezone * 60 * 60)
            send_raid_webhook(gym, 'RAID', wh_start, wh_end, lvl, pkm)

        return True
Exemple #3
0
    def auto_hatch_eggs(self):
        try:
            connection = mysql.connector.connect(host=self.host,
                                                 user=self.user, port=self.port, passwd=self.password,
                                                 db=self.database)
        except:
            log.error("Could not connect to the SQL database")
            return False

        mon_id = args.auto_hatch_number

        if mon_id == 0:
            log.warn('You have enabled auto hatch but not the mon_id '
                     'so it will mark them as zero so they will remain unhatched...')

        cursor = connection.cursor()
        db_time_to_check = datetime.datetime.now() - datetime.timedelta(hours=self.timezone)
        log.debug("Time used to find eggs " + str(db_time_to_check))

        query_for_count = "SELECT gym_id, start, end from raid " \
                          "WHERE start <= FROM_UNIXTIME({0}) " \
                          "AND end >= FROM_UNIXTIME({0}) " \
                          "AND level = 5 " \
                          "AND IFNULL(pokemon_id,0) = 0" \
            .format(self.dbTimeStringToUnixTimestamp(str(db_time_to_check)))


        log.debug(query_for_count)
        cursor.execute(query_for_count)
        result = cursor.fetchall()
        rows_that_need_hatch_count = cursor.rowcount
        log.debug("Rows that need updating: {0}".format(rows_that_need_hatch_count))

        if rows_that_need_hatch_count > 0:
            counter = 0
            for row in result:
                log.debug(row)
                query = "UPDATE raid SET pokemon_id = {0} WHERE gym_id = \'{1}\'".format(mon_id, row[0])
                log.debug(query)
                cursor.execute(query)
                affected_rows = cursor.rowcount
                connection.commit()
                if affected_rows == 1:
                    counter = counter + 1
                    log.debug('Sending auto hatched raid for raid id {0}'.format(row[0]))
                    send_raid_webhook(row[0],
                                 'MON',
                                      self.dbTimeStringToUnixTimestamp(row[1]),
                                      self.dbTimeStringToUnixTimestamp(row[2]),
                                      5,
                                      mon_id)
                elif affected_rows > 1:
                    log.error('Something is wrong with the indexing on your table you raids on this id {0}'
                              .format(row[0]))
                else:
                    log.error('The row we wanted to update did not get updated that had id {0}'
                              .format(row[0]))

            if counter == rows_that_need_hatch_count:
                log.info("{0} gym(s) were updated as part of the regular level 5 egg hatching checks"
                         .format(counter))
            else:
                log.warn('There was an issue and the number expected the hatch did not match the successful updates. '
                         'Expected {0} Actual {1}'.format(rows_that_need_hatch_count, counter))

            cursor.close()
        else:
            log.info('No Eggs due for hatching')
Exemple #4
0
    def submitRaid(self,
                   gym,
                   pkm,
                   lvl,
                   start,
                   end,
                   type,
                   raidNo,
                   capture_time,
                   MonWithNoEgg=False):
        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                  ') ] ' + 'submitRaid: Submitting raid')

        try:
            connection = mysql.connector.connect(host=self.host,
                                                 user=self.user,
                                                 port=self.port,
                                                 passwd=self.password,
                                                 db=self.database)
        except:
            log.error("Could not connect to the SQL database")
            return False

        wh_send = False
        eggHatched = False
        cursor = connection.cursor()
        log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                  ') ] ' +
                  'submitRaid: Submitting something of type %s' % type)

        log.info(
            '[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) + ') ] ' +
            "Submitting. Gym: %s, Lv: %s, Start and Spawn: %s, End: %s, Mon: %s"
            % (gym, lvl, start, end, pkm))

        # always insert timestamp to time_spawn to have rows change if raid has been reported before
        updateStr = 'UPDATE raids '
        whereStr = 'WHERE fort_id = %s AND time_end >= %s' % (
            str(gym), str(int(time.time())))
        if MonWithNoEgg:
            # submit mon without egg info -> we have an endtime
            start = end - (int(args.raid_time) * 60)
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                     ') ] ' + "Updating mon without egg")

            setStr = 'SET level = %s, time_spawn = %s, time_battle = %s, time_end = %s, ' \
                     'pokemon_id = %s, last_updated = %s '
            data = (lvl, int(float(capture_time)), start, end, pkm,
                    int(time.time()))

        elif end is None or start is None:
            # no end or start time given, just update the other stuff
            # TODO: consider skipping UPDATING/INSERTING
            # TODO: this will be an update of a hatched egg to a boss!
            log.info(
                '[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                ') ] ' +
                "Updating without endtime or starttime - we should've seen an egg before "
                "then")
            setStr = 'SET level = %s, pokemon_id = %s, last_updated = %s '
            data = (lvl, pkm, int(time.time()))

            foundEndTime, EndTime = self.getRaidEndtime(gym, raidNo)

            if foundEndTime:
                wh_send = True
                wh_start = int(EndTime) - 2700
                wh_end = EndTime
                eggHatched = True
            else:
                wh_send = False

            # TODO: check for start/end
        else:
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                     ') ] ' + "Updating everything")
            # we have start and end, mon is either with egg or we're submitting an egg
            setStr = 'SET level = %s, time_spawn = %s, time_battle = %s, time_end = %s, pokemon_id = %s, ' \
                     'last_updated = %s '
            data = (lvl, int(float(capture_time)), start, end, pkm,
                    int(time.time()))

        query = updateStr + setStr + whereStr
        log.debug(query % data)
        cursor.execute(query, data)
        affectedRows = cursor.rowcount
        connection.commit()
        cursor.close()
        if affectedRows == 0 and not eggHatched:
            # we need to insert the raid...
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                     ') ] ' + "Gotta insert")
            if MonWithNoEgg:
                # submit mon without egg info -> we have an endtime
                log.info('[Crop: ' + str(raidNo) + ' (' +
                         str(self.uniqueHash) + ') ] ' +
                         "Inserting mon without egg")
                start = end - 45 * 60
                query = (
                    'INSERT INTO raids (fort_id, level, time_spawn, time_battle, time_end, pokemon_id) '
                    'VALUES (%s, %s, %s, %s, %s, %s)')
                data = (gym, lvl, int(float(capture_time)), start, end, pkm)
            elif end is None or start is None:
                log.info('[Crop: ' + str(raidNo) + ' (' +
                         str(self.uniqueHash) + ') ] ' +
                         "Inserting without end or start")
                # no end or start time given, just inserting won't help much...
                log.warning("Useless to insert without endtime...")
                return False
            else:
                # we have start and end, mon is either with egg or we're submitting an egg
                log.info('[Crop: ' + str(raidNo) + ' (' +
                         str(self.uniqueHash) + ') ] ' +
                         "Inserting everything")
                query = (
                    'INSERT INTO raids (fort_id, level, time_spawn, time_battle, time_end, pokemon_id) '
                    'VALUES (%s, %s, %s, %s, %s, %s)')
                data = (gym, lvl, int(float(capture_time)), start, end, pkm)

            cursorIns = connection.cursor()
            log.debug('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                      ') ] ' + query % data)
            cursorIns.execute(query, data)
            connection.commit()
            cursorIns.close()

            wh_send = True
            if MonWithNoEgg:
                wh_start = int(end) - 2700
            else:
                wh_start = start
            wh_end = end
            if pkm is None:
                pkm = 0

        connection.close()
        log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                 ') ] ' + 'submitRaid: Submit finished')
        self.refreshTimes(gym, raidNo, capture_time)

        if args.webhook and wh_send:
            log.info('[Crop: ' + str(raidNo) + ' (' + str(self.uniqueHash) +
                     ') ] ' + 'submitRaid: Send webhook')
            send_raid_webhook(gym, 'RAID', wh_start, wh_end, lvl, pkm)

        return True