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
3 changes: 1 addition & 2 deletions docs/getting-started/getting-player-details/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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 +65,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
34 changes: 34 additions & 0 deletions rustplus/utils/rust_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
FONT_PATH = "rustplus.utils.fonts"
GRID_DIAMETER = 146.28571428571428

PLAYER_MARKER_ONLINE_COLOR = (201, 242, 155, 255)
PLAYER_MARKER_OFFLINE_COLOR = (128, 128, 128, 255)


def format_time(protobuf: AppMessage) -> RustTime:
def convert_time(time) -> str:
Expand Down Expand Up @@ -261,3 +264,34 @@ 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,
PLAYER_MARKER_ONLINE_COLOR if player_online else PLAYER_MARKER_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