'Python TCP Game Server
So I am making a multiplayer CCG (collectable card game) in Unity (client side). I have chosen Python as the programming language of the server since I feel really confident with it. My concept of the server is the following:
- A general server that stores the currently active players and the matchmaking system
- A smaller server/lobby like a game room that is an instance created from the matchmaking system with 2 players playing 1v1.
I am currently working on the first part. The image bellow is a diagram of an example of the server working.
My server looks like this:
import threading
import socketserver
import json
from my_requests.requests import RequestID
active_players = []
queued_players = []
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = str(self.request.recv(1024), 'ascii')
data = data.replace("\'", "\"")
# Convert string to json and handle request
json_obj = json.loads(data)
if json_obj["request"] == RequestID.player_join:
active_players.append(json_obj["uuid"])
elif json_obj["request"] == RequestID.player_queue_up:
active_players.remove(json_obj["uuid"])
queued_players.append(json_obj["uuid"])
# Server response (echo for now)
cur_thread = threading.current_thread()
response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
self.request.sendall(response)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
I have set up a basic python tcp client for testing purposes:
import socket
import uuid
from my_requests.requests import RequestID, create_client_request
class Client:
def __init__(self, ip, port):
self.my_uuid = str(uuid.uuid4())
self.ip = ip
self.port = port
def join(self):
message = create_client_request(self.my_uuid, RequestID.player_join)
self.send(str(message))
def queue_up(self):
message = create_client_request(
self.my_uuid, RequestID.player_queue_up)
self.send(str(message))
def send(self, message):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((self.ip, self.port))
sock.sendall(bytes(message, 'ascii'))
response = str(sock.recv(1024), 'ascii')
print("[Client] Received: {}".format(response))
So far the server seems to add the players to the active list and move them to the queued up list once requested. However I am not sure how the matchmaking part should be. Should I check for worthy opponents in the queued up list for each new player in the list and create a room with him and the first same skill level one? Or should I have a server tick that is going to proc the matchmaking n times each second? Either way what are your suggestions about this?
Last but not least any paper or link that you think that would help me get through this project is highly appreciated! Thank you in advance!
EDIT: I implemented a modified version of the ELO rating system for the matchmaking. Since I now know the player's rating I theoretically queue him up with some worthy opponents. That creates an additional question though about the matchmaking. What is the most efficient way to find the worthy opponents from the queue up list?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

