Schedule ok !

This commit is contained in:
Pascal P. 2026-08-06 23:03:56 +02:00
parent 3ef295339a
commit 0086bb57d8
2 changed files with 46 additions and 28 deletions

View File

@ -1,7 +1,7 @@
TODO:
- [ ] Schedule opening
- [x] Schedule opening
- [ ] Notification
- [ ] Complete API (for selling)
- [ ] Add a scoring for cards pulled
- [ ] Multiusers
- [ ] Multiusers

View File

@ -4,19 +4,23 @@ from loguru import logger
from pathlib import Path
# from sqlalchemy import create_engine
from .lib.utils import EveryRunRotator, HumanReadable as hr
from .services.wiki_masters import WikiMastersClient
from .lib.utils import EveryRunRotator
from .services.wiki_masters import WikiMastersClient, UserSession
from dotenv import load_dotenv
from datetime import datetime, timedelta, timezone
from apscheduler.schedulers.blocking import BlockingScheduler
class App:
def __init__(self):
self.engine = None
self.users: dict[str, UserSession] = {}
self.scheduler = BlockingScheduler()
self.setup()
def setup(self):
load_dotenv()
logs_folder = Path(__file__).parents[1].joinpath("logs")
logs_folder.mkdir(exist_ok=True)
@ -27,7 +31,9 @@ class App:
)
logger.add(
logs_folder.joinpath("app.log.json"),
format="",
serialize=True,
enqueue=True,
rotation=EveryRunRotator(),
retention=1,
compression="gz",
@ -38,46 +44,58 @@ class App:
# self.engine = create_engine(f"sqlite+pysqlite:///{db_folder}/app.db", echo=True)
def start(self):
logger.info("Starting the bot...")
load_dotenv()
wm_client = WikiMastersClient()
user = wm_client.login(
environ.get("ACCOUNT_EMAIL"), environ.get("ACCOUNT_PASSWORD")
)
def check_and_open(self, username: str):
user = self.users.get(username)
if not user:
raise Exception(f"User {username} not found")
d = user.sync()
logger.debug("sync user {}", d.username)
packs_remaining = d.packs_remaining
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 - (
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
+ WikiMastersClient.ONE_PACK_TIME * (9 - packs_remaining)
- timedelta(seconds=10)
)
logger.info(
"Next pack is in {}",
hr.seconds_to_human(next_pack_in.total_seconds()),
"Schedule for {}",
next_run,
)
logger.debug(
"We should schedule in {}", hr.seconds_to_human(next_run_in.total_seconds())
self.scheduler.add_job(
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():
app = App()