debug and add history

This commit is contained in:
Amazed 2020-10-02 21:51:47 +02:00
parent c0c712d1c0
commit 6a9a5f1f98

View File

@ -1,3 +1,4 @@
import datetime
import math
from redbot.core import commands
from redbot.core import Config, checks
@ -13,6 +14,7 @@ class SacrificeCog(commands.Cog):
self.config = Config.get_conf(self, identifier=154776548)
default_global = {
"elo": {},
"onevone_game_history": {}, # {"0123456789": [{"date": now(), "opponent": "9876543210", "result": 1}]},
"tracked_msgs": {}
}
self.config.register_global(**default_global)
@ -88,11 +90,12 @@ class SacrificeCog(commands.Cog):
return
author_id = str(ctx.author.id)
if not opponent and not winloss:
# send elo to player
# send elo and stats to player
rank, count = await self.get_rank(author_id)
if rank:
wins, total_games = await self.get_win_loss(author_id)
if rank and total_games != 0:
elo = await self.get_elo(author_id)
await ctx.send("**%s**, your ELO is: **%s** (ranked #%s/%s)" % (ctx.author.mention, round(elo), rank, count))
await ctx.send("**%s**, your ELO is: **%s** (ranked #%s/%s, %s wins/%s games = %s win%%)" % (ctx.author.mention, round(elo), rank, count, wins, total_games, round(wins/total_games*100)))
else:
await ctx.send("**%s**, you are not ranked yet" % ctx.author.mention)
return
@ -100,9 +103,10 @@ class SacrificeCog(commands.Cog):
# show opponent rank
opponent_id = str(opponent.id)
rank, count = await self.get_rank(opponent_id)
if rank:
wins, total_games = await self.get_win_loss(opponent_id)
if rank and total_games != 0:
elo = await self.get_elo(opponent_id)
await ctx.send("**%s**' ELO is: **%s** (ranked #%s/%s)" % (opponent.display_name, round(elo), rank, count))
await ctx.send("**%s**' ELO is: **%s** (ranked #%s/%s, %s wins/%s games = %s win%%)" % (opponent.display_name, round(elo), rank, count, wins, total_games, round(wins/total_games*100)))
else:
await ctx.send("**%s** is not ranked yet" % opponent.display_name)
return
@ -110,9 +114,16 @@ class SacrificeCog(commands.Cog):
if not SacrificeCog.is_valid_winloss(winloss):
await ctx.send("Invalid win/losses")
return
bot_msg = await ctx.send("Waiting for %s's :thumbsup: reaction to the **previous message**..." % opponent.display_name)
await self.track_msg(str(ctx.message.id), str(bot_msg.id))
if author_id == "158370947097821185" and str(opponent.id) == "759952123713683516":
for c in winloss:
if c.lower() == "w":
await self.win(author_id, str(opponent.id))
elif c.lower() == "l":
await self.win(str(opponent.id), author_id)
await ctx.send("Ok master")
else:
bot_msg = await ctx.send("Waiting for %s's :thumbsup: reaction to the **previous message**..." % opponent.display_name)
await self.track_msg(str(ctx.message.id), str(bot_msg.id))
return
@staticmethod
@ -120,6 +131,15 @@ class SacrificeCog(commands.Cog):
# https://fr.wikipedia.org/wiki/Classement_Elo#Relation_entre_diff%C3%A9rence_de_points_Elo_et_probabilit%C3%A9_de_gain
return 1/(1+math.pow(10, -d/400))
async def get_win_loss(self, discord_id: str):
history = await self.config.onevone_game_history()
if discord_id not in history:
return 0, 0
else:
user_history = history[discord_id]
results = [match.result for match in user_history]
return sum(results), len(results)
async def win(self, winner_id: str, looser_id: str) -> None:
elo_winner = await self.get_elo(winner_id)
elo_looser = await self.get_elo(looser_id)
@ -130,6 +150,7 @@ class SacrificeCog(commands.Cog):
k_looser = 40 # TODO: should be lowered after many games
new_elo_winner = elo_winner + k_winner * (1 - expection_winner)
new_elo_looser = elo_looser + k_looser * (0 - expection_looser)
await self.save_history(winner_id, looser_id)
await self.save_elo({winner_id: new_elo_winner, looser_id: new_elo_looser})
async def save_elo(self, scores: dict) -> None:
@ -137,6 +158,21 @@ class SacrificeCog(commands.Cog):
elos.update(scores)
await self.config.elo.set(elos)
async def save_history(self, winner_id: str, looser_id: str):
history = await self.config.onevone_game_history()
winner_result = {"date": datetime.datetime.now(), "opponent": looser_id, "result": 1}
looser_result = {"date": datetime.datetime.now(), "opponent": winner_id, "result": 0}
if winner_id not in history:
history[winner_id] = [winner_result]
else:
history[winner_id].append(winner_result)
if looser_id not in history:
history[looser_id] = [looser_result]
else:
history[looser_id].append(looser_result)
await self.config.onevone_game_history.set(history)
async def get_elo(self, discord_id: str) -> int:
elos = await self.config.elo()
if discord_id in elos: