Skip to content

Commit

Permalink
Revert "Format code with black"
Browse files Browse the repository at this point in the history
This reverts commit 34ec24d.
  • Loading branch information
olijeffers0n committed Apr 24, 2023
1 parent c314f5c commit d555f36
Show file tree
Hide file tree
Showing 39 changed files with 256 additions and 128 deletions.
19 changes: 12 additions & 7 deletions rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
"""

from .api import RustSocket
from .api.remote.camera import (CameraManager, CameraMovementOptions,
MovementControls)
from .api.remote.events import (ChatEvent, EntityEvent, MarkerEvent,
ProtobufEvent, RegisteredListener, TeamEvent)
from .api.remote.events import (
EntityEvent,
TeamEvent,
ChatEvent,
MarkerEvent,
ProtobufEvent,
RegisteredListener,
)
from .api.structures import RustMarker, Vector
from .api.remote.fcm_listener import FCMListener
from .api.remote.ratelimiter import RateLimiter
from .api.structures import RustMarker, Vector
from .commands import Command, CommandOptions
from .conversation import Conversation, ConversationFactory, ConversationPrompt
from .api.remote.camera import CameraManager, MovementControls, CameraMovementOptions
from .commands import CommandOptions, Command
from .exceptions import *
from .conversation import ConversationFactory, Conversation, ConversationPrompt
from .utils import *

__name__ = "rustplus"
Expand Down
34 changes: 21 additions & 13 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import asyncio
from typing import Callable, List, Union

from typing import List, Callable, Union
from PIL import Image

from ..commands import CommandHandler, CommandOptions
from ..commands.command_data import CommandData
from ..conversation import ConversationFactory
from ..exceptions import *
from ..utils import ServerID, deprecated
from .remote import (HeartBeat, MapEventListener, RateLimiter, RustRemote,
ServerChecker)
from .remote.camera import CameraManager
from .remote.events import (ChatEvent, EntityEvent, ProtobufEvent,
RegisteredListener, TeamEvent)
from .remote.events.event_loop_manager import EventLoopManager
from .remote.rustplus_proto import AppEmpty, AppRequest
from .structures import *
from .remote.rustplus_proto import AppEmpty, AppRequest
from .remote import RustRemote, HeartBeat, MapEventListener, ServerChecker, RateLimiter
from .remote.camera import CameraManager
from ..commands import CommandOptions, CommandHandler
from ..commands.command_data import CommandData
from ..exceptions import *
from .remote.events import (
RegisteredListener,
EntityEvent,
TeamEvent,
ChatEvent,
ProtobufEvent,
)
from ..utils import deprecated
from ..conversation import ConversationFactory
from ..utils import ServerID


class BaseRustSocket:
Expand All @@ -35,6 +39,7 @@ def __init__(
event_loop: asyncio.AbstractEventLoop = None,
rate_limiter: RateLimiter = None,
) -> None:

if ip is None:
raise ValueError("Ip cannot be None")
if steam_id is None:
Expand Down Expand Up @@ -75,6 +80,7 @@ async def _handle_ratelimit(self, amount=1) -> None:
:return: None
"""
while True:

