Skip to content

Commit

Permalink
More Lax with resubscribing
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Mar 15, 2023
1 parent 9a466bd commit 108bcbc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.5.7"
__version__ = "5.5.8"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
5 changes: 4 additions & 1 deletion rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ async def exit_camera(self) -> None:
self.rust_socket.remote.ignored_responses.append(app_request.seq)

self._open = False
self._last_packets = None
self._last_packets.clear()

async def resubscribe(self) -> None:
await self.rust_socket.remote.subscribe_to_camera(self._cam_id, True)
self.time_since_last_subscribe = time.time()
self._open = True
self._last_packets.clear()
self.rust_socket.remote.camera_manager = self

async def get_entities_in_frame(self) -> List[Entity]:
if self._last_packets is None:
Expand Down
29 changes: 9 additions & 20 deletions rustplus/api/remote/camera/camera_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from PIL import Image

from .camera_constants import LOOKUP_CONSTANTS
from .structures import RayPacket


@dataclasses.dataclass
Expand All @@ -29,7 +28,7 @@ def __init__(self, width, height) -> None:
[0.7, 0.7, 0.7],
[0.8, 0.6, 0.4],
[1, 0.4, 0.4],
[0.5, 0.5, 0.5],
[1, 0.1, 0.1],
]

self.output = [None for _ in range(self.width * self.height)]
Expand All @@ -45,7 +44,7 @@ async def handle_camera_ray_data(self, data) -> None:
async def step(self) -> None:

if self._rays is None:
return None
return

while True:
if await self.process_rays_batch():
Expand Down Expand Up @@ -174,35 +173,25 @@ async def render(self) -> Image.Image:
image.putpixel(
(i % self.width, self.height - 1 - (i // self.width)),
await self._convert_colour(
(
int(colour[0] * 255),
int(colour[1] * 255),
int(colour[2] * 255),
int(alignment * 255),
)
(colour[0], colour[1], colour[2], alignment)
),
)

return image

@staticmethod
async def _convert_colour(
colour: Tuple[int, int, int, int], background: Tuple[int, int, int] = (0, 0, 0)
colour: Tuple[float, float, float, float],
background: Tuple[int, int, int] = (0, 0, 0),
) -> Tuple[int, int, int]:
normalised_colour = (
colour[0] / 255,
colour[1] / 255,
colour[2] / 255,
colour[3] / 255,
)
target_colour = (
((1 - normalised_colour[3]) * background[0]) + (normalised_colour[3] * normalised_colour[0]),
((1 - normalised_colour[3]) * background[1]) + (normalised_colour[3] * normalised_colour[1]),
((1 - normalised_colour[3]) * background[2]) + (normalised_colour[3] * normalised_colour[2])
((1 - colour[3]) * background[0]) + (colour[3] * colour[0]),
((1 - colour[3]) * background[1]) + (colour[3] * colour[1]),
((1 - colour[3]) * background[2]) + (colour[3] * colour[2]),
)

return (
min(255, int(target_colour[0] * 255)),
min(255, int(target_colour[1] * 255)),
min(255, int(target_colour[2] * 255))
min(255, int(target_colour[2] * 255)),
)
5 changes: 4 additions & 1 deletion rustplus/api/remote/camera/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, entity_data) -> None:
self.position = entity_data.position
self.rotation = entity_data.rotation
self.size = entity_data.size
self.name = entity_data.name
self.name: str = entity_data.name

def __str__(self) -> str:
return (
Expand Down Expand Up @@ -72,5 +72,8 @@ def get_last(self) -> Any:
def pop(self) -> Any:
return self._queue.pop(0)

def clear(self) -> None:
self._queue.clear()

def __len__(self) -> int:
return len(self._queue)
6 changes: 4 additions & 2 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ def entity_event_callback(future_inner: Future) -> None:
)
future.add_done_callback(entity_event_callback)

async def subscribe_to_camera(self, entity_id: int, ignore: bool = False) -> AppRequest:
async def subscribe_to_camera(
self, entity_id: int, ignore_response: bool = False
) -> AppRequest:
await self.api._handle_ratelimit()
app_request = self.api._generate_protobuf()
subscribe = AppCameraSubscribe()
Expand All @@ -235,7 +237,7 @@ async def subscribe_to_camera(self, entity_id: int, ignore: bool = False) -> App

await self.send_message(app_request)

if ignore:
if ignore_response:
self.ignored_responses.append(app_request.seq)

return app_request
Expand Down

0 comments on commit 108bcbc

Please sign in to comment.