feat: draft ddnet uuid manager structure
This commit is contained in:
parent
5ef77a77a2
commit
651efbe95a
13
tests/uuid_manager_test.py
Normal file
13
tests/uuid_manager_test.py
Normal file
|
@ -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'
|
43
twnet_parser/uuid_manager.py
Normal file
43
twnet_parser/uuid_manager.py
Normal file
|
@ -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]
|
||||
|
Loading…
Reference in a new issue