Skip to content

Commit

Permalink
Add #serialize to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Dec 28, 2023
1 parent 0394c9d commit 068c77f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
3 changes: 2 additions & 1 deletion rustplus/api/structures/rust_chat_message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ..remote.rustplus_proto import AppTeamMessage
from .serialization import Serializable


class RustChatMessage:
class RustChatMessage(Serializable):
def __init__(self, data: AppTeamMessage):
self._steam_id: int = data.steam_id
self._name: str = data.name
Expand Down
4 changes: 2 additions & 2 deletions rustplus/api/structures/rust_contents.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import timedelta
from typing import List

from .serialization import Serializable
from .rust_item import RustItem


class RustContents:
class RustContents(Serializable):
def __init__(self, protection_time, has_protection, contents) -> None:
self._protection_time: timedelta = protection_time
self._has_protection: bool = has_protection
Expand Down
6 changes: 3 additions & 3 deletions rustplus/api/structures/rust_entity_info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List

from .serialization import Serializable
from ..remote import AppEntityInfo, AppEntityPayloadItem


class RustEntityInfoItem:
class RustEntityInfoItem(Serializable):
def __init__(self, data: AppEntityPayloadItem) -> None:
self._item_id: int = data.item_id
self._quantity: int = data.quantity
Expand All @@ -29,7 +29,7 @@ def __str__(self) -> str:
)


class RustEntityInfo:
class RustEntityInfo(Serializable):
def __init__(self, data: AppEntityInfo) -> None:
self._type: int = data.type
self._value: bool = data.payload.value
Expand Down
3 changes: 2 additions & 1 deletion rustplus/api/structures/rust_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ..remote.rustplus_proto import AppInfo
from .serialization import Serializable


class RustInfo:
class RustInfo(Serializable):
def __init__(self, data: AppInfo) -> None:
self._name: str = data.name
self._url: str = data.url
Expand Down
3 changes: 2 additions & 1 deletion rustplus/api/structures/rust_item.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Union
from .serialization import Serializable


class RustItem:
class RustItem(Serializable):
def __init__(
self, name: str, item_id: Union[str, int], quantity: int, is_blueprint: bool
) -> None:
Expand Down
6 changes: 3 additions & 3 deletions rustplus/api/structures/rust_map.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List

from .serialization import Serializable
from ..remote.rustplus_proto import AppMap


class RustMonument:
class RustMonument(Serializable):
def __init__(self, token, x, y) -> None:
self._token: str = token
self._x: float = x
Expand All @@ -27,7 +27,7 @@ def __str__(self) -> str:
)


class RustMap:
class RustMap(Serializable):
def __init__(self, data: AppMap) -> None:
self._width: int = data.width
self._height: int = data.height
Expand Down
8 changes: 4 additions & 4 deletions rustplus/api/structures/rust_marker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List

from .serialization import Serializable
from ..remote.rustplus_proto import Vector4, AppMarkerSellOrder, AppMarker


class RustColour:
class RustColour(Serializable):
def __init__(self, data: Vector4) -> None:
self._x: float = data.x
self._y: float = data.y
Expand Down Expand Up @@ -32,7 +32,7 @@ def __str__(self) -> str:
)


class RustSellOrder:
class RustSellOrder(Serializable):
def __init__(self, data: AppMarkerSellOrder) -> None:
self._item_id: int = data.item_id
self._quantity: int = data.quantity
Expand Down Expand Up @@ -84,7 +84,7 @@ def __str__(self) -> str:
)


class RustMarker:
class RustMarker(Serializable):
PlayerMarker = 1
ExplosionMarker = 2
VendingMachineMarker = 3
Expand Down
8 changes: 4 additions & 4 deletions rustplus/api/structures/rust_team_info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List

from .serialization import Serializable
from ..remote.rustplus_proto import AppTeamInfoMember, AppTeamInfoNote, AppTeamInfo


class RustTeamMember:
class RustTeamMember(Serializable):
def __init__(self, data: AppTeamInfoMember) -> None:
self._steam_id: int = data.steam_id
self._name: str = data.name
Expand Down Expand Up @@ -59,7 +59,7 @@ def __str__(self) -> str:
)


class RustTeamNote:
class RustTeamNote(Serializable):
def __init__(self, data: AppTeamInfoNote) -> None:
self._type: int = data.type
self._x: float = data.x
Expand Down Expand Up @@ -98,7 +98,7 @@ def __str__(self) -> str:
)


class RustTeamInfo:
class RustTeamInfo(Serializable):
def __init__(self, data: AppTeamInfo) -> None:
self._leader_steam_id: int = data.leader_steam_id
self._members = [RustTeamMember(member) for member in data.members]
Expand Down
5 changes: 4 additions & 1 deletion rustplus/api/structures/rust_time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class RustTime:
from .serialization import Serializable


class RustTime(Serializable):
def __init__(self, day_length, sunrise, sunset, time, raw_time, time_scale) -> None:
self._day_length: float = day_length
self._sunrise: str = sunrise
Expand Down
17 changes: 17 additions & 0 deletions rustplus/api/structures/serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from collections.abc import Sequence


class Serializable:

def serialize(self):
data = {}
for k, v in self.__dict__.items():
key = k if k[0] != "_" else k[1:]
if isinstance(v, Serializable):
data[key] = v.serialize()
elif isinstance(v, Sequence) and all(isinstance(element, Serializable) for element in v):
data[key] = [e.serialize() for e in v]
else:
data[key] = str(v)

return data

0 comments on commit 068c77f

Please sign in to comment.