Schedule ok !
This commit is contained in:
parent
3ef295339a
commit
0086bb57d8
|
|
@ -1,6 +1,6 @@
|
||||||
TODO:
|
TODO:
|
||||||
|
|
||||||
- [ ] Schedule opening
|
- [x] Schedule opening
|
||||||
- [ ] Notification
|
- [ ] Notification
|
||||||
- [ ] Complete API (for selling)
|
- [ ] Complete API (for selling)
|
||||||
- [ ] Add a scoring for cards pulled
|
- [ ] Add a scoring for cards pulled
|
||||||
|
|
|
||||||
70
src/app.py
70
src/app.py
|
|
@ -4,19 +4,23 @@ from loguru import logger
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# from sqlalchemy import create_engine
|
# from sqlalchemy import create_engine
|
||||||
from .lib.utils import EveryRunRotator, HumanReadable as hr
|
from .lib.utils import EveryRunRotator
|
||||||
from .services.wiki_masters import WikiMastersClient
|
from .services.wiki_masters import WikiMastersClient, UserSession
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.engine = None
|
self.engine = None
|
||||||
|
self.users: dict[str, UserSession] = {}
|
||||||
|
self.scheduler = BlockingScheduler()
|
||||||
self.setup()
|
self.setup()
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
load_dotenv()
|
||||||
logs_folder = Path(__file__).parents[1].joinpath("logs")
|
logs_folder = Path(__file__).parents[1].joinpath("logs")
|
||||||
|
|
||||||
logs_folder.mkdir(exist_ok=True)
|
logs_folder.mkdir(exist_ok=True)
|
||||||
|
|
@ -27,7 +31,9 @@ class App:
|
||||||
)
|
)
|
||||||
logger.add(
|
logger.add(
|
||||||
logs_folder.joinpath("app.log.json"),
|
logs_folder.joinpath("app.log.json"),
|
||||||
|
format="",
|
||||||
serialize=True,
|
serialize=True,
|
||||||
|
enqueue=True,
|
||||||
rotation=EveryRunRotator(),
|
rotation=EveryRunRotator(),
|
||||||
retention=1,
|
retention=1,
|
||||||
compression="gz",
|
compression="gz",
|
||||||
|
|
@ -38,46 +44,58 @@ class App:
|
||||||
|
|
||||||
# self.engine = create_engine(f"sqlite+pysqlite:///{db_folder}/app.db", echo=True)
|
# self.engine = create_engine(f"sqlite+pysqlite:///{db_folder}/app.db", echo=True)
|
||||||
|
|
||||||
def start(self):
|
def check_and_open(self, username: str):
|
||||||
logger.info("Starting the bot...")
|
user = self.users.get(username)
|
||||||
|
if not user:
|
||||||
load_dotenv()
|
raise Exception(f"User {username} not found")
|
||||||
|
|
||||||
wm_client = WikiMastersClient()
|
|
||||||
user = wm_client.login(
|
|
||||||
environ.get("ACCOUNT_EMAIL"), environ.get("ACCOUNT_PASSWORD")
|
|
||||||
)
|
|
||||||
|
|
||||||
d = user.sync()
|
d = user.sync()
|
||||||
logger.debug("sync user {}", d.username)
|
logger.debug("sync user {}", d.username)
|
||||||
packs_remaining = d.packs_remaining
|
packs_remaining = d.packs_remaining
|
||||||
packs_last_regen_at = d.packs_last_regen_at
|
packs_last_regen_at = d.packs_last_regen_at
|
||||||
|
|
||||||
if packs_remaining == 10:
|
|
||||||
packs_remaining, packs_last_regen_at, cards = wm_client.open_pack(
|
|
||||||
user.auth_cookies
|
|
||||||
)
|
|
||||||
for card in cards:
|
|
||||||
logger.info("Pulled [{}] {} (uid={})", card.rarity, card.name, card._id)
|
|
||||||
|
|
||||||
# Does this math still works when we are opening the 9th pack ?
|
|
||||||
next_pack_in = WikiMastersClient.ONE_PACK_TIME - (
|
next_pack_in = WikiMastersClient.ONE_PACK_TIME - (
|
||||||
datetime.now(tz=timezone.utc) - packs_last_regen_at
|
datetime.now(tz=timezone.utc) - packs_last_regen_at
|
||||||
)
|
)
|
||||||
next_run_in = (
|
|
||||||
|
if packs_remaining == 10 or next_pack_in < timedelta(seconds=20):
|
||||||
|
packs_remaining, packs_last_regen_at, cards = user.pull()
|
||||||
|
for card in cards:
|
||||||
|
logger.info("Pulled [{}] {} (uid={})", card.rarity, card.name, card._id)
|
||||||
|
|
||||||
|
next_pack_in = WikiMastersClient.ONE_PACK_TIME - (
|
||||||
|
datetime.now(tz=timezone.utc) - packs_last_regen_at
|
||||||
|
)
|
||||||
|
next_run = datetime.now() + (
|
||||||
next_pack_in
|
next_pack_in
|
||||||
+ WikiMastersClient.ONE_PACK_TIME * (9 - packs_remaining)
|
+ WikiMastersClient.ONE_PACK_TIME * (9 - packs_remaining)
|
||||||
- timedelta(seconds=10)
|
- timedelta(seconds=10)
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"Next pack is in {}",
|
"Schedule for {}",
|
||||||
hr.seconds_to_human(next_pack_in.total_seconds()),
|
next_run,
|
||||||
)
|
)
|
||||||
logger.debug(
|
self.scheduler.add_job(
|
||||||
"We should schedule in {}", hr.seconds_to_human(next_run_in.total_seconds())
|
self.check_and_open,
|
||||||
|
"date",
|
||||||
|
id=f"schedule_{username}",
|
||||||
|
replace_existing=True,
|
||||||
|
args=[username],
|
||||||
|
run_date=next_run,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
logger.info("Starting the bot...")
|
||||||
|
|
||||||
|
wm_client = WikiMastersClient()
|
||||||
|
self.users["pasterp"] = wm_client.login(
|
||||||
|
environ.get("ACCOUNT_EMAIL"), environ.get("ACCOUNT_PASSWORD")
|
||||||
|
)
|
||||||
|
|
||||||
|
self.check_and_open("pasterp")
|
||||||
|
|
||||||
|
self.scheduler.start()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
app = App()
|
app = App()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue