Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite! #71

Merged
merged 38 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
71b52d3
Its a start
olijeffers0n Jun 21, 2024
f706cbc
Formatting and add all files
olijeffers0n Jun 21, 2024
1fd4a76
Add a load of Implementations
olijeffers0n Jun 24, 2024
4231d9f
Add Icons
olijeffers0n Jun 24, 2024
b391cc7
Add Commands
olijeffers0n Jun 24, 2024
7c586e6
Imports
olijeffers0n Jun 24, 2024
c7769a0
Lots of work
olijeffers0n Jun 24, 2024
3cff66d
Work
olijeffers0n Jun 24, 2024
b4056eb
Fixup to camera system
olijeffers0n Jun 24, 2024
f3b7465
Black
olijeffers0n Jun 24, 2024
85bcf3a
Return `None` if a response is not received for some magic reason, ra…
olijeffers0n Jul 1, 2024
f60d5b6
Trialling `ServerID` -> `ServerDetails`
olijeffers0n Jul 1, 2024
061a4e7
rework addresses
olijeffers0n Jul 1, 2024
edefc00
Partial Map Impl
olijeffers0n Jul 1, 2024
a8ff748
Finalise Map Impl for initial testing
olijeffers0n Jul 1, 2024
d1261e7
Add `convert_event_type_to_name`
FreezeSpell Jul 1, 2024
f02d038
Merge Master
olijeffers0n Jul 1, 2024
9f00b06
Formatting
olijeffers0n Jul 1, 2024
799dc6f
Delete old project
olijeffers0n Jul 1, 2024
e75ffa7
Seems ready to me
olijeffers0n Jul 7, 2024
4aa1ed9
Add FCM Listener
olijeffers0n Jul 7, 2024
b199d36
Fix bug, remove entity types from entity events to support subscripti…
olijeffers0n Jul 12, 2024
ca56433
Couple Of fixes
olijeffers0n Jul 12, 2024
6a75f8d
Add some entity subscription helper methods
olijeffers0n Jul 12, 2024
76f813d
Black
olijeffers0n Jul 12, 2024
08fe1f4
Add emojis back
olijeffers0n Jul 12, 2024
20b3e17
Add test server support
olijeffers0n Jul 14, 2024
ee78d8f
Update docs and fix issues with the ws closing
olijeffers0n Aug 7, 2024
b19430b
Update docs
olijeffers0n Aug 7, 2024
5589d86
Update docs
olijeffers0n Aug 7, 2024
ad789d6
Update formatting
olijeffers0n Aug 7, 2024
4942247
Fixup Emojis
olijeffers0n Aug 7, 2024
3da2741
Merge branch 'master' into rewrite
olijeffers0n Aug 7, 2024
db0cc81
Merge branch 'master' into rewrite
olijeffers0n Sep 3, 2024
7e1230b
Fix Camera System
olijeffers0n Sep 6, 2024
6a86664
Merge remote-tracking branch 'origin/rewrite' into rewrite
olijeffers0n Sep 6, 2024
0362fce
Strict credential type enforcement
olijeffers0n Sep 6, 2024
767b6dd
Black
olijeffers0n Sep 6, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v3
- uses: psf/black@stable
with:
options: "--check --verbose --exclude 'rustplus/api/remote/camera/camera_constants.py'"
options: "--check --verbose --exclude 'rustplus/remote/camera/camera_constants.py'"
src: "./rustplus"
version: "~= 24.4.2"
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
recursive-include rustplus *.png
recursive-include rustplus *.ttf
recursive-include rustplus *.ttf
include requirements.txt
2 changes: 0 additions & 2 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
* [Getting the Time](api-methods/getting-the-time.md)
* [Getting Entity Information](api-methods/getting-entity-information.md)
* [Getting Map Markers](api-methods/getting-map-markers.md)
* [Getting Current Map Events](api-methods/getting-current-map-events.md)
* [Getting Contents of Monitors](api-methods/getting-contents-of-monitors.md)
* [Promoting Players to Team Leader](api-methods/promoting-players-to-team-leader.md)
* [Toggling Smart Switches](api-methods/toggling-smart-switches.md)

## Command System

Expand Down
12 changes: 0 additions & 12 deletions docs/api-methods/getting-current-map-events.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/api-methods/getting-entity-information.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ Alarm = 2
StorageMonitor = 3
```

## Setting Entity Information

Calling `rust_socket.set_entity_value(entity_id: int, value: bool)` will set the value of the entity with the given ID to the given value.

16 changes: 14 additions & 2 deletions docs/api-methods/removing-listeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

### Registered Listeners

A registered listener is a wrapper object around the coroutine itself that will allow the listener to be removed later on. Should you need the coroutine back, call `RegisteredListener.get_coro()`.
A registered listener is a wrapper object around the coroutine itself that will allow the listener to be removed later
on. Should you need the coroutine back, call `RegisteredListener.get_coro()`.

### Removing The listener

Removing a listener is as simple as calling `RustSocket.remove_listener(RegisteredListener)` and will return a boolean value. True if a listener was removed and false otherwise
Removing a listener is as simple as using an Event's HandlerList. This is one example:

```python
@EntityEvent(server_details, 25743493)
async def on_entity_event(payload: EntityEventPayload):
await rust_socket.set_entity_value(payload.entity_id, not payload.value)


EntityEventPayload.HANDLER_LIST.unregister(on_entity_event, server_details)

# You can also unregister all listeners for a specific event
EntityEventPayload.HANDLER_LIST.unregister_all()
```
8 changes: 0 additions & 8 deletions docs/api-methods/toggling-smart-switches.md

This file was deleted.

16 changes: 9 additions & 7 deletions docs/command-system/command-decorator.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from main import server_details

# Command Decorator

The command decorator is used to mark a coroutine as a command listener. Usage:

```python
@rust_socket.command
async def hi(command: Command):
@Command(server_details)
async def hi(command: ChatCommand):
print("Command Ran!")
```

The fact that the coroutine's name is `hi` means that the command will be `<prefix>hi` .

You also get access to this `Command` object which has a slew of useful information about the how the command was called.
You also get access to this `ChatCommand` object which has a slew of useful information about the how the command was called.

| Field | Value |
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -27,16 +29,16 @@ This decorator returns a [`RegisteredListener`](../api-methods/removing-listener
You don't want to have to register 100's of commands for every permutation of phrasing, so why should you!

```python
@rust_socket.command(aliases=["hello", "hey"])
async def hi(command: Command):
@ChatCommand(server_details, aliases=["hello", "hey"])
async def hi(command: ChatCommand):
print("Command Ran!")
```

This is a simple example of how you could incorporate different function names into one command, but sometimes we need more than that!

```python
@rust_socket.command(alais_func=lambda x: x.lower() == "test")
async def pair(command: Command):
@ChatCommand(server_details, alais_func=lambda x: x.lower() == "test")
async def pair(command: ChatCommand):
print("Command Ran!")
```

Expand Down
11 changes: 4 additions & 7 deletions docs/command-system/command-options.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Command Options

Command options are what you use to tell the [`RustSocket`](../getting-started/rustsocket/) what the general structure of your commands will be. These define the prefix for the command, as well as any "overruling commands" which are commands that do not require a prefix. Usage:
Command options are what you use to tell the [`RustSocket`](../getting-started/rustsocket/) what the general structure of your commands will be.
These define the prefix for the command, as well as any "overruling commands" which are commands that do not require a prefix. Usage:

```python
from rustplus import RustSocket
from rustplus import CommandOptions

options = CommandOptions(prefix="!", overruling_commands = ["time"])

# Prefix is a string, and the overruling_commands are a list of strings which would be the name of the coroutines
options = CommandOptions(prefix="!")
```

You can then just pass these into the RustSocket constructor using the `overruling_commands` kwarg.

9 changes: 5 additions & 4 deletions docs/command-system/commands-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ Commands allow the triggering of custom coroutines when a specific keyword is se

{% code title="main.py" %}
```python
from rustplus import RustSocket, CommandOptions, Command
from rustplus import RustSocket, CommandOptions, Command, ServerDetails, Command, ChatCommand

