implemented simple queue online matchmaking

This commit is contained in:
UncleJim 2023-11-17 03:36:14 +00:00
parent 9c40bad89c
commit 9605c13e8e

View File

@ -8,58 +8,82 @@ from core import CoreConfig
from titles.idac.season2 import IDACBase from titles.idac.season2 import IDACBase
from titles.idac.config import IDACConfig from titles.idac.config import IDACConfig
from random import randint
class IDACMatching(resource.Resource): class IDACMatching(resource.Resource):
isLeaf = True isLeaf = True
SessionQueue = {}
Rooms = {}
def __init__(self, cfg: CoreConfig, game_cfg: IDACConfig) -> None: def __init__(self, cfg: CoreConfig, game_cfg: IDACConfig) -> None:
self.core_config = cfg self.core_config = cfg
self.game_config = game_cfg self.game_config = game_cfg
self.base = IDACBase(cfg, game_cfg) self.base = IDACBase(cfg, game_cfg)
self.logger = logging.getLogger("idac") self.logger = logging.getLogger("idac")
self.queue = 0 def getMatchingState(self, machineSerial): #We use official state code here
if len(self.SessionQueue) == 1:
def get_matching_state(self): self.logger.info(f"IDAC Matching queued player {machineSerial}: empty dict, returned by default")
if self.queue >= 1: return self.SessionQueue[machineSerial]
self.queue -= 1 elif self.SessionQueue[machineSerial] == 0:
return 0 self.logger.info(f"IDAC Matching queued player {machineSerial}: matched player, returned by default")
return self.SessionQueue[machineSerial]
else: else:
return 1 for sessionID in self.SessionQueue.keys():
if sessionID == machineSerial:
continue
if self.SessionQueue[sessionID] == 1:
#uncomment these to process into actual game
#self.SessionQueue[machineSerial] = 0
#self.SessionQueue[sessionID] = 0
self.joinRoom(machineSerial, sessionID)
self.logger.info(f"IDAC Matching queued player {machineSerial}: rival {sessionID} found!! return matched state")
return self.SessionQueue[machineSerial]
self.logger.info(f"IDAC Matching queued player {machineSerial}: cannot find any rival, returned by default")
return self.SessionQueue[machineSerial]
def joinRoom(self, machineSerial, sessionID): #Random room name, It should be handled by game itself in later process
roomName = "INDTA-Zenkoku-Room" #+randint(1, 1001)
self.Rooms[machineSerial] = roomName
self.Rooms[sessionID] = roomName
def render_POST(self, req) -> bytes: def render_POST(self, req) -> bytes:
url = req.uri.decode() url = req.uri.decode()
req_data = json.loads(req.content.getvalue().decode()) req_data = json.loads(req.content.getvalue().decode())
header_application = self.decode_header(req.getAllHeaders()) header_application = self.decode_header(req.getAllHeaders())
user_id = int(header_application["session"]) machineSerial = header_application["a_serial"]
# self.getMatchingStatus(user_id)
self.logger.info( self.logger.info(
f"IDAC Matching request from {req.getClientIP()}: {url} - {req_data}" f"IDAC Matching request from {req.getClientIP()}: {url} - {req_data}"
) )
resp = {"status_code": "0"}
if url == "/regist": if url == "/regist":
self.queue = self.queue + 1 self.SessionQueue[machineSerial] = 1
self.logger.info(f"IDAC Matching registed player {machineSerial}")
return json.dumps({"status_code": "0"}, ensure_ascii=False).encode("utf-8")
elif url == "/status": elif url == "/status":
if req_data.get("cancel_flag"): if req_data.get('cancel_flag'):
self.queue = self.queue - 1 if machineSerial in self.SessionQueue:
self.logger.info( self.SessionQueue.pop(machineSerial)
f"IDAC Matching endpoint {req.getClientIP()} had quited" self.logger.info(f"IDAC Matching endpoint {req.getClientIP()} had quited")
) return json.dumps({"status_code": "0", "host": "", "port": self.game_config.server.matching_p2p, "room_name": self.Rooms[machineSerial], "state": 1}, ensure_ascii=False).encode("utf-8")
if machineSerial not in self.Rooms.keys():
self.Rooms[machineSerial] = "None"
return json.dumps({"status_code": "0", "host": self.game_config.server.matching_host, "port": self.game_config.server.matching_p2p, "room_name": self.Rooms[machineSerial], "state": self.getMatchingState(machineSerial)}, ensure_ascii=False).encode("utf-8")
resp = { # resp = {
"status_code": "0", # "status_code": "0",
# Only IPv4 is supported # # Only IPv4 is supported
"host": self.game_config.server.matching_host, # "host": self.game_config.server.matching_host,
"port": self.game_config.server.matching_p2p, # "port": self.game_config.server.matching_p2p,
"room_name": "INDTA", # "room_name": "INDTA",
"state": 1, # "state": self.get_matching_state(),
} # }
#
self.logger.debug(f"Response {resp}") #self.logger.debug(f"Response {resp}")
return json.dumps(resp, ensure_ascii=False).encode("utf-8") #return json.dumps(resp, ensure_ascii=False).encode("utf-8")
def decode_header(self, data: Dict) -> Dict: def decode_header(self, data: Dict) -> Dict:
app: str = data[b"application"].decode() app: str = data[b"application"].decode()