added udp connectivity to game server
This commit is contained in:
parent
39e07e4cb6
commit
0b447f9505
@ -5,36 +5,48 @@ import threading
|
|||||||
class Server:
|
class Server:
|
||||||
|
|
||||||
__address:str
|
__address:str
|
||||||
__port:str
|
__tcpPort:str
|
||||||
__socket:socket
|
__udpPort:str
|
||||||
|
__tcpSocket:socket
|
||||||
|
__udpSocket:socket
|
||||||
__clientThread:threading.Thread
|
__clientThread:threading.Thread
|
||||||
|
|
||||||
def __init__(self, address:str, port:str):
|
def __init__(self, address:str, tcpPort:str, udpPort:str):
|
||||||
self.__address = address
|
self.__address = address
|
||||||
self.__port = port
|
self.__tcpPort = tcpPort
|
||||||
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
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()
|
self.startServer()
|
||||||
|
|
||||||
# handles starting the server and assigning socket values to the local reference
|
# handles starting the server and assigning socket values to the local reference
|
||||||
def startServer(self):
|
def startServer(self):
|
||||||
self.__socket.bind(self.__address, self.__port)
|
self.__tcpSocket.bind(self.__address, self.__tcpPort)
|
||||||
self.__socket.listen()
|
self.__tcpSocket.listen()
|
||||||
print(f"server started on: {self.__address}:{self.__port}")
|
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
|
# server loop forwards connection to handleConnection
|
||||||
while True:
|
while True:
|
||||||
# accept incoming connection
|
# accept incoming connection
|
||||||
# TODO: validate this connection is a valid game 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
|
# create network thread for connection
|
||||||
self.__clientThread = threading.Thread(target=self.handleConnection, args=(client_socket, client_address))
|
self.__TCPclientThread = threading.Thread(target=self.handleTCPConnection, args=(client_tcpSocket, client_tcpAddress))
|
||||||
self.__clientThread.start()
|
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
|
# 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
|
# states that a connection has been established
|
||||||
print(f"Connected with {address}")
|
print(f"Connected with {address}")
|
||||||
|
|
||||||
@ -49,14 +61,36 @@ class Server:
|
|||||||
messageJson = json.loads(message)
|
messageJson = json.loads(message)
|
||||||
|
|
||||||
if messageJson["user"] in self.__users:
|
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:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
print(f"received message from {address}: {message}")
|
print(f"received message from {address}: {message}")
|
||||||
|
|
||||||
# handles passing of event data to the right functions
|
# handles passing of event data to the right functions
|
||||||
def handleEvents(self, event):
|
def handleTCPEvents(self, event):
|
||||||
# decide which event should be performed
|
# decide which event should be performed
|
||||||
if event["event"] == "login":
|
if event["event"] == "login":
|
||||||
pass
|
pass
|
||||||
@ -68,6 +102,8 @@ class Server:
|
|||||||
elif event["event"] == "leave_queue":
|
elif event["event"] == "leave_queue":
|
||||||
# just remove player from the queue
|
# just remove player from the queue
|
||||||
pass
|
pass
|
||||||
elif event["event"] == "gameAction":
|
|
||||||
|
def handleUDPEvents(self, event):
|
||||||
|
if event["event"] == "gameAction":
|
||||||
# pass of to own handler function which handles game logic
|
# pass of to own handler function which handles game logic
|
||||||
pass
|
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):
|
def buildGameWorld(self):
|
||||||
# construct elements arround the playerfield
|
# 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
|
# presets the y position later passed down to the vector2
|
||||||
eRow1Height = 85
|
eRow1Height = 85
|
||||||
@ -43,15 +43,18 @@ class World():
|
|||||||
pNamePos = pygame.Vector2(20, pRow2Height + 195)
|
pNamePos = pygame.Vector2(20, pRow2Height + 195)
|
||||||
pHPPos = pygame.Vector2(20, pRow2Height + 225)
|
pHPPos = pygame.Vector2(20, pRow2Height + 225)
|
||||||
|
|
||||||
|
# labeling
|
||||||
self.__labels.append(Label("PlayerHP", self.__screen, "1000 / 1000", pHPPos))
|
self.__labels.append(Label("PlayerHP", self.__screen, "1000 / 1000", pHPPos))
|
||||||
self.__labels.append(Label("PlayerName", self.__screen, "Player", pNamePos))
|
self.__labels.append(Label("PlayerName", self.__screen, "Player", pNamePos))
|
||||||
self.__labels.append(Label("EnemyHP", self.__screen, "1000 / 1000", eHPPos))
|
self.__labels.append(Label("EnemyHP", self.__screen, "1000 / 1000", eHPPos))
|
||||||
self.__labels.append(Label("EnemyName", self.__screen, "Enemy", eNamePos))
|
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("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("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("PlayerDeck", "Player", "Deck", pDeckPos, "Assets/Cards/Arena/field.png"))
|
||||||
self.__boardFields.append(BoardField("PlayerGraveyard", "Player", "Grave", pGravePos, "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):
|
for i in range(5):
|
||||||
pMonsterPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow1Height)
|
pMonsterPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow1Height)
|
||||||
pEffectPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow2Height)
|
pEffectPos = pygame.Vector2((self.__cardOffset + (((self.__cardWidth + 10) - 30) * i)), pRow2Height)
|
||||||
@ -80,4 +83,8 @@ class World():
|
|||||||
return self.__cards
|
return self.__cards
|
||||||
|
|
||||||
def spawnCard(self, asset:str, pos:pygame.Vector2, inputHandler:InputHandler) -> MonsterCard:
|
def spawnCard(self, asset:str, pos:pygame.Vector2, inputHandler:InputHandler) -> MonsterCard:
|
||||||
self.__cards.add(MonsterCard(pos, asset, inputHandler))
|
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()
|
field.image = card.image.copy()
|
||||||
card.setDragging(False)
|
card.setDragging(False)
|
||||||
# card.kill()
|
# card.kill()
|
||||||
|
|
||||||
elif event.type == pygame.MOUSEBUTTONUP:
|
elif event.type == pygame.MOUSEBUTTONUP:
|
||||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||||
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user