Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

from .rust_api import RustSocket
from .identification import ServerDetails
from .annotations import Command, ChatEvent, ProtobufEvent, TeamEvent, EntityEvent
from .annotations import (
Command,
ChatEvent,
ProtobufEvent,
TeamEvent,
EntityEvent,
ClanEvent,
)
from .remote.fcm import FCMListener
from .remote.camera import MovementControls, CameraMovementOptions
from .commands import CommandOptions, ChatCommand
Expand All @@ -14,5 +21,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "6.0.11"
__version__ = "6.0.12"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
1 change: 1 addition & 0 deletions rustplus/annotations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .chat_event import ChatEvent
from .team_event import TeamEvent
from .protobuf_event import ProtobufEvent
from .clan_event import ClanEvent
21 changes: 21 additions & 0 deletions rustplus/annotations/clan_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Callable

from .. import ServerDetails
from ..identification import RegisteredListener
from ..events import ClanInfoEventPayload as ClanInfoEventManager


def ClanEvent(server_details: ServerDetails) -> Callable:

def wrapper(func) -> RegisteredListener:

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

listener = RegisteredListener(func.__name__, func)

ClanInfoEventManager.HANDLER_LIST.register(listener, server_details)

return listener

return wrapper
1 change: 1 addition & 0 deletions rustplus/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .entity_event import EntityEventPayload
from .team_event import TeamEventPayload
from .protobuf_event import ProtobufEventPayload
from .clan_info_event import ClanInfoEventPayload
16 changes: 15 additions & 1 deletion rustplus/events/chat_event.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
from rustplus.identification.handler_list import HandlerList
from ..structs import RustChatMessage

from typing import Union


class ChatEventPayload:
HANDLER_LIST = HandlerList()

def __init__(self, message: RustChatMessage) -> None:
def __init__(
self, message: RustChatMessage, is_clan: bool, clan_id: Union[int, None]
) -> None:
self._message = message
self._is_clan = is_clan
self._clan_id = clan_id

@property
def message(self) -> RustChatMessage:
return self._message

@property
def is_clan(self) -> bool:
return self._is_clan

@property
def clan_id(self) -> Union[int, None]:
return self._clan_id
13 changes: 13 additions & 0 deletions rustplus/events/clan_info_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rustplus.identification.handler_list import HandlerList
from ..structs import RustClanInfo


class ClanInfoEventPayload:
HANDLER_LIST = HandlerList()

def __init__(self, clan_info: RustClanInfo) -> None:
self._clan_info = clan_info

@property
def clan_info(self) -> RustClanInfo:
return self._clan_info
64 changes: 39 additions & 25 deletions rustplus/remote/websocket/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
TeamEventPayload,
ChatEventPayload,
)
from ...events.clan_info_event import ClanInfoEventPayload
from ...exceptions import RequestError
from ...identification import ServerDetails, RegisteredListener
from ...structs import RustChatMessage, RustTeamInfo
from ...structs import RustChatMessage, RustTeamInfo, RustClanInfo
from ...utils import YieldingEvent, convert_time
from ...utils.utils import error_present

Expand Down Expand Up @@ -274,14 +275,40 @@ async def handle_message(self, app_message: AppMessage) -> None:
for handler in handlers:
await handler.get_coro()(team_event)

elif self.is_message(app_message):
elif self.is_clan_broadcast(app_message):
# Clan Event
if self.debug:
self.logger.info(f"Running Clan Event: {app_message}")

# This means that the clan of the current player has changed
handlers = ClanInfoEventPayload.HANDLER_LIST.get_handlers(
self.server_details
)
clan_event = ClanInfoEventPayload(
RustClanInfo(app_message.broadcast.clan_changed.clan_info)
)
for handler in handlers:
await handler.get_coro()(clan_event)

elif self.is_message(app_message) or self.is_clan_message(app_message):
# Chat message event

is_clan = self.is_clan_message(app_message)

if self.debug:
self.logger.info(f"Running Chat Event: {app_message}")
self.logger.info(
f"Running Chat Event: {app_message}, is_clan={is_clan}"
)

handlers = ChatEventPayload.HANDLER_LIST.get_handlers(self.server_details)
chat_event = ChatEventPayload(
RustChatMessage(app_message.broadcast.team_message.message)
RustChatMessage(
app_message.broadcast.clan_message.message
if is_clan
else app_message.broadcast.team_message.message
),
is_clan,
app_message.broadcast.clan_message.clan_id if is_clan else None,
)
for handler in handlers:
await handler.get_coro()(chat_event)
Expand Down Expand Up @@ -321,6 +348,12 @@ def is_message(app_message: AppMessage) -> bool:
app_message.broadcast.team_message.message
)