options = CommandOptions(prefix="!") # Use whatever prefix you want here
rust_socket = RustSocket("IP", "PORT", STEAMID, PLAYERTOKEN, command_options=options)
server_details = ServerDetails("IP", "PORT", STEAMID, PLAYERTOKEN)
socket = RustSocket(server_details)

@rust_socket.command
async def hi(command : Command):
@Command(server_details)
async def hi(command : ChatCommand):
await socket.send_team_message(f"Hi, {command.sender_name}")
```
{% endcode %}
Expand Down
4 changes: 4 additions & 0 deletions docs/community-examples/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Community Examples

This page contains a list of community-contributed examples that demonstrate how to use the Library.
Contact me on Discord if you would like to add your example to this list!
46 changes: 25 additions & 21 deletions docs/event-system/events-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ Socket Events are called whenever a specific action / event happens. There are c
These will be called by the socket when the respective events occur. Here are some example usages:

{% code title="listeners.py" %}

```python
from rustplus import EntityEvent, TeamEvent, ChatEvent

@rust_socket.entity_event(ENTITYID)
async def alarm(event : EntityEvent):
value = "On" if event.value else "Off"
print(f"{entity_type_to_string(event.type)} has been turned {value}")

@rust_socket.team_event
async def team(event : TeamEvent):
print(f"The team leader's steamId is: {event.team_info.leader_steam_id}")

@rust_socket.chat_event
async def chat(event : ChatEvent):
print(f"{event.message.name}: {event.message.message}")

@rust_socket.protobuf_received
from rustplus import EntityEventPayload, TeamEventPayload, ChatEventPayload, ProtobufEvent, ChatEvent, EntityEvent, TeamEvent


@EntityEvent(server_details, 25743493)
async def alarm(event: EntityEventPayload):
value = "On" if event.value else "Off"
print(f"Entity has been turned {value}")


@TeamEvent(server_details)
async def team(event: TeamEventPayload):
print(f"The team leader's steamId is: {event.team_info.leader_steam_id}")


@ChatEvent(server_details)
async def chat(event: ChatEventPayload):
print(f"{event.message.name}: {event.message.message}")


@ProtobufEvent(server_details)
async def proto(data: bytes):
print(data)
print(data)
```
{% endcode %}

Expand All @@ -37,8 +42,7 @@ async def proto(data: bytes):
The `entity_event` decorator takes an extra parameter of the [entity id](../getting-started/getting-player-details/getting-entity-ids.md) that you are listening for changes to. The `EntityEvent` object holds information on the entity:

| Name | Description |
| ------------------- | ------------------------------------- |
| `type` | The type of entity, as an `int` |
|---------------------|---------------------------------------|
| `entity_id` | The Entity Id |
| `value` | The value of the entity, `boolean` |
| `capacity` | The capacity of the entity |
Expand All @@ -51,7 +55,7 @@ The `entity_event` decorator takes an extra parameter of the [entity id](../gett
This event is typically called when the team changes, e.g. a player leaves or joins. The `team_event` decorator will pass a `TeamEvent` object as a parameter with the following information:

| Name | Description |
| ------------- | ------------------------------------------------------------------------------- |
|---------------|---------------------------------------------------------------------------------|
| `player_info` | The `player_id` of the changed information |
| `team_info` | The [`team info`](../api-methods/getting-team-info.md) on the team that changed |

Expand All @@ -60,7 +64,7 @@ This event is typically called when the team changes, e.g. a player leaves or jo
This event is called when a message is sent to the team chat. It will give you a `ChatEvent` object when called with this information:

| Name | Description |
| --------- | ---------------------------------------------------------------- |
|-----------|------------------------------------------------------------------|
| `message` | The [message](../api-methods/getting-team-chat.md) that was sent |

### Protobuf Event
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/getting-player-details/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: This will show you how to get your personal player details using the RustCli
description: This will show you how to get your personal player details using the Web tool
---

# Getting Player Details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FCM(fcm_details).start()
```
{% endcode %}

