From b12f61198f75361913e2f88da82816c864b5ac22 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Sat, 4 Mar 2023 22:46:26 -0500 Subject: [PATCH] mucha: fixups --- core/allnet.py | 3 --- core/mucha.py | 35 ++++++++++++++++++++++++++++++----- index.py | 2 +- titles/pokken/index.py | 12 ++++++++++++ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/core/allnet.py b/core/allnet.py index ec39158..a02c902 100644 --- a/core/allnet.py +++ b/core/allnet.py @@ -54,9 +54,6 @@ class AllnetServlet: if enabled: self.uri_registry[code] = (uri, host) - - else: - self.logger.warn("Game {_} has no get_allnet_info method.") self.logger.info(f"Allnet serving {len(self.uri_registry)} games on port {core_cfg.allnet.port}") diff --git a/core/mucha.py b/core/mucha.py index 0848c70..6cabd59 100644 --- a/core/mucha.py +++ b/core/mucha.py @@ -1,4 +1,4 @@ -from typing import Dict, Any, Optional +from typing import Dict, Any, Optional, List import logging, coloredlogs from logging.handlers import TimedRotatingFileHandler from twisted.web import resource @@ -7,10 +7,13 @@ from datetime import datetime import pytz from core.config import CoreConfig +from core.utils import Utils class MuchaServlet: - def __init__(self, cfg: CoreConfig) -> None: + def __init__(self, cfg: CoreConfig, cfg_dir: str) -> None: self.config = cfg + self.config_dir = cfg_dir + self.mucha_registry: List[str] = [] self.logger = logging.getLogger('mucha') log_fmt_str = "[%(asctime)s] Mucha | %(levelname)s | %(message)s" @@ -28,6 +31,16 @@ class MuchaServlet: self.logger.setLevel(logging.INFO) coloredlogs.install(level=logging.INFO, logger=self.logger, fmt=log_fmt_str) + all_titles = Utils.get_all_titles() + + for _, mod in all_titles.items(): + if hasattr(mod, "index") and hasattr(mod.index, "get_mucha_info"): + enabled, game_cd = mod.index.get_mucha_info(self.config, self.config_dir) + if enabled: + self.mucha_registry.append(game_cd) + + self.logger.info(f"Serving {len(self.mucha_registry)} games on port {self.config.mucha.port}") + def handle_boardauth(self, request: Request, _: Dict) -> bytes: req_dict = self.mucha_preprocess(request.content.getvalue()) if req_dict is None: @@ -36,6 +49,13 @@ class MuchaServlet: req = MuchaAuthRequest(req_dict) self.logger.debug(f"Mucha request {vars(req)}") + self.logger.info(f"Boardauth request from {request.getClientAddress().host} for {req.gameVer}") + + if req.gameCd not in self.mucha_registry: + self.logger.warn(f"Unknown gameCd {req.gameCd}") + return b"" + + # TODO: Figure out why the S/N is the way it is. if self.config.server.is_develop: resp = MuchaAuthResponse(mucha_url=f"{self.config.mucha.hostname}:{self.config.mucha.port}") @@ -54,11 +74,16 @@ class MuchaServlet: req = MuchaUpdateRequest(req_dict) self.logger.debug(f"Mucha request {vars(req)}") + self.logger.info(f"Updatecheck request from {request.getClientAddress().host} for {req.gameVer}") + + if req.gameCd not in self.mucha_registry: + self.logger.warn(f"Unknown gameCd {req.gameCd}") + return b"" if self.config.server.is_develop: - resp = MuchaUpdateResponse(mucha_url=f"{self.config.mucha.hostname}:{self.config.mucha.port}") + resp = MuchaUpdateResponse(req.gameVer, f"{self.config.mucha.hostname}:{self.config.mucha.port}") else: - resp = MuchaUpdateResponse(mucha_url=f"{self.config.mucha.hostname}") + resp = MuchaUpdateResponse(req.gameVer, f"{self.config.mucha.hostname}") self.logger.debug(f"Mucha response {vars(resp)}") @@ -94,7 +119,7 @@ class MuchaServlet: class MuchaAuthRequest(): def __init__(self, request: Dict) -> None: self.gameVer = "" if "gameVer" not in request else request["gameVer"] - self.sendDate = "" if "sendDate" not in request else request["sendDate"] + self.sendDate = "" if "sendDate" not in request else datetime.strptime(request["sendDate"], "%Y%m%d") self.serialNum = "" if "serialNum" not in request else request["serialNum"] self.gameCd = "" if "gameCd" not in request else request["gameCd"] self.boardType = "" if "boardType" not in request else request["boardType"] diff --git a/index.py b/index.py index 4c0ece9..f545edb 100644 --- a/index.py +++ b/index.py @@ -23,7 +23,7 @@ class HttpDispatcher(resource.Resource): self.allnet = AllnetServlet(cfg, config_dir) self.title = TitleServlet(cfg, config_dir) - self.mucha = MuchaServlet(cfg) + self.mucha = MuchaServlet(cfg, config_dir) self.map_post.connect('allnet_ping', '/naomitest.html', controller="allnet", action='handle_naomitest', conditions=dict(method=['GET'])) self.map_post.connect('allnet_poweron', '/sys/servlet/PowerOn', controller="allnet", action='handle_poweron', conditions=dict(method=['POST'])) diff --git a/titles/pokken/index.py b/titles/pokken/index.py index a8b13f2..ccf21de 100644 --- a/titles/pokken/index.py +++ b/titles/pokken/index.py @@ -58,6 +58,18 @@ class PokkenServlet(resource.Resource): return (True, f"https://{game_cfg.server.hostname}:{game_cfg.server.port}/{game_code}/$v/", f"{game_cfg.server.hostname}:{game_cfg.server.port}/") return (True, f"https://{game_cfg.server.hostname}/{game_code}/$v/", f"{game_cfg.server.hostname}/") + + @classmethod + def get_mucha_info(cls, core_cfg: CoreConfig, cfg_dir: str) -> Tuple[bool, str, str]: + game_cfg = PokkenConfig() + + if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"): + game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))) + + if not game_cfg.server.enable: + return (False, "", "") + + return (True, "PKFN") def setup(self): """