2023-11-09 03:17:48 +01:00
|
|
|
from typing import Tuple, List, Dict
|
2023-02-17 08:09:26 +01:00
|
|
|
from twisted.web.http import Request
|
2023-03-12 05:42:12 +01:00
|
|
|
from twisted.web import resource
|
2023-06-25 07:09:23 +02:00
|
|
|
from twisted.internet import reactor
|
2023-03-12 05:42:12 +01:00
|
|
|
import json, ast
|
|
|
|
from datetime import datetime
|
2023-02-17 08:09:26 +01:00
|
|
|
import yaml
|
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-03-12 05:42:12 +01:00
|
|
|
import inflection
|
2023-02-27 22:51:17 +01:00
|
|
|
from os import path
|
2023-02-28 06:41:32 +01:00
|
|
|
from google.protobuf.message import DecodeError
|
2023-02-17 08:09:26 +01:00
|
|
|
|
2023-03-12 07:00:51 +01:00
|
|
|
from core import CoreConfig, Utils
|
2023-11-09 03:17:48 +01:00
|
|
|
from core.title import BaseServlet
|
2023-06-23 06:30:25 +02:00
|
|
|
from .config import PokkenConfig
|
|
|
|
from .base import PokkenBase
|
|
|
|
from .const import PokkenConstants
|
|
|
|
from .proto import jackal_pb2
|
2023-06-25 07:09:23 +02:00
|
|
|
from .services import PokkenAdmissionFactory
|
2023-02-17 08:09:26 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
class PokkenServlet(BaseServlet):
|
2023-02-17 08:09:26 +01:00
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
2023-11-09 03:17:48 +01:00
|
|
|
super().__init__(core_cfg, cfg_dir)
|
2023-02-17 08:09:26 +01:00
|
|
|
self.config_dir = cfg_dir
|
|
|
|
self.game_cfg = PokkenConfig()
|
2023-03-05 03:36:15 +01:00
|
|
|
if path.exists(f"{cfg_dir}/pokken.yaml"):
|
|
|
|
self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/pokken.yaml")))
|
2023-02-17 08:09:26 +01:00
|
|
|
|
|
|
|
self.logger = logging.getLogger("pokken")
|
|
|
|
if not hasattr(self.logger, "inited"):
|
|
|
|
log_fmt_str = "[%(asctime)s] Pokken | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
2023-03-09 17:38:58 +01:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.core_cfg.server.log_dir, "pokken"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-17 08:09:26 +01:00
|
|
|
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 08:09:26 +01:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 08:09:26 +01:00
|
|
|
self.logger.setLevel(self.game_cfg.server.loglevel)
|
2023-03-09 17:38:58 +01:00
|
|
|
coloredlogs.install(
|
|
|
|
level=self.game_cfg.server.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
2023-02-17 08:09:26 +01:00
|
|
|
self.logger.inited = True
|
|
|
|
|
|
|
|
self.base = PokkenBase(core_cfg, self.game_cfg)
|
2023-03-05 03:27:52 +01:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-09 03:17:48 +01:00
|
|
|
def is_game_enabled(cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str) -> bool:
|
2023-03-05 03:27:52 +01:00
|
|
|
game_cfg = PokkenConfig()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 03:27:52 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
2023-11-09 03:17:48 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_endpoint_matchers(self) -> Tuple[List[Tuple[str, str, Dict]], List[Tuple[str, str, Dict]]]:
|
2023-03-09 17:38:58 +01:00
|
|
|
return (
|
2023-11-09 03:17:48 +01:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
("render_POST", "/pokken/", {}),
|
|
|
|
("handle_matching", "/pokken/matching", {}),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_allnet_info(self, game_code: str, game_ver: int, keychip: str) -> Tuple[str, str]:
|
|
|
|
if self.game_cfg.ports.game != 443:
|
|
|
|
return (
|
|
|
|
f"https://{self.game_cfg.server.hostname}:{self.game_cfg.ports.game}/pokken/",
|
|
|
|
f"{self.game_cfg.server.hostname}/pokken/",
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
f"https://{self.game_cfg.server.hostname}/pokken/",
|
|
|
|
f"{self.game_cfg.server.hostname}/pokken/",
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-03-05 04:46:26 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
def get_mucha_info(self, core_cfg: CoreConfig, cfg_dir: str) -> Tuple[bool, str]:
|
2023-03-05 04:46:26 +01:00
|
|
|
game_cfg = PokkenConfig()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-05 04:46:26 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 04:46:26 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
2023-03-09 19:46:28 +01:00
|
|
|
return (False, "")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-04-12 08:34:29 +02:00
|
|
|
return (True, "PKF1")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-12 02:17:05 +01:00
|
|
|
def setup(self) -> None:
|
2023-06-25 07:09:23 +02:00
|
|
|
if self.game_cfg.server.enable_matching:
|
|
|
|
reactor.listenTCP(
|
|
|
|
self.game_cfg.ports.admission, PokkenAdmissionFactory(self.core_cfg, self.game_cfg)
|
|
|
|
)
|
2023-02-17 08:09:26 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
def render_POST(self, request: Request, game_code: str, matchers: Dict) -> bytes:
|
2023-02-28 06:41:32 +01:00
|
|
|
content = request.content.getvalue()
|
|
|
|
if content == b"":
|
|
|
|
self.logger.info("Empty request")
|
|
|
|
return b""
|
2023-02-17 08:09:26 +01:00
|
|
|
|
|
|
|
pokken_request = jackal_pb2.Request()
|
2023-02-28 06:41:32 +01:00
|
|
|
try:
|
|
|
|
pokken_request.ParseFromString(content)
|
|
|
|
except DecodeError as e:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(f"{e} {content}")
|
2023-02-28 06:41:32 +01:00
|
|
|
return b""
|
|
|
|
|
2023-03-11 02:31:29 +01:00
|
|
|
endpoint = jackal_pb2.MessageType.DESCRIPTOR.values_by_number[
|
|
|
|
pokken_request.type
|
|
|
|
].name.lower()
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-04-12 08:34:29 +02:00
|
|
|
self.logger.debug(pokken_request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 08:09:26 +01:00
|
|
|
handler = getattr(self.base, f"handle_{endpoint}", None)
|
|
|
|
if handler is None:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(f"No handler found for message type {endpoint}")
|
2023-02-17 08:09:26 +01:00
|
|
|
return self.base.handle_noop(pokken_request)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-12 21:30:57 +01:00
|
|
|
self.logger.info(f"{endpoint} request from {Utils.get_ip_addr(request)}")
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-12 02:02:58 +01:00
|
|
|
ret = handler(pokken_request)
|
|
|
|
return ret
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
def handle_matching(self, request: Request, game_code: str, matchers: Dict) -> bytes:
|
2023-06-25 07:09:23 +02:00
|
|
|
if not self.game_cfg.server.enable_matching:
|
|
|
|
return b""
|
|
|
|
|
2023-03-12 05:42:12 +01:00
|
|
|
content = request.content.getvalue()
|
2023-03-12 07:00:51 +01:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
2023-03-12 05:42:12 +01:00
|
|
|
|
|
|
|
if content is None or content == b"":
|
|
|
|
self.logger.info("Empty matching request")
|
|
|
|
return json.dumps(self.base.handle_matching_noop()).encode()
|
|
|
|
|
2023-04-24 03:04:52 +02:00
|
|
|
json_content = ast.literal_eval(
|
|
|
|
content.decode()
|
|
|
|
.replace("null", "None")
|
|
|
|
.replace("true", "True")
|
|
|
|
.replace("false", "False")
|
|
|
|
)
|
2023-03-12 05:42:12 +01:00
|
|
|
self.logger.info(f"Matching {json_content['call']} request")
|
|
|
|
self.logger.debug(json_content)
|
|
|
|
|
2023-04-24 03:04:52 +02:00
|
|
|
handler = getattr(
|
|
|
|
self.base,
|
|
|
|
f"handle_matching_{inflection.underscore(json_content['call'])}",
|
|
|
|
None,
|
|
|
|
)
|
2023-03-12 05:42:12 +01:00
|
|
|
if handler is None:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(
|
2023-04-24 03:04:52 +02:00
|
|
|
f"No handler found for message type {json_content['call']}"
|
|
|
|
)
|
2023-03-12 05:42:12 +01:00
|
|
|
return json.dumps(self.base.handle_matching_noop()).encode()
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-12 05:42:12 +01:00
|
|
|
ret = handler(json_content, client_ip)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-12 05:42:12 +01:00
|
|
|
if ret is None:
|
2023-04-24 03:04:52 +02:00
|
|
|
ret = {}
|
2023-03-12 05:42:12 +01:00
|
|
|
if "result" not in ret:
|
|
|
|
ret["result"] = "true"
|
|
|
|
if "data" not in ret:
|
|
|
|
ret["data"] = {}
|
|
|
|
if "timestamp" not in ret:
|
|
|
|
ret["timestamp"] = int(datetime.now().timestamp() * 1000)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-12 05:42:12 +01:00
|
|
|
self.logger.debug(f"Response {ret}")
|
|
|
|
|
|
|
|
return json.dumps(ret).encode()
|