commands.py
1.96 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
58
59
60
61
62
class CommandHandler(object):
def tokenize_params(self):
cleaned = self.params.lower().strip()
tokens = []
for token in cleaned.split(' '):
token = token.strip()
if len(token) > 0:
tokens.append(token)
self.tokens = tokens
def look(self):
self.mud.send_message(self.id, "")
if len(self.tokens) > 0 and self.tokens[0] == 'at':
del self.tokens[0]
if len(self.tokens) == 0:
self.mud.send_message(self.id, self.room.get_description())
else:
subject = self.tokens[0]
items = self.room.get_look_items()
for item in items:
if subject in item:
self.mud.send_message(self.id, items[item])
return True
self.mud.send_message(self.id, "That doesn't seem to be here.")
playershere = []
# go through every player in the game
for pid, pl in self.players.items():
# if they're in the same room as the player
if self.players[pid]["room"] == self.players[self.id]["room"]:
# ... and they have a name to be shown
if self.players[pid]["name"] is not None:
# add their name to the list
playershere.append(self.players[pid]["name"])
# send player a message containing the list of players in the room
self.mud.send_message(self.id, "Players here: {}".format(
", ".join(playershere)))
# send player a message containing the list of exits from this room
self.mud.send_message(self.id, "Exits are: {}".format(
", ".join(self.room.get_exits())))
return True
def parse(self, id, cmd, params, room, mud, players):
self.id = id
self.cmd = cmd
self.params = params
self.tokenize_params()
self.room = room
self.mud = mud
self.players = players
try:
method = getattr(self, cmd)
return method()
except AttributeError:
return False