diff --git a/.pylintrc b/.pylintrc index 0a369cd..116a128 100644 --- a/.pylintrc +++ b/.pylintrc @@ -425,6 +425,7 @@ disable=raw-checker-failed, trailing-newlines, missing-class-docstring, missing-module-docstring, + missing-function-docstring, fixme, use-symbolic-message-instead diff --git a/README.md b/README.md index 7d05cc0..72cde5a 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ A Python teeworlds client & server library, designed according to sans I/O (http://sans-io.readthedocs.io/) principles -## goal +## sample usage ```python -msg = twnet_parser.parse(b'\x10\x10\x00\x04\x9a\xcb9\xc9') -print(msg) - -# : {'name': 'disconnect', 'token': b'\x9a\xcb9\xc9', 'fields': {}} +pck = parse(b'\x10\x10\x00\x04\x9a\xcb9\xc9') +for msg in pck.messages: + print(msg.name) # => close ``` ## setup diff --git a/main.py b/main.py index 782efb1..90d196a 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,17 @@ from typing import Union -class CtrlMessage: +class BaseMessage: + def __init__(self, name): + self.name = name + +class CtrlMessage(BaseMessage): pass -class GameMessage: +class GameMessage(BaseMessage): pass -class SysMessage: +class SysMessage(BaseMessage): pass class PacketFlags7(): @@ -36,10 +40,17 @@ class TwPacket: def __init__(self) -> None: self.header: Header = Header() self.messages: list[Union[CtrlMessage, GameMessage, SysMessage]] = [] - self.messages.append(GameMessage()) def __str__(self): return str(self.__class__) + ": " + str(self.__dict__) -pck = TwPacket() -print(pck) +def parse(data: bytes): + if data[2] == 0x00: # control + if data[3] == 0x04: # close + pck = TwPacket() + msg_dc = CtrlMessage('close') + pck.messages.append(msg_dc) + return pck + return TwPacket() + +