added udp connectivity to game server
This commit is contained in:
parent
39e07e4cb6
commit
0b447f9505
@ -5,36 +5,48 @@ import threading
|
||||
class Server:
|
||||
|
||||
__address:str
|
||||
__port:str
|
||||
__socket:socket
|
||||
__tcpPort:str
|
||||
__udpPort:str
|
||||
__tcpSocket:socket
|
||||
__udpSocket:socket
|
||||
__clientThread:threading.Thread
|
||||
|
||||
def __init__(self, address:str, port:str):
|
||||
def __init__(self, address:str, tcpPort:str, udpPort:str):
|
||||
self.__address = address
|
||||
self.__port = port
|
||||
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.__tcpPort = tcpPort
|
||||
self.__udpPort = tcpPort
|
||||
self.__tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.__udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
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}")
|
||||
self.__tcpSocket.bind(self.__address, self.__tcpPort)
|
||||
self.__tcpSocket.listen()
|
||||
print(f"tcp server started on: {self.__address}:{self.__tcpPort}")
|
||||
|
||||
self.__udpSocket.bind(self.__address, self.__udpPort)
|
||||
self.__udpSocket.listen()
|
||||
print(f"tcp server started on: {self.__address}:{self.__udpPort}")
|
||||
|
||||
# server loop forwards connection to handleConnection
|
||||
while True:
|
||||
# accept incoming connection
|
||||
# TODO: validate this connection is a valid game connection
|
||||
client_socket, client_address = self.__socket.accept()
|
||||
client_tcpSocket, client_tcpAddress = self.__tcpSocket.accept()
|
||||
|
||||
# create network thread for connection
|
||||
self.__clientThread = threading.Thread(target=self.handleConnection, args=(client_socket, client_address))
|
||||
self.__clientThread.start()
|
||||
self.__TCPclientThread = threading.Thread(target=self.handleTCPConnection, args=(client_tcpSocket, client_tcpAddress))
|
||||
self.__TCPclientThread.start()
|
||||
|
||||
client_udpSocket, client_udpAddress = self.__udpSocket.accept()
|
||||
self.__UDPclientThread = threading.Thread(target=self.handleUDPConnection, args=(client_udpSocket, client_udpAddress))
|
||||
self.__UDPclientThread.start()
|
||||
|
||||
|
||||
# handles ticking the game loop server side converting data and passing of to the event handler
|
||||
def handleConnection(self, socket:socket, address):
|
||||
def handleTCPConnection(self, socket:socket, address):
|
||||
# states that a connection has been established
|
||||
print(f"Connected with {address}")
|
||||
|
||||
@ -49,14 +61,36 @@ class Server:
|
||||
messageJson = json.loads(message)
|
||||
|
||||
if messageJson["user"] in self.__users:
|
||||
self.handleEvents(messageJson)
|
||||
self.handleTCPEvents(messageJson)
|
||||
else:
|
||||
break
|
||||
|
||||
print(f"received message from {address}: {message}")
|
||||
|
||||
# handles ticking the game loop server side converting data and passing of to the event handler
|
||||
def handleUDPConnection(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.handleUDPEvents(messageJson)
|
||||
else:
|
||||
break
|
||||
|
||||
print(f"received message from {address}: {message}")
|
||||
|
||||
# handles passing of event data to the right functions
|
||||
def handleEvents(self, event):
|
||||
def handleTCPEvents(self, event):
|
||||
# decide which event should be performed
|
||||
if event["event"] == "login":
|
||||
pass
|
||||
@ -68,6 +102,8 @@ class Server:
|
||||
elif event["event"] == "leave_queue":
|
||||
# just remove player from the queue
|
||||
pass
|
||||
elif event["event"] == "gameAction":
|
||||
|
||||
def handleUDPEvents(self, event):
|
||||
if event["event"] == "gameAction":
|
||||
# pass of to own handler function which handles game logic
|
||||
pass
|
29
Game_Client/Classes/Objects/Player.py
Normal file
29
Game_Client/Classes/Objects/Player.py
Normal file
@ -0,0 +1,29 @@
|
||||
import pygame
|
||||
|
||||
class Player:
|
||||
__hp:int
|
||||
__name:str
|
||||
__handCards:pygame.Sprite.Group
|
||||
|
||||
def __init__(self, hp:int, name:str):
|
||||
self.__hp = hp
|
||||
self.__name = name
|
||||
|
||||
def getName(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def getHP(self) -> int:
|
||||
return self.__hp
|
||||
|
||||
def adjustHP(self, hp:int) -> int:
|
||||
self.__hp = self.__hp + hp
|
||||
|
||||
def getHand(self) -> pygame.Sprite.Group:
|
||||
return self.__handCards
|
||||
|
||||
def getAddToHand(self, card) -> pygame.Sprite.Group:
|
||||
return self.__handCards
|
||||
|
||||
def removeFromHand(self, pos:int) -> pygame.Sprite.Group:
|
||||
self.__handCards.remove(pos)
|
||||
return self.__handCards
|
@ -25,7 +25,7 @@ class World():
|
||||
|
||||
def buildGameWorld(self):
|
||||
# construct elements arround the playerfield
|
||||
# Todo add lifepoint label for player and enemy and make them scriptable
|
||||
# Todo add lifepoint label for player and enemy and make them scriptable move to player class
|
||||
|
||||
# presets the y position later passed down to the vector2
|
||||
eRow1Height = 85
|
||||
@ -43,15 +43,18 @@ class World():
|
||||
pNamePos = pygame.Vector2(20, pRow2Height + 195)
|
||||
pHPPos = pygame.Vector2(20, pRow2Height + 225)
|
||||
|
||||
# labeling
|
||||
self.__labels.append(Label("PlayerHP", self.__screen, "1000 / 1000", pHPPos))
|
||||
self.__labels.append(Label("PlayerName", self.__screen, "Player", pNamePos))
|
||||
self.__labels.append(Label("EnemyHP", self.__screen, "1000 / 1000", eHPPos))
|
||||
self.__labels.append(Label("EnemyName", self.__screen, "Enemy", eNamePos))
|
||||
|
||||
self.__boardFields.append(BoardField("EnemyDeck", "Enemy", "Deck", eDeckPos, "Assets/Cards/Arena/field.png"))
|
||||
self.__boardFields.append(BoardField("EnemyGraveyard", "Enemy", "Grave", eGravePos, "Assets/Cards/Arena/field.png"))
|
||||
self.__boardFields.append(BoardField("PlayerDeck", "Player", "Deck", pDeckPos, "Assets/Cards/Arena/field.png"))
|
||||
self.__boardFields.append(BoardField("PlayerGraveyard", "Player", "Grave", pGravePos, "Assets/Cards/Arena/field.png"))
|
||||
|
||||
# handle field creation
|
||||
for i in range(5):
|
||||
pMonsterPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow1Height)
|
||||
pEffectPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow2Height)
|
||||
@ -81,3 +84,7 @@ class World():
|
||||
|
||||
def spawnCard(self, asset:str, pos:pygame.Vector2, inputHandler:InputHandler) -> MonsterCard:
|
||||
self.__cards.add(MonsterCard(pos, asset, inputHandler))
|
||||
|
||||
def spawnCards(self, cards:pygame.sprite.Group):
|
||||
for card in cards:
|
||||
self.__cards.append(card)
|
@ -92,7 +92,6 @@ class App:
|
||||
field.image = card.image.copy()
|
||||
card.setDragging(False)
|
||||
# card.kill()
|
||||
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
||||
|
Loading…
x
Reference in New Issue
Block a user