The `on_notification` method will be called everytime a message is recieved from the game server.
The `on_notification` method will be called everytime a message is received from the game server.

The `rustplus.py.config.json` is the file created by the RustCli, when you register for FCM notifications. See:

Expand Down
10 changes: 6 additions & 4 deletions docs/getting-started/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ In order to access the API, you must first install the package using pip:
pip install rustplus
```

You must then get your Personal details using the RustCli, as shown here:
You must then get your Personal details using the web tool, as shown here:

{% content-ref url="getting-player-details/" %}
[getting-player-details](getting-player-details/)
Expand All @@ -15,10 +15,11 @@ You must then get your Personal details using the RustCli, as shown here:
{% code title="main.py" %}
```python
import asyncio
from rustplus import RustSocket
from rustplus import RustSocket, ServerDetails

async def main():
socket = RustSocket("IP", "PORT", STEAMID, PLAYERTOKEN)
server_details = ServerDetails("IP", "PORT", STEAMID, PLAYERTOKEN)
socket = RustSocket(server_details)
await socket.connect()

print(f"It is {(await socket.get_time()).time}")
Expand All @@ -29,5 +30,6 @@ asyncio.run(main())
```
{% endcode %}

This will run, and print the time on the Rust Server
This will run, and print the time on the Rust Server.

API methods will return `None` if they are not successful. This is to allow for better error handling, and reconnecting to the server.
26 changes: 8 additions & 18 deletions rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@
RustPlus, An API wrapper for interfacing with the Rust+ App API
"""

from .api import RustSocket
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.remote.camera import CameraManager, MovementControls, CameraMovementOptions
from .commands import CommandOptions, Command
from .exceptions import *
from .conversation import ConversationFactory, Conversation, ConversationPrompt
from .utils import *
from .rust_api import RustSocket
from .identification import ServerDetails
from .annotations import Command, ChatEvent, ProtobufEvent, TeamEvent, EntityEvent
from .remote.fcm import FCMListener
from .commands import CommandOptions, ChatCommand
from .events import ChatEventPayload, TeamEventPayload, EntityEventPayload
from .utils import convert_event_type_to_name, Emoji

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.6.18"
__version__ = "6.0.0"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
5 changes: 5 additions & 0 deletions rustplus/annotations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .command import Command
from .entity_event import EntityEvent
from .chat_event import ChatEvent
from .team_event import TeamEvent
from .protobuf_event import ProtobufEvent
21 changes: 21 additions & 0 deletions rustplus/annotations/chat_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 ChatEventPayload as ChatEventManager


def ChatEvent(server_details: ServerDetails) -> Callable:

def wrapper(func) -> RegisteredListener:

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

listener = RegisteredListener(func.__name__, func)

ChatEventManager.HANDLER_LIST.register(listener, server_details)

return listener

return wrapper
23 changes: 23 additions & 0 deletions rustplus/annotations/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Callable

from ..identification import RegisteredListener, ServerDetails
from ..commands import ChatCommand, ChatCommandData


def Command(
server_details: ServerDetails, aliases: list = None, alias_func: Callable = None
) -> Callable:

def wrapper(func):

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

command_data = ChatCommandData(
coroutine=func, aliases=aliases, callable_func=alias_func
)
ChatCommand.REGISTERED_COMMANDS[server_details][func.__name__] = command_data

return RegisteredListener(func.__name__, func)

return wrapper
19 changes: 19 additions & 0 deletions rustplus/annotations/entity_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Callable

from .. import ServerDetails
from ..identification import RegisteredListener
from ..events import EntityEventPayload as EntityEventManager


def EntityEvent(server_details: ServerDetails, eid: int) -> Callable:
def wrapper(func) -> RegisteredListener:
if isinstance(func, RegisteredListener):
func = func.get_coro()

listener = RegisteredListener(str(eid), func)

EntityEventManager.HANDLER_LIST.register(listener, server_details)

return listener

return wrapper
Loading
Loading