From 3f0b4120242352f61b82799454d548b2ca934494 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 22 Sep 2020 19:42:57 +0200 Subject: [PATCH] Make more progress --- royalpack/__init__.py | 0 royalpack/commands/trionfireali.py | 5 +- royalpack/halloween2020/__init__.py | 0 royalpack/halloween2020/check.py | 76 ++++++++++++ .../trionfilist.py} | 108 +++++++++--------- .../trionfistatus.py} | 6 +- royalpack/halloween2020/trionfoinfo.py | 44 +++++++ royalpack/tables/__init__.py | 2 +- royalpack/tables/steam.py | 2 +- 9 files changed, 184 insertions(+), 59 deletions(-) create mode 100644 royalpack/__init__.py create mode 100644 royalpack/halloween2020/__init__.py create mode 100644 royalpack/halloween2020/check.py rename royalpack/{types/halloween2020tarots.py => halloween2020/trionfilist.py} (53%) rename royalpack/{tables/halloween2020.py => halloween2020/trionfistatus.py} (96%) create mode 100644 royalpack/halloween2020/trionfoinfo.py diff --git a/royalpack/__init__.py b/royalpack/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/royalpack/commands/trionfireali.py b/royalpack/commands/trionfireali.py index 384861ab..f473c29d 100644 --- a/royalpack/commands/trionfireali.py +++ b/royalpack/commands/trionfireali.py @@ -1,8 +1,9 @@ from typing import * import logging -import aiohttp +import steam.webapi import royalnet.commands as rc import royalnet.utils as ru +import requests from royalnet.backpack import tables as rbt from .abstract.linker import LinkerCommand @@ -12,6 +13,8 @@ from ..types import DotaRank log = logging.getLogger(__name__) + + class TrionfirealiCommand(LinkerCommand): name: str = "trionfireali" diff --git a/royalpack/halloween2020/__init__.py b/royalpack/halloween2020/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/royalpack/halloween2020/check.py b/royalpack/halloween2020/check.py new file mode 100644 index 00000000..11830f96 --- /dev/null +++ b/royalpack/halloween2020/check.py @@ -0,0 +1,76 @@ +from typing import * +import abc +import aiohttp + +if TYPE_CHECKING: + from .trionfistatus import TrionfiStatus + + +__all__ = [ + "Check", + "CheckPlayedSteamGame", + "CheckAchievementSteamGame", +] + + +class Check(metaclass=abc.ABCMeta): + @abc.abstractmethod + async def check(self, status: "TrionfiStatus") -> bool: + raise NotImplementedError() + + +class CheckPlayedSteamGame(Check): + def __init__(self, appid: int, *args, **kwargs): + super().__init__(*args, **kwargs) + self.appid: int = appid + + async def check(self, status: "TrionfiStatus") -> bool: + async with aiohttp.ClientSession() as ah_session: + # noinspection PyProtectedMember + async with ah_session.get("https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/", + params={ + "steamid": status._steamid, + "include_appinfo": True, + "include_played_free_games": True, + "include_free_sub": True, + "appids_filter": self.appid, + }) as response: + try: + j = await response.json() + except Exception: + return False + + games = j["response"]["games"] + for game in games: + if game["appid"] != self.appid: + continue + if game["playtime_forever"] >= 1: + return True + return False + + +class CheckAchievementSteamGame(Check): + def __init__(self, appid: int, achievement_name: str, *args, **kwargs): + super().__init__(*args, **kwargs) + self.appid: int = appid + self.achivement_name: str = achievement_name + + async def check(self, status: "TrionfiStatus") -> bool: + async with aiohttp.ClientSession() as ah_session: + # noinspection PyProtectedMember + async with ah_session.get("http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v1/", + params={ + "steamid": status._steamid, + "appid": self.appid, + }) as response: + try: + j = await response.json() + except Exception: + return False + + achievements = j["playerstats"]["achievements"] + for ach in achievements: + if ach["apiname"] != self.achivement_name: + continue + return ach["achieved"] == 1 + return False diff --git a/royalpack/types/halloween2020tarots.py b/royalpack/halloween2020/trionfilist.py similarity index 53% rename from royalpack/types/halloween2020tarots.py rename to royalpack/halloween2020/trionfilist.py index c3504972..851b2a62 100644 --- a/royalpack/types/halloween2020tarots.py +++ b/royalpack/halloween2020/trionfilist.py @@ -1,168 +1,170 @@ -from typing import * +from .trionfoinfo import TrionfoInfo +from .check import * - -class Halloween2020Tarot: - def __init__(self, - variable: str, - title: str, - roman: str, - name: str, - objective: str, - puzzle: str, - check: Optional[Callable[..., Awaitable[...]]]): - self.variable: str = variable - self.title: str = title - self.roman: str = roman - self.name: str = name - self.objective: str = objective - self.puzzle: str = puzzle - self.check: Optional[Callable[..., Awaitable[...]]] = check - - -halloween2020tarots = ( - Halloween2020Tarot( +trionfilist = ( + TrionfoInfo( variable="zero", title="o", roman="0", name="Il Folle", + puzzle="IL DESTINO TI ATTENDE", objective="Partecipa ai Trionfi Reali.", - puzzle="Scopri nuovi indizi ottenendo dei Trionfi!", check=None, ), - Halloween2020Tarot( + TrionfoInfo( variable="i", title="i", roman="I", name="Il Mago", - objective="Trova una magia che possa fare almeno 250 danni.", - puzzle="L'ultimo giorno del decimo mese...", + puzzle="L'ULTIMO GIORNO", + objective="Trova una /spell che possa fare almeno 250 danni.", check=None, ), - Halloween2020Tarot( + TrionfoInfo( variable="ii", title="ii", roman="II", - name="La Papessa", + name="La Sacerdotessa", + puzzle="DEL DECIMO MESE", + objective="Gioca almeno un'ora a [url=https://store.steampowered.com/app/881100]Noita[/url].", + check=CheckPlayedSteamGame(881100), ), - Halloween2020Tarot( + TrionfoInfo( variable="iii", title="iii", roman="III", name="L'Imperatrice", + puzzle="NON IMPEGNARTI", + objective="Gioca a [url=https://store.steampowered.com/app/245170]Skullgirls[/url].", + check=CheckPlayedSteamGame(245170), ), - Halloween2020Tarot( + TrionfoInfo( variable="iv", title="iv", roman="IV", name="L'Imperatore", + puzzle="ESEGUI GLI ORDINI", + objective="Vinci una partita su [url=https://store.steampowered.com/app/611500]Quake Champions[/url].", + check=CheckAchievementSteamGame(611500, "qc_victory") ), - Halloween2020Tarot( + TrionfoInfo( variable="v", title="v", roman="V", name="Il Papa", + puzzle="ALLA SEDICESIMA ORA", ), - Halloween2020Tarot( + TrionfoInfo( variable="vi", title="vi", roman="VI", name="Gli Amanti", - objective="Completa [url=https://store.steampowered.com/app/698780]Doki Doki " - "Literature Club[/url].", + puzzle="ANCORA DIECI MINUTI", + objective="Finisci l'Atto 3 di [url=https://store.steampowered.com/app/698780]Doki Doki Literature Club[" + "/url].", + check=CheckPlayedSteamGame(698780), ), - Halloween2020Tarot( + TrionfoInfo( variable="vii", title="vii", roman="VII", name="Il Carro", + puzzle="SOPRA UN CARRO", ), - Halloween2020Tarot( + TrionfoInfo( variable="viii", title="viii", roman="VIII", name="La Giustizia", + objective="Porta la giustizia dalla tua parte su [url=https://store.steampowered.com/app/1289310]Helltaker[" + "/url].", + check=CheckAchievementSteamGame(1289310, "achiev_05"), ), - Halloween2020Tarot( + TrionfoInfo( variable="ix", title="ix", roman="IX", name="L'Eremita", + puzzle="SEGRETAMENTE", ), - Halloween2020Tarot( + TrionfoInfo( variable="x", title="x", roman="X", name="La Fortuna", ), - Halloween2020Tarot( + TrionfoInfo( variable="xi", title="xi", roman="XI", name="La Forza", - objective="Gioca 3 partite Ranked 1v1 su " - "[url=https://steamcommunity.com/id/steffo1999/stats/appid/291550/achievements]Brawlhalla[/url]." ), - Halloween2020Tarot( + TrionfoInfo( variable="xii", title="xii", roman="XII", name="L'Appeso", ), - Halloween2020Tarot( + TrionfoInfo( variable="xiii", title="xiii", roman="XIII", name="La Morte", + objective="Raggiungi la Tenuta dell'Antenato su [url=https://store.steampowered.com/app/262060]Darkest Dungeon[" + "/url].", + check=CheckAchievementSteamGame(262060, "welcome_home"), ), - Halloween2020Tarot( + TrionfoInfo( variable="xiv", title="xiv", roman="XIV", name="La Temperanza", ), - Halloween2020Tarot( + TrionfoInfo( variable="xv", title="xv", roman="XV", name="Il Diavolo", ), - Halloween2020Tarot( + TrionfoInfo( variable="xvi", title="xvi", roman="XVI", name="La Torre", ), - Halloween2020Tarot( + TrionfoInfo( variable="xvii", title="xvii", roman="XVII", name="Le Stelle", ), - Halloween2020Tarot( + TrionfoInfo( variable="xviii", title="xviii", roman="XVIII", name="La Luna", + objective="Gioca a [url=https://store.steampowered.com/app/388880]Oxenfree[/url].", + check=CheckPlayedSteamGame(388880), ), - Halloween2020Tarot( + TrionfoInfo( variable="xix", title="xix", roman="XIX", name="Il Sole", ), - Halloween2020Tarot( + TrionfoInfo( variable="xx", title="xx", roman="XX", name="Il Giudizio", ), - Halloween2020Tarot( + TrionfoInfo( variable="xxi", title="xxi", roman="XII", name="Il Mondo", - objective="Risolvi il mistero dei Trionfi Reali.", puzzle="""44°35'45.0"N 11°02'58.9"E""", + objective="Risolvi il mistero dei Trionfi Reali.", check=None, ), -) +) \ No newline at end of file diff --git a/royalpack/tables/halloween2020.py b/royalpack/halloween2020/trionfistatus.py similarity index 96% rename from royalpack/tables/halloween2020.py rename to royalpack/halloween2020/trionfistatus.py index eb80a020..1c0e998f 100644 --- a/royalpack/tables/halloween2020.py +++ b/royalpack/halloween2020/trionfistatus.py @@ -3,8 +3,8 @@ from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declared_attr -class Halloween2020: - __tablename__ = "halloween2020" +class TrionfiStatus: + __tablename__ = "trionfistatus" @declared_attr def _steamid(self): @@ -12,7 +12,7 @@ class Halloween2020: @declared_attr def steam(self): - return relationship("Steam", backref=backref("halloween2020", uselist=False)) + return relationship("Steam", backref=backref("trionfistatus", uselist=False)) @declared_attr def zero(self): diff --git a/royalpack/halloween2020/trionfoinfo.py b/royalpack/halloween2020/trionfoinfo.py new file mode 100644 index 00000000..61c63cd0 --- /dev/null +++ b/royalpack/halloween2020/trionfoinfo.py @@ -0,0 +1,44 @@ +import royalnet.utils as ru +from typing import * + +if TYPE_CHECKING: + from .check import Check + + +class TrionfoInfo: + def __init__(self, + variable: str, + title: str, + roman: str, + name: str, + objective: str, + puzzle: str, + check: Optional["Check"]): + self.variable: str = variable + self.title: str = title + self.roman: str = roman + self.name: str = name + self.objective: str = objective + self.puzzle: str = puzzle + self.check: Optional["Check"] = check + + def json_anonymous(self) -> ru.JSON: + return { + "variable": self.variable, + "title": self.title, + "roman": self.roman, + "name": self.name, + "objective": self.objective, + } + + def json_user(self, obj) -> ru.JSON: + status = obj.__getattribute__(self.variable) + return { + "variable": self.variable, + "title": self.title, + "roman": self.roman, + "name": self.name, + "objective": self.objective, + "puzzle": self.puzzle if status is not None else None, + "completed_on": status + } diff --git a/royalpack/tables/__init__.py b/royalpack/tables/__init__.py index 3d9f35f6..26eb3e0d 100644 --- a/royalpack/tables/__init__.py +++ b/royalpack/tables/__init__.py @@ -19,7 +19,7 @@ from .mmresponse import MMResponse from .cvstats import Cvstats from .treasure import Treasure from .osu import Osu -from .halloween2020 import Halloween2020 +from ..halloween2020.trionfistatus import Halloween2020 # Enter the tables of your Pack here! available_tables = [ diff --git a/royalpack/tables/steam.py b/royalpack/tables/steam.py index 8b318002..f27ad88d 100644 --- a/royalpack/tables/steam.py +++ b/royalpack/tables/steam.py @@ -77,7 +77,7 @@ class Steam: "dota": self.dota.json() if self.dota is not None else None, "brawlhalla": self.brawlhalla.json() if self.brawlhalla is not None else None, - "halloween2020": self.halloween2020.json() if self.halloween2020 is not None else None, + "trionfistatus": self.trionfistatus.json() if self.trionfistatus is not None else None, } def __repr__(self):