diff --git a/docs/classes/network/Packet.md b/docs/classes/network/Packet.md new file mode 100644 index 0000000..bb8b0c0 --- /dev/null +++ b/docs/classes/network/Packet.md @@ -0,0 +1,3 @@ +# `class` Packet +--- +::: g2o.packets.Packet \ No newline at end of file diff --git a/docs/defaultEvents/network/onPacket.md b/docs/defaultEvents/network/onPacket.md new file mode 100644 index 0000000..82948ca --- /dev/null +++ b/docs/defaultEvents/network/onPacket.md @@ -0,0 +1,22 @@ +# `event` onPacket +This event is triggered when new packet arrives from the client. + +Original: [onPacket](https://gothicmultiplayerteam.gitlab.io/docs/0.3.0/script-reference/server-events/network/onPacket/) + +## Parameters +* `dict` **kwargs**: + * `int` **playerid**: the id of the player who sent the packet. + * `Packet` **packet**: data sended over network from client side. + +## Usage +```python +import g2o + +@g2o.event('onPacket') +def onPacketEvt(**kwargs): + data = kwargs['data'] + message = data.readString() + + print(f'Got packet from {kwargs['playerid']} (bytes used {data.bytesUsed})') + print(f'Packet says: {message}') +``` \ No newline at end of file diff --git a/g2o/packets.py b/g2o/packets.py index d4cd6d5..21e96cb 100644 --- a/g2o/packets.py +++ b/g2o/packets.py @@ -1,67 +1,190 @@ import sqg2o class Packet(sqg2o.Packet): + """ + This class represents data packet that gets send over the network. + Original: [Packet](https://gothicmultiplayerteam.gitlab.io/docs/0.3.0/script-reference/server-classes/network/Packet/) + + ## `int` bitsUsed + Represents the total number of bits used by the script packet data. + + ## `int` bytesUsed + Represents the total number of bytes used by the script packet data. + """ def __init__(self): return super().__init__() def reset(self): + """ + This method will clear the packet data, making it empty. + """ return super().reset() def send(self, playerid : int, reliability : int): + """ + This method will send the packet data to the specified player. + **Parameters:** + * `int` **playerid**: the id of the player to whom you want to send the packet. + * `int` **reliability**: the reliability type, for more information see **TBD**. + """ + return super().send(playerid, reliability) + + def sendToAll(self, reliability : int): + """ + This method will send the packet data to the specified player. + **Parameters:** + * `int` **reliability**: the reliability type, for more information see **TBD**. + """ return super().send(playerid, reliability) def writeInt8(self, value : int): + """ + This method will append signed int8 value to the packet. (1 byte) + **Parameters:** + * `int` **value**: the number value in range -128 to 127. + """ return super().writeInt8(value) def writeUInt8(self, value : int): + """ + This method will append unsigned int8 value to the packet. (1 byte) + **Parameters:** + * `int` **value**: the number value in range 0 to 255. + """ return super().writeInt8(value) def writeInt16(self, value : int): + """ + This method will append signed int16 value to the packet. (2 bytes) + **Parameters:** + * `int` **value**: the number value in range -32768 to 32767. + """ return super().writeInt16(value) def writeUInt16(self, value : int): + """ + This method will append unsigned int16 value to the packet. (2 bytes) + **Parameters:** + * `int` **value**: the number value in range 0 to 65535. + """ return super().writeInt16(value) def writeInt32(self, value : int): + """ + This method will append signed int32 value to the packet. (4 bytes) + **Parameters:** + * `int` **value**: the number value in range -2147483648 to 2147483647. + """ return super().writeInt32(value) def writeUInt32(self, value : int): + """ + !!! note + By default squirrel uses int32 values, which means that this method behaves exactly the same as [writeInt32](Packet.md#g2o.packets.Packet.writeInt32) in scripts. + This method will append unsigned int32 value to the packet. (4 bytes) + **Parameters:** + * `int` **value**: the number value in range 0 to 4294967295. + """ return super().writeInt32(value) def writeBool(self, value : bool): + """ + This method will append boolean value to the packet. (1 bit) + **Parameters:** + * `bool` **value**: the boolean value. + """ return super().writeBool(value) def writeFloat(self, value : float): + """ + This method will append float value to the packet. (4 bytes) + **Parameters:** + * `float` **value**: the number value. + """ return super().writeFloat(value) def writeString(self, value : str): + """ + !!! note + The amount of bytes appended to the packet depends on the length of string, 1 byte = 1 char. + This method will append string value to the packet. + **Parameters:** + * `str` **value**: the text value. + """ return super().writeString(value) def readInt8(self,) -> int: + """ + This method will get signed int8 value from the packet. (1 byte) + **Returns `int`:** + the number value in range -128 to 127. + """ return super().readInt8() def readUInt8(self,) -> int: + """ + This method will get unsigned int8 value from the packet. (1 byte) + **Returns `int`:** + the number value in range 0 to 255. + """ return super().readInt8() def readInt16(self) -> int: + """ + This method will get signed int16 value from the packet. (2 bytes) + **Returns `int`:** + the number value in range -32768 to 32767. + """ return super().readInt16() def readUInt16(self) -> int: + """ + This method will get unsigned int16 value from the packet. (2 bytes) + **Returns `int`:** + the number value in range 0 to 65535. + """ return super().readInt16() def readInt32(self) -> int: + """ + This method will get signed int32 value from the packet. (4 bytes) + **Returns `int`:** + the number value in range -2147483648 to 2147483647. + """ return super().readInt32() def readUInt32(self) -> int: + """ + This method will get unsigned int32 value from the packet. (4 bytes) + **Returns `int`:** + the number value in range 0 to 4294967295. + """ return super().readInt32() def readBool(self) -> bool: + """ + This method will get signed int8 value from the packet. (1 byte) + **Returns `bool`:** + the boolean value. + """ return super().readBool() def readFloat(self) -> float: + """ + This method will get float value from the packet. (4 bytes) + **Returns `float`:** + the number value. + """ return super().readFloat() def readString(self) -> str: + """ + !!! note + The amount of bytes appended to the packet depends on the length of string, 1 byte = 1 char. + This method will get string value from the packet. + **Returns `str`:** + the text value. + """ return super().readString() @property diff --git a/mkdocs.yml b/mkdocs.yml index 9dced9d..37b06aa 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -25,6 +25,8 @@ nav: - onTime: defaultEvents/general/onTime.md - onBan: defaultEvents/general/onBan.md - onUnban: defaultEvents/general/onUnban.md + - Network: + - onPacket: defaultEvents/network/onPacket.md - Player: - onPlayerChangeColor: defaultEvents/player/onPlayerChangeColor.md - onPlayerChangeFocus: defaultEvents/player/onPlayerChangeFocus.md @@ -57,6 +59,9 @@ nav: - onPlayerSpellSetup: defaultEvents/player/onPlayerSpellSetup.md - onPlayerTeleport: defaultEvents/player/onPlayerTeleport.md - onPlayerToggleFaceAni: defaultEvents/player/onPlayerToggleFaceAni.md + - Classes: + - Network: + - Packet: classes/network/Packet.md - Functions: - Events: - event: events/event.md