theoretically reworked packet structure for gamestart event + fixed client issue flooding the server with packets
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -7,10 +7,10 @@ from Classes.System.Components.InputHandler import InputHandler
|
||||
|
||||
# send from the server to tell the player the game starts
|
||||
# gives the client its and the opponents stats (not cards!!)
|
||||
def GameStart(world: World, handCards:list, inputHandler:InputHandler, owner:Player, opponent:Player):
|
||||
def GameStart(world: World, handCards:list, inputHandler:InputHandler, opponent:Player):
|
||||
index:int = 0
|
||||
world.setEnemy(opponent)
|
||||
|
||||
for card in handCards:
|
||||
world.AddToPlayerHand(Card(pygame.Vector2(500 + (index + 100), 1050), f"Assets/Cards/{card}/", inputHandler, owner))
|
||||
world.AddToPlayerHand(Card(pygame.Vector2(500 + (index + 100), 1050), f"Assets/Cards/{card}/", inputHandler, world.getPlayer()))
|
||||
|
@ -16,7 +16,9 @@ def Login(tcpClient:TCPClient):
|
||||
tcpClient.send(payload)
|
||||
|
||||
# server response for login event
|
||||
def LoginResponse(message:json):
|
||||
def LoginResponse(message:dict, world:World):
|
||||
# checks if the response on the login request is successfull
|
||||
if message["status"] != "success":
|
||||
print("login failed")
|
||||
print("login failed")
|
||||
else:
|
||||
world.setPlayer(Player(0,0, message["username"], message["id"]))
|
||||
|
@ -3,7 +3,7 @@ from Classes.Game.World import World
|
||||
from Classes.System.Components.InputHandler import InputHandler
|
||||
|
||||
# the event the client sends to the server when it places a card
|
||||
def PlaceCard(tcpClient, card):
|
||||
def PlaceCard(tcpClient, card, id):
|
||||
# todo: send card information to the server
|
||||
# todo: required info is:
|
||||
# - position
|
||||
@ -13,6 +13,7 @@ def PlaceCard(tcpClient, card):
|
||||
"event":"placecard",
|
||||
"card": card.getID(),
|
||||
"type": card.getType(),
|
||||
"user": id,
|
||||
"x": card.getPos().x,
|
||||
"y": card.getPos().y,
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -7,10 +7,11 @@ class Player:
|
||||
__name:str
|
||||
__handCards:pygame.sprite.Group
|
||||
|
||||
def __init__(self, hp:int, mana:int, name:str):
|
||||
def __init__(self, hp:int, mana:int, name:str, id:int):
|
||||
self.__hp = hp
|
||||
self.__mana = mana
|
||||
self.__name = name
|
||||
self.__id = id
|
||||
|
||||
def setID(self, id:int):
|
||||
self.__id = id
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -27,6 +27,9 @@ class App:
|
||||
self.__myFont = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
self.__window = Window(width=width, height=height, title=title)
|
||||
self.__inputHandler = InputHandler()
|
||||
|
||||
# store selected card globaly in order to prevent packet flooding
|
||||
self.selectedCard = None
|
||||
|
||||
# game word
|
||||
self.__world = World(self.__window.getScreen())
|
||||
@ -47,12 +50,12 @@ class App:
|
||||
# create sprite groups
|
||||
# todo: remove these and let server handle card creation instead
|
||||
# blocker: server - client communication [WIP]
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(500, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(600, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(700, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(800, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(900, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(1000, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(500, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(600, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(700, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(800, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(900, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
# self.__world.spawnCard("Assets/Cards/1/", pygame.Vector2(1000, 1050), self.__inputHandler, Player(1000, 0, "test"))
|
||||
|
||||
while self.__running:
|
||||
self.__clock.tick(self.__FPS)
|
||||
@ -79,7 +82,7 @@ class App:
|
||||
# handles incoming event queue
|
||||
def handleEvent(self, events):
|
||||
# TODO: fix bug that stacks cards when dragging them around
|
||||
self.selectedCard = None
|
||||
# TODO: something prevents selected card from becoming function global
|
||||
|
||||
for event in events:
|
||||
if event.type == pygame.QUIT:
|
||||
@ -112,7 +115,6 @@ class App:
|
||||
self.__world.spawnMonsterCard("Assets/Cards/MonsterCards/testmonstercard/", self.__inputHandler.getMousePos(), self.__inputHandler)
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
mouse_x, mouse_y = pygame.mouse.get_pos()
|
||||
mouse_pos = pygame.Vector2(mouse_x, mouse_y)
|
||||
if event.button == 1: # Wenn linke Maustaste losgelassen wird
|
||||
for card in self.__world.getCards():
|
||||
card.setDragging(False)
|
||||
@ -120,14 +122,14 @@ class App:
|
||||
# TODO: send place card event to server
|
||||
# resets the currently selected card in order to prevent it getting moved
|
||||
try:
|
||||
print(self.selectedCard)
|
||||
if self.selectedCard == card:
|
||||
PlaceCard(self.__tcpClient, card) # tells the server that the player placed this card
|
||||
print(self.selectedCard)
|
||||
PlaceCard(self.__tcpClient, self.selectedCard, 1) # tells the server that the player placed this card
|
||||
except Exception as e:
|
||||
print(f"failed to place card on server due to error: {e}")
|
||||
|
||||
if not card == None:
|
||||
card = None
|
||||
if not self.selectedCard == None:
|
||||
self.selectedCard = None
|
||||
|
||||
# sets the running state for the gameloop
|
||||
def setRunning(self, running:bool):
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -7,14 +7,16 @@ from Classes.System.Components.InputHandler import InputHandler
|
||||
from Classes.Game.World import World
|
||||
from Classes.Game.Events.GameStart import GameStart
|
||||
from Classes.Game.Player import Player
|
||||
from Classes.Game.Events.Login import LoginResponse
|
||||
|
||||
class TCPEventHandler:
|
||||
def __init__(self, socket:socket):
|
||||
self.tcp_socket = socket
|
||||
|
||||
def handleEvents(self, message, inputHandler:InputHandler, world:World):
|
||||
if message["event"] == "login":
|
||||
def handleEvents(self, message:dict, inputHandler:InputHandler, world:World):
|
||||
if message["event"] == "loginresponse":
|
||||
# todo: handle login response here
|
||||
LoginResponse(message, world)
|
||||
pass
|
||||
elif message["event"] == "startgame":
|
||||
print("gamestart")
|
||||
@ -24,14 +26,14 @@ class TCPEventHandler:
|
||||
CardPlaced(world, message["card"], message["type"], message["owner"], pygame.Vector2(int(message["x"]), int(message["y"]), inputHandler))
|
||||
pass
|
||||
elif message["event"] == "MoveCard":
|
||||
CardMoved(
|
||||
world,
|
||||
message["card"],
|
||||
message["type"],
|
||||
message["owner"],
|
||||
pygame.Vector2(int(message["old_x"]), int(message["old_y"])),
|
||||
pygame.Vector2(int(message["new_x"]), int(message["new_y"])),
|
||||
inputHandler)
|
||||
# CardMoved(
|
||||
# world,
|
||||
# message["card"],
|
||||
# message["type"],
|
||||
# message["owner"],
|
||||
# pygame.Vector2(int(message["old_x"]), int(message["old_y"])),
|
||||
# pygame.Vector2(int(message["new_x"]), int(message["new_y"])),
|
||||
# inputHandler, world)
|
||||
pass
|
||||
elif message["event"] == "RemoveCard":
|
||||
pass
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user