1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add rocket league tracking

This commit is contained in:
Steffo 2017-10-05 20:21:47 +02:00
parent f9995666e2
commit 314e7c6acd
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D
3 changed files with 117 additions and 3 deletions

95
db.py
View file

@ -66,8 +66,8 @@ class Steam(Base):
return f"<Steam {self.steam_id}>"
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"<RocketLeague {self.steam_id}>"
@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)

21
rocket_league.py Normal file
View file

@ -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()

View file

@ -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))