From 6e63ec671626210b0da097228a7cf9095c78095d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 9 Jan 2019 16:52:34 +0100 Subject: [PATCH] Dr. Frankenstein, fatti da parte --- db.py | 91 ++++++++++++++++++++++++++++-------------- templates/game.html | 10 ++--- templates/profile.html | 51 ++--------------------- webserver.py | 63 +++++------------------------ 4 files changed, 77 insertions(+), 138 deletions(-) diff --git a/db.py b/db.py index a576a2cc..82155ecf 100644 --- a/db.py +++ b/db.py @@ -87,10 +87,11 @@ class Mini(object): """Mixin for every table that has an associated mini.""" _mini_full_name = NotImplemented _mini_name = NotImplemented + _mini_order = NotImplemented @classmethod def mini_get_all(cls, session: Session) -> list: - return session.query(cls).all() + return session.query(cls).order_by(*cls._mini_order).all() @classmethod def mini_get_single(cls, session: Session, **kwargs): @@ -110,8 +111,6 @@ class Mini(object): class Royal(Base, Mini): __tablename__ = "royals" - _mini_full_name = "Royalnet" - _mini_name = "ryg" id = Column(Integer, primary_key=True) username = Column(String, unique=True, nullable=False) @@ -121,6 +120,10 @@ class Royal(Base, Mini): member_since = Column(Date) email = Column(String) + _mini_full_name = "Royalnet" + _mini_name = "ryg" + _mini_order = [fiorygi.desc()] + @staticmethod def create(session: Session, username: str): r = session.query(Royal).filter_by(username=username).first() @@ -144,7 +147,6 @@ class Royal(Base, Mini): class Telegram(Base, Mini): __tablename__ = "telegram" - _mini_full_name = "Telegram" royal_id = Column(Integer, ForeignKey("royals.id")) royal = relationship("Royal", backref="telegram", lazy="joined") @@ -154,6 +156,10 @@ class Telegram(Base, Mini): last_name = Column(String) username = Column(String) + _mini_full_name = "Telegram" + _mini_name = "tg" + _mini_order = [telegram_id] + @staticmethod def create(session: Session, royal_username, telegram_user: TelegramUser): t = session.query(Telegram).filter_by(telegram_id=telegram_user.id).first() @@ -195,8 +201,6 @@ class Telegram(Base, Mini): class Steam(Base, Mini): __tablename__ = "steam" - _mini_full_name = "Steam" - _mini_name = "steam" royal_id = Column(Integer, ForeignKey("royals.id")) royal = relationship("Royal", backref="steam", lazy="joined") @@ -207,6 +211,10 @@ class Steam(Base, Mini): trade_token = Column(String) most_played_game_id = Column(BigInteger) + _mini_full_name = "Steam" + _mini_name = "steam" + _mini_order = [steam_id] + def __repr__(self): if not self.persona_name: return f"" @@ -289,8 +297,6 @@ class Steam(Base, Mini): class RocketLeague(Base, Mini): __tablename__ = "rocketleague" - _mini_full_name = "Rocket League" - _mini_name = "rl" steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) steam = relationship("Steam", backref="rl", lazy="joined") @@ -315,6 +321,13 @@ class RocketLeague(Base, Mini): wins = Column(Integer) + _mini_full_name = "Rocket League" + _mini_name = "rl" + _mini_order = [solo_std_mmr.desc().nullslast(), + doubles_mmr.desc().nullslast(), + standard_mmr.desc().nullslast(), + single_mmr.desc().nullslast()] + def __repr__(self): return f"" @@ -352,19 +365,19 @@ class RocketLeague(Base, Mini): class Dota(Base, Mini): __tablename__ = "dota" - _mini_full_name = "DOTA 2" - _mini_name = "dota" steam_id = Column(String, ForeignKey("steam.steam_id"), primary_key=True) steam = relationship("Steam", backref="dota", lazy="joined") rank_tier = Column(Integer) - wins = Column(Integer) losses = Column(Integer) - most_played_hero = Column(Integer) + _mini_full_name = "DOTA 2" + _mini_name = "dota" + _mini_order = [rank_tier.desc().nullslast(), wins.desc().nullslast()] + def __repr__(self): return f"" @@ -465,8 +478,6 @@ class RomanNumerals(enum.Enum): class LeagueOfLegends(Base, Mini): __tablename__ = "leagueoflegends" - _mini_full_name = "League of Legends" - _mini_name = "lol" royal_id = Column(Integer, ForeignKey("royals.id")) royal = relationship("Royal", backref="lol", lazy="joined") @@ -475,7 +486,6 @@ class LeagueOfLegends(Base, Mini): summoner_id = Column(String, primary_key=True) account_id = Column(String) summoner_name = Column(String) - level = Column(Integer) solo_division = Column(Enum(LeagueOfLegendsRanks)) solo_rank = Column(Enum(RomanNumerals)) @@ -483,9 +493,17 @@ class LeagueOfLegends(Base, Mini): flex_rank = Column(Enum(RomanNumerals)) twtr_division = Column(Enum(LeagueOfLegendsRanks)) twtr_rank = Column(Enum(RomanNumerals)) - highest_mastery_champ = Column(Integer) + _mini_full_name = "League of Legends" + _mini_name = "lol" + _mini_order = [solo_division.desc().nullslast(), + solo_rank.desc().nullslast(), + flex_division.desc().nullslast(), + flex_rank.desc().nullslast(), + twtr_division.desc().nullslast(), + twtr_rank.desc().nullslast()] + def __repr__(self): if not self.summoner_name: return f"" @@ -564,20 +582,24 @@ class LeagueOfLegends(Base, Mini): class Osu(Base, Mini): __tablename__ = "osu" - _mini_full_name = "osu!" - _mini_name = "osu" royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal = relationship("Royal", backref="osu", lazy="joined") 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) + _mini_full_name = "osu!" + _mini_name = "osu" + _mini_order = [mania_pp.desc().nullslast(), + std_pp.desc().nullslast(), + taiko_pp.desc().nullslast(), + catch_pp.desc().nullslast()] + @staticmethod def create(session: Session, royal_id, osu_name): o = session.query(Osu).filter(Osu.osu_name == osu_name).first() @@ -633,8 +655,6 @@ class Osu(Base, Mini): class Discord(Base, Mini): __tablename__ = "discord" __table_args__ = tuple(UniqueConstraint("name", "discriminator")) - _mini_full_name = "Discord" - _mini_name = "discord" royal_id = Column(Integer, ForeignKey("royals.id")) royal = relationship("Royal", backref="discord", lazy="joined") @@ -644,6 +664,10 @@ class Discord(Base, Mini): discriminator = Column(Integer) avatar_hex = Column(String) + _mini_full_name = "Discord" + _mini_name = "discord" + _mini_order = [discord_id] + def __str__(self): return f"{self.name}#{self.discriminator}" @@ -679,8 +703,6 @@ class Discord(Base, Mini): class Overwatch(Base, Mini): __tablename__ = "overwatch" - _mini_full_name = "Overwatch" - _mini_name = "ow" royal_id = Column(Integer, ForeignKey("royals.id"), nullable=False) royal = relationship("Royal", backref="overwatch", lazy="joined") @@ -688,10 +710,13 @@ class Overwatch(Base, Mini): battletag = Column(String, primary_key=True) discriminator = Column(Integer, primary_key=True) icon = Column(String) - level = Column(Integer) rank = Column(Integer) + _mini_full_name = "Overwatch" + _mini_name = "ow" + _mini_order = [rank.desc().nullslast(), level.desc()] + def __str__(self, separator="#"): return f"{self.battletag}{separator}{self.discriminator}" @@ -1027,14 +1052,11 @@ class LoginToken(Base): class Halloween(Base, Mini): """This is some nice spaghetti, don't you think?""" __tablename__ = "halloween" - _mini_full_name = "Halloween 2018" - _mini_name = "halloween2018" royal_id = Column(Integer, ForeignKey("royals.id"), primary_key=True) royal = relationship("Royal", backref="halloween", lazy="joined") first_trigger = Column(DateTime) - puzzle_piece_a = Column(DateTime) puzzle_piece_b = Column(DateTime) puzzle_piece_c = Column(DateTime) @@ -1042,9 +1064,12 @@ class Halloween(Base, Mini): puzzle_piece_e = Column(DateTime) puzzle_piece_f = Column(DateTime) puzzle_piece_g = Column(DateTime) - boss_battle = Column(DateTime) + _mini_full_name = "Halloween 2018" + _mini_name = "halloween2018" + _mini_order = [first_trigger] + def __getitem__(self, item): if not isinstance(item, int): raise TypeError("The index should be an int") @@ -1143,8 +1168,6 @@ class Quest(Base): class Terraria13(Base, Mini): __tablename__ = "terraria13" - _mini_full_name = "Terraria 13" - _mini_name = "terraria13" game_name = "Terraria 13" @@ -1154,10 +1177,18 @@ class Terraria13(Base, Mini): character_name = Column(String) contribution = Column(Integer) + _mini_full_name = "Terraria 13" + _mini_name = "terraria13" + _mini_order = [contribution.desc()] + def __repr__(self): return f"" +mini_list = [Royal, Telegram, Steam, RocketLeague, Dota, LeagueOfLegends, Osu, Discord, Overwatch, Halloween, + Terraria13] + + # If run as script, create all the tables in the db if __name__ == "__main__": print("Creating new tables...") diff --git a/templates/game.html b/templates/game.html index c53f67aa..e23c6ab1 100644 --- a/templates/game.html +++ b/templates/game.html @@ -6,19 +6,17 @@ {% endblock %} {% block pagetitle %} - {{ game_name }} + {{ mini_type._mini_full_name }} {% endblock %} {% block body %}

