<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import math
import time
from geopy.distance import great_circle

from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.cell_workers.utils import coord2merc, merc2coord
from pokemongo_bot.constants import Constants
from pokemongo_bot.walkers.polyline_walker import PolylineWalker
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.item_list import Item
from pokemongo_bot import inventory

LOG_TIME_INTERVAL = 60
NO_LURED_TIME_MALUS = 5
NO_BALLS_MOVING_TIME = 5 * 60


class CampFort(BaseTask):
    SUPPORTED_TASK_API_VERSION = 1

    def __init__(self, bot, config):
        super(CampFort, self).__init__(bot, config)

    def initialize(self):
        self.destination = None
        self.stay_until = 0
        self.move_until = 0
        self.walker = None
        self.no_log_until = 0

        self.config_max_distance = self.config.get("max_distance", 2000)
        self.config_min_forts_count = self.config.get("min_forts_count", 2)
        self.config_min_lured_forts_count = self.config.get("min_lured_forts_count", 1)
        self.config_camping_time = self.config.get("camping_time", 1800)
        self.config_moving_time = self.config.get("moving_time", 600)

    def work(self):
        if not self.enabled:
            return WorkerResult.SUCCESS

        now = time.time()

        if now < self.move_until:
            return WorkerResult.SUCCESS

        if 0 < self.stay_until < now:
            self.destination = None
            self.stay_until = 0
            self.move_until = now + self.config_moving_time

            if self.config_moving_time > 0:
                return WorkerResult.SUCCESS

        # Let's make sure we have balls before we sit at a lure!
        # See also catch_pokemon.py
        if self.get_pokeball_count() <= 0:
            self.logger.info("No pokeballs left, refuse to sit at lure!")
            # Move away from lures for a time
            self.destination = None
            self.stay_until = 0
            self.move_until = now + max(self.config_moving_time, NO_BALLS_MOVING_TIME)

            return WorkerResult.SUCCESS

        if self.destination is None:
            forts = self.get_forts()
            forts_clusters = self.get_forts_clusters(forts)

            if len(forts_clusters) > 0:
                self.destination = forts_clusters[0]
                self.walker = PolylineWalker(self.bot, self.destination[0], self.destination[1])
                self.logger.info("New destination at %s meters: %s forts, %s lured", round(self.destination[4], 2), self.destination[3], self.destination[2])
                self.no_log_until = now + LOG_TIME_INTERVAL
            else:
                return WorkerResult.SUCCESS

        if self.stay_until >= now:
            cluster = self.get_current_cluster()

            if self.no_log_until < now:
                self.logger.info("Staying at destination: %s forts, %s lured", cluster[3], cluster[2])
                self.no_log_until = now + LOG_TIME_INTERVAL

            if cluster[2] == 0:
                self.stay_until -= NO_LURED_TIME_MALUS

            self.walker.step(speed=0)
        elif self.walker.step():
            cluster = self.get_current_cluster()
            self.logger.info("Arrived at destination: %s forts, %s lured", cluster[3], cluster[2])
            self.stay_until = now + self.config_camping_time
        elif self.no_log_until < now:
            cluster = self.get_current_cluster()
            self.logger.info("Moving to destination at %s meters: %s forts, %s lured", round(cluster[4], 2), cluster[3], cluster[2])
            self.no_log_until = now + LOG_TIME_INTERVAL

        return WorkerResult.RUNNING

    def get_pokeball_count(self):
        return sum([inventory.items().get(ball.value).count for ball in [Item.ITEM_POKE_BALL, Item.ITEM_GREAT_BALL, Item.ITEM_ULTRA_BALL]])

    def get_forts(self):
        radius = self.config_max_distance + Constants.MAX_DISTANCE_FORT_IS_REACHABLE

        forts = [f for f in self.bot.cell["forts"] if ("latitude" in f) and ("type" in f)]
        forts = [f for f in forts if self.get_distance(self.bot.start_position, f) <= radius]

        return forts
