added first ground tech for game server

This commit is contained in:
2023-12-16 21:27:39 +01:00
parent b70ced94f7
commit c71980789c
5 changed files with 188 additions and 70 deletions

View File

@ -0,0 +1,52 @@
import json
class Player:
__queue:list
__users:list
def __init__(self):
self.__queue = []
self.__users = []
def getQueue(self) -> list:
return self.__queue
def addToQueue(self, user):
if self.isInQueue(user["id"]):
self.__queue.append(user)
def isInQueue(self, user:int) -> bool:
for user in self.__queue:
if int(user["id"]) == user:
return user
def isInQueue(self, user:str) -> bool:
for user in self.__queue:
if user["username"] == user:
return user
def createUser(self, user:json):
self.__users.append(user)
def createUser(self, user:json):
if self.getUser(user["id"]) == None:
self.__users.append(user)
def removeUser(self, user:int):
self.__users.remove(user)
def removeUser(self, user:str):
self.__users.remove(user)
def getUsers(self) -> list:
return self.__users
def getUser(self, user:int) -> (any | None):
for user in self.__users:
if int(user["id"]) == user:
return user
def getUser(self, user:str) -> (any | None):
for user in self.__users:
if user["name"] == user:
return user

View File

@ -0,0 +1,60 @@
import json
import socket
class Server:
__address:str
__port:str
__socket:socket
def __init__(self, address:str, port:str):
self.__address = address
self.__port = port
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.startServer()
# handles starting the server and assigning socket values to the local reference
def startServer(self):
self.__socket.bind(self.__address, self.__port)
self.__socket.listen()
print(f"server started on: {self.__address}:{self.__port}")
# handles ticking the game loop server side converting data and passing of to the event handler
def handleConnection(self, socket:socket, address):
# states that a connection has been established
print(f"Connected with {address}")
# Communication with client
while True:
data = socket.recv(1024)
if not data:
break
# decode message for handling
message = data.decode()
messageJson = json.loads(message)
if messageJson["user"] in self.__users:
self.handleEvents(messageJson)
else:
break
print(f"received message from {address}: {message}")
# handles passing of event data to the right functions
def handleEvents(self, event):
# decide which event should be performed
if event["event"] == "login":
pass
elif event["event"] == "join_queue":
# count queue
# move start game if 2 players are in the queue
# remove player from the queue once game starts
pass
elif event["event"] == "leave_queue":
# just remove player from the queue
pass
elif event["event"] == "gameAction":
# pass of to own handler function which handles game logic
pass

76
Game Server/index.py Normal file
View File

@ -0,0 +1,76 @@
import threading
import os
import json
from dotenv import load_dotenv
__users:list
# TODO: setup tcp service for authorization
def handleConnection(self, socket:socket, address):
# states that a connection has been established
print(f"Connected with {address}")
# Communication with client
while True:
data = socket.recv(1024)
if not data:
break
# decode message for handling
message = data.decode()
messageJson = json.loads(message)
if messageJson["user"] in self.__users:
handleEvents(messageJson)
else:
break
print(f"received message from {address}: {message}")
# connection is not required anymore and gets closed
socket.close()
print(f"connection closed for {address}")
def handleEvents(self, event):
# decide which event should be performed
if event["event"] == "login":
pass
elif event["event"] == "join_queue":
# count queue
# move start game if 2 players are in the queue
# remove player from the queue once game starts
pass
elif event["event"] == "leave_queue":
# just remove player from the queue
pass
elif event["event"] == "gameAction":
# pass of to own handler function which handles game logic
pass
def main(self):
load_dotenv()
# retrieves host data from environment
HOST = os.getenv("HOST")
PORT = os.getenv("PORT")
self.__users = []
# create tcp socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# make socket bind connection info and listen for connections
print(f"authorization server online on {HOST}:{PORT}")
# server loop forwards connection to handleConnection
while True:
# accept incoming connection
# TODO: validate this connection is a valid game connection
client_socket, client_address = server_socket.accept()
# create network thread for connection
client_thread = threading.Thread(target=handleConnection, args=(client_socket, client_address))
client_thread.start()
if __name__ == "__main__":
main()

View File

@ -1,70 +0,0 @@
import socket
import threading
import os
import json
from dotenv import load_dotenv
load_dotenv()
# retrieves host data from environment
HOST = os.getenv("HOST")
PORT = os.getenv("PORT")
# TODO: setup tcp service for authorization
def handle_connection(socket:socket, address):
# states that a connection has been established
print(f"Connected with {address}")
# Communication with client
while True:
data = socket.recv(1024)
if not data:
break
# decode message for handling
message = data.decode()
message_json = json.loads(message)
print(f"received message from {address}: {message}")
# decide which event should be performed
if message_json["event"] == "login":
# encode message and respond to client
# TODO: Handle login
# TODO: request user information based on input
# TODO: validate informateion given is valid
# TODO: create session id for user
# TODO: send session information to user
response = f"<place_holder_message>"
socket.sendall(response.encode())
elif message_json["event"] == "register":
# encode message and respond to client
# TODO: Handle registration
response = f"<place_holder_message>"
socket.sendall(response.encode())
elif message_json["event"] == "logout":
# encode message and respond to client
# TODO: Handle registration
response = f"<place_holder_message>"
socket.sendall(response.encode())
# connection is not required anymore and gets closed
socket.close()
print(f"connection closed for {address}")
# create tcp socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# make socket bind connection info and listen for connections
socket.bind(HOST, PORT)
socket.listen()
print(f"authorization server online on {HOST}:{PORT}")
# server loop forwards connection to handle_connection
while True:
# accept incomming connection
# TODO: validate this connection is a valid game connection
client_socket, client_address = server_socket.accept()
# create network thread for connection
client_thread = threading.Thread(target=handle_connection, args=(client_socket, client_address))
client_thread.start()