<commit_before>from app.airport.airport_builder import build_airport
from app.common.http_methods import get_request
from app.common.target_urls import STAFF_PAGE, AIRPORT_PAGE, MISSION_DASHBOARD, PLANES_PAGE
from app.missions.missionparser import get_ongoing_missions, subtract
from app.parsers.planesparser import build_planes_from_html
from app.planes.plane_garage import PlaneGarage
from app.planes.planes_util2 import split_planes_list_by_type
from fm.databases.database import sort_missions_by_type, db_get_ordered_missions_multi_type
from fm.mission_handler import accept_all_missions


class BotPlayer(object):
    def __init__(self):
        self.airport = self.build_airport()
        self.planes = self.build_planes()
        self.ongoing_missions = self.get_ongoing_missions()
        # TODO move sessions retrieval to the upper class
        self.missions = self.get_missions()

    def build_airport(self):
        staff_page = get_request(STAFF_PAGE)
        airport_page = get_request(AIRPORT_PAGE)
        return build_airport(airport_page, staff_page)

    def build_planes(self):
        html_page = get_request(PLANES_PAGE)
        planes_list = build_planes_from_html(html_page)
        sorted_planes = split_planes_list_by_type(planes_list)
        return sorted_planes

    def get_ongoing_missions(self):
        dashboard = get_request(MISSION_DASHBOARD)
        return get_ongoing_missions(dashboard)

    def get_missions(self):
        return db_get_ordered_missions_multi_type(200, '-reputation_per_hour')

    @property
    def usable_planes(self):
        return self.planes['commercial_ready_planes'] + self.planes['supersonic_ready_planes'] + self.planes['jet_ready_planes']

    def launch_missions(self):
        mission_list = self.missions
        ongoing_missions = self.ongoing_missions
        mission_list = subtract(mission_list, ongoing_missions)
        garage = PlaneGarage(self.usable_planes, self.airport)
        garage.prepare_all_planes()
        accept_all_missions(mission_list, garage.ready_planes)



###################################################



    def are_missions_expired(missions):
        # TODO improve environment handling
        if fm.singleton_session.local_mode:
            return False
        expiry_date = missions[0].expiry_date
        today = datetime.datetime.now()
        return (expiry_date - today) <= datetime.timedelta(0)



def send_planes():
    list_missions = db_get_ordered_missions('Suisse', CONCORDE_SPEED, CONCORDE_CAPACITY, MAX_PLANES_NB,
                                            '-reputation_per_hour')

    other_airports = get_other_airports_id()
    other_airports = filter_airports(other_airports)

    if len(list_missions) < 84 or are_missions_expired(list_missions):
        logger.error('Refresh missions')
        update_missions()
        list_missions = db_get_ordered_missions('Suisse', CONCORDE_SPEED, CONCORDE_CAPACITY, MAX_PLANES_NB,
                                                '-reputation_per_hour')

    # switch on all airports
    for j in other_airports:
        switch_to_airport(j)
<commit_msg>stable version able to send planes in missions<commit_after>from app.airport.airport_builder import build_airport
from app.common.http_methods import get_request
from app.common.target_urls import STAFF_PAGE, AIRPORT_PAGE, MISSION_DASHBOARD, PLANES_PAGE
from app.missions.missionparser import get_ongoing_missions, subtract
from app.parsers.planesparser import build_planes_from_html
from app.planes.plane_garage import PlaneGarage
from app.planes.planes_util2 import split_planes_list_by_type
from fm.databases.database import sort_missions_by_type, db_get_ordered_missions_multi_type
from fm.mission_handler import accept_all_missions


class BotPlayer(object):
    def __init__(self):
        self.airport = self.build_airport()
        self.planes = self.build_planes()
        self.ongoing_missions = self.get_ongoing_missions()
        # TODO move sessions retrieval to the upper class
        self.missions = self.get_missions()

    def build_airport(self):
        staff_page = get_request(STAFF_PAGE)
        airport_page = get_request(AIRPORT_PAGE)
        return build_airport(airport_page, staff_page)

    def build_planes(self):
        html_page = get_request(PLANES_PAGE)
        planes_list = build_planes_from_html(html_page)
        sorted_planes = split_planes_list_by_type(planes_list)
        return sorted_planes

    def get_ongoing_missions(self):
        dashboard = get_request(MISSION_DASHBOARD)
        return get_ongoing_missions(dashboard)

    def get_missions(self):
        db_missions = db_get_ordered_missions_multi_type(210, '-reputation_per_hour')
        # if are_expired(db_missions)

    @property
    def usable_planes(self):
        return self.planes['commercial_ready_planes'] + self.planes['supersonic_ready_planes'] + self.planes['jet_ready_planes']

    def launch_missions(self):
        mission_list = self.missions

        # other_airports = get_other_airports_id()
        # other_airports = filter_airports(other_airports)

        ongoing_missions = self.ongoing_missions
        mission_list = subtract(mission_list, ongoing_missions)
        garage = PlaneGarage(self.usable_planes, self.airport)
        garage.prepare_all_planes()
        accept_all_missions(mission_list, garage.ready_planes)



###################################################







def send_planes():




    if len(list_missions) < 84 or are_missions_expired(list_missions):
        logger.error('Refresh missions')
        update_missions()
        list_missions = db_get_ordered_missions('Suisse', CONCORDE_SPEED, CONCORDE_CAPACITY, MAX_PLANES_NB,
                                                '-reputation_per_hour')

    # switch on all airports
    for j in other_airports:
        switch_to_airport(j)

{'fraction_non_alphanumeric': 0.0845262440354465, 'fraction_numerical': 0.0023858214042263124, 'mean_word_length': 3.744543249797898, 'pattern_counts': {'":': 0, '<': 6, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5771008', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1712, 'n_words': 317}