@staticmethod
def is_clan_message(app_message: AppMessage) -> bool:
return betterproto.serialized_on_wire(
app_message.broadcast.clan_message.message
)

@staticmethod
def is_camera_broadcast(app_message: AppMessage) -> bool:
return betterproto.serialized_on_wire(app_message.broadcast.camera_rays)
Expand All @@ -334,27 +367,8 @@ def is_team_broadcast(app_message: AppMessage) -> bool:
return betterproto.serialized_on_wire(app_message.broadcast.team_changed)

@staticmethod
def get_proto_cost(app_request: AppRequest) -> int:
"""
Gets the cost of an AppRequest
"""
costs = [
(app_request.get_time, 1),
(app_request.send_team_message, 2),
(app_request.get_info, 1),
(app_request.get_team_chat, 1),
(app_request.get_team_info, 1),
(app_request.get_map_markers, 1),
(app_request.get_map, 5),
(app_request.set_entity_value, 1),
(app_request.get_entity_info, 1),
(app_request.promote_to_leader, 1),
]
for request, cost in costs:
if betterproto.serialized_on_wire(request):
return cost

raise ValueError()
def is_clan_broadcast(app_message: AppMessage) -> bool:
return betterproto.serialized_on_wire(app_message.broadcast.clan_changed)

@staticmethod
async def run_coroutine_non_blocking(coroutine: Coroutine) -> Task:
Expand Down
70 changes: 69 additions & 1 deletion rustplus/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
RustEntityInfo,
RustContents,
RustItem,
RustClanInfo,
RustClanMessage,
)
from .structs.rust_error import RustError
from .utils import (
Expand Down Expand Up @@ -340,7 +342,7 @@ async def get_map(

for marker in map_markers:
if add_events:
if marker.type in [2, 4, 5, 6, 8]:
if marker.type in RustMarker.Events:
icon = convert_marker(int(marker.type), marker.rotation)
if marker.type == 6:
x, y = marker.x, marker.y
Expand Down Expand Up @@ -566,3 +568,69 @@ async def get_camera_manager(self, cam_id: str) -> Union[CameraManager, RustErro
return RustError("get_camera_manager", response.response.error.error)

return CameraManager(self, cam_id, response.response.camera_subscribe_info)

async def get_clan_info(self) -> Union[RustClanInfo, RustError]:
"""
Gets the clan information for the player's current clan.

:return RustClanInfo: The clan information
"""
packet = await self._generate_request(tokens=1)
packet.get_clan_info = AppEmpty()
response = await self.ws.send_and_get(packet)

if response is None:
return RustError("get_clan_info", "No response received")

if error_present(response):
return RustError("get_clan_info", response.response.error.error)

return RustClanInfo(response.response.clan_info.clan_info)

async def get_clan_chat(self) -> Union[List[RustClanMessage], RustError]:
"""
Gets the clan chat for the player's current clan.

:return List[RustClanMessage]: The clan chat
"""
packet = await self._generate_request(tokens=1)
packet.get_clan_chat = AppEmpty()
response = await self.ws.send_and_get(packet)

if response is None:
return RustError("get_clan_chat", "No response received")

if error_present(response):
return RustError("get_clan_chat", response.response.error.error)

return [
RustClanMessage(message) for message in response.response.clan_chat.messages
]

async def send_clan_message(self, message: str) -> None:
"""
Sends a message to the in-game clan chat

:param message: The string message to send
"""

packet = await self._generate_request(tokens=2)
send_message = AppSendMessage()
send_message.message = message
packet.send_clan_message = send_message

await self.ws.send_message(packet, True)

async def set_clan_motd(self, message: str) -> None:
"""
Sets the clan's message of the day

:param message: The string message to set as the clan's message of the day
"""

packet = await self._generate_request()
send_message = AppSendMessage()
send_message.message = message
packet.set_clan_motd = send_message

await self.ws.send_message(packet, True)
1 change: 1 addition & 0 deletions rustplus/structs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .rust_item import RustItem
from .util import Vector
from .rust_error import RustError
from .clans import *
2 changes: 2 additions & 0 deletions rustplus/structs/clans/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .rust_clan_message import RustClanMessage
from .rust_clan_info import RustClanInfo
Loading
Loading