<commit_msg>Stop moving to lures if no lures active anymore (#5395)<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import math
import time
from geopy.distance import great_circle

from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.cell_workers.utils import coord2merc, merc2coord
from pokemongo_bot.constants import Constants
from pokemongo_bot.walkers.polyline_walker import PolylineWalker
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.item_list import Item
from pokemongo_bot import inventory

LOG_TIME_INTERVAL = 60
NO_LURED_TIME_MALUS = 5
NO_BALLS_MOVING_TIME = 5 * 60


class CampFort(BaseTask):
    SUPPORTED_TASK_API_VERSION = 1

    def __init__(self, bot, config):
        super(CampFort, self).__init__(bot, config)

    def initialize(self):
        self.destination = None
        self.stay_until = 0
        self.move_until = 0
        self.walker = None
        self.no_log_until = 0

        self.config_max_distance = self.config.get("max_distance", 2000)
        self.config_min_forts_count = self.config.get("min_forts_count", 2)
        self.config_min_lured_forts_count = self.config.get("min_lured_forts_count", 1)
        self.config_camping_time = self.config.get("camping_time", 1800)
        self.config_moving_time = self.config.get("moving_time", 600)

    def work(self):
        if not self.enabled:
            return WorkerResult.SUCCESS

        now = time.time()

        if now < self.move_until:
            return WorkerResult.SUCCESS

        if 0 < self.stay_until < now:
            self.destination = None
            self.stay_until = 0
            self.move_until = now + self.config_moving_time

            if self.config_moving_time > 0:
                return WorkerResult.SUCCESS

        # Let's make sure we have balls before we sit at a lure!
        # See also catch_pokemon.py
        if self.get_pokeball_count() <= 0:
            self.logger.info("No pokeballs left, refuse to sit at lure!")
            # Move away from lures for a time
            self.destination = None
            self.stay_until = 0
            self.move_until = now + max(self.config_moving_time, NO_BALLS_MOVING_TIME)

            return WorkerResult.SUCCESS

        if self.destination is None:
            forts = self.get_forts()
            forts_clusters = self.get_forts_clusters(forts)

            if len(forts_clusters) > 0:
                self.destination = forts_clusters[0]
                self.walker = PolylineWalker(self.bot, self.destination[0], self.destination[1])
                self.logger.info("New destination at %s meters: %s forts, %s lured", round(self.destination[4], 2), self.destination[3], self.destination[2])
                self.no_log_until = now + LOG_TIME_INTERVAL
            else:
                return WorkerResult.SUCCESS

        if self.stay_until >= now:
            cluster = self.get_current_cluster()

            if self.no_log_until < now:
                self.logger.info("Staying at destination: %s forts, %s lured", cluster[3], cluster[2])
                self.no_log_until = now + LOG_TIME_INTERVAL

            if cluster[2] == 0:
                self.stay_until -= NO_LURED_TIME_MALUS

            self.walker.step(speed=0)
        elif self.walker.step():
            cluster = self.get_current_cluster()
            self.logger.info("Arrived at destination: %s forts, %s lured", cluster[3], cluster[2])
            self.stay_until = now + self.config_camping_time
        elif self.no_log_until < now:
            cluster = self.get_current_cluster()

            if cluster[2] == 0:
                self.logger.info("Lures gone! Resetting destination!")
                self.destination = None
                self.stay_until = 0
                return WorkerResult.SUCCESS

            self.logger.info("Moving to destination at %s meters: %s forts, %s lured", round(cluster[4], 2), cluster[3], cluster[2])
            self.no_log_until = now + LOG_TIME_INTERVAL

        return WorkerResult.RUNNING

    def get_pokeball_count(self):
        return sum([inventory.items().get(ball.value).count for ball in [Item.ITEM_POKE_BALL, Item.ITEM_GREAT_BALL, Item.ITEM_ULTRA_BALL]])

    def get_forts(self):
        radius = self.config_max_distance + Constants.MAX_DISTANCE_FORT_IS_REACHABLE

        forts = [f for f in self.bot.cell["forts"] if ("latitude" in f) and ("type" in f)]
        forts = [f for f in forts if self.get_distance(self.bot.start_position, f) <= radius]

        return forts

{'fraction_non_alphanumeric': 0.09016672337529771, 'fraction_numerical': 0.012135647045480322, 'mean_word_length': 2.506163021868787, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '397706', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2662, 'n_words': 632}