From 91f397e025866ec927cbda5bcb41624bf1389cf2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 23 Jan 2019 15:00:30 +0100 Subject: [PATCH] osu! improvements --- db.py | 37 +++++++++++++++++++++++++------------ dirty.py | 10 ++++++++++ statsupdate.py | 29 +++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/db.py b/db.py index 7e897269..dcab8ac2 100644 --- a/db.py +++ b/db.py @@ -20,7 +20,7 @@ from discord import User as DiscordUser # noinspection PyPackageRequirements from telegram import User as TelegramUser import loldata -from dirty import Dirty +from dirty import Dirty, DirtyDelta import query_discord_music from flask import escape import libgravatar @@ -589,10 +589,14 @@ class Osu(Base, Mini): osu_id = Column(Integer, primary_key=True) osu_name = Column(String) - std_pp = Column(Float) - taiko_pp = Column(Float) - catch_pp = Column(Float) - mania_pp = Column(Float) + std_pp = Column(Float, default=0) + std_best_song = Column(BigInteger) + taiko_pp = Column(Float, default=0) + taiko_best_song = Column(BigInteger) + catch_pp = Column(Float, default=0) + catch_best_song = Column(BigInteger) + mania_pp = Column(Float, default=0) + mania_best_song = Column(BigInteger) _mini_full_name = "osu!" _mini_name = "osu" @@ -629,23 +633,32 @@ class Osu(Base, Mini): # noinspection PyUnusedLocal def update(self, session=None): + std = DirtyDelta(self.std_pp) + taiko = DirtyDelta(self.taiko_pp) + catch = DirtyDelta(self.catch_pp) + mania = DirtyDelta(self.mania_pp) r0 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=0") r0.raise_for_status() + j0 = r0.json()[0] r1 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=1") r1.raise_for_status() + j1 = r1.json()[0] r2 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=2") r2.raise_for_status() + j2 = r2.json()[0] r3 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=3") r3.raise_for_status() - j0 = r0.json()[0] - j1 = r1.json()[0] - j2 = r2.json()[0] j3 = r3.json()[0] self.osu_name = j0["username"] - self.std_pp = j0["pp_raw"] - self.taiko_pp = j1["pp_raw"] - self.catch_pp = j2["pp_raw"] - self.mania_pp = j3["pp_raw"] + std.value = j0["pp_raw"] + taiko.value = j1["pp_raw"] + catch.value = j2["pp_raw"] + mania.value = j3["pp_raw"] + self.std_pp = std.value + self.taiko_pp = taiko.value + self.catch_pp = catch.value + self.mania_pp = mania.value + return std, taiko, catch, mania def __repr__(self): if not self.osu_name: diff --git a/dirty.py b/dirty.py index 9a95ede8..96ad0c94 100644 --- a/dirty.py +++ b/dirty.py @@ -11,3 +11,13 @@ class Dirty: def __bool__(self): return self.is_dirty() + + +class DirtyDelta(Dirty): + @property + def delta(self): + return self.value - self.initial_value + + @delta.setter + def delta(self, value): + self.value = self.initial_value + value diff --git a/statsupdate.py b/statsupdate.py index 2dd0ef6a..2ff1fc48 100644 --- a/statsupdate.py +++ b/statsupdate.py @@ -11,7 +11,7 @@ import telegram import sys import coloredlogs import requests -from dirty import Dirty +from dirty import Dirty, DirtyDelta from sentry_sdk import configure_scope logging.getLogger().disabled = True @@ -78,10 +78,8 @@ def new_dota_rank(item: db.Dota, change): logger.warning(f"Couldn't notify on Telegram: {item}") -def new_lol_rank(item, change: typing.Tuple[Dirty]): +def new_lol_rank(item, change: typing.Tuple[Dirty, Dirty, Dirty]): # It always gets called, even when there is no change - # Assignment inspection is wrong - # noinspection PyTupleAssignmentBalance solo, flex, twtr = change try: if solo: @@ -109,6 +107,29 @@ def new_lol_rank(item, change: typing.Tuple[Dirty]): logger.warning(f"Couldn't notify on Telegram: {item}") +def osu_pp_change(item, change: typing.Tuple[DirtyDelta, DirtyDelta, DirtyDelta, DirtyDelta]): + std, taiko, catch, mania = change + try: + if std.delta >= 1: + telegram_bot.send_message(config["Telegram"]["main_group"], + f"✳️ {item.royal.username} ha ora **{std.value}pp** (+{std.value.delta}) su osu!", + parse_mode="Markdown") + if taiko.delta >= 1: + telegram_bot.send_message(config["Telegram"]["main_group"], + f"✳️ {item.royal.username} ha ora **{std.value}pp** (+{std.value.delta}) su osu!taiko!", + parse_mode="Markdown") + if catch.delta >= 1: + telegram_bot.send_message(config["Telegram"]["main_group"], + f"✳️ {item.royal.username} ha ora **{std.value}pp** (+{std.value.delta}) su osu!catch!", + parse_mode="Markdown") + if mania.delta >= 1: + telegram_bot.send_message(config["Telegram"]["main_group"], + f"✳️ {item.royal.username} ha ora **{std.value}pp** (+{std.value.delta}) su osu!mania!", + parse_mode="Markdown") + except Exception: + logger.warning(f"Couldn't notify on Telegram: {item}") + + def process(): while True: session = db.Session()