Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added matchmaking feature #232

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dndserver/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
from dndserver.enums.items import ItemType, Rarity, Item as ItemEnum
from dndserver.models import Item
from dndserver.utils import get_user
from dndserver.matchmaking import virtualServers
from dndserver.protos.Defines import Define_Game


def list_servers():
for difficulty, servers in virtualServers.items():
print(f"Servers for Difficulty: {Define_Game.DifficultyType.Name(difficulty)}")
for server in servers:
print(
f"IP: {server.ip}:{server.port}, Capacity: {server.slots}, Players: {len(server.players)}"
)


def list_items(filter=None):
Expand Down Expand Up @@ -54,6 +65,7 @@ def exit():
"/give_item": {"function": give_item, "help": "/give_item <user> <item_name> <rarity> [amount]"},
"/list_items": {"function": list_items, "help": "/list_items [filter]"},
"/list_rarity": {"function": list_rarity, "help": "/list_rarity [filter]"},
"/servers": {"function": list_servers, "help": "/servers - display every server available"},
"/exit": {"function": exit, "help": "/exit"}
# add more commands here
}
Expand Down
32 changes: 31 additions & 1 deletion dndserver/handlers/lobby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dndserver.models import Character
from dndserver.objects.party import Party
from dndserver.objects.state import State
from dndserver.persistent import parties, sessions
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.Lobby import (
Expand All @@ -16,6 +16,12 @@
SS2C_LOBBY_REGION_SELECT_RES,
SS2C_OPEN_LOBBY_MAP_RES,
)
from dndserver.protos.InGame import (
SC2S_AUTO_MATCH_REG_REQ,
SS2C_AUTO_MATCH_REG_RES,
SS2C_AUTO_MATCH_REG_TEAM_NOT,
)
from dndserver.utils import get_party, get_user, make_header


def enter_lobby(ctx, msg):
Expand Down Expand Up @@ -74,3 +80,27 @@ def open_map_select(ctx, msg):
req.ParseFromString(msg)
res = SS2C_OPEN_LOBBY_MAP_RES()
return res


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)
matchteam = SS2C_AUTO_MATCH_REG_TEAM_NOT(result=pc.SUCCESS, mode=req.mode)
header = make_header(matchteam)
acoudray marked this conversation as resolved.
Show resolved Hide resolved
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())
acoudray marked this conversation as resolved.
Show resolved Hide resolved
return SS2C_AUTO_MATCH_REG_RES(result=SS2C_AUTO_MATCH_REG_RES.RESULT.SUCCESS)
91 changes: 91 additions & 0 deletions dndserver/matchmaking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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
from dndserver.protos.Character import SACCOUNT_NICKNAME
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, 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, [], 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, [], 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, [], 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, [], 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 and
x.status == ServerStatus.STARTED, virtualServers[party["difficulty"]])
)
if len(availableServers) > 0:
return availableServers[0]
else:
return None


def matchmaking():
"""Main loop for the matchmaking thread"""
while True:
time.sleep(2)
for party in matchmaking_users:
server = get_available_server(party)
if server is None:
print("Server not found. Searching...")
continue
# We have found a server for the party. Now we need to notify them.
for player in party["party"].players:
transport, _ = get_user(account_id=player.account.id)
match = SS2C_FLOOR_MATCHMAKED_NOT(port=server.port, ip=server.ip, sessionId=str(player.account.id))
header = make_header(match)
transport.write(header + match.SerializeToString())
res = SS2C_ENTER_GAME_SERVER_NOT(
port=server.port,
ip=server.ip,
sessionId=str(player.account.id),
accountId=str(player.account.id),
isReconnect=0,
nickName=SACCOUNT_NICKNAME(
originalNickName=player.character.nickname,
streamingModeNickName=player.character.streaming_nickname,
),
)
header = make_header(res)
transport.write(header + res.SerializeToString())
server.players.append(player)
matchmaking_users.remove(party)
1 change: 1 addition & 0 deletions dndserver/persistent.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
parties = []
sessions = {}
matchmaking_users = []
1 change: 1 addition & 0 deletions dndserver/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def dataReceived(self, data: bytes) -> None:
pc.C2S_GATHERING_HALL_CHANNEL_SELECT_REQ: gatheringhall.gathering_hall_select_channel,
pc.C2S_GATHERING_HALL_CHANNEL_EXIT_REQ: gatheringhall.gathering_hall_channel_exit,
pc.C2S_GATHERING_HALL_TARGET_EQUIPPED_ITEM_REQ: gatheringhall.gathering_hall_equip,
pc.C2S_AUTO_MATCH_REG_REQ: lobby.auto_match,
}
handler = [k for k in handlers.keys() if k == _id]
if not handler:
Expand Down
6 changes: 5 additions & 1 deletion dndserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from twisted.internet import reactor
import threading


from dndserver.config import config
from dndserver.protocol import GameFactory
from dndserver.console import console
from dndserver.matchmaking import matchmaking


async def main():
Expand All @@ -23,6 +23,10 @@ async def main():
tcpFactory = GameFactory()
reactor.listenTCP(config.server.port, tcpFactory)

# Start the matchmaking function in a separate thread
matchmaking_thread = threading.Thread(target=matchmaking)
matchmaking_thread.start()

# Start the console function in a separate thread
console_thread = threading.Thread(target=console)
console_thread.start()
Expand Down