<commit_before># -*- coding: utf-8 -*-

import os
import datetime
import json
import logging
import random
import re
import sys
import time

from geopy.geocoders import GoogleV3
from pgoapi import PGoApi
from pgoapi.utilities import f2i

import logger
from cell_workers import SpinNearestFortWorker, CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker, IncubateEggsWorker
from cell_workers.utils import distance, get_cellid, encode, i2f
from human_behaviour import sleep
from item_list import Item
from metrics import Metrics
from pokemongo_bot.event_handlers import LoggingHandler
from pokemongo_bot.event_handlers import SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from spiral_navigator import SpiralNavigator
from worker_result import WorkerResult
from event_manager import EventManager
from api_wrapper import ApiWrapper


class PokemonGoBot(object):

    @property
    def position(self):
        return self.api._position_lat, self.api._position_lng, 0

    def __init__(self, config):
        self.config = config
        self.fort_timeouts = dict()
        self.pokemon_list = json.load(open(os.path.join('data', 'pokemon.json')))
        self.item_list = json.load(open(os.path.join('data', 'items.json')))
        self.metrics = Metrics(self)
        self.latest_inventory = None
        self.cell = None
        self.recent_forts = [None] * config.max_circle_size
        self.tick_count = 0


    def start(self):
        self._setup_logging()
        self._setup_api()
        self.navigator = SpiralNavigator(self)
        random.seed()

    def _setup_event_system(self):
        handlers = [LoggingHandler()]
        if self.config.websocket_server:
            websocket_handler = SocketIoHandler(self.config.websocket_server)
            handlers.append(websocket_handler)

            self.sio_runner = SocketIoRunner(self.config.websocket_server)
            self.sio_runner.start_listening_async()

        self.event_manager = EventManager(*handlers)

        # Registering event:
        # self.event_manager.register_event("location", parameters=['lat', 'lng'])
        #
        # Emitting event should be enough to add logging and send websocket
        # message: :
        # self.event_manager.emit('location', 'level'='info', data={'lat': 1, 'lng':1}),

    def tick(self):
        self.cell = self.get_meta_cell()

        # Check if session token has expired
        self.check_session(self.position[0:2])

        workers = [
            IncubateEggsWorker,
            PokemonTransferWorker,
            EvolveAllWorker,
            RecycleItemsWorker,
            CatchVisiblePokemonWorker,
            SpinNearestFortWorker
        ]

        for worker in workers:
            if worker(self).work() == WorkerResult.RUNNING:
                return

        self.navigator.take_step()

        self.tick_count +=1

    def get_meta_cell(self):
        location = self.position[0:2]
        cells = self.find_close_cells(*location)

        # Combine all cells into a single dict of the items we care about.<commit_msg>Allow worker order to be more easily customised in future with PokemonGoBot (#1600)<commit_after># -*- coding: utf-8 -*-

import os
import datetime
import json
import logging
import random
import re
import sys
import time

from geopy.geocoders import GoogleV3
from pgoapi import PGoApi
from pgoapi.utilities import f2i

import logger
import cell_workers
from cell_workers.utils import distance, get_cellid, encode, i2f
from human_behaviour import sleep
from item_list import Item
from metrics import Metrics
from pokemongo_bot.event_handlers import LoggingHandler
from pokemongo_bot.event_handlers import SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from spiral_navigator import SpiralNavigator
from worker_result import WorkerResult
from event_manager import EventManager
from api_wrapper import ApiWrapper


class PokemonGoBot(object):

    WORKERS = [
        cell_workers.IncubateEggsWorker,
        cell_workers.PokemonTransferWorker,
        cell_workers.EvolveAllWorker,
        cell_workers.RecycleItemsWorker,
        cell_workers.CatchVisiblePokemonWorker,
        cell_workers.SpinNearestFortWorker
    ]

    @property
    def position(self):
        return self.api._position_lat, self.api._position_lng, 0

    def __init__(self, config):
        self.config = config
        self.fort_timeouts = dict()
        self.pokemon_list = json.load(open(os.path.join('data', 'pokemon.json')))
        self.item_list = json.load(open(os.path.join('data', 'items.json')))
        self.metrics = Metrics(self)
        self.latest_inventory = None
        self.cell = None
        self.recent_forts = [None] * config.max_circle_size
        self.tick_count = 0

        # Make our own copy of the workers for this instance
        self.workers = list(self.WORKERS)

    def start(self):
        self._setup_logging()
        self._setup_api()
        self.navigator = SpiralNavigator(self)
        random.seed()

    def _setup_event_system(self):
        handlers = [LoggingHandler()]
        if self.config.websocket_server:
            websocket_handler = SocketIoHandler(self.config.websocket_server)
            handlers.append(websocket_handler)

            self.sio_runner = SocketIoRunner(self.config.websocket_server)
            self.sio_runner.start_listening_async()

        self.event_manager = EventManager(*handlers)

        # Registering event:
        # self.event_manager.register_event("location", parameters=['lat', 'lng'])
        #
        # Emitting event should be enough to add logging and send websocket
        # message: :
        # self.event_manager.emit('location', 'level'='info', data={'lat': 1, 'lng':1}),

    def tick(self):
        self.cell = self.get_meta_cell()

        # Check if session token has expired
        self.check_session(self.position[0:2])

        for worker in self.workers:
            if worker(self).work() == WorkerResult.RUNNING:
                return

        self.navigator.take_step()

        self.tick_count +=1

    def get_meta_cell(self):
        location = self.position[0:2]
        cells = self.find_close_cells(*location)

        # Combine all cells into a single dict of the items we care about.
{'fraction_non_alphanumeric': 0.08169014084507042, 'fraction_numerical': 0.004694835680751174, 'mean_word_length': 3.0603557814485387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5868502', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1835, 'n_words': 445}