28639cf7 by barry

Added changes for look IN objects and some checking for bad people.

1 parent adca0e9f
...@@ -5,6 +5,10 @@ def look(id, mud, players, tokens): ...@@ -5,6 +5,10 @@ def look(id, mud, players, tokens):
5 mud.send_message(id, "") 5 mud.send_message(id, "")
6 if len(tokens) > 0 and tokens[0] == 'at': 6 if len(tokens) > 0 and tokens[0] == 'at':
7 del tokens[0] 7 del tokens[0]
8 container = False
9 if len(tokens) > 0 and tokens[0] == 'in':
10 del tokens[0]
11 container = True
8 if len(tokens) == 0: 12 if len(tokens) == 0:
9 mud.send_message(id, room_data.get('title'), color=['bold', 'green']) 13 mud.send_message(id, room_data.get('title'), color=['bold', 'green'])
10 mud.send_message(id, room_data.get('description'), line_ending='\r\n\r\n', color='green') 14 mud.send_message(id, room_data.get('description'), line_ending='\r\n\r\n', color='green')
...@@ -16,12 +20,41 @@ def look(id, mud, players, tokens): ...@@ -16,12 +20,41 @@ def look(id, mud, players, tokens):
16 mud.send_message(id, items[item]) 20 mud.send_message(id, items[item])
17 return True 21 return True
18 if subject in room_data['inventory']: 22 if subject in room_data['inventory']:
19 item = utils.load_object_from_file('inventory/' + subject + '.json') 23 if container and room_data["inventory"][subject][0].get("inventory") is not None:
20 mud.send_message(id, 'You see {}'.format(item.get('description'))) 24 items = room_data["inventory"][subject][0]["inventory"]
25 if len(items) > 0:
26 mud.send_message(id, 'You look in the {} and see:\r\n'.format(subject))
27 inv_list = []
28 for name, i in items.items():
29 if len(i) > 1:
30 inv_list.append(str(len(i)) + " " + name + "s")
31 else:
32 inv_list.append(name)
33 mud.send_list(id, inv_list, "look")
34
35 else:
36 mud.send_message(id, 'You look in the {} and see nothing.'.format(subject))
37 else:
38 item = utils.load_object_from_file('inventory/' + subject + '.json')
39 mud.send_message(id, 'You see {}'.format(item.get('description')))
21 return True 40 return True
22 if subject in players[id]['inventory']: 41 if subject in players[id]['inventory']:
23 item = utils.load_object_from_file('inventory/' + subject + '.json') 42 if container and players[id]["inventory"][subject][0].get("inventory") is not None:
24 mud.send_message(id, 'You check your inventory and see {}'.format(item.get('description'))) 43 items = players[id]["inventory"][subject][0]["inventory"]
44 if len(items) > 0:
45 mud.send_message(id, 'You look in your {} and see:\r\n'.format(subject))
46 inv_list = []
47 for name, i in items.items():
48 if len(i) > 1:
49 inv_list.append(str(len(i)) + " " + name + "s")
50 else:
51 inv_list.append(name)
52 mud.send_list(id, inv_list, "look")
53 else:
54 mud.send_message(id, 'You look in your {} and see nothing.'.format(subject))
55 else:
56 item = utils.load_object_from_file('inventory/' + subject + '.json')
57 mud.send_message(id, 'You check your inventory and see {}'.format(item.get('description')))
25 return True 58 return True
26 if subject in room_monsters: 59 if subject in room_monsters:
27 if len(room_monsters[subject]['active']) > 0: 60 if len(room_monsters[subject]['active']) > 0:
......
...@@ -142,8 +142,10 @@ while True: ...@@ -142,8 +142,10 @@ while True:
142 # if the player hasn't given their name yet, use this first command as 142 # if the player hasn't given their name yet, use this first command as
143 # their name and move them to the starting room. 143 # their name and move them to the starting room.
144 if players[id]["name"] is None: 144 if players[id]["name"] is None:
145 if command.strip() == '' or command is None: 145 if command.strip() == '' or len(command) > 12 or not command.isalnum() or command is None:
146 mud.send_message(id, "Invalid Name") 146 mud.send_message(id, "Invalid Name")
147 print("Bad guy!")
148 print(mud.get_remote_ip(id))
147 continue 149 continue
148 already_logged_in = False 150 already_logged_in = False
149 for pid, pl in players.items(): 151 for pid, pl in players.items():
......
...@@ -124,7 +124,7 @@ class MudServer(object): ...@@ -124,7 +124,7 @@ class MudServer(object):
124 # this requires root permissions, so we use a higher arbitrary port 124 # this requires root permissions, so we use a higher arbitrary port
125 # number instead: 1234. Address 0.0.0.0 means that we will bind to all 125 # number instead: 1234. Address 0.0.0.0 means that we will bind to all
126 # of the available network interfaces 126 # of the available network interfaces
127 self._listen_socket.bind(("0.0.0.0", 1234)) 127 self._listen_socket.bind(("0.0.0.0", 3333))
128 128
129 # set to non-blocking mode. This means that when we call 'accept', it 129 # set to non-blocking mode. This means that when we call 'accept', it
130 # will return immediately without waiting for a connection 130 # will return immediately without waiting for a connection
...@@ -152,6 +152,10 @@ class MudServer(object): ...@@ -152,6 +152,10 @@ class MudServer(object):
152 self._events = list(self._new_events) 152 self._events = list(self._new_events)
153 self._new_events = [] 153 self._new_events = []
154 154
155 def get_remote_ip(self, clid):
156 cl = self._clients[clid]
157 return cl.socket.getpeername()
158
155 def disconnect_player(self, clid): 159 def disconnect_player(self, clid):
156 cl = self._clients[clid] 160 cl = self._clients[clid]
157 cl.socket.close() 161 cl.socket.close()
......
1 {"name": "test", "password": true, "room": "Outside", "inventory": {"candle": [{"expires": 0}, {"expires": 0}]}, "prompt": "h %hp m %mp s %st> ", "aliases": {}, "hp": 74, "mp": 10.0, "sta": 10.00000000000004, "aa": "1d2", "mpr": 0.25, "star": 0.4, "maxhp": 953, "maxmp": 10, "maxsta": 10, "weapon": "sword", "sp": {"fireball": {"cost": 5, "dmg": "2d4", "desc": "A fireball blasts from your fingertips striking the target"}}, "at": {"kick": {"cost": 5, "dmg": "2d4", "desc": "You unleash a powerful kick"}}}
...\ No newline at end of file ...\ No newline at end of file
1 {"at": {"kick": {"desc": "You unleash a powerful kick", "dmg": "2d4", "cost": 5}}, "sp": {"fireball": {"desc": "A fireball blasts from your fingertips striking the target", "dmg": "2d4", "cost": 5}}, "mp": 10.0, "prompt": "h %hp m %mp s %st> ", "password": true, "mpr": 0.25, "maxmp": 10, "hp": 74, "maxhp": 953, "star": 0.4, "room": "Outside", "sta": 10.00000000000004, "inventory": {"bag": [{"inventory": {"candle": [{"expires": 0}, {"expires": 0}]}, "expires": 0}], "candle": [{"expires": 0}, {"expires": 0}]}, "maxsta": 10, "aa": "1d2", "weapon": "sword", "aliases": {}, "name": "test"}
...\ No newline at end of file ...\ No newline at end of file
......
1 {"title": "Outside the Tavern", "description": "You are standing outside of a simple tavern made of pressed clay and shale roof. The door becons you to come in and have a drink.", "exits": {"tavern": "Tavern"}, "look_items": {"tavern": "A roughly constructed building."}, "inventory": {"candle": [{"age": 0}, {"age": 0}], "bag": [{"expires": 0, "inventory": {}}]}}
...\ No newline at end of file ...\ No newline at end of file
1 {"inventory": {"candle": [{"age": 0}, {"age": 0}]}, "look_items": {"tavern": "A roughly constructed building."}, "title": "Outside the Tavern", "exits": {"tavern": "Tavern"}, "description": "You are standing outside of a simple tavern made of pressed clay and shale roof. The door becons you to come in and have a drink."}
...\ No newline at end of file ...\ No newline at end of file
......
1 {
2 "title": "Behind the bar",
3 "description": "The back of the bar gives a full view of the tavern. The bar top is a large wooden plank thrown across some barrels.",
4 "exits": {
5 "tavern": "Tavern"
6 },
7 "look_items": {
8 "bar": "The bar is a long wooden plank thrown over roughly hewn barrels.",
9 "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.",
10 "wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks."
11 },
12 "inventory": {}
13 }
...\ No newline at end of file ...\ No newline at end of file
1 {"description": "The back of the bar gives a full view of the tavern. The bar top is a large wooden plank thrown across some barrels.", "look_items": {"bar": "The bar is a long wooden plank thrown over roughly hewn barrels.", "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", "wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks."}, "title": "Behind the bar", "exits": {"tavern": "Tavern"}, "inventory": {}}
...\ No newline at end of file ...\ No newline at end of file
......
1 {"cricket": {"max": 1, "active": [{"mp": 10, "maxhp": 15, "hp": 15, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
...\ No newline at end of file ...\ No newline at end of file
1 {"cricket": {"active": [{"sta": 10, "target": "", "maxhp": 15, "maxmp": 10, "maxsta": 10, "hp": 15, "mp": 10, "action": "attack"}], "max": 1}}
...\ No newline at end of file ...\ No newline at end of file
......
1 {"cricket": {"max": 2, "active": [{"mp": 10, "maxhp": 20, "hp": 20, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}, {"mp": 10, "maxhp": 23, "hp": 23, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
...\ No newline at end of file ...\ No newline at end of file
1 {"cricket": {"active": [{"sta": 10, "target": "", "maxhp": 20, "maxmp": 10, "maxsta": 10, "hp": 20, "mp": 10, "action": "attack"}, {"sta": 10, "target": "", "maxhp": 23, "maxmp": 10, "maxsta": 10, "hp": 23, "mp": 10, "action": "attack"}], "max": 2}}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -37,13 +37,13 @@ def multiple_replace(text, adict): ...@@ -37,13 +37,13 @@ def multiple_replace(text, adict):
37 37
38 38
39 def save_object_to_file(obj, filename): 39 def save_object_to_file(obj, filename):
40 with open(filename, 'w', encoding='utf-8') as f: 40 with open(filename.lower(), 'w', encoding='utf-8') as f:
41 f.write(json.dumps(obj)) 41 f.write(json.dumps(obj))
42 42
43 43
44 def load_object_from_file(filename): 44 def load_object_from_file(filename):
45 try: 45 try:
46 with open(filename, 'r', encoding='utf-8') as f: 46 with open(filename.lower(), 'r', encoding='utf-8') as f:
47 return json.loads(f.read()) 47 return json.loads(f.read())
48 except Exception as e: 48 except Exception as e:
49 print('Error opening file: ' + filename) 49 print('Error opening file: ' + filename)
......