Skip to content

Commit

Permalink
Merge pull request #17 from chao813/opponent_stats
Browse files Browse the repository at this point in the history
add opponent ave stats today and rate command
  • Loading branch information
chao813 committed Mar 29, 2021
2 parents 72d2f2b + c02a359 commit 11ae03b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 deletions.
89 changes: 87 additions & 2 deletions clients/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,80 @@ def _pad_symbol(val: str):
""" Left pad a plus sign if the number if above zero """
return f"+{val}" if float(val) >= 0 else val

async def get_opponent_stats_today():
def _breakdown_opponent_average_stats(opponent_stats):
""" Format opponent snapshots into a dict
TODO: Refactor as part of discord_base
"""

stats = {}
# Format data
for row in opponent_stats:
mode = row["MODE"]

stats[mode] = {
"KD": row["AVG(kd)"],
"Wins": row["AVG(wins)"],
"Win Percentage": row["AVG(win_rate)"],
"Matches": row["AVG(games)"],
"TRNRating": row["AVG(trn)"]
}

return stats

def _create_opponents_message(opponent_stats_breakdown):
""" Create Discord message """
embed=discord.Embed(
title="Opponent Average Stats Today",
description=_create_opponent_wins_str(opponent_stats_breakdown['all']),
color=_calculate_skill_color_indicator(opponent_stats_breakdown["all"]["KD"]))

for mode in MODES:
if mode not in opponent_stats_breakdown:
continue

if mode == "all":
name = "Overall"
else:
name = mode.capitalize()

embed.add_field(name=f"[{name}]", value=_create_opponent_stats_str(mode, opponent_stats_breakdown), inline=False)

return embed

def _create_opponent_wins_str(opponent_win_stats):
""" Create opponent stats string for output """
wins_str = int(opponent_win_stats["Wins"])
matches_str = int(opponent_win_stats["Matches"])
return f"Wins: {wins_str} / {matches_str} played"

def _create_opponent_stats_str(mode, opponent_stats_breakdown):
""" Create stats string for output """
mode_stats = opponent_stats_breakdown[mode]
return (f"KD: {mode_stats['KD']:,.2f} • "
f"Wins: {int(mode_stats['Wins'])} • "
f"Win Percentage: {mode_stats['Win Percentage']:,.1f}% • "
f"Matches: {int(mode_stats['Matches'])} • "
f"TRN: {int(mode_stats['TRNRating'])} ")

async def get_opponent_stats_today(ctx):
""" Outputs the stats of the opponents faced today """
mysql = await MySQL.create()
return await mysql.fetch_avg_player_stats_today()
opponent_stats = await mysql.fetch_avg_player_stats_today()

opponent_stats_breakdown = _breakdown_opponent_average_stats(opponent_stats)

message = _create_opponents_message(opponent_stats_breakdown)
await ctx.send(embed=message)

async def rate_opponent_stats_today(ctx):
""" Outputs the stats of the opponents faced today """
mysql = await MySQL.create()
opponent_stats = await mysql.fetch_avg_player_stats_today()

opponent_stats_breakdown = _breakdown_opponent_average_stats(opponent_stats)

message = _calculate_skill_rate_indicator(opponent_stats_breakdown["all"]["KD"])
await ctx.send(message)


# TODO: Move the following to discord_base so they can be inherited and overloaded
Expand Down Expand Up @@ -142,6 +212,21 @@ def _calculate_skill_color_indicator(overall_kd):
else:
return 0x17b532

def _calculate_skill_rate_indicator(overall_kd):
""" Return the skill rate indicator """
if overall_kd >= 5:
return "Hackers"
elif overall_kd >= 4:
return "Aim Botters"
elif overall_kd >= 3:
return "Sweats"
elif overall_kd < 3 and overall_kd >= 2:
return "High"
elif overall_kd < 2 and overall_kd >= 1:
return "Medium"
else:
return "Bots"


def _create_stats_str(mode, stats_breakdown):
""" Create stats string for output """
Expand Down
14 changes: 13 additions & 1 deletion commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"LigmaBalls12`, `!stats played`)."),
"diff_commands": ["today", "diff"],
"opponent_commands": ["played", "opponents", "noobs", "enemy"]
}
},
"Rate Difficulty": {
"command": "rate",
"aliases": ["gg"],
"description": "Rate how good opponents are today"
}
}

# Help
Expand All @@ -45,3 +50,10 @@
STATS_DESCRIPTION = COMMANDS["Stats"]["description"]
STATS_DIFF_COMMANDS = COMMANDS["Stats"]["diff_commands"]
STATS_OPPONENTS_COMMANDS = COMMANDS["Stats"]["opponent_commands"]

# Rate Difficulty
RATE_COMMAND = COMMANDS["Rate Difficulty"]["command"]
RATE_DESCRIPTION = COMMANDS["Rate Difficulty"]["description"]
RATE_ALIASES = COMMANDS["Rate Difficulty"]["aliases"]


12 changes: 8 additions & 4 deletions fortnite_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ async def track(ctx, silent=False):
tasks = [player_search(ctx, username, silent=silent) for username in SQUAD_PLAYERS_LIST]
await asyncio.gather(*tasks)

@bot.command(name=commands.RATE_COMMAND, help=commands.RATE_DESCRIPTION,
aliases=commands.RATE_ALIASES)
async def rate(ctx, silent=False):
""" Rate how good opponents are today """
await stats.rate_opponent_stats_today(ctx)


@bot.command(name=commands.STATS_COMMAND, help="returns the stats based on parameters provided")
async def stats_operations(ctx, *params):
Expand Down Expand Up @@ -175,10 +181,8 @@ async def stats_diff_today(ctx, usernames):

async def opponent_stats_today(ctx):
""" Outputs the stats of the players faced today """
# TODO: Wrap this up
res = await stats.get_opponent_stats_today()
print(res)

# TODO:
await stats.get_opponent_stats_today(ctx)

def should_log_traceback(e):
""" Returns True if a traceback should be logged,
Expand Down

0 comments on commit 11ae03b

Please sign in to comment.