From 38b3aee00a81d06241914074c6eb6eab3aedb6e9 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sun, 1 Jul 2018 11:45:45 -0700 Subject: [PATCH] First pass at quake functionality, #18 --- app/dragon-bot.py | 7 +++ app/quake.py | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 app/quake.py diff --git a/app/dragon-bot.py b/app/dragon-bot.py index 391550b4..e347e375 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -21,6 +21,7 @@ import get_from_reddit import gitlab import help_methods import lewds +import quake import questions import role_check import set_avatar @@ -263,6 +264,12 @@ async def on_message(message): embed=generate_embed(embed_url=get_from_reddit.get_image('pout')) ) + if message.content.startswith('!quake'): + await client.send_message( + message.channel, + embed=quake.parse_message(message) + ) + if message.content.startswith('!smug'): await client.send_message( message.channel, diff --git a/app/quake.py b/app/quake.py new file mode 100644 index 00000000..f0f16bc3 --- /dev/null +++ b/app/quake.py @@ -0,0 +1,123 @@ +import requests + +import discord +import help_methods + +def parse_message(message): + if len(message.content.split()) == 1: + return help_methods.get_help_message('quake') + + # Return the player's name + name = message.content.split()[1] + if len(message.content.split()) > 1: + name = '%20'.join(message.content.split()[1:]) + + return get_stats(player=name) + + +def get_stats(player): + base_url = 'https://stats.quake.com/api/v2/' + player_endpoint = "Player/Stats?name={}".format(player) + + try: + request = requests.get("{}{}".format(base_url, player_endpoint)).json() + return create_embed(stats=request) + except Exception: + return discord.Embed( + description="404, Player not found", + color=discord.Color.dark_red(), + type="rich" + ) + + +def create_embed(stats): + """ + create_embed(stats) + + Expects a json blob to be passed in and then builds the embed + object + """ + + + # Parse the json and pull out the numbers we want + champ_name, play_time = get_favorite_champion(stats) + fav_weapon, fav_weapon_kills = get_favorite_weapon(stats) + kd_ratio = get_kd(stats) + + embed = discord.Embed( + description="**All Time account statistics**", + color=discord.Color.dark_red(), + type="rich" + ) + embed.set_author( + name="\nShowing stats for {}".format(stats['name']), + icon_url="https://stats.quake.com/icons/{}.png".format(stats['playerLoadOut']['iconId']) + ) + + embed.add_field(name="Current level:", value=stats['playerLevelState']['level']) + embed.add_field(name="Total XP:", value=stats['playerLevelState']['exp']) + embed.add_field(name="K/D:", value=kd_ratio) + + embed.add_field(name="\u200b", value='\u200b', inline=False) + + embed.add_field(name="Favorite Champion:", value=champ_name) + embed.add_field(name="Total Playtime:", value="~{} hours".format(play_time)) + + embed.add_field(name="\u200b", value='\u200b', inline=False) + + embed.add_field(name="Favorite Weapon:", value=fav_weapon) + embed.add_field(name="Num of kills with it:", value=fav_weapon_kills) + + embed.set_footer(text="Stats pulled from https://stats.quake.com", icon_url='https://stats.quake.com/icons/profile_icon_cbt_participant.png') + + return embed + + +def get_favorite_champion(blob): + play_times = {} + all_champions = blob['playerProfileStats']['champions'] + + for champion in all_champions: + for game_mode in blob['playerProfileStats']['champions'][champion]['gameModes']: + if champion in play_times: + play_times[champion] += blob['playerProfileStats']['champions'][champion]['gameModes'][game_mode]['timePlayed'] + else: + play_times[champion] = blob['playerProfileStats']['champions'][champion]['gameModes'][game_mode]['timePlayed'] + + champion_play_time = max(play_times.values()) # maximum value + champion_name = [k for k, v in play_times.items() if v == champion_play_time][0] # getting all keys containing the `maximum` + + # Convert play_time from miliseconds to hours + champion_play_time = round(float(champion_play_time)/(1000*60*60), 2) + return(champion_name, champion_play_time) + + +def get_favorite_weapon(blob): + weapon_stats = {} + all_champions = blob['playerProfileStats']['champions'] + + for champion in all_champions: + for weapon in blob['playerProfileStats']['champions'][champion]['damageStatusList']: + if weapon in weapon_stats: + weapon_stats[weapon] += blob['playerProfileStats']['champions'][champion]['damageStatusList'][weapon]['kills'] + else: + weapon_stats[weapon] = blob['playerProfileStats']['champions'][champion]['damageStatusList'][weapon]['kills'] + + total_kills = max(weapon_stats.values()) # maximum value + weapon_name = [k for k, v in weapon_stats.items() if v == total_kills][0] # getting all keys containing the `maximum` + + return(weapon_name, total_kills) + + + +def get_kd(blob): + total_kills = 0 + total_deaths = 0 + all_champions = blob['playerProfileStats']['champions'] + + for champion in all_champions: + for game_mode in blob['playerProfileStats']['champions'][champion]['gameModes']: + total_kills += blob['playerProfileStats']['champions'][champion]['gameModes'][game_mode]['kills'] + total_deaths += blob['playerProfileStats']['champions'][champion]['gameModes'][game_mode]['deaths'] + + return round(float(total_kills/total_deaths), 2) \ No newline at end of file