diff --git a/NOTE.md b/NOTE.md index d441fec..bf62086 100644 --- a/NOTE.md +++ b/NOTE.md @@ -8,6 +8,12 @@ A faire : - Voir si les event listener intégré à pyglet sont plus pratique que l'event propagation - Documenter +- Ecran de victoire +- Affichage des coups de l'opposant +- Faire marcher le score +- Faire marcher le tchat +- Sauvegarde / Quitter + Bug : - / @@ -15,10 +21,5 @@ Autre : - Tester sur Linux -A expliquer : -- in_pyglet_context -- Packet - - Bonus ultime : - Envoyer la texture de la grille à l'adversaire (???) \ No newline at end of file diff --git a/source/core/enums/bomb.py b/source/core/enums/bomb.py index 946f070..9b2c196 100644 --- a/source/core/enums/bomb.py +++ b/source/core/enums/bomb.py @@ -11,4 +11,4 @@ class BombState(Enum): SUNKEN = 2 # the bomb touched the last part of a boat WON = 3 # the bomb sunk the last boat - ERROR = 10 # the bomb could not be placed + ERROR = -1 # the bomb could not be placed diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index f617405..d1b539a 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -18,6 +18,10 @@ class Game(Scene): def __init__(self, window: "Window", connection: socket.socket, **kwargs): super().__init__(window, **kwargs) + self.my_turn = False + self.boat_ready_ally = False + self.boat_ready_enemy = False + self.connection = connection self.batch_label = pyglet.graphics.Batch() @@ -55,6 +59,7 @@ class Game(Scene): ) def board_ally_ready(): + self.boat_ready_ally = True PacketBoatPlaced().send_connection(connection) self.grid_ally.add_listener("on_all_boats_placed", board_ally_ready) @@ -75,7 +80,10 @@ class Game(Scene): ) def board_enemy_bomb(cell: Point2D): + if not (self.boat_ready_ally and self.boat_ready_enemy): return + if not self.my_turn: return PacketBombPlaced(position=cell).send_connection(connection) + self.my_turn = False self.grid_enemy.add_listener("on_request_place_bomb", board_enemy_bomb) diff --git a/source/network/Client.py b/source/network/Client.py index 0df6cea..a5e4482 100644 --- a/source/network/Client.py +++ b/source/network/Client.py @@ -30,4 +30,4 @@ class Client(StoppableThread): print(f"[Client] Connecté avec {connection}") - game_network(self, self.window, connection) + game_network(self, self.window, connection, False) diff --git a/source/network/Host.py b/source/network/Host.py index cae2786..85db6b0 100644 --- a/source/network/Host.py +++ b/source/network/Host.py @@ -39,4 +39,4 @@ class Host(StoppableThread): print(f"[Serveur] Connecté avec {address}") - game_network(self, self.window, connection) + game_network(self, self.window, connection, True) diff --git a/source/network/game_network.py b/source/network/game_network.py index 0e485f8..9b9854e 100644 --- a/source/network/game_network.py +++ b/source/network/game_network.py @@ -12,7 +12,7 @@ from source.utils import StoppableThread from source.utils.thread import in_pyglet_context -def game_network(thread: "StoppableThread", window: "Window", connection: socket.socket): +def game_network(thread: "StoppableThread", window: "Window", connection: socket.socket, host: bool): """ Run the networking to make the game work and react with the other player :param thread: the thread where this function is called. @@ -21,6 +21,7 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket """ game_scene = in_pyglet_context(window.set_scene, scene.Game, connection=connection) + game_scene.my_turn = host while True: data: Any = Packet.from_connection(connection) @@ -34,6 +35,7 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket print(data.message) case packet.PacketBoatPlaced: + game_scene.boat_ready_enemy = True print("adversaire à posé ses bateaux") case packet.PacketBombPlaced: @@ -44,9 +46,16 @@ def game_network(thread: "StoppableThread", window: "Window", connection: socket packet.PacketBombState(position=data.position, bomb_state=bomb_state).send_connection(connection) + touched = bomb_state in [BombState.TOUCHED, BombState.SUNKEN, BombState.WON, BombState.ERROR] + game_scene.my_turn = not touched + case packet.PacketBombState: - if data.bomb_state is BombState.ERROR: continue + print(data.bomb_state) + if data.bomb_state is BombState.ERROR: + game_scene.my_turn = True + continue touched = data.bomb_state in [BombState.TOUCHED, BombState.SUNKEN, BombState.WON] + game_scene.my_turn = touched in_pyglet_context(game_scene.grid_enemy.place_bomb, data.position, touched) diff --git a/source/network/packet/PacketChat.py b/source/network/packet/PacketChat.py index 25d18ca..6ed7b05 100644 --- a/source/network/packet/PacketChat.py +++ b/source/network/packet/PacketChat.py @@ -13,7 +13,7 @@ class PacketChat(Packet): packet_size: int = 256 - def to_bytes(self): + def to_bytes(self) -> bytes: return self.message.encode("utf-8") @classmethod diff --git a/source/network/packet/abc/Packet.py b/source/network/packet/abc/Packet.py index 522013b..f566995 100644 --- a/source/network/packet/abc/Packet.py +++ b/source/network/packet/abc/Packet.py @@ -83,4 +83,3 @@ class Packet(ABC): """ subcls = cls.cls_from_connection(connection) return None if subcls is None else subcls.instance_from_connection(connection) -