diff --git a/tests/uuid_manager_test.py b/tests/uuid_manager_test.py new file mode 100644 index 0000000..9e36265 --- /dev/null +++ b/tests/uuid_manager_test.py @@ -0,0 +1,13 @@ +from twnet_parser.uuid_manager import UuuidMangager + + +def test_register_name_should_not_crash() -> None: + manager = UuuidMangager() + manager.register_name(0, 'foo') + +def test_register_name_get_name() -> None: + manager = UuuidMangager() + manager.register_name(0, 'foo') + + uuid = manager.get_uuid_by_type(0) + assert uuid.name == 'foo' diff --git a/twnet_parser/uuid_manager.py b/twnet_parser/uuid_manager.py new file mode 100644 index 0000000..06faa12 --- /dev/null +++ b/twnet_parser/uuid_manager.py @@ -0,0 +1,43 @@ +""" +12345678-0123-5678-0123-567890123456 +""" +UUID_MAXSTRSIZE = 37 + +UUID_INVALID = -2 + +UUID_UNKNOWN = -1 + +""" +OFFSET_UUID (65536) +""" +OFFSET_UUID = 1 << 16 + + +class DDNetUuid: + def __init__(self, type_id: int, name: str) -> None: + self.name = name + self.type_id = type_id + +class UuuidMangager: + def __init__(self) -> None: + self.uuids: list[DDNetUuid] = [] + + def get_index(self, type_id: int) -> int: + return type_id - OFFSET_UUID + + def get_id(self, type_index: int) -> int: + return type_index + OFFSET_UUID + + def register_name(self, type_id: int, name: str) -> None: + """ + Every ddnet ex message has a uuid and a unique type_id which is an + integer incremented starting from OFFSET_UUID (65536) + + What is sent over the network can also be a type_index which is + the type_id minus the OFFSET_UUID + """ + self.uuids.append(DDNetUuid(type_id, name)) + + def get_uuid_by_type(self, type_id: int) -> DDNetUuid: + return self.uuids[type_id] +