Exemplo n.º 1
0
async def get_user_infos_from_tournament_data_by_name(
        user_name: str, users_data: EntitiesData) -> List[EntityInfo]:
    user_name_lower = user_name.lower()
    result = {
        user_id: user_info
        for (user_id, user_info) in users_data.items()
        if user_name_lower in user_info.get(
            user.USER_DESCRIPTION_PROPERTY_NAME, '').lower()
    }
    user_infos_current = await __get_users_data(user_name)
    if user_infos_current:
        for user_info in user_infos_current.values():
            user_id = user_info[user.USER_KEY_NAME]
            if user_id in users_data:
                current_user_info = await __get_user_info_by_id(user_id) or {}
                current_user_name = current_user_info.get(
                    user.USER_DESCRIPTION_PROPERTY_NAME)
                if user_id not in result:
                    result[user_id] = users_data[user_id]
                if current_user_name and current_user_name != result[user_id][
                        user.USER_DESCRIPTION_PROPERTY_NAME]:
                    result[user_id]['CurrentName'] = current_user_name
    else:
        for tournament_user_id, tournament_user_info in result.items():
            user_info = await __get_user_info_by_id(tournament_user_id)
            if result[tournament_user_id][
                    user.USER_DESCRIPTION_PROPERTY_NAME] != user_info[
                        user.USER_DESCRIPTION_PROPERTY_NAME]:
                result[tournament_user_id]['CurrentName'] = user_info[
                    user.USER_DESCRIPTION_PROPERTY_NAME]
    return list(result.values())
Exemplo n.º 2
0
def __get_fleet_division_designs(
        divisions_designs_data: EntitiesData) -> EntitiesData:
    result = {
        key: value
        for key, value in divisions_designs_data.items()
        if value.get('DivisionType') == 'Fleet'
    }
    return result
Exemplo n.º 3
0
async def get_full_fleet_info_as_text(
    ctx: Context,
    fleet_info: EntityInfo,
    max_tourney_battle_attempts: int = None,
    past_fleets_data: EntitiesData = None,
    past_users_data: EntitiesData = None,
    past_retrieved_at: datetime = None,
    as_embed: bool = settings.USE_EMBEDS
) -> Tuple[Union[List[Embed], List[str]], List[str]]:
    """Returns a list of lines for the post, as well as the paths to the spreadsheet created"""
    fleet_id = fleet_info[FLEET_KEY_NAME]
    fleet_name = fleet_info[FLEET_DESCRIPTION_PROPERTY_NAME]
    is_past_data = past_fleets_data and past_users_data and past_retrieved_at

    if is_past_data:
        retrieved_at = past_retrieved_at
        if fleet_info.get('CurrentAllianceName') is None:
            current_fleet_info = await get_fleets_data_by_id(fleet_id)
            if current_fleet_info[
                    FLEET_DESCRIPTION_PROPERTY_NAME] != fleet_info[
                        FLEET_DESCRIPTION_PROPERTY_NAME]:
                fleet_info['CurrentAllianceName'] = current_fleet_info[
                    FLEET_DESCRIPTION_PROPERTY_NAME]
        fleet_users_data = {
            user_id: user_info
            for user_id, user_info in past_users_data.items()
            if user_info.get(FLEET_KEY_NAME) == fleet_id
        }
    else:
        retrieved_at = utils.get_utc_now()
        fleet_info = await get_fleets_data_by_id(fleet_id)
        fleet_users_data = await __get_fleet_users_data_by_fleet_id(fleet_id)

    post_content = await __get_fleet_details_by_info(
        ctx,
        fleet_info,
        fleet_users_data,
        max_tourney_battle_attempts=max_tourney_battle_attempts,
        retrieved_at=retrieved_at,
        is_past_data=is_past_data,
        as_embed=as_embed)
    fleet_sheet_file_name = excel.get_file_name(fleet_name,
                                                retrieved_at,
                                                excel.FILE_ENDING.XL,
                                                consider_tourney=False)
    file_path = __create_fleet_sheet_xl(
        fleet_users_data,
        retrieved_at,
        fleet_sheet_file_name,
        max_tourney_battle_attempts=max_tourney_battle_attempts)
    file_paths = []
    if file_path:
        file_paths.append(file_path)

    return post_content, file_paths
