From 314e7c6acd0ebf9665b6352c062fc25518e30d6e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 5 Oct 2017 20:21:47 +0200 Subject: [PATCH] Add rocket league tracking --- db.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++- rocket_league.py | 21 +++++++++++ telebot.py | 4 +- 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 rocket_league.py diff --git a/db.py b/db.py index de9b0f59..d0442d5d 100644 --- a/db.py +++ b/db.py @@ -66,8 +66,8 @@ class Steam(Base): return f"" def __str__(self): - if self.steam_name is not None: - return self.steam_name + if self.persona_name is not None: + return self.persona_name else: return self.steam_id @@ -79,6 +79,97 @@ class Steam(Base): self.persona_name = j["response"]["players"][0]["personaname"] self.avatar_hex = re.search("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/../(.+).jpg", j["response"]["players"][0]["avatar"]).group(1) + +class RocketLeague(Base): + __tablename__ = "rocketleague" + + steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) + steam = relationship("Steam") + + season = Column(Integer) + + single_rank = Column(Integer) + single_div = Column(Integer) + single_mmr = Column(Integer) + + doubles_rank = Column(Integer) + doubles_div = Column(Integer) + doubles_mmr = Column(Integer) + + standard_rank = Column(Integer) + standard_div = Column(Integer) + standard_mmr = Column(Integer) + + solo_std_rank = Column(Integer) + solo_std_div = Column(Integer) + solo_std_mmr = Column(Integer) + + wins = Column(Integer) + + def __repr__(self): + return f"" + + @staticmethod + def check_and_create(steam_id): + rl = session.query(RocketLeague).filter(RocketLeague.steam_id == steam_id).first() + if rl is not None: + return None + r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={steam_id}&platform_id=1") + if r.status_code != 200: + return None + new_record = RocketLeague(steam_id=steam_id) + new_record.update() + return new_record + + def update(self): + r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={self.steam_id}&platform_id=1") + if r.status_code != 200: + raise RequestError(f"Rocket League Stats returned {r.status_code}") + j = r.json() + # Get current season + current_season = 0 + for season in j["rankedSeasons"]: + if int(season) > current_season: + current_season = int(season) + self.season = current_season + if current_season == 0: + return + current_season = str(current_season) + # Get ranked data + # Single 1v1 + self.single_mmr = j["rankedSeasons"][current_season]["10"]["rankPoints"] + if j["rankedSeasons"][current_season]["10"]["matchesPlayed"] >= 10: + self.single_rank = j["rankedSeasons"][current_season]["10"]["tier"] + self.single_div = j["rankedSeasons"][current_season]["10"]["division"] + else: + self.single_rank = None + self.single_div = None + # Doubles 2v2 + self.doubles_mmr = j["rankedSeasons"][current_season]["11"]["rankPoints"] + if j["rankedSeasons"][current_season]["11"]["matchesPlayed"] >= 10: + self.doubles_rank = j["rankedSeasons"][current_season]["11"]["tier"] + self.doubles_div = j["rankedSeasons"][current_season]["11"]["division"] + else: + self.doubles_rank = None + self.doubles_div = None + # Standard 3v3 + self.standard_mmr = j["rankedSeasons"][current_season]["13"]["rankPoints"] + if j["rankedSeasons"][current_season]["13"]["matchesPlayed"] >= 10: + self.standard_rank = j["rankedSeasons"][current_season]["13"]["tier"] + self.standard_div = j["rankedSeasons"][current_season]["13"]["division"] + else: + self.standard_rank = None + self.standard_div = None + # Solo Standard 3v3 + self.solo_std_mmr = j["rankedSeasons"][current_season]["12"]["rankPoints"] + if j["rankedSeasons"][current_season]["12"]["matchesPlayed"] >= 10: + self.solo_std_rank = j["rankedSeasons"][current_season]["12"]["tier"] + self.solo_std_div = j["rankedSeasons"][current_season]["12"]["division"] + else: + self.solo_std_rank = None + self.solo_std_div = None + + # If run as script, create all the tables in the db if __name__ == "__main__": Base.metadata.create_all(bind=engine) \ No newline at end of file diff --git a/rocket_league.py b/rocket_league.py new file mode 100644 index 00000000..b57979f0 --- /dev/null +++ b/rocket_league.py @@ -0,0 +1,21 @@ +from db import session, Steam, RocketLeague +from time import sleep + +def check_for_new_players(): + steam_users = session.query(Steam).all() + for user in steam_users: + print(user) + rl = RocketLeague.check_and_create(user.steam_id) + if rl is not None: + session.add(rl) + sleep(1) + session.commit() + +def update_existing_players(): + rocket_league_players = session.query(RocketLeague).all() + for player in rocket_league_players: + player.update() + session.commit() + +if __name__ == "__main__": + check_for_new_players() diff --git a/telebot.py b/telebot.py index b4c81f2e..518cdaa2 100644 --- a/telebot.py +++ b/telebot.py @@ -43,7 +43,9 @@ def message_sync(bot: Bot, update: Update): db_user.last_name = tg_user.last_name db_user.username = tg_user.username session.commit() - bot.send_message(update.message.chat.id, "✅ Sincronizzazione completata!") + bot.send_message(update.message.chat.id, f"✅ Sincronizzazione Telegram completata!\n" + f"Sincronizza il tuo account Steam qui:\n" + f"http://79.26.47.209:1234/login/{db_user.royal_id}") updater.dispatcher.add_handler(CommandHandler("sync", message_sync))