commandhandler.py
1.52 KB
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
import utils
global_aliases = {
'n': 'north',
's': 'south',
'e': 'east',
'w': 'west',
'u': 'up',
'd': 'down',
'l': 'look',
'\'': 'say',
'i': 'inventory',
'inv': 'inventory',
}
class CommandHandler(object):
def parse(self, id, cmd, params, mud, players):
if cmd in global_aliases:
cmd = global_aliases[cmd]
else:
if 'aliases' not in players[id]:
players[id]["aliases"] = {}
if cmd in players[id]["aliases"]:
cmd = players[id]["aliases"][cmd]
tokens = []
for token in params.lower().strip().split(' '):
token = token.strip()
if len(token) > 0:
tokens.append(token)
locals()['tokens'] = tokens
locals()['next_command'] = None
# try:
if cmd in utils.load_object_from_file('rooms/' + players[id]["room"] + '.json').get('exits'):
params = cmd + " " + params.lower().strip()
cmd = "go"
if cmd == '':
return True
command = cmd
ldict = locals()
with open('commands/{}.txt'.format(cmd), 'r', encoding='utf-8') as f:
exec(f.read(), globals(), ldict)
if ldict['next_command'] != None:
locals()['tokens'] = []
tokens = []
with open('commands/{}.txt'.format(ldict['next_command']), 'r', encoding='utf-8') as f:
exec(f.read())
return True
# except Exception as e:
# print('Something happened...')
# print(e)
# mud.send_message(id, "Unknown command '{}'".format(cmd))
# return False