-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
181 lines (144 loc) · 6.04 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from filters.players import PlayerIter
from players.helpers import index_from_userid, index_from_playerinfo, playerinfo_from_index
from messages import SayText2
from messages import TextMsg
from commands import CommandReturn
from commands.client.manager import client_command_manager
from commands.server.manager import server_command_manager
from commands.say.manager import say_command_manager
from filters.errors import FilterError
from engines.server import engine_server
from .colourizer import colourize, strip_colours
import collections
def change_map(level):
engine_server.change_level(level, None)
def target_filter(filterby, source=None, multitarget=True):
"""Processes a target string and resolves it to one or more players
Args:
filterby: A string filterby in the form of
Name: "<playername>" ex: "necavi"
Userid: "#<index>" ex: "#5"
Multi-Filter: "@<filterby>" ex "@me"
source: A player to consider the source, used for filters such as @me
Returns:
A list of players that fit the filterby string
:param source:
:param filterby:
:param multitarget:
"""
playerlist = []
if filterby == "":
pass
elif multitarget and filterby[0] == "@":
if len(filterby) > 1 and filterby[1] == "!":
if source is not None and len(filterby) > 2 and filterby[2:] == "me":
source_index = source
for index in PlayerIter():
if index != source_index:
playerlist.append(index)
else:
try:
playerlist = [x for x in PlayerIter(not_filters=filterby[2:])]
except FilterError:
pass
else:
if source is not None and filterby[1:] == "me":
playerlist.append(source)
else:
try:
playerlist = [x for x in PlayerIter(is_filters=filterby[1:])]
except FilterError:
pass
elif filterby[0] == "#":
index = filterby[1:]
if index.isnumeric():
try:
playerlist.append(index_from_userid(int(index)))
except ValueError:
pass
else:
for index in PlayerIter():
playerinfo = playerinfo_from_index(index)
filterby = filterby.casefold()
if filterby in playerinfo.get_name().casefold():
playerlist.append(index)
return playerlist if multitarget else playerlist[0] if len(playerlist) == 1 else None
def message_server(message):
print(strip_colours(message))
def message_client(index, message):
SayText2(message=colourize(message)).send(index)
def message_all_clients(message):
for index in PlayerIter("human"):
message_client(index, message)
def message_console(index, message):
TextMsg(message=strip_colours(message), destination=2).send(index)
def message_all_consoles(message):
for index in PlayerIter("human"):
message_console(index, message)
class CommandSourceProxy(object):
def __init__(self, source, index=None):
self.source = source
self.index = index
def message(self, message):
if self.source == "server":
message_server(message)
elif self.source == "console":
message_console(self.index, message)
elif self.source == "say":
message_client(self.index, message)
command_list = []
class SayCommandProxy(object):
def __init__(self, command):
self.args = command.get_arg_string().strip("\"").split(" ")
self.command = command
def __getitem__(self, item):
return self.args[item]
def get_arg_count(self):
return len(self.args)
def get_arg_string(self):
return " ".join(self.args[1:])
def get_command_string(self):
return " ".join(self.args)
def get_arg(self, item):
return self[item]
def get_max_command_length(self):
return self.command.get_max_command_length()
class Command(object):
def __init__(self, names, *args, **kwargs):
"""Store the base values for the decorator."""
self.names = names
self.saynames = []
if not isinstance(names, str) and isinstance(names, collections.Iterable):
for name in self.names:
self.saynames.append("!" + name)
self.saynames.append("/" + name)
else:
self.saynames.append("!" + self.names)
self.saynames.append("/" + self.names)
self.args = args
self.kwargs = kwargs
command_list.append(self)
def command_callback(self, command, player=None, teamonly=None):
if teamonly is not None:
command = SayCommandProxy(command)
silent = True if command.args[0].startswith("/") else False
command.args[0] = command.args[0][1:]
self.callback(CommandSourceProxy("say", index_from_playerinfo(player)), command)
return CommandReturn.BLOCK if silent else CommandReturn.CONTINUE
elif player is not None:
return self.callback(CommandSourceProxy("console", index_from_playerinfo(player)), command)
else:
return self.callback(CommandSourceProxy("server"), command)
def __call__(self, callback):
self.callback = callback
client_command_manager.register_commands(self.names, self.command_callback, *self.args, **self.kwargs)
server_command_manager.register_commands(self.names, self.command_callback, *self.args, **self.kwargs)
say_command_manager.register_commands(self.saynames, self.command_callback, *self.args, **self.kwargs)
return self
def unload(self):
client_command_manager.unregister_commands(self.names, self.command_callback)
server_command_manager.unregister_commands(self.names, self.command_callback)
say_command_manager.unregister_commands(self.saynames, self.command_callback)
def unload():
for command in command_list:
command.unload()