コード例 #1
0
def save_residents(region_id, residents):
    """Save residents to database"""
    session = SESSION()
    player_ids = []
    new_residents = 0
    for player_dict in residents:
        player = session.query(Player).get(player_dict['id'])
        if player is None:
            player = save_player(session, player_dict)
        player_ids.append(player.id)
        last_residency = player.residencies \
            .filter(PlayerResidency.region_id == region_id) \
            .filter(PlayerResidency.until_date_time == None) \
            .first()
        if not last_residency:
            new_residents += 1
            player_location = PlayerResidency()
            player_location.player_id = player.id
            player_location.region_id = region_id
            player_location.from_date_time = datetime.now().replace(second=0,
                                                                    minute=0)
            session.add(player_location)
    LOGGER.info('regio %6s: "%s" new residents', region_id, new_residents)
    session.commit()
    current_residents = session.query(PlayerResidency) \
        .filter(PlayerResidency.region_id == region_id) \
        .filter(PlayerResidency.until_date_time == None).all()
    for current_resident in current_residents:
        if current_resident.player_id not in player_ids:
            current_resident.until_date_time = datetime.now().replace(second=0,
                                                                      minute=0)
    session.commit()
    session.close()
コード例 #2
0
def save_work_permits(state_id, work_permits):
    """Save residents to database"""
    session = SESSION()
    player_ids = []
    new_work_permits = 0
    for player_dict in work_permits:
        player = session.query(Player).get(player_dict['id'])
        if player is None:
            player = save_player(session, player_dict)
        player_ids.append(player.id)
        last_work_permit = player.state_work_permits \
            .filter(StateWorkPermit.state_id == state_id) \
            .filter(StateWorkPermit.until_date_time == None) \
            .first()
        if not last_work_permit:
            new_work_permits += 1
            state_work_permit = StateWorkPermit()
            state_work_permit.player_id = player.id
            state_work_permit.state_id = state_id
            state_work_permit.from_date_time = player_dict['from']
            session.add(state_work_permit)
    session.commit()
    LOGGER.info('state %6s: "%s" new work permits', state_id, new_work_permits)
    current_work_permits = session.query(StateWorkPermit) \
        .filter(StateWorkPermit.state_id == state_id) \
        .filter(StateWorkPermit.until_date_time == None).all()
    for current_work_permit in current_work_permits:
        if current_work_permit.player_id not in player_ids:
            current_work_permit.until_date_time = datetime.now().replace(
                second=0, minute=0)
    session.commit()
    session.close()
コード例 #3
0
def save_regions(state_id, regions):
    """Save residents to database"""
    session = SESSION()
    region_ids = []
    state = session.query(State).get(state_id)
    if state is None:
        state = save_state(session, state_id)
    for region_dict in regions:
        region = session.query(Region).get(region_dict['id'])
        if region is None:
            region = save_region(session, region_dict)
        region_ids.append(region.id)
        state_region = session.query(StateRegion) \
            .filter(StateRegion.state_id == state.id) \
            .filter(StateRegion.region_id == region.id) \
            .filter(StateRegion.until_date_time == None) \
            .first()
        if not state_region:
            state_region = StateRegion()
            state_region.state_id = state.id
            state_region.region_id = region.id
            state_region.from_date_time = datetime.now().replace(second=0, minute=0)
            session.add(state_region)
            session.commit()

    saved_state_regions = session.query(StateRegion) \
        .filter(StateRegion.state_id == state.id) \
        .filter(StateRegion.until_date_time == None) \
        .all()
    for saved_state_region in saved_state_regions:
        if saved_state_region.region_id not in region_ids:
            saved_state_region.until_date_time = datetime.now().replace(second=0, minute=0)
    session.commit()
    session.close()
コード例 #4
0
def save_resources(state_id, regions, resource_id):
    """Save resources to database"""
    session = SESSION()
    resource_track = ResourceTrack()
    resource_track.state_id = state_id
    resource_track.resource_type = resource_id
    resource_track.date_time = datetime.now()
    session.add(resource_track)
    session.commit()

    for region_id, region_dict in regions.items():
        region = session.query(Region).get(region_id)
        if not region:
            region = save_region(session, region_id, region_dict)

        resource_stat = ResourceStat()
        resource_stat.resource_track_id = resource_track.id
        resource_stat.region_id = region.id
        resource_stat.explored = region_dict['explored']
        resource_stat.deep_exploration = region_dict['deep_exploration']
        resource_stat.limit_left = region_dict['limit_left']
        session.add(resource_stat)

    session.commit()
    session.close()
コード例 #5
0
def add_telegram_account(update):
    """Add new Telegram account"""
    session = SESSION()
    telegram_account = TelegramAccount()
    telegram_account.id = update.message.from_user.id
    telegram_account.name = update.message.from_user.name
    telegram_account.registration_date = datetime.now()
    session.add(telegram_account)
    session.commit()
    session.close()
    return telegram_account
コード例 #6
0
def save_resource_market(player_market, state_market):
    """Save factories to database"""
    session = SESSION()

    market_track = get_new_market_track(session, True, True, False)
    market_track.player_resources = True
    market_track.state_resources = True
    _save_player_market(session, market_track, player_market)
    _save_state_market(session, market_track, state_market)

    session.commit()
    session.close()
コード例 #7
0
def remove_verified_player(telegram_account_id, player_id):
    """Remove Telegram player"""
    session = SESSION()
    player_telegram = session.query(PlayerTelegram) \
        .filter(PlayerTelegram.telegram_id == telegram_account_id) \
        .filter(PlayerTelegram.player_id == player_id) \
        .filter(PlayerTelegram.until_date_time == None) \
        .first()
    if player_telegram:
        player_telegram.until_date_time = datetime.now()
        session.commit()
        return True
    return False
