-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
218 lines (167 loc) · 4.98 KB
/
commands.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import net.mapserv as mapserv
from net.inventory import get_item_index
import monsterdb
import itemdb
from utils import preprocess_argument
from textutils import expand_links
from loggers import debuglog
__all__ = [ 'whisper_msg', 'whisper_to', 'commands',
'parse_player_name', 'process_line' ]
whisper_to = ''
whisper_msg = ''
def must_have_arg(func):
def wrapper(cmd, arg):
if len(arg) > 0:
return func(cmd, arg)
return wrapper
@preprocess_argument(expand_links)
def general_chat(msg):
mapserv.cmsg_chat_message(msg)
@preprocess_argument(expand_links, 1)
def send_whisper(to_, msg):
global whisper_to, whisper_msg
whisper_to = to_
whisper_msg = msg
mapserv.cmsg_chat_whisper(to_, msg)
@must_have_arg
def send_whisper_internal(_, arg):
nick, message = parse_player_name(arg)
if len(nick) > 0 and len(message) > 0:
send_whisper(nick, message)
@preprocess_argument(expand_links)
def send_party_message(msg):
mapserv.cmsg_party_message(msg)
@must_have_arg
def send_party_message_internal(_, arg):
send_party_message(arg)
@must_have_arg
def set_direction(_, dir_str):
d = {"down": 0, "left": 2, "up": 4, "right": 6}
dir_num = d.get(dir_str.lower(), 0)
mapserv.cmsg_player_change_dir(dir_num)
def sit_or_stand(cmd, _):
a = {"sit": 2, "stand": 3}
try:
action = a[cmd]
mapserv.cmsg_player_change_act(0, action)
except KeyError:
pass
@must_have_arg
def set_destination(_, xy):
try:
x, y = map(int, xy.split())
mapserv.cmsg_player_change_dest(x, y)
except ValueError:
pass
@must_have_arg
def show_emote(_, emote):
try:
mapserv.cmsg_player_emote(int(emote))
except ValueError:
pass
@must_have_arg
def attack(_, name_or_id):
target_id = -10
mob_db = monsterdb.monster_db
try:
target_id = int(name_or_id)
if target_id not in mapserv.beings_cache:
raise ValueError
except ValueError:
for b in mapserv.beings_cache:
if b.name == name_or_id:
target_id = b.id
break
if b.job in mob_db:
if mob_db[b.job] == name_or_id:
target_id = b.id
break
if target_id > 0:
mapserv.cmsg_player_change_act(target_id, 7)
else:
debuglog.warning("Being %s not found", name_or_id)
@must_have_arg
def me_action(_, arg):
general_chat("*{}*".format(arg))
@must_have_arg
def item_use(_, name_or_id):
item_id = -10
item_db = itemdb.item_names
try:
item_id = int(name_or_id)
except ValueError:
for id_, name in item_db.iteritems():
if name == name_or_id:
item_id = id_
if item_id < 0:
debuglog.warning("Unknown item: %s", name_or_id)
return
index = get_item_index(item_id)
if index > 0:
mapserv.cmsg_player_inventory_use(index, item_id)
else:
debuglog.error("You don't have %s", name_or_id)
def print_beings(cmd, _):
for being in mapserv.beings_cache.itervalues():
debuglog.info("id: %d name: %s type: %s",
being.id, being.name, being.type)
def print_help(cmd, _):
s = ' '.join(commands.keys())
debuglog.info("[help] commands: %s", s)
def command_not_found(cmd):
debuglog.warning("[warning] command not found: %s. Try /help.", cmd)
def parse_player_name(line):
line = line.lstrip()
if len(line) < 2:
return "", ""
if line[0] == '"':
end = line[1:].find('"')
if end < 0:
return line[1:], ""
else:
return line[1:end + 1], line[end + 3:]
else:
end = line.find(" ")
if end < 0:
return line, ""
else:
return line[:end], line[end + 1:]
commands = {
"w" : send_whisper_internal,
"whisper" : send_whisper_internal,
"p" : send_party_message_internal,
"party" : send_party_message_internal,
"e" : show_emote,
"emote" : show_emote,
"dir" : set_direction,
"direction" : set_direction,
"turn" : set_direction,
"sit" : sit_or_stand,
"stand" : sit_or_stand,
"goto" : set_destination,
"nav" : set_destination,
"dest" : set_destination,
"me" : me_action,
"use" : item_use,
"attack" : attack,
"beings" : print_beings,
"help" : print_help,
}
def process_line(line):
if line == "":
return
elif line[0] == "/":
end = line.find(" ")
if end < 0:
cmd = line[1:]
arg = ""
else:
cmd = line[1:end]
arg = line[end + 1:]
if cmd in commands:
func = commands[cmd]
func(cmd, arg)
else:
command_not_found(cmd)
else:
general_chat(line)