1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00
royalnet/royalpack/halloween2020/check.py

136 lines
4.4 KiB
Python
Raw Normal View History

2020-09-22 17:42:57 +00:00
from typing import *
import abc
import aiohttp
2020-09-24 01:20:35 +00:00
import logging
import royalnet.commands as rc
2020-09-22 17:42:57 +00:00
if TYPE_CHECKING:
from .trionfistatus import TrionfiStatus
2020-09-24 01:20:35 +00:00
log = logging.getLogger(__name__)
2020-09-22 17:42:57 +00:00
__all__ = [
"Check",
2020-09-24 01:20:35 +00:00
"NullCheck",
2020-09-22 17:42:57 +00:00
"CheckPlayedSteamGame",
"CheckAchievementSteamGame",
]
class Check(metaclass=abc.ABCMeta):
@abc.abstractmethod
2020-09-24 01:20:35 +00:00
async def check(self, status: "TrionfiStatus", key: str) -> bool:
2020-09-22 17:42:57 +00:00
raise NotImplementedError()
2020-09-22 19:00:49 +00:00
def __or__(self, other: "Check"):
return CheckOr(self, other)
def __and__(self, other):
return CheckAnd(self, other)
2020-09-22 17:42:57 +00:00
2020-09-24 01:20:35 +00:00
class NullCheck(Check):
def __repr__(self):
return f"{self.__class__.__name__}()"
async def check(self, status: "TrionfiStatus", key: str) -> bool:
return False
2020-09-22 17:42:57 +00:00
class CheckPlayedSteamGame(Check):
def __init__(self, appid: int, *args, **kwargs):
super().__init__(*args, **kwargs)
self.appid: int = appid
2020-09-24 01:20:35 +00:00
def __repr__(self):
return f"{self.__class__.__name__}({self.appid=})"
async def check(self, status: "TrionfiStatus", key: str) -> bool:
log.debug(f"{self}")
2020-09-22 17:42:57 +00:00
async with aiohttp.ClientSession() as ah_session:
# noinspection PyProtectedMember
2020-10-15 17:21:43 +00:00
async with ah_session.get("https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v1/",
2020-09-22 17:42:57 +00:00
params={
"steamid": status._steamid,
2020-09-24 01:20:35 +00:00
"key": key,
2020-09-22 17:42:57 +00:00
}) as response:
try:
j = await response.json()
2020-09-24 01:20:35 +00:00
except Exception as e:
log.error(f"{e}")
2020-09-22 17:42:57 +00:00
return False
games = j["response"]["games"]
for game in games:
if game["appid"] != self.appid:
continue
2020-09-22 19:00:49 +00:00
if game["playtime_forever"] >= 30:
2020-09-22 17:42:57 +00:00
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
2020-09-24 01:20:35 +00:00
def __repr__(self):
return f"{self.__class__.__name__}({self.appid=}, {self.achivement_name=})"
async def check(self, status: "TrionfiStatus", key: str) -> bool:
log.debug(f"{self}")
2020-09-22 17:42:57 +00:00
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,
2020-09-24 01:20:35 +00:00
"key": key,
2020-09-22 17:42:57 +00:00
}) as response:
try:
j = await response.json()
2020-09-24 01:20:35 +00:00
except Exception as e:
log.error(f"{e}")
2020-09-22 17:42:57 +00:00
return False
2020-09-23 01:10:27 +00:00
if not j["playerstats"]["success"]:
2020-09-24 01:20:35 +00:00
log.warning(f"{j}")
2020-09-23 01:10:27 +00:00
return False
2020-09-22 17:42:57 +00:00
achievements = j["playerstats"]["achievements"]
for ach in achievements:
if ach["apiname"] != self.achivement_name:
continue
return ach["achieved"] == 1
return False
2020-09-22 19:00:49 +00:00
class CheckOr(Check):
def __init__(self, first: Check, second: Check, *args, **kwargs):
super().__init__(*args, **kwargs)
self.first: Check = first
self.second: Check = second
2020-09-24 01:20:35 +00:00
def __repr__(self):
return f"{self.first} or {self.second}"
async def check(self, status: "TrionfiStatus", key: str) -> bool:
log.debug(f"{self}")
return (await self.first.check(status, key)) or (await self.second.check(status, key))
2020-09-22 19:00:49 +00:00
class CheckAnd(Check):
def __init__(self, first: Check, second: Check, *args, **kwargs):
super().__init__(*args, **kwargs)
self.first: Check = first
self.second: Check = second
2020-09-24 01:20:35 +00:00
def __repr__(self):
return f"{self.first} and {self.second}"
async def check(self, status: "TrionfiStatus", key: str) -> bool:
log.debug(f"{self}")
return (await self.first.check(status, key)) and (await self.second.check(status, key))