コード例 #8
0
def save_professors(state_id, department_type, professors):
    """Save professors to database"""
    session = SESSION()
    department = get_department(session, state_id, department_type)

    for professor in professors:
        player = get_player(session, professor['id'], professor['name'])
        department_stat = DepartmentStat()
        department_stat.department_id = department.id
        department_stat.date_time = professor['date_time']
        department_stat.points = professor['points']
        department_stat.player_id = player.id
        session.add(department_stat)
    session.commit()
    session.close()
コード例 #9
0
def save_factories(state_id, factories):
    """Save factories to database"""
    session = SESSION()
    session.close()

    factory_track = FactoryTrack()
    factory_track.state_id = state_id
    factory_track.date_time = datetime.now()
    session.add(factory_track)

    for factory_dict in factories:
        factory = session.query(Factory).get(factory_dict['id'])
        if factory is None:
            factory = save_factory(session, factory_dict)
        factory_stat = FactoryStat()
        factory_stat.level = factory_dict['level']
        factory_stat.experience = factory_dict['experience']
        factory_stat.wage = factory_dict['wage']
        factory_stat.workers = factory_dict['workers']
        factory_stat.factory_id = factory.id
        factory_stat.factory_track_id = factory_track.id
        session.add(factory_stat)

        current_location = session.query(FactoryLocation) \
            .filter(FactoryLocation.factory_id == factory.id) \
            .filter(FactoryLocation.until_date_time == None).first()

        if not current_location or current_location.region_id != factory_dict[
                'region_id']:
            region = session.query(Region) \
                .filter(Region.id == factory_dict['region_id']).first()
            factory_location = FactoryLocation()
            factory_location.factory_id = factory.id
            factory_location.region_id = region.id
            factory_location.from_date_time = datetime.now()
            session.add(factory_location)
            if current_location:
                current_location.until_date_time = datetime.now()

    session.commit()
    session.close()
コード例 #10
0
ファイル: database.py プロジェクト: joostsijm/account_manager
def save_deep_explorations(region_id, deep_explorations):
    """Save resources to database"""
    session = SESSION()
    for deep_exploration_id, deep_exploration_dict in deep_explorations.items(
    ):
        deep_exploration = session.query(DeepExploration).get(
            deep_exploration_id)
        if deep_exploration:
            break
        deep_exploration = DeepExploration()
        deep_exploration.id = deep_exploration_id
        region = session.query(Region).get(region_id)
        if not region:
            region = save_region(session, region_id)
        deep_exploration.region_id = region_id
        deep_exploration.resource_type = deep_exploration_dict['resource_type']
        deep_exploration.until_date_time = deep_exploration_dict[
            'until_date_time']
        session.add(deep_exploration)
    session.commit()
    session.close()
コード例 #11
0
def verify_rr_player(telegram_id, player_id):
    """Verify RR player in database"""
    session = SESSION()
    telegram_account = _get_telegram_account(session, telegram_id)
    players = _get_rr_players(session, telegram_id)
    for player in players:
        if player.id == player_id:
            LOGGER.info(
                '"%s" player already connected "%s"',
                telegram_id,
                player_id
            )
            session.close()
            return

    active_player_telegrams = session.query(PlayerTelegram) \
        .filter(PlayerTelegram.until_date_time == None) \
        .filter(PlayerTelegram.player_id == player_id) \
        .all()
    for active_player_telegram in active_player_telegrams:
        LOGGER.info(
            '"%s" unconnect player "%s"',
            active_player_telegram.telegram_id,
            player_id
        )
        active_player_telegram.until_date_time = datetime.now()

    LOGGER.info(
        '"%s" connecting player "%s"',
        telegram_id,
        player_id
    )
    player_telegram = PlayerTelegram()
    player_telegram.telegram_id = telegram_account.id
    player_telegram.player_id = player_id
    player_telegram.from_date_time = datetime.now()
    session.add(player_telegram)
    session.commit()
    session.close()
コード例 #12
0
def save_citizens(region_id, citizens):
    """Save citizens to database"""
    session = SESSION()
    player_ids = []
    new_citizens = 0
    for player_dict in citizens:
        player = session.query(Player).get(player_dict['id'])
        if player is None:
            player = save_player(session, player_dict)
        player_ids.append(player.id)
        last_location = player.locations \
            .filter(PlayerLocation.region_id == region_id) \
            .filter(PlayerLocation.until_date_time == None) \
            .first()
        if not last_location:
            new_citizens += 1
            player_location = PlayerLocation()
            player_location.player_id = player.id
            player_location.region_id = region_id
            player_location.from_date_time = datetime.now().replace(second=0,
                                                                    minute=0)
            session.add(player_location)
    LOGGER.info('regio %6s: "%s" new citizens', region_id, new_citizens)
    session.commit()
    current_citizens = session.query(PlayerLocation) \
        .filter(PlayerLocation.region_id == region_id) \
        .filter(PlayerLocation.until_date_time == None).all()
    left_citizens = 0
    for current_citizen in current_citizens:
        if current_citizen.player_id not in player_ids:
            left_citizens += 1
            current_citizen.until_date_time = datetime.now().replace(second=0,
                                                                     minute=0)
    LOGGER.info('regio %6s: "%s" citizens left', region_id, left_citizens)
    session.commit()
    session.close()