if self.remote.ratelimiter.can_consume(self.server_id, amount):
self.remote.ratelimiter.consume(self.server_id, amount)
break
Expand Down Expand Up @@ -278,6 +284,7 @@ def command(
return RegisteredListener(coro.__name__, cmd_data.coro)

def wrap_func(coro):

if self.command_options is None:
raise CommandsNotEnabledError("Not enabled")

Expand Down Expand Up @@ -334,6 +341,7 @@ def entity_event(self, eid):
"""

def wrap_func(coro) -> RegisteredListener:

if isinstance(coro, RegisteredListener):
coro = coro.get_coro()

Expand Down
10 changes: 5 additions & 5 deletions rustplus/api/remote/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from ..remote.events.event_handler import EventHandler
from ..remote.events.map_event_listener import MapEventListener
from .heartbeat import HeartBeat
from .ratelimiter import RateLimiter
from .rust_remote_interface import RustRemote
from .rustplus_proto import *
from .rustws import RustWebsocket
from .ratelimiter import RateLimiter
from .rust_remote_interface import RustRemote
from .heartbeat import HeartBeat
from .server_checker import ServerChecker
from ..remote.events.event_handler import EventHandler
from ..remote.events.map_event_listener import MapEventListener
2 changes: 1 addition & 1 deletion rustplus/api/remote/camera/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .camera_constants import CameraMovementOptions, MovementControls
from .camera_manager import CameraManager
from .camera_constants import CameraMovementOptions, MovementControls
from .structures import CameraInfo
16 changes: 11 additions & 5 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import time
from typing import Coroutine, Iterable, List, Set, TypeVar, Union
from typing import Iterable, Union, List, Coroutine, TypeVar, Set

from PIL import Image

from ...structures import Vector
from ..events import EventHandler, EventLoopManager
from ..rustplus_proto import (AppCameraInfo, AppCameraInput, AppEmpty,
AppRequest, Vector2)
from .camera_parser import Parser
from ..events import EventLoopManager, EventHandler
from ..rustplus_proto import (
AppCameraInput,
Vector2,
AppEmpty,
AppRequest,
AppCameraInfo,
)
from ...structures import Vector
from .structures import CameraInfo, Entity, LimitedQueue

RS = TypeVar("RS", bound="RustSocket")
Expand Down Expand Up @@ -110,6 +115,7 @@ async def send_mouse_movement(self, mouse_delta: Vector) -> None:
async def send_combined_movement(
self, movements: Iterable[int] = None, joystick_vector: Vector = None
) -> None:

if joystick_vector is None:
joystick_vector = Vector()

Expand Down
22 changes: 18 additions & 4 deletions rustplus/api/remote/camera/camera_parser.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import math
import random
from importlib import resources
from math import radians, tan
from typing import Any, Dict, List, Tuple, Union

import random
from typing import Union, Tuple, List, Any, Dict
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from scipy.spatial import ConvexHull
from PIL import Image, ImageDraw, ImageFont

from .camera_constants import LOOKUP_CONSTANTS
from .structures import Entity, Vector3
Expand Down Expand Up @@ -55,6 +54,7 @@ def reset_output(self) -> None:
)

def handle_camera_ray_data(self, data) -> None:

if data is None:
return

Expand All @@ -66,6 +66,7 @@ def handle_camera_ray_data(self, data) -> None:
self._ray_lookback = [[0 for _ in range(3)] for _ in range(64)]

def step(self) -> None:

if self._rays is None:
return

Expand All @@ -79,6 +80,7 @@ def process_rays_batch(self) -> bool:
return True

for h in range(100):

if self.data_pointer >= len(self._rays.ray_data) - 1:
return True

Expand All @@ -105,6 +107,7 @@ def process_rays_batch(self) -> bool:
not (distance == 1 and alignment == 0 and material == 0)
and material != 7
):

self.colour_output[
x : x + self.scale_factor, y : y + self.scale_factor
] = MathUtils._convert_colour(
Expand Down Expand Up @@ -150,6 +153,7 @@ def next_ray(self, ray_data) -> List[Union[float, int]]:
self._ray_lookback[u][2] = i

else:

c = 192 & byte

if c == 0:
Expand Down Expand Up @@ -213,6 +217,7 @@ def handle_entities(
entity_render_distance: float,
max_entity_amount: int,
) -> Any:

image_data = np.array(image_data)

players = [player for player in entities if player.type == 2]
Expand Down Expand Up @@ -260,6 +265,7 @@ def handle_entities(
text = set()

for entity in entities:

if entity.position.z > entity_render_distance and entity.type == 1:
continue

Expand Down Expand Up @@ -301,6 +307,7 @@ def handle_entity(
aspect_ratio,
text,
) -> None:

entity.size.x = min(entity.size.x, 5)
entity.size.y = min(entity.size.y, 5)
entity.size.z = min(entity.size.z, 5)
Expand Down Expand Up @@ -411,6 +418,7 @@ def render(
entity_render_distance: float,
max_entity_amount: int,
) -> Image.Image:

# We have the output array filled with RayData objects
# We can get the material at each pixel and use that to get the colour
# We can then use the alignment to get the alpha value
Expand Down Expand Up @@ -548,6 +556,7 @@ def _convert_colour(
cls,
colour: Tuple[float, float, float, float],
) -> Tuple[int, int, int]:

if colour in cls.COLOUR_CACHE:
return cls.COLOUR_CACHE[colour]

Expand Down Expand Up @@ -580,6 +589,7 @@ def solve_quadratic(a: float, b: float, c: float, larger: bool) -> float:

@classmethod
def get_tree_vertices(cls, size) -> np.ndarray:

if size in cls.VERTEX_CACHE:
return cls.VERTEX_CACHE[size]

Expand All @@ -589,6 +599,7 @@ def get_tree_vertices(cls, size) -> np.ndarray:
vertex_list = []

for x_value in [size.y / 8, -size.y / 8]:

for i in range(number_of_segments):
angle = segment_angle * i

Expand All @@ -605,6 +616,7 @@ def get_tree_vertices(cls, size) -> np.ndarray:

@classmethod
def get_player_vertices(cls, size) -> np.ndarray:

if size in cls.VERTEX_CACHE:
return cls.VERTEX_CACHE[size]

Expand All @@ -621,7 +633,9 @@ def get_player_vertices(cls, size) -> np.ndarray:

x = 0
while x <= width:

for offset in range(-1, 2, 2):

x_value = x * offset

# Use the quadratic formula to find the y values of the top and bottom of the pill
Expand Down
2 changes: 1 addition & 1 deletion rustplus/api/remote/camera/structures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from ..rustplus_proto import AppCameraInfo, AppCameraRays, AppCameraRaysEntity
from ..rustplus_proto import AppCameraInfo, AppCameraRaysEntity, AppCameraRays


class CameraInfo:
Expand Down
7 changes: 3 additions & 4 deletions rustplus/api/remote/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .event_handler import EventHandler
from .event_loop_manager import EventLoopManager
from .events import (ChatEvent, EntityEvent, MarkerEvent, ProtobufEvent,
TeamEvent)
from .registered_listener import RegisteredListener
from .events import EntityEvent, TeamEvent, ChatEvent, MarkerEvent, ProtobufEvent
from .event_loop_manager import EventLoopManager
from .event_handler import EventHandler
12 changes: 8 additions & 4 deletions rustplus/api/remote/events/event_handler.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio
import logging
from asyncio.futures import Future
from typing import Any, Coroutine, Set
from typing import Set, Coroutine, Any

from ....utils import ServerID
from ..rustplus_proto import AppMessage
from .event_loop_manager import EventLoopManager
from .events import ChatEvent, EntityEvent, ProtobufEvent, TeamEvent
from .events import EntityEvent, TeamEvent, ChatEvent, ProtobufEvent
from .registered_listener import RegisteredListener
from .event_loop_manager import EventLoopManager
from ..rustplus_proto import AppMessage


class EventHandler:
Expand All @@ -25,6 +25,7 @@ def callback(inner_future: Future):
def run_entity_event(
self, name: str, app_message: AppMessage, server_id: ServerID
) -> None:

handlers: Set[RegisteredListener] = EntityEvent.handlers.get_handlers(
server_id
).get(str(name))
Expand All @@ -42,6 +43,7 @@ def run_entity_event(
)

def run_team_event(self, app_message: AppMessage, server_id: ServerID) -> None:

handlers: Set[RegisteredListener] = TeamEvent.handlers.get_handlers(server_id)
for handler in handlers.copy():
coro = handler.data
Expand All @@ -51,6 +53,7 @@ def run_team_event(self, app_message: AppMessage, server_id: ServerID) -> None:
)

def run_chat_event(self, app_message: AppMessage, server_id: ServerID) -> None:

handlers: Set[RegisteredListener] = ChatEvent.handlers.get_handlers(server_id)
for handler in handlers.copy():
coro = handler.data
Expand All @@ -60,6 +63,7 @@ def run_chat_event(self, app_message: AppMessage, server_id: ServerID) -> None:
)

def run_proto_event(self, byte_data: bytes, server_id: ServerID) -> None:

handlers: Set[RegisteredListener] = ProtobufEvent.handlers.get_handlers(
server_id
)
Expand Down
1 change: 1 addition & 0 deletions rustplus/api/remote/events/event_loop_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


class EventLoopManager:

_loop: Dict[ServerID, asyncio.AbstractEventLoop] = {}

@staticmethod
Expand Down
Loading

1 comment on commit d555f36

@olijeffers0n
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@psykzz It seems like the import optimiser breaks the code from a quick test locally. I know you hadn't pushed it to my repo yet, but thought I would check it

Please sign in to comment.