L3-Bataille-Navale/source/network/packet/PacketBombPlaced.py

26 lines
623 B
Python

import struct
from dataclasses import dataclass, field
from source.network.packet.abc import SimplePacket
from source.type import Point2D
@dataclass
class PacketBombPlaced(SimplePacket):
"""
A packet that signal that a bomb have been placed on the board
"""
position: Point2D = field()
packet_format: str = ">BB"
def to_bytes(self) -> bytes:
x, y = self.position
return struct.pack(self.packet_format, x, y)
@classmethod
def from_bytes(cls, data: bytes) -> "PacketBombPlaced":
x, y = struct.unpack(cls.packet_format, data)
return cls(position=(x, y))