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):
mud.send_message(id, "")
if len(tokens) > 0 and tokens[0] == 'at':
del tokens[0]
container = False
if len(tokens) > 0 and tokens[0] == 'in':
del tokens[0]
container = True
if len(tokens) == 0:
mud.send_message(id, room_data.get('title'), color=['bold', 'green'])
mud.send_message(id, room_data.get('description'), line_ending='\r\n\r\n', color='green')
......@@ -16,10 +20,39 @@ def look(id, mud, players, tokens):
mud.send_message(id, items[item])
return True
if subject in room_data['inventory']:
if container and room_data["inventory"][subject][0].get("inventory") is not None:
items = room_data["inventory"][subject][0]["inventory"]
if len(items) > 0:
mud.send_message(id, 'You look in the {} and see:\r\n'.format(subject))
inv_list = []
for name, i in items.items():
if len(i) > 1:
inv_list.append(str(len(i)) + " " + name + "s")
else:
inv_list.append(name)
mud.send_list(id, inv_list, "look")
else:
mud.send_message(id, 'You look in the {} and see nothing.'.format(subject))
else:
item = utils.load_object_from_file('inventory/' + subject + '.json')
mud.send_message(id, 'You see {}'.format(item.get('description')))
return True
if subject in players[id]['inventory']:
if container and players[id]["inventory"][subject][0].get("inventory") is not None:
items = players[id]["inventory"][subject][0]["inventory"]
if len(items) > 0:
mud.send_message(id, 'You look in your {} and see:\r\n'.format(subject))
inv_list = []
for name, i in items.items():
if len(i) > 1:
inv_list.append(str(len(i)) + " " + name + "s")
else:
inv_list.append(name)
mud.send_list(id, inv_list, "look")
else:
mud.send_message(id, 'You look in your {} and see nothing.'.format(subject))
else:
item = utils.load_object_from_file('inventory/' + subject + '.json')
mud.send_message(id, 'You check your inventory and see {}'.format(item.get('description')))
return True
......
......@@ -142,8 +142,10 @@ while True:
# if the player hasn't given their name yet, use this first command as
# their name and move them to the starting room.
if players[id]["name"] is None:
if command.strip() == '' or command is None:
if command.strip() == '' or len(command) > 12 or not command.isalnum() or command is None:
mud.send_message(id, "Invalid Name")
print("Bad guy!")
print(mud.get_remote_ip(id))
continue
already_logged_in = False
for pid, pl in players.items():
......
......@@ -124,7 +124,7 @@ class MudServer(object):
# this requires root permissions, so we use a higher arbitrary port
# number instead: 1234. Address 0.0.0.0 means that we will bind to all
# of the available network interfaces
self._listen_socket.bind(("0.0.0.0", 1234))
self._listen_socket.bind(("0.0.0.0", 3333))
# set to non-blocking mode. This means that when we call 'accept', it
# will return immediately without waiting for a connection
......@@ -152,6 +152,10 @@ class MudServer(object):
self._events = list(self._new_events)
self._new_events = []
def get_remote_ip(self, clid):
cl = self._clients[clid]
return cl.socket.getpeername()
def disconnect_player(self, clid):
cl = self._clients[clid]
cl.socket.close()
......
{"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
{"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
......
{"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
{"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
......
{
"title": "Behind the bar",
"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.",
"exits": {
"tavern": "Tavern"
},
"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."
},
"inventory": {}
}
\ No newline at end of file
{"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
......
{"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
{"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
......
{"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
{"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
......
......@@ -37,13 +37,13 @@ def multiple_replace(text, adict):
def save_object_to_file(obj, filename):
with open(filename, 'w', encoding='utf-8') as f:
with open(filename.lower(), 'w', encoding='utf-8') as f:
f.write(json.dumps(obj))
def load_object_from_file(filename):
try:
with open(filename, 'r', encoding='utf-8') as f:
with open(filename.lower(), 'r', encoding='utf-8') as f:
return json.loads(f.read())
except Exception as e:
print('Error opening file: ' + filename)
......