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

Added drawing of team players on the map #67

Merged
merged 17 commits into from
May 19, 2024
2 changes: 1 addition & 1 deletion docs/api-methods/getting-the-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Getting the Image

Calling `rust_socket.get_map(add_icons: bool, add_events: bool, add_vending_machines: bool, override_images: dict)` will return a `PIL.Image` with the respective additions.
Calling `rust_socket.get_map(add_icons: bool, add_events: bool, add_vending_machines: bool, add_team_positions: bool, override_images: dict, add_grid: bool)` will return a `PIL.Image` with the respective additions.

### Getting the Map Data

Expand Down
5 changes: 3 additions & 2 deletions docs/getting-started/getting-player-details/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ There is a [Chrome Extension](https://chrome.google.com/webstore/detail/rustplus
2\) Download to file \
3\) Use on the website to - get this - listen for notifications on the web!



Shurik244 marked this conversation as resolved.
Show resolved Hide resolved
### Web Listener:

This will allow you to listen for paring notifications without even opening an IDE. The messages are listened for, and then sent to your browser via a WebSocket connection. If you have any issues with them not coming through you can refresh the page and they should be there!
Expand Down Expand Up @@ -54,7 +56,7 @@ In order to get your data, you need to:
"name": "",
"playerId": "", <- This is your steam player ID
"playerToken": "", <- This is your unique token
"port": "", <- This is the token
"port": "", <- This is the port
"type": "",
"url": ""
}
Expand All @@ -65,4 +67,3 @@ You can then use these details in the Python Wrapper here:
```python
rust_socket = RustSocket("IPADDRESS", "PORT", 64BITSTEAMID, PLAYERTOKEN)
```

26 changes: 25 additions & 1 deletion rustplus/api/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
translate_id_to_stack,
deprecated,
generate_grid,
avatar_processing,
)


Expand Down Expand Up @@ -168,6 +169,7 @@ async def get_map(
add_icons: bool = False,
add_events: bool = False,
add_vending_machines: bool = False,
add_team_positions: bool = False,
override_images: dict = None,
add_grid: bool = False,
) -> Image.Image:
Expand All @@ -181,7 +183,7 @@ async def get_map(
+ (
1
if [add_icons, add_events, add_vending_machines].count(True) >= 1
else 0
else 0 + 1 if add_team_positions else 0
)
)

Expand Down Expand Up @@ -271,6 +273,28 @@ async def get_map(
(int(marker.x) - 50, map_size - int(marker.y) - 50),
vending_machine,
)
if add_team_positions:
team = await self.get_team_info()
for member in team.members:
if member.is_alive:
avatar = (
Image.open(
requests.get(
f"https://companion-rust.facepunch.com/api/avatar/{member.steam_id}",
stream=True,
).raw
)
.resize((100, 100), Image.LANCZOS)
.convert("RGBA")
)

player_avatar = avatar_processing(avatar, 5, member.is_online)

game_map.paste(
player_avatar,
(format_coord(int(member.x), int(member.y), map_size)),
player_avatar,
)

return game_map.resize((2000, 2000), Image.LANCZOS)

Expand Down
33 changes: 33 additions & 0 deletions rustplus/utils/rust_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PIL import Image, ImageDraw, ImageFont
import logging
import string
import requests
olijeffers0n marked this conversation as resolved.
Show resolved Hide resolved

from ..api.remote.rustplus_proto import AppMessage
from ..api.structures import RustTime
Expand All @@ -11,6 +12,9 @@
FONT_PATH = "rustplus.utils.fonts"
GRID_DIAMETER = 146.28571428571428

ONLINE_COLOR = (201, 242, 155, 255)
OFFLINE_COLOR = (128, 128, 128, 255)
Shurik244 marked this conversation as resolved.
Show resolved Hide resolved


def format_time(protobuf: AppMessage) -> RustTime:
def convert_time(time) -> str:
Expand Down Expand Up @@ -261,3 +265,32 @@ def generate_grid(
d.text(text_pos, text, fill=color, font=font)

return img


def avatar_processing(
image: Image.Image, border_size: int, player_online: bool = False
) -> Image.Image:
size_with_border = (
image.size[0] + 2 * border_size,
image.size[1] + 2 * border_size,
)

border_image = Image.new("RGBA", size_with_border, (0, 0, 0, 0))

mask = Image.new("L", size_with_border, 0)
draw = ImageDraw.Draw(mask)

draw.ellipse([0, 0, size_with_border[0], size_with_border[1]], fill=255)

border_layer = Image.new(
"RGBA", size_with_border, ONLINE_COLOR if player_online else OFFLINE_COLOR
)
border_image.paste(border_layer, mask=mask)

image_mask = Image.new("L", image.size, 0)
draw = ImageDraw.Draw(image_mask)
draw.ellipse([0, 0, image.size[0], image.size[1]], fill=255)

border_image.paste(image, (border_size, border_size), image_mask)

return border_image
Loading