diff --git a/dndserver/handlers/gatheringhall.py b/dndserver/handlers/gatheringhall.py index 7be2247..6a51566 100644 --- a/dndserver/handlers/gatheringhall.py +++ b/dndserver/handlers/gatheringhall.py @@ -18,7 +18,6 @@ SS2C_GATHERING_HALL_CHANNEL_SELECT_RES, SS2C_GATHERING_HALL_TARGET_EQUIPPED_ITEM_RES, ) -from dndserver.matchmaking import virtualServers channels = {} for i in range(1, 7): @@ -71,11 +70,6 @@ def cleanup(ctx): if current_channel: channels[current_channel]["clients"].remove(ctx) - # for _, servers in virtualServers.items(): - # for server in servers: - # if sessions[ctx.transport] in server.players: - # server.players.remove(sessions[ctx.transport]) - def gathering_hall_channel_exit(ctx, msg): req = SC2S_GATHERING_HALL_CHANNEL_EXIT_REQ() diff --git a/dndserver/handlers/lobby.py b/dndserver/handlers/lobby.py index 4756050..46ec163 100644 --- a/dndserver/handlers/lobby.py +++ b/dndserver/handlers/lobby.py @@ -6,7 +6,6 @@ from dndserver.persistent import parties, sessions, matchmaking_users from dndserver.protos import PacketCommand as pc from dndserver.protos.Account import SC2S_LOBBY_ENTER_REQ, SS2C_LOBBY_ENTER_RES -from dndserver.protos.Character import SACCOUNT_NICKNAME from dndserver.protos.Lobby import ( SC2S_CHARACTER_SELECT_ENTER_REQ, SC2S_LOBBY_GAME_DIFFICULTY_SELECT_REQ, @@ -20,12 +19,9 @@ from dndserver.protos.InGame import ( SC2S_AUTO_MATCH_REG_REQ, SS2C_AUTO_MATCH_REG_RES, - SS2C_ENTER_GAME_SERVER_NOT, - SS2C_FLOOR_MATCHMAKED_NOT, SS2C_AUTO_MATCH_REG_TEAM_NOT, ) from dndserver.utils import get_party, get_user, make_header -from dndserver.matchmaking import matchmaking def enter_lobby(ctx, msg): @@ -87,25 +83,24 @@ def open_map_select(ctx, msg): def auto_match(ctx, msg): + """Occurs when the client attempts to find a match""" req = SC2S_AUTO_MATCH_REG_REQ() req.ParseFromString(msg) party = get_party(account_id=sessions[ctx.transport].account.id) - for user in party.players: - transport, session = get_user(account_id=user.account.id) - if req.mode == 1: - if len(party.players) > 1: - matchteam = SS2C_AUTO_MATCH_REG_TEAM_NOT(result=1, mode=req.mode) - header = make_header(matchteam) + matchteam = SS2C_AUTO_MATCH_REG_TEAM_NOT(result=pc.SUCCESS, mode=req.mode) + header = make_header(matchteam) + if req.mode == SC2S_AUTO_MATCH_REG_REQ.MODE.REGISTER: + matchmaking_users.append({"party": party, "difficulty": req.mode}) + elif req.mode == SC2S_AUTO_MATCH_REG_REQ.MODE.CANCEL: + try: + matchmaking_users.remove({"party": party, "difficulty": req.mode}) + except ValueError: + pass + if len(party.players) > 1: + for user in party.players: + transport, _ = get_user(account_id=user.account.id) + if req.mode == SC2S_AUTO_MATCH_REG_REQ.MODE.REGISTER: + transport.write(header + matchteam.SerializeToString()) + elif req.mode == SC2S_AUTO_MATCH_REG_REQ.MODE.CANCEL: transport.write(header + matchteam.SerializeToString()) - matchmaking_users.append({"party": party, "difficulty": req.mode}) - elif req.mode == 2: - try: - if len(party.players) > 1: - res = SS2C_AUTO_MATCH_REG_TEAM_NOT(result=1, mode=req.mode) - header = make_header(res) - transport.write(header + res.SerializeToString()) - matchmaking_users.remove({"party": party, "difficulty": req.mode}) - return - except ValueError: - pass return SS2C_AUTO_MATCH_REG_RES(result=SS2C_AUTO_MATCH_REG_RES.RESULT.SUCCESS) diff --git a/dndserver/matchmaking.py b/dndserver/matchmaking.py index afb82f9..aec4eb3 100644 --- a/dndserver/matchmaking.py +++ b/dndserver/matchmaking.py @@ -1,48 +1,57 @@ import time +from enum import Enum from dndserver.persistent import matchmaking_users -from dndserver.protos.InGame import SS2C_FLOOR_MATCHMAKED_NOT, SS2C_ENTER_GAME_SERVER_NOT, SS2C_AUTO_MATCH_REG_TEAM_NOT +from dndserver.protos.InGame import SS2C_FLOOR_MATCHMAKED_NOT, SS2C_ENTER_GAME_SERVER_NOT from dndserver.protos.Character import SACCOUNT_NICKNAME -from dndserver.utils import make_header from dndserver.protos.Defines import Define_Game from dndserver.utils import get_user, make_header +class ServerStatus(Enum): + OFF = 0 + STARTED = 1 + + class Server: - def __init__(self, ip, port, slots, players): + def __init__(self, ip, port, slots, players, status): self.ip = ip self.port = port self.slots = slots self.players = players + self.status = status virtualServers = { Define_Game.DifficultyType.NORMAL: [ - Server("127.0.0.1", 7777, 10, []), - Server("127.0.0.1", 10002, 10, []), - Server("127.0.0.1", 10003, 10, []), + Server("127.0.0.1", 7777, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 10002, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 10003, 10, [], ServerStatus.STARTED), ], Define_Game.DifficultyType.HIGH_ROLLER: [ - Server("127.0.0.1", 7777, 10, []), - Server("127.0.0.1", 20002, 10, []), - Server("127.0.0.1", 20003, 10, []), + Server("127.0.0.1", 7777, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 20002, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 20003, 10, [], ServerStatus.STARTED), ], Define_Game.DifficultyType.GOBLIN: [ - Server("127.0.0.1", 7777, 10, []), - Server("127.0.0.1", 30002, 10, []), - Server("127.0.0.1", 30003, 10, []), + Server("127.0.0.1", 7777, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 30002, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 30003, 10, [], ServerStatus.STARTED), ], Define_Game.DifficultyType.RUINS: [ - Server("127.0.0.1", 7777, 10, []), - Server("127.0.0.1", 40002, 10, []), - Server("127.0.0.1", 40003, 10, []), + Server("127.0.0.1", 7777, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 40002, 10, [], ServerStatus.STARTED), + Server("127.0.0.1", 40003, 10, [], ServerStatus.STARTED), ], } def get_available_server(party): + """Gets the available servers, taking into account the party size""" + """and the difficulty (gamemode)""" playerCount = len(party["party"].players) availableServers = list( - filter(lambda x: len(x.players) + playerCount <= x.slots, virtualServers[party["difficulty"]]) + filter(lambda x: len(x.players) + playerCount <= x.slots and + x.status == ServerStatus.STARTED, virtualServers[party["difficulty"]]) ) if len(availableServers) > 0: return availableServers[0] @@ -51,6 +60,7 @@ def get_available_server(party): def matchmaking(): + """Main loop for the matchmaking thread""" while True: time.sleep(2) for party in matchmaking_users: diff --git a/dndserver/server.py b/dndserver/server.py index a5d7cbd..14ab973 100644 --- a/dndserver/server.py +++ b/dndserver/server.py @@ -11,7 +11,6 @@ from dndserver.matchmaking import matchmaking - async def main(): """Entrypoint where the server first initializes""" signal.signal(signal.SIGINT, signal.default_int_handler)