Skip to content

Commit

Permalink
PR comments solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine COUDRAY committed May 18, 2023
1 parent c80692c commit 06ae71f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
6 changes: 0 additions & 6 deletions dndserver/handlers/gatheringhall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
37 changes: 16 additions & 21 deletions dndserver/handlers/lobby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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)
42 changes: 26 additions & 16 deletions dndserver/matchmaking.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion dndserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 06ae71f

Please sign in to comment.