Skip to content

Commit c893074

Browse files
committed
commands fix and client improvements
1 parent 873269f commit c893074

4 files changed

Lines changed: 103 additions & 78 deletions

File tree

.github/workflows/format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: format py files
22

33
on:
4-
workflow_dispatch:
4+
push:
55

66
permissions:
77
contents: write

entityx-test/dist/ba_root/mods/clients.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,46 @@
33
import bascenev1 as bs
44
import utils
55
from enums import Authority
6-
from typing import override
76

8-
class ClientPlayer:
9-
""" the related player to the given Client. """
7+
class Player:
8+
""" Player object. """
109
def __init__(self, player: bs.SessionPlayer) -> None:
11-
self.player = player
10+
self._player = player
11+
12+
@property
13+
def name(self) -> str:
14+
""" name of the player. """
15+
if self.exists():
16+
return self._player.getname(True, True)
17+
return ""
1218

1319
def handle(self, message) -> None:
1420
""" handles message for the player. """
15-
player = self.player.activityplayer
16-
if not player.actor:
17-
return
18-
player.actor.node.handlemessage(message)
21+
if self.is_alive():
22+
player = self._player.activityplayer
23+
player.actor.handlemessage(message)
24+
25+
def exists(self) -> bool:
26+
""" returns whether the bascenev1.SessionPlayer exists. """
27+
return self._player.exists()
28+
29+
def is_alive(self) -> bool:
30+
""" returns whether the bascenev1.Player is alive """
31+
return self.exists() and self._player.activityplayer.is_alive()
1932

2033
def remove(self) -> None:
2134
""" removes the player from game. """
22-
self.player.remove_from_game()
35+
if self.exists():
36+
self._player.remove_from_game()
2337

2438
def profiles(self) -> list[str]:
2539
""" returns the profiles of the session player.. """
26-
return self.player.inputdevice.get_player_profiles()
40+
if self.exists():
41+
return self._player.inputdevice.get_player_profiles()
42+
return []
2743

2844
def kill(self) -> None:
45+
""" kill this player. """
2946
self.handle(bs.DieMessage())
3047

3148
def freeze(self) -> None:
@@ -35,7 +52,7 @@ def thaw(self) -> None:
3552
self.handle(bs.ThawMessage())
3653

3754
class Client:
38-
""" Client class for multiple features at one place. """
55+
""" Client object. """
3956
__mute_clients = set() # class level var
4057
def __init__(
4158
self, client_id: int = 0, account_id: str = "", name: str = "", is_v2: bool = True, in_lobby: bool = True
@@ -45,14 +62,6 @@ def __init__(
4562
self.name = name
4663
self.is_v2 = is_v2
4764
self.in_lobby = in_lobby
48-
49-
@property
50-
def player(self) -> ClientPlayer | None:
51-
""" returns the ClientPlayer associated with this client.. """
52-
for splayer in bs.get_foreground_host_session().sessionplayers:
53-
if splayer.inputdevice.client_id == self.client_id:
54-
return ClientPlayer(splayer)
55-
return
5665

5766
@property
5867
def authority(self) -> Authority:
@@ -126,10 +135,18 @@ def get_clients() -> list[Client]:
126135
return clients
127136

128137
def get_client(client_id: int | str) -> Client | None:
129-
""" fetches and returns a Client class based on the client_id. """
138+
""" fetches and tries to returns a valid client. """
130139
# manual converting to avoid str cases.
131140
client_id = int(client_id)
132141
for client in get_clients():
133142
if client.client_id == client_id:
134143
return client
135-
return
144+
return
145+
146+
def get_player(player_index: int | str) -> Player | None:
147+
""" fetches and tries to returns a valid player. """
148+
# manual converting to avoid str cases.
149+
player_index = int(player_index)
150+
if session := bs.get_foreground_host_session():
151+
sessionplayer = session.sessionplayers[player_index]
152+
return Player(sessionplayer)

entityx-test/dist/ba_root/mods/cmd_core.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" core command file. """
22
# thanks to snoweee for enlightening me with decorators <3
33
from __future__ import annotations
4-
from clients import Client
4+
from clients import Client, get_client, get_player
55
from enums import Authority
66
_commands = {}
77

@@ -29,6 +29,14 @@ def command_line(msg: str, client: Client) -> str | None:
2929
try:
3030
if "args" in params:
3131
function(client, args)
32+
elif "target" in params:
33+
target = get_client(args[0])
34+
function(client, target)
35+
elif "player" in params:
36+
player = get_player(args[0])
37+
function(client, player)
38+
elif "account_id" in params:
39+
function(client, args[0])
3240
else:
3341
function(client)
3442
except:

entityx-test/dist/ba_root/mods/commands.py

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@
55
from utils import success, send
66
import bascenev1 as bs
77
import babase as ba
8-
from clients import Client, get_clients, get_client
8+
from clients import Client, Player, get_client
99

1010
# =================== #
1111
# USER COMMANDS #
1212
# =================== #
1313
@on_command(name="/list", aliases=["/ls"])
1414
def list(client: Client):
15-
""" shows the client a list of clients. """
16-
clients = get_clients()
17-
heads = u"{0:^16}{1:^16}"
15+
""" shows the client, a list of players. """
16+
heads = u"{0:^16}{1:^14}{2:^12}"
1817
sep = "\n------------------------------------------------------\n"
19-
string = heads.format("Name", "Client ID") + sep
20-
for i in clients:
21-
string += heads.format(i.name, i.client_id) + "\n"
18+
string = heads.format("Name", "Client ID", "Index ID") + sep
19+
for index, player in enumerate(bs.get_foreground_host_session().sessionplayers):
20+
string += heads.format(player.getname(True, False), player.inputdevice.client_id, index) + "\n"
2221
client.success(string)
2322

2423
@on_command(name="/pb", aliases=["/ac", "/id"])
25-
def show_account_id(client: Client, args: list[str]):
24+
def show_account_id(client: Client, target: Client):
2625
"""Shows the client's or target's account ID."""
27-
target = get_client(int(args[0])) if args else client
26+
target = target or client
2827
client.send(target.account_id, sender=f"{target.name}'s ID")
2928

3029
@on_command(name="/pm", aliases=["/dm"], usage="/pm <client id> <message>")
31-
def private_message(client: Client, args: list[str]):
32-
"""Sends a private message to another client."""
33-
target = int(args[0])
34-
target = get_client(target)
30+
def private_message(client: Client, target: Client):
31+
"""Sends a private message to target client."""
3532
message = " ".join(args[1:])
3633
name = f"{client.name} (pvt)"
3734
target.send(message, sender = name)
@@ -48,10 +45,33 @@ def end_game(client: Client):
4845
activity.end_game()
4946
success(f"{client.name} ended the game")
5047

51-
@on_command(name="/kill", authority=Authority.ADMIN, usage="/kill <client_id>")
52-
def kill_player(client: Client, args: list[str]):
48+
@on_command(name="/kill", authority=Authority.ADMIN, usage="/kill <index id>")
49+
def kill_player(client: Client, player: Player):
5350
"""Kill target player"""
54-
get_client(int(args[0])).player.kill()
51+
player.kill()
52+
53+
@on_command(name="/freeze", authority=Authority.ADMIN, usage="/freeze <index id>")
54+
def freeze_player(client: Client, player: Player):
55+
"""Freeze target player"""
56+
player.freeze()
57+
58+
@on_command(name="/thaw", authority=Authority.ADMIN, usage="/thaw <index id>")
59+
def thaw_player(client: Client, player: Player):
60+
"""thaw the freezed target player"""
61+
player.thaw()
62+
63+
@on_command(name="/remove", aliases=["/rm"], authority=Authority.ADMIN, usage="/remove <index id>")
64+
def remove_player(client: Client, player: Player):
65+
"""Remove player from game"""
66+
player.remove()
67+
client.success(f"Removed {player.name}")
68+
69+
@on_command(name="/info", aliases=["/gp", "/profiles", "/pf"], authority=Authority.ADMIN, usage="/info <index id>")
70+
def show_profiles(client: Client, player: Player):
71+
"""Show player profiles"""
72+
profiles = player.profiles()
73+
for index, profile in enumerate(profiles, start=1):
74+
client.send(f"{profile}", sender=f"{index}")
5575

5676
@on_command(name="/say", authority=Authority.ADMIN, usage="/say <message>")
5777
def server_say(client: Client, args: list[str]):
@@ -67,22 +87,15 @@ def server_say(client: Client, args: list[str]):
6787
send(message)
6888

6989
@on_command(name="/kick", authority=Authority.ADMIN, usage="/kick <client id>")
70-
def kick_player(client: Client, args: list[str]):
90+
def kick_player(client: Client, target: Client):
7191
"""Kicks a player from the server."""
72-
if target := get_client(args[0]):
92+
if target:
7393
if target.authority < client.authority:
7494
target.kick()
7595
client.success(f"kicked {target.name}")
7696
else:
7797
client.error(f"Cannot kick {target.name} (higher authority)")
7898

79-
@on_command(name="/info", aliases=["/gp", "/profiles", "/pf"], authority=Authority.ADMIN, usage="/info <client_id>")
80-
def show_profiles(client: Client, args: list[str]):
81-
"""Show player profiles"""
82-
profiles = get_client(int(args[0])).player.profiles()
83-
for index, profile in enumerate(profiles, start=1):
84-
client.send(f"{profile}", sender=f"{index}")
85-
8699
@on_command(name="/resume", authority=Authority.ADMIN)
87100
def resume_game(client: Client):
88101
"""Resume paused game"""
@@ -113,24 +126,15 @@ def set_max_players(client: Client, args: list[str]):
113126
bs.get_foreground_host_session().max_players = limit
114127
client.success(f"Limit set to {limit}")
115128

116-
@on_command(name="/remove", aliases=["/rm"], authority=Authority.ADMIN, usage="/remove <client_id>")
117-
def remove_player(client: Client, args: list[str]):
118-
"""Remove player from game"""
119-
target = get_client(int(args[0]))
120-
if not target.in_lobby:
121-
target.player.remove()
122-
client.success(f"Removed {target.name}")
123-
124129
@on_command(name="/spectator", aliases=["/lobby"], authority=Authority.ADMIN)
125130
def toggle_spectators(client: Client):
126131
"""Toggle spectator mode"""
127132
status = "allowed" if bs.storage.config.toggle(Utility.SPECTATOR) else "disallowed"
128133
success(f"{client.name} has {status} spectators")
129134

130135
@on_command(name="/mute", authority=Authority.ADMIN, usage="/mute <client_id>")
131-
def mute_player(client: Client, args: list[str]):
136+
def mute_player(client: Client, target: Client):
132137
"""Mute player"""
133-
target = get_client(int(args[0]))
134138
if target.authority < client.authority:
135139
if target.mute():
136140
client.success(f"Muted {target.name}")
@@ -140,9 +144,8 @@ def mute_player(client: Client, args: list[str]):
140144

141145

142146
@on_command(name="/unmute", authority=Authority.ADMIN, usage="/unmute <client_id>")
143-
def unmute_player(client: Client, args: list[str]):
147+
def unmute_player(client: Client, target: Client):
144148
"""Unmute player"""
145-
target = get_client(int(args[0]))
146149
if target.unmute():
147150
client.success(f"Unmuted {target.name}")
148151
target.success(f"You've been unmuted by {client.name}")
@@ -161,18 +164,17 @@ def set_teams_playlist(client: Client):
161164
# LEADER COMMANDS #
162165
# =================== #
163166

164-
@on_command(name="/ban", authority=Authority.LEADER, usage="/ban <client_id>")
165-
def ban_player(client: Client, args: list[str]):
167+
@on_command(name="/ban", authority=Authority.LEADER, usage="/ban <account_id>")
168+
def ban_player(client: Client, account_id: str):
166169
"""Ban player"""
167-
target = get_client(int(args[0]))
168-
bs.storage.roles.add(Role.BANLIST, target.account_id)
169-
client.success(f"Banned {target.name}")
170+
bs.storage.roles.add(Role.BANLIST, account_id)
171+
client.success(f"Banned {account_id}")
170172

171173
@on_command(name="/unban", authority=Authority.LEADER, usage="/unban <account_id>")
172-
def unban_player(client: Client, args: list[str]):
174+
def unban_player(client: Client, account_id: str):
173175
"""Unban account"""
174-
bs.storage.roles.remove(Role.BANLIST, args[0])
175-
client.success(f"Unbanned {args[0]}")
176+
bs.storage.roles.remove(Role.BANLIST, account_id)
177+
client.success(f"Unbanned {account_id}")
176178

177179
@on_command(name="/whitelist", aliases=["/wl"], authority=Authority.LEADER)
178180
def toggle_whitelist(client: Client):
@@ -181,27 +183,25 @@ def toggle_whitelist(client: Client):
181183
success(f"{client.name} has {status} whitelist")
182184

183185
@on_command(name="/addwl", authority=Authority.LEADER, usage="/addwl <account_id>")
184-
def add_to_whitelist(client: Client, args: list[str]):
186+
def add_to_whitelist(client: Client, account_id: str):
185187
"""Add to whitelist"""
186-
bs.storage.roles.add(Role.WHITELIST, args[0])
187-
client.success(f"Whitelisted {args[0]}")
188+
bs.storage.roles.add(Role.WHITELIST, account_id)
189+
client.success(f"Whitelisted {account_id}")
188190

189191
@on_command(name="/removewl", aliases=["/rmwl"], authority=Authority.LEADER, usage="/removewl <account_id>")
190-
def remove_from_whitelist(client: Client, args: list[str]):
192+
def remove_from_whitelist(client: Client, account_id: str):
191193
"""Remove from whitelist"""
192-
bs.storage.roles.remove(Role.WHITELIST, args[0])
193-
client.success(f"Removed {args[0]} from whitelist")
194+
bs.storage.roles.remove(Role.WHITELIST, account_id)
195+
client.success(f"Removed {account_id} from whitelist")
194196

195197
@on_command(name="/admin", authority=Authority.LEADER, usage="/admin <client_id>")
196-
def add_admin(client: Client, args: list[str]):
198+
def add_admin(client: Client, target: Client):
197199
"""Add admin"""
198-
target = get_client(int(args[0]))
199200
bs.storage.roles.add(Role.ADMIN, target.account_id)
200201
client.success(f"Added {target.name} as admin")
201202

202203
@on_command(name="/rmadmin", authority=Authority.LEADER, usage="/rmadmin <client_id>")
203-
def remove_admin(client: Client, args: list[str]):
204+
def remove_admin(client: Client, target: Client):
204205
"""Remove admin"""
205-
target = get_client(int(args[0]))
206206
bs.storage.roles.remove(Role.ADMIN, target.account_id)
207207
client.success(f"Removed {target.name} as admin")

0 commit comments

Comments
 (0)