async def get_top_fleets( ctx: Context, take: int = 100, as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]: tourney_running = tourney.is_tourney_running() divisions_designs_data = await divisions_designs_retriever.get_data_dict3() fleets_divisions_max_ranks = [ int(fleet_division_design_info['MaxRank']) for fleet_division_design_info in __get_fleet_division_designs( divisions_designs_data).values() ] raw_data = await core.get_data_from_path(TOP_FLEETS_BASE_PATH + str(take)) data = utils.convert.xmltree_to_dict3(raw_data) if data: title = f'Top {take} fleets' prepared_data = __prepare_top_fleets(data) body_lines = __create_body_lines_top_fleets( prepared_data, tourney_running, fleets_divisions_max_ranks) if as_embed: colour = utils.discord.get_bot_member_colour(ctx.bot, ctx.guild) return __create_top_embeds(title, body_lines, colour) else: result = [f'**{title}**'] result.extend(body_lines) return result else: raise Error( f'An unknown error occured while retrieving the top fleets. Please contact the bot\'s author!' )
def get_file_name(file_prefix: str, data_retrieved_at: datetime, file_ending: FILE_ENDING, consider_tourney: bool = True) -> str: if not file_ending or file_ending not in __FILE_ENDING_LOOKUP.keys(): file_ending = FILE_ENDING.XL file_prefix = file_prefix.replace(' ', '_') if consider_tourney and tourney.is_tourney_running(utc_now=data_retrieved_at): file_timestamp = f'tournament-{data_retrieved_at.year}-{utils.datetime.get_month_short_name(data_retrieved_at).lower()}' else: file_timestamp = data_retrieved_at.strftime('%Y%m%d-%H%M%S') suffix = __FILE_ENDING_LOOKUP[file_ending] result = f'{file_prefix}_{file_timestamp}.{suffix}' return result
def get_file_name(file_prefix: str, data_retrieved_at: datetime, consider_tourney: bool = True) -> str: file_prefix = file_prefix.replace(' ', '_') if consider_tourney and tourney.is_tourney_running( utc_now=data_retrieved_at): file_timestamp = f'tournament-{data_retrieved_at.year}-{util.get_month_short_name(data_retrieved_at).lower()}' else: file_timestamp = data_retrieved_at.strftime('%Y%m%d-%H%M%S') result = f'{file_prefix}_{file_timestamp}.xlsx' return result
async def get_user_details_by_info(user_info: dict, max_tourney_battle_attempts: int = None, retrieved_at: datetime = None, past_fleet_infos: entity.EntitiesData = None) -> list: is_past_data = past_fleet_infos is not None and past_fleet_infos user_id = user_info[USER_KEY_NAME] retrieved_at = retrieved_at or util.get_utcnow() tourney_running = tourney.is_tourney_running(utc_now=retrieved_at) if past_fleet_infos: ship_info = {} fleet_info = past_fleet_infos.get(user_info.get(fleet.FLEET_KEY_NAME)) else: _, ship_info = await ship.get_inspect_ship_for_user(user_id) fleet_info = await __get_fleet_info_by_user_info(user_info) user_name = __get_user_name_as_text(user_info) is_in_tourney_fleet = fleet.is_tournament_fleet(fleet_info) and tourney_running attempts = __get_tourney_battle_attempts(user_info, retrieved_at) if attempts and max_tourney_battle_attempts: attempts_left = max_tourney_battle_attempts - int(attempts) else: attempts_left = None details = { 'Account created': __get_timestamp_as_text(user_info, 'CreationDate', retrieved_at), 'Last login': __get_timestamp_as_text(user_info, 'LastLoginDate', retrieved_at), 'Fleet': __get_fleet_name_and_rank_as_text(user_info, fleet_info), 'Division': fleet.get_division_name_as_text(fleet_info), 'Joined fleet': __get_fleet_joined_at_as_text(user_info, fleet_info, retrieved_at), 'Trophies': __get_trophies_as_text(user_info), 'League': __get_league_as_text(user_info), 'Stars': __get_stars_as_text(user_info, is_in_tourney_fleet, attempts_left), 'Crew donated': __get_crew_donated_as_text(user_info, fleet_info), 'Crew borrowed': __get_crew_borrowed_as_text(user_info, fleet_info), 'PVP win/lose/draw': __get_pvp_attack_stats_as_text(user_info), 'Defense win/lose/draw': __get_pvp_defense_stats_as_text(user_info), 'Level': await __get_level_as_text(ship_info), 'Status': __get_ship_status_as_text(ship_info), 'User type': __get_user_type_as_text(user_info) } lines = [f'**```{user_name}```**```'] for detail_name, detail_value in details.items(): if detail_value is not None: lines.append(f'{detail_name} - {detail_value}') if is_past_data: lines.append(f'``````{util.get_historic_data_note(retrieved_at)}```') else: lines[-1] += '```' return lines
async def get_user_details_by_info( ctx: Context, user_info: EntityInfo, max_tourney_battle_attempts: int = None, retrieved_at: datetime = None, past_fleet_infos: EntitiesData = None, as_embed: bool = settings.USE_EMBEDS) -> Union[List[Embed], List[str]]: is_past_data = past_fleet_infos is not None and past_fleet_infos user_id = user_info[USER_KEY_NAME] retrieved_at = retrieved_at or utils.get_utc_now() tourney_running = tourney.is_tourney_running(utc_now=retrieved_at) if past_fleet_infos: ship_info = {} fleet_info = past_fleet_infos.get(user_info.get(fleet.FLEET_KEY_NAME)) current_user_info = await __get_user_info_by_id(user_id) or {} current_user_name = current_user_info.get( USER_DESCRIPTION_PROPERTY_NAME) if current_user_name and current_user_name != user_info.get( USER_DESCRIPTION_PROPERTY_NAME): user_info['CurrentName'] = current_user_name else: _, ship_info = await ship.get_inspect_ship_for_user(user_id) fleet_info = await __get_fleet_info_by_user_info(user_info) is_in_tourney_fleet = fleet.is_tournament_fleet( fleet_info) and tourney_running user_details = __create_user_details_from_info( user_info, fleet_info, ship_info, max_tourney_battle_attempts=max_tourney_battle_attempts, retrieved_at=retrieved_at, is_past_data=is_past_data, is_in_tourney_fleet=is_in_tourney_fleet) if as_embed: return [(await user_details.get_details_as_embed(ctx, display_inline=False))] else: return (await user_details.get_details_as_text(entity.EntityDetailsType.LONG ))
def _get_fleet_sheet_lines(fleet_users_infos: dict, retrieved_at: datetime, max_tourney_battle_attempts: int = None, fleet_name: str = None, include_player_id: bool = False, include_fleet_id: bool = False) -> list: result = [FLEET_SHEET_COLUMN_NAMES] if include_player_id: result[0].append('Player ID') if include_fleet_id: result[0].append('Fleet ID') tourney_running = tourney.is_tourney_running(retrieved_at) for user_info in fleet_users_infos.values(): last_login_date = user_info.get('LastLoginDate') alliance_join_date = user_info.get('AllianceJoinDate') logged_in_ago = None joined_ago = None if last_login_date: logged_in_ago = retrieved_at - util.parse_pss_datetime(last_login_date) if alliance_join_date: joined_ago = retrieved_at - util.parse_pss_datetime(alliance_join_date) if fleet_name is None and FLEET_DESCRIPTION_PROPERTY_NAME in user_info.keys(): fleet_name = user_info[FLEET_DESCRIPTION_PROPERTY_NAME] attempts_left = None if tourney_running: attempts = user.__get_tourney_battle_attempts(user_info, retrieved_at) if attempts and max_tourney_battle_attempts: attempts_left = max_tourney_battle_attempts - attempts line = [ util.format_excel_datetime(retrieved_at), fleet_name or user_info.get(FLEET_DESCRIPTION_PROPERTY_NAME, user_info.get('Alliance', {}).get(FLEET_DESCRIPTION_PROPERTY_NAME, '')), user_info.get(user.USER_DESCRIPTION_PROPERTY_NAME, ''), user_info.get('AllianceMembership', ''), util.convert_pss_timestamp_to_excel(last_login_date), int(user_info['Trophy']) if 'Trophy' in user_info else '', int(user_info['AllianceScore'] if 'AllianceScore' in user_info else ''), util.convert_pss_timestamp_to_excel(alliance_join_date), int(user_info['CrewDonated']) if 'CrewDonated' in user_info else '', int(user_info['CrewReceived']) if 'CrewReceived' in user_info else '', util.get_formatted_timedelta(logged_in_ago, include_relative_indicator=False), util.get_formatted_timedelta(joined_ago, include_relative_indicator=False), attempts_left or '', user_info.get(user.USER_KEY_NAME, '') if include_player_id else '', user_info.get(FLEET_KEY_NAME, '') if include_fleet_id else '' ] result.append(line) return result
def __get_star_value(user_info: EntityInfo, max_tourney_battle_attempts: int = None, retrieved_at: datetime = None, is_in_tourney_fleet: bool = None, **kwargs) -> Optional[str]: result = None if is_in_tourney_fleet and tourney.is_tourney_running(retrieved_at): star_value = user_info.get('StarValue') if star_value is None: star_value, source = get_star_value_from_user_info(user_info) if star_value is not None: if source < 0: result = f'{star_value} (based on trophies)' elif source > 0: result = f'{star_value} (based on yesterday\'s stars)' else: result = str(star_value) else: result = str(star_value) return result
def _get_top_fleets_as_text(alliance_data: dict, take: int = 100): tourney_running = tourney.is_tourney_running() headline = f'**Top {take} fleets**' lines = [headline] position = 0 for entry in alliance_data.values(): position += 1 name = util.escape_markdown(entry['AllianceName']) trophies = entry['Trophy'] stars = entry['Score'] trophy_txt = f'{trophies} {emojis.trophy}' if tourney_running: stars_txt = f', {stars} {emojis.star}' else: stars_txt = '' line = f'**{position}.** {name} ({trophy_txt}{stars_txt})' lines.append(line) return lines
async def get_top_fleets(ctx: commands.Context, take: int = 100, as_embed: bool = settings.USE_EMBEDS): tourney_running = tourney.is_tourney_running() raw_data = await core.get_data_from_path(TOP_FLEETS_BASE_PATH + str(take)) data = core.xmltree_to_dict3(raw_data) if data: title = f'Top {take} fleets' prepared_data = __prepare_top_fleets(data) body_lines = __create_body_lines_top_fleets(prepared_data, tourney_running) if as_embed: colour = util.get_bot_member_colour(ctx.bot, ctx.guild) return __create_top_embeds(title, body_lines, colour), True else: result = [f'**{title}**'] result.extend(body_lines) return result, True else: return [ 'An unknown error occured while retrieving the top fleets. Please contact the bot\'s author!' ], False
fleet_info: dict, fleet_data: dict, user_data: dict, retrieved_date: datetime) -> list: fleet_id = fleet_info[FLEET_KEY_NAME] fleet_users_infos = {} if fleet_id in fleet_data.keys(): fleet_info['DivisionDesignId'] = fleet_data[fleet_id][ 'DivisionDesignId'] fleet_users_infos = dict({ user_info[user.USER_KEY_NAME]: user_info for user_info in user_data.values() if user_info[FLEET_KEY_NAME] == fleet_id }) return get_fleet_users_stars_from_info(fleet_info, fleet_users_infos, retrieved_date=retrieved_date) # ---------- Testing ---------- if __name__ == '__main__': test_fleets = ['Fallen An'] for fleet_name in test_fleets: os.system('clear') is_tourney_running = tourney.is_tourney_running() fleet_infos = get_fleet_details_by_name(fleet_name) lines = [ get_fleet_search_details(fleet_info) for fleet_info in fleet_infos ] for line in lines: print(line)