Exemplo n.º 4
0
def __filter_data_dict(data: EntitiesData, by_key: Any, by_value: Any, ignore_case: bool) -> Optional[EntitiesData]:
    """Parameter 'data':
       - A dict with entity ids as keys and entity info as values. """
    if data:
        result = {}
        for key, entry in data.items():
            entry_value = entry[by_key]
            value = by_value
            if ignore_case:
                entry_value = str(entry_value).lower()
                value = str(value).lower()
            if isinstance(by_value, list):
                if entry_value in value:
                    result[key] = entry
            elif entry_value == value:
                    result[key] = entry
        return result
    else:
        return data
Exemplo n.º 5
0
def __flatten_raw_dict_for_excel(raw_dict: EntitiesData) -> List[EntityInfo]:
    entity = {}
    result = []
    children = []
    for key, value in raw_dict.items():
        if isinstance(value, dict):
            children.extend(__flatten_raw_dict_for_excel(value))
        elif isinstance(value, list):
            for child in value:
                children.extend(__flatten_raw_dict_for_excel(child))
        else:
            entity[key] = excel.fix_field(value)
    if children:
        for child in children:
            result_entity = dict(entity)
            result_entity.update(child)
            result.append(result_entity)
    else:
        result = [entity]
    return result
Exemplo n.º 6
0
async def get_fleet_infos_from_tourney_data_by_name(
        fleet_name: str, fleet_data: EntitiesData) -> List[EntityInfo]:
    fleet_name_lower = fleet_name.lower()
    result = {
        fleet_id: fleet_info
        for (fleet_id, fleet_info) in fleet_data.items()
        if fleet_name_lower in fleet_info.get(
            fleet.FLEET_DESCRIPTION_PROPERTY_NAME, '').lower()
    }
    fleet_infos_current = await __get_fleets_data_by_name(fleet_name)
    for fleet_info in fleet_infos_current.values():
        fleet_id = fleet_info[fleet.FLEET_KEY_NAME]
        if fleet_id in fleet_data:
            if fleet_id not in result:
                result[fleet_id] = fleet_data[fleet_id]
            if result[fleet_id][
                    fleet.FLEET_DESCRIPTION_PROPERTY_NAME] != fleet_info[
                        fleet.FLEET_DESCRIPTION_PROPERTY_NAME]:
                result[fleet_id]['CurrentAllianceName'] = fleet_info[
                    fleet.FLEET_DESCRIPTION_PROPERTY_NAME]
    return list(result.values())
Exemplo n.º 7
0
def get_ids_from_property_value(data: EntitiesData, property_name: str, property_value: str, fix_data_delegate: Callable[[str], str] = None, match_exact: bool = False) -> List[str]:
    # data structure: {id: content}
    # fixed_data structure: {description: id}
    if not data or not property_name or not property_value:
        print(f'- get_ids_from_property_value: invalid data or property info. Return empty list.')
        return []

    if not fix_data_delegate:
        fix_data_delegate = __fix_property_value

    fixed_value = fix_data_delegate(property_value)
    fixed_data = {entry_id: fix_data_delegate(entry_data[property_name]) for entry_id, entry_data in data.items() if entry_data[property_name]}

    if match_exact:
        results = [key for key, value in fixed_data.items() if value == fixed_value]
    else:
        similarity_map = {}
        for entry_id, entry_property in fixed_data.items():
            if entry_property.startswith(fixed_value) or fixed_value in entry_property:
                similarity_value = utils.get_similarity(entry_property, fixed_value)
                if similarity_value in similarity_map.keys():
                    similarity_map[similarity_value].append((entry_id, entry_property))
                else:
                    similarity_map[similarity_value] = [(entry_id, entry_property)]
        for similarity_value, entries in similarity_map.items():
            similarity_map[similarity_value] = sorted(entries, key=lambda entry: entry[1])
        similarity_values = sorted(list(similarity_map.keys()), reverse=True)
        results = []
        for similarity_value in similarity_values:
            if not match_exact or (match_exact is True and similarity_value.is_integer()):
                entry_ids = [entry_id for (entry_id, _) in similarity_map[similarity_value]]
                results.extend(entry_ids)

    return results