- Royal Games su {{ game_name }} + Royal Games su {{ mini_type._mini_full_name }}

- {% for mini in minis %} - {% with record = mini %} - {% include "minis/" + game_short_name + ".html" %} - {% endwith %} + {% for record in mini_data %} + {% include "minis/" + record._mini_name + ".html" %} {% endfor %}
diff --git a/templates/profile.html b/templates/profile.html index a6a33ebc..1d91953e 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -35,54 +35,9 @@ {% endif %}
- {% with record = ryg %} - {% include "minis/ryg.html" %} - {% endwith %} - {% if halloween %} - {% with record = halloween %} - {% include "minis/halloween2018.html" %} - {% endwith %} - {% endif %} - {% if tg %} - {% with record = tg %} - {% include "minis/tg.html" %} - {% endwith %} - {% endif %} - {% if discord %} - {% with record = discord %} - {% include "minis/discord.html" %} - {% endwith %} - {% endif %} - {% if steam %} - {% with record = steam %} - {% include "minis/steam.html" %} - {% endwith %} - {% endif %} - {% if dota %} - {% with record = dota %} - {% include "minis/dota.html" %} - {% endwith %} - {% endif %} - {% if osu %} - {% with record = osu %} - {% include "minis/osu.html" %} - {% endwith %} - {% endif %} - {% if lol %} - {% with record = lol %} - {% include "minis/lol.html" %} - {% endwith %} - {% endif %} - {% if ow %} - {% with record = ow %} - {% include "minis/ow.html" %} - {% endwith %} - {% endif %} - {% if terraria13 %} - {% with record = terraria13 %} - {% include "minis/terraria13.html" %} - {% endwith %} - {% endif %} + {% for record in mini_data %} + {% include "minis/" + record._mini_name + ".html" %} + {% endfor %}
{% endblock %} \ No newline at end of file diff --git a/webserver.py b/webserver.py index aafeb22d..49270ec2 100644 --- a/webserver.py +++ b/webserver.py @@ -112,24 +112,15 @@ def page_profile(name: str): abort(404) return css = fl_g.session.query(db.ProfileData).filter_by(royal=user).one_or_none() - steam = fl_g.session.query(db.Steam).filter_by(royal=user).one_or_none() - osu = fl_g.session.query(db.Osu).filter_by(royal=user).one_or_none() - dota = fl_g.session.query(db.Dota).join(db.Steam).filter_by(royal=user).one_or_none() - lol = fl_g.session.query(db.LeagueOfLegends).filter_by(royal=user).one_or_none() - ow = fl_g.session.query(db.Overwatch).filter_by(royal=user).one_or_none() - tg = fl_g.session.query(db.Telegram).filter_by(royal=user).one_or_none() - discord = fl_g.session.execute(query_discord_music.one_query, {"royal": user.id}).fetchone() - gamelog = fl_g.session.query(db.GameLog).filter_by(royal=user).one_or_none() - halloween = fl_g.session.query(db.Halloween).filter_by(royal=user).one_or_none() - terraria13 = fl_g.session.query(db.Terraria13).filter_by(royal=user).one_or_none() + mini_data = [] + for game in db.mini_list: + mini_data.append(game.mini_get_single_from_royal(fl_g.session, user)) if css is not None: converted_bio = Markup(markdown2.markdown(css.bio.replace("<", "<"), extras=["spoiler", "tables", "smarty-pants", "fenced-code-blocks"])) else: converted_bio = "" - return render_template("profile.html", ryg=user, css=css, osu=osu, dota=dota, lol=lol, steam=steam, ow=ow, - tg=tg, discord=discord, bio=converted_bio, gamelog=gamelog, - halloween=halloween, terraria13=terraria13) + return render_template("profile.html", ryg=user, css=css, bio=converted_bio, mini_data=mini_data) @app.route("/login") @@ -216,50 +207,14 @@ def page_editprofile(): @app.route("/game/") def page_game(name: str): - if name == "rl": - game_name = "Rocket League" - query = fl_g.session.query(db.RocketLeague).join(db.Steam).order_by(db.RocketLeague.solo_std_rank).all() - elif name == "dota": - game_name = "Dota 2" - query = fl_g.session.query(db.Dota).join(db.Steam).order_by(db.Dota.rank_tier.desc().nullslast(), - db.Dota.wins.desc()).all() - elif name == "lol": - game_name = "League of Legends" - query = fl_g.session.query(db.LeagueOfLegends).order_by(db.LeagueOfLegends.solo_division.desc().nullslast(), - db.LeagueOfLegends.solo_rank, - db.LeagueOfLegends.flex_division.desc().nullslast(), - db.LeagueOfLegends.flex_rank, - db.LeagueOfLegends.twtr_division.desc().nullslast(), - db.LeagueOfLegends.twtr_rank, - db.LeagueOfLegends.level).all() - elif name == "osu": - game_name = "osu!" - query = fl_g.session.query(db.Osu).order_by(db.Osu.mania_pp.desc().nullslast()).all() - elif name == "ow": - game_name = "Overwatch" - query = fl_g.session.query(db.Overwatch).order_by(db.Overwatch.rank.desc().nullslast()).all() - elif name == "steam": - game_name = "Steam" - query = fl_g.session.query(db.Steam).order_by(db.Steam.persona_name).all() - elif name == "ryg": - game_name = "Royalnet" - query = fl_g.session.query(db.Royal).order_by(db.Royal.username).all() - elif name == "tg": - game_name = "Telegram" - query = fl_g.session.query(db.Telegram).order_by(db.Telegram.telegram_id).all() - elif name == "discord": - game_name = "Discord" - query = [dict(row) for row in fl_g.session.execute(query_discord_music.all_query)] - elif name == "halloween2018": - game_name = "Rituale di Halloween" - query = fl_g.session.query(db.Halloween).all() - elif name == "terraria13": - game_name = "Terraria 13" - query = fl_g.session.query(db.Terraria13).all() + for game in db.mini_list: + if game._mini_name == name: + query = game.mini_get_all(fl_g.session) + break else: abort(404) return - return render_template("game.html", minis=query, game_name=game_name, game_short_name=name) + return render_template("game.html", mini_type=game, mini_data=query) @app.route("/wiki/", methods=["GET", "POST"])