Skip to content

Commit 497d00d

Browse files
author
KirillMysnik
committed
Added command frontend
1 parent e764cae commit 497d00d

File tree

1 file changed

+173
-0
lines changed
  • srcds/addons/source-python/plugins/admin/core/frontends

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# =============================================================================
2+
# >> IMPORTS
3+
# =============================================================================
4+
# Source.Python
5+
from commands import CommandReturn
6+
from commands.typed import TypedClientCommand, TypedSayCommand
7+
from filters.players import PlayerIter
8+
9+
# Source.Python Admin
10+
from admin.core.clients import clients
11+
12+
13+
# =============================================================================
14+
# >> FUNCTIONS
15+
# =============================================================================
16+
def filter_player(filter_str, filter_args, issuer, player):
17+
if filter_str[0] == '!':
18+
return not filter_player(filter_str[1:], filter_args, issuer, player)
19+
20+
if filter_str[0] == '@':
21+
player_filter = filter_str[1:]
22+
if player_filter in PlayerIter.filters:
23+
return PlayerIter.filters[player_filter](player)
24+
25+
if player_filter in ('me', 'self'):
26+
return issuer == player
27+
28+
return False
29+
30+
if filter_str[0] == '#':
31+
try:
32+
userid = int(filter_str[1:])
33+
except ValueError:
34+
return False
35+
36+
return player.userid == userid
37+
38+
if filter_str.lower() == 'name':
39+
return player.name.casefold() == filter_args.casefold()
40+
41+
if filter_str.lower() in ('steamid', 'steam'):
42+
return player.steamid.lower() == filter_args.lower()
43+
44+
if filter_str.lower() == 'index':
45+
try:
46+
index = int(filter_args)
47+
except ValueError:
48+
return False
49+
50+
return player.index == index
51+
52+
return False
53+
54+
55+
def iter_filter_targets(filter_str, filter_args, issuer):
56+
for player in PlayerIter():
57+
if filter_player(filter_str, filter_args, issuer, player):
58+
yield player
59+
60+
61+
# =============================================================================
62+
# >> CLASSES
63+
# =============================================================================
64+
class BaseFeatureCommand:
65+
def __init__(self, commands, feature):
66+
if isinstance(commands, str):
67+
commands = [commands, ]
68+
69+
commands = list(commands)
70+
71+
self.feature = feature
72+
73+
TypedSayCommand(
74+
['!spa', ] + commands, feature.flag
75+
)(self._get_public_chat_callback())
76+
77+
TypedSayCommand(
78+
['/spa', ] + commands, feature.flag
79+
)(self._get_private_chat_callback())
80+
81+
TypedClientCommand(
82+
['spa', ] + commands, feature.flag
83+
)(self._get_client_callback())
84+
85+
def _get_public_chat_callback(self):
86+
raise NotImplementedError
87+
88+
def _get_private_chat_callback(self):
89+
raise NotImplementedError
90+
91+
def _get_client_callback(self):
92+
raise NotImplementedError
93+
94+
95+
class FeatureCommand(BaseFeatureCommand):
96+
def _get_public_chat_callback(self):
97+
def public_chat_callback(command_info):
98+
client = clients[command_info.index]
99+
100+
# Sync execution to avoid issuer replacement if the feature itself
101+
# is going to execute any commands
102+
client.sync_execution(self.feature.execute, (client, ))
103+
104+
return CommandReturn.CONTINUE
105+
106+
return public_chat_callback
107+
108+
def _get_private_chat_callback(self):
109+
def private_chat_callback(command_info):
110+
client = clients[command_info.index]
111+
client.sync_execution(self.feature.execute, (client,))
112+
return CommandReturn.BLOCK
113+
114+
return private_chat_callback
115+
116+
def _get_client_callback(self):
117+
def client_callback(command_info):
118+
client = clients[command_info.index]
119+
client.sync_execution(self.feature.execute, (client,))
120+
121+
return client_callback
122+
123+
124+
class PlayerBasedFeatureCommand(BaseFeatureCommand):
125+
def _get_public_chat_callback(self):
126+
def public_chat_callback(
127+
command_info, filter_str:str, filter_args:str=""):
128+
129+
client = clients[command_info.index]
130+
131+
for player in iter_filter_targets(
132+
filter_str, filter_args, client.player):
133+
134+
if not self.feature.filter(client, player):
135+
continue
136+
137+
client.sync_execution(self.feature.execute, (client, player))
138+
139+
return CommandReturn.CONTINUE
140+
141+
return public_chat_callback
142+
143+
def _get_private_chat_callback(self):
144+
def private_chat_callback(
145+
command_info, filter_str:str, filter_args:str=""):
146+
147+
client = clients[command_info.index]
148+
149+
for player in iter_filter_targets(
150+
filter_str, filter_args, client.player):
151+
152+
if not self.feature.filter(client, player):
153+
continue
154+
155+
client.sync_execution(self.feature.execute, (client, player))
156+
157+
return CommandReturn.BLOCK
158+
159+
return private_chat_callback
160+
161+
def _get_client_callback(self):
162+
def client_callback(command_info, filter_str:str, filter_args:str=""):
163+
client = clients[command_info.index]
164+
165+
for player in iter_filter_targets(
166+
filter_str, filter_args, client.player):
167+
168+
if not self.feature.filter(client, player):
169+
continue
170+
171+
client.sync_execution(self.feature.execute, (client, player))
172+
173+
return client_callback

0 commit comments

Comments
 (0)