From e01e80da44ca0c997d06f3b2ee2607fe45069f16 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sun, 1 Jul 2018 16:19:59 -0700 Subject: [PATCH] Attempting to add data about last match, #18 --- app/quake.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/app/quake.py b/app/quake.py index 490417c7..b36d9a93 100644 --- a/app/quake.py +++ b/app/quake.py @@ -3,6 +3,8 @@ import requests import discord import help_methods +base_url = 'https://stats.quake.com/api/v2/' + def parse_message(message): """ parse_message(message) @@ -14,17 +16,16 @@ def parse_message(message): if len(message.content.split()) > 1: name = '%20'.join(message.content.split()[1:]) - return get_stats(player=name) + return get_player_stats(player=name) -def get_stats(player): +def get_player_stats(player): """ - get_stats(player) + get_player_stats(player) Makes the request to stats.quake.com and returns an embed object with a bunch of data about the player """ - base_url = 'https://stats.quake.com/api/v2/' player_endpoint = "Player/Stats?name={}".format(player) try: @@ -75,6 +76,13 @@ def create_embed(stats): embed.add_field(name="Favorite Weapon:", value=fav_weapon) embed.add_field(name="Num of kills with it:", value=fav_weapon_kills) + embed.add_field(name="\u200b", value='\u200b', inline=False) + + # If they've played a ranked match, show the stats + if len(stats['playerRatings']['tdm']['history']): + match_blob = get_match_stats(stats, stats['name']) + embed.add_field(name="Last Ranked Game:", value=match_blob) + 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 @@ -146,4 +154,41 @@ def get_kd(blob): 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 + return round(float(total_kills/total_deaths), 2) + + +def get_match_stats(blob, target_player): + """ + get_match_stats(blob, target_player) + + Takes the stats blob and a player name (so we know who's K/D we care about). + + Attempts to figure out what the ranked game type was and then pulls stats from that game + """ + + # Figure out which ranked gametype they last played + game_type = [x for x in blob['playerRatings'] if blob['playerRatings'][x]['lastChange'] != 0][0] + + most_recent_match_id = blob['playerRatings'][game_type]['history'][0]['sessionId'] + match_endpoint = "Player/Games?id={}".format(most_recent_match_id) + match_data = requests.get("{}{}".format(base_url, match_endpoint)).json() + fav_weapon = match_data['battleReportPersonalStatistics'][0]['bestWeapon'] + accuracy = match_data['battleReportPersonalStatistics'][0]['bestWeaponAccuracyPercent'] + + for team_mate in match_data['battleReportPersonalStatistics']: + if team_mate['nickname'].lower() == target_player.lower(): + earnedXp = team_mate['earnedXp'] + earnedFavor = team_mate['earnedFavor'] + + kill_death_ratio = round(float(match_data['battleReportPersonalStatistics'][0]['kills']) / match_data['battleReportPersonalStatistics'][0]['deaths'], 2) + + return_blob = "Final score: {}\nK/D: {}\nFavorite Weapon {} | {}% Accuracy\nXP Eaned: {} | Favor Earned: {}".format( + match_data['teamScores'], + kill_death_ratio, + fav_weapon, + accuracy, + earnedXp, + earnedFavor, + ) + + return return_blob \ No newline at end of file