From e4b7809e34f6db12120cc976672e46c138875339 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Sat, 11 Mar 2023 23:42:12 -0500 Subject: [PATCH] pokken: add matching server skeleton --- example_config/pokken.yaml | 7 +++--- titles/pokken/base.py | 39 +++++++++++++++++++++++++++--- titles/pokken/config.py | 12 +++------- titles/pokken/index.py | 49 +++++++++++++++++++++++++++++++------- 4 files changed, 83 insertions(+), 24 deletions(-) diff --git a/example_config/pokken.yaml b/example_config/pokken.yaml index 48db520..7400060 100644 --- a/example_config/pokken.yaml +++ b/example_config/pokken.yaml @@ -3,7 +3,6 @@ server: enable: True loglevel: "info" port: 9000 - port_matching: 9001 - port_stun: 9002 - port_turn: 9003 - port_admission: 9004 \ No newline at end of file + port_stun: 9001 + port_turn: 9002 + port_admission: 9003 \ No newline at end of file diff --git a/titles/pokken/base.py b/titles/pokken/base.py index ca4f0b1..1fccab8 100644 --- a/titles/pokken/base.py +++ b/titles/pokken/base.py @@ -1,6 +1,6 @@ from datetime import datetime, timedelta import json -from typing import Any +from typing import Any, Dict from core.config import CoreConfig from titles.pokken.config import PokkenConfig @@ -37,8 +37,8 @@ class PokkenBase: biwa_setting = { "MatchingServer": { "host": f"https://{self.game_cfg.server.hostname}", - "port": self.game_cfg.server.port_matching, - "url": "SDAK/100/matching", + "port": self.game_cfg.server.port, + "url": "/SDAK/100/matching", }, "StunServer": { "addr": self.game_cfg.server.hostname, @@ -108,3 +108,36 @@ class PokkenBase: res.load_client_settings.CopyFrom(settings) return res.SerializeToString() + + def handle_load_ranking(self, request: jackal_pb2.CheckDiagnosisRequestData) -> bytes: + res = jackal_pb2.Response() + res.result = 1 + res.type = jackal_pb2.MessageType.LOAD_RANKING + ranking = jackal_pb2.LoadRankingResponseData() + + ranking.ranking_id = 1 + ranking.ranking_start = 0 + ranking.ranking_end = 1 + ranking.event_end = True + ranking.modify_date = int(datetime.now().timestamp() / 1000) + res.load_ranking.CopyFrom(ranking) + + def handle_matching_noop(self, data: Dict = {}, client_ip: str = "127.0.0.1") -> Dict: + return {} + + def handle_matching_start_matching(self, data: Dict = {}, client_ip: str = "127.0.0.1") -> Dict: + return {} + + def handle_matching_is_matching(self, data: Dict = {}, client_ip: str = "127.0.0.1") -> Dict: + """ + "sessionId":"12345678", + "A":{ + "pcb_id": data["data"]["must"]["pcb_id"], + "gip": client_ip + }, + "list":[] + """ + return {} + + def handle_matching_stop_matching(self, data: Dict = {}, client_ip: str = "127.0.0.1") -> Dict: + return {} \ No newline at end of file diff --git a/titles/pokken/config.py b/titles/pokken/config.py index 608bbd7..b53fc86 100644 --- a/titles/pokken/config.py +++ b/titles/pokken/config.py @@ -31,28 +31,22 @@ class PokkenServerConfig: self.__config, "pokken", "server", "port", default=9000 ) - @property - def port_matching(self) -> int: - return CoreConfig.get_config_field( - self.__config, "pokken", "server", "port_matching", default=9001 - ) - @property def port_stun(self) -> int: return CoreConfig.get_config_field( - self.__config, "pokken", "server", "port_stun", default=9002 + self.__config, "pokken", "server", "port_stun", default=9001 ) @property def port_turn(self) -> int: return CoreConfig.get_config_field( - self.__config, "pokken", "server", "port_turn", default=9003 + self.__config, "pokken", "server", "port_turn", default=9002 ) @property def port_admission(self) -> int: return CoreConfig.get_config_field( - self.__config, "pokken", "server", "port_admission", default=9004 + self.__config, "pokken", "server", "port_admission", default=9003 ) class PokkenConfig(dict): diff --git a/titles/pokken/index.py b/titles/pokken/index.py index 8d8ef4c..9bcd90b 100644 --- a/titles/pokken/index.py +++ b/titles/pokken/index.py @@ -1,11 +1,12 @@ from typing import Tuple from twisted.web.http import Request -from twisted.web import resource, server -from twisted.internet import reactor, endpoints +from twisted.web import resource +import json, ast +from datetime import datetime import yaml import logging, coloredlogs from logging.handlers import TimedRotatingFileHandler -from titles.pokken.proto import jackal_pb2 +import inflection from os import path from google.protobuf.message import DecodeError @@ -13,6 +14,7 @@ from core.config import CoreConfig from titles.pokken.config import PokkenConfig from titles.pokken.base import PokkenBase from titles.pokken.const import PokkenConstants +from titles.pokken.proto import jackal_pb2 class PokkenServlet(resource.Resource): @@ -88,15 +90,14 @@ class PokkenServlet(resource.Resource): return (True, "PKF2") def setup(self) -> None: - # TODO: Setup matching, stun, turn and admission servers + # TODO: Setup stun, turn (UDP) and admission (WSS) servers pass def render_POST( self, request: Request, version: int = 0, endpoints: str = "" ) -> bytes: - if endpoints.startswith("/matching"): - self.logger.info("Matching request") - self.logger.debug(request.content) + if endpoints == "matching": + return self.handle_matching(request) content = request.content.getvalue() if content == b"": @@ -122,5 +123,37 @@ class PokkenServlet(resource.Resource): return self.base.handle_noop(pokken_request) ret = handler(pokken_request) - self.logger.debug(f"Response: {ret}") + #self.logger.debug(f"Response: {ret}") return ret + + def handle_matching(self, request: Request) -> bytes: + content = request.content.getvalue() + client_ip = request.getClientAddress().host + + if content is None or content == b"": + self.logger.info("Empty matching request") + return json.dumps(self.base.handle_matching_noop()).encode() + + json_content = ast.literal_eval(content.decode().replace('null', 'None').replace('true', 'True').replace('false', 'False')) + self.logger.info(f"Matching {json_content['call']} request") + self.logger.debug(json_content) + + handler = getattr(self.base, f"handle_matching_{inflection.underscore(json_content['call'])}", None) + if handler is None: + self.logger.warn(f"No handler found for message type {json_content['call']}") + return json.dumps(self.base.handle_matching_noop()).encode() + + ret = handler(json_content, client_ip) + + if ret is None: + ret = {} + 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) + + self.logger.debug(f"Response {ret}") + + return json.dumps(ret).encode()