1d912c1b by Barry

Created zone support / subdir support for rooms and basic GMCP protocol

1 parent 021a4bc9
......@@ -41,7 +41,7 @@ class CommandHandler(object):
locals()['next_command'] = None
try:
if cmd in utils.load_object_from_file('rooms/' + players[id]["room"] + '.json').get('exits'):
if cmd in (exit.lower() for exit in utils.load_object_from_file('rooms/' + players[id]["room"] + '.json').get('exits')):
params = cmd + " " + params.lower().strip()
cmd = "go"
if cmd in players[id]["sp"]:
......
def go(id, params, players, mud, tokens, command):
# store the exit name
if params == '':
params = command.strip()
params = command.strip().lower()
else:
params = params.strip()
params = params.strip().lower()
room_data = utils.load_object_from_file('rooms/' + players[id]["room"] + '.json')
exits = room_data['exits']
tmp_exits = room_data['exits']
# if the specified exit is found in the room's exits list
exits = {}
for exit, value in tmp_exits.items():
exits[exit.lower()] = value
print(exits)
if params in exits:
# go through all the players in the game
for pid, pl in players.items():
......@@ -23,7 +27,7 @@ def go(id, params, players, mud, tokens, command):
# update the player's current room to the one the exit leads to
if room_data['exits'][params] == None:
if exits[params] == None:
mud.send_message(id, "An invisible force prevents you from going in that direction.")
return None
else:
......@@ -33,6 +37,7 @@ def go(id, params, players, mud, tokens, command):
return None
else:
players[id]["room"] = exits[params]
utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
mud.send_message(id, "You arrive at '{}'".format(new_room['title']))
# go through all the players in the game
......
......@@ -10,6 +10,7 @@ def look(id, mud, players, tokens):
del tokens[0]
container = True
if len(tokens) == 0:
mud.send_room(id, room_data.get('title'), room_data.get('description'), 'exits')
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')
else:
......
......@@ -254,7 +254,7 @@ while True:
loaded_player = load_object_from_file("players/{}.json".format(players[pid]["name"]))
if loaded_player is None:
players[id]["password"] = password_hash(players[pid]["name"], command)
players[id]["room"] = "Tavern"
players[id]["room"] = "town/tavern"
else:
if loaded_player["password"] == password_hash(players[pid]["name"], command):
players[id] = loaded_player
......
......@@ -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", 3333))
self._listen_socket.bind(("0.0.0.0", 4000))
# set to non-blocking mode. This means that when we call 'accept', it
# will return immediately without waiting for a connection
......@@ -241,6 +241,30 @@ class MudServer(object):
# stop listening for new clients
self._listen_socket.close()
def send_room(self, clid, name, description, exits):
if self._clients[clid].MXP_ENABLED:
self.mxp_secure(clid, name, 10)
# def send_description(self, clid, description, command=''):
# if self._clients[clid].MXP_ENABLED:
# output = ""
# for idx, item in enumerate(list_items):
# if idx > 0:
# output += ", "
# parts = item.split(' ')
# mod_item = item
# if len(parts) > 1:
# try:
# num = int(parts[0])
# mod_item = ' '.join(parts[1:])[:-1]
# except ValueError:
# pass
# output += "<send \"{} {}\">{}</send>".format(command, mod_item, item)
# self.mxp_secure(clid, output)
# else:
# self.send_message(clid, ', '.join(list_items))
def send_list(self, clid, list_items, command=''):
if self._clients[clid].MXP_ENABLED:
output = ""
......@@ -261,8 +285,8 @@ class MudServer(object):
else:
self.send_message(clid, ', '.join(list_items))
def mxp_secure(self, clid, message):
bytes_to_send = bytearray("\x1b[1z{}\x1b[6z\r\n".format(message), 'utf-8')
def mxp_secure(self, clid, message, mxp_code="1"):
bytes_to_send = bytearray("\x1b[{}z{}\x1b[3z\r\n".format(mxp_code, message), 'utf-8')
client_socket = self._clients[clid].socket
client_socket.sendall(bytes_to_send)
......@@ -431,8 +455,11 @@ class MudServer(object):
bytes_to_send = bytearray([self._TN_IAC, self._TN_WONT, self._ECHO])
else:
bytes_to_send = bytearray([self._TN_IAC, self._TN_WILL, self._ECHO])
try:
client_socket = self._clients[clid].socket
client_socket.sendall(bytes_to_send)
except:
pass
def _send_mssp(self, client):
byte_data = bytearray([self._TN_IAC, self._TN_SUB_START, self._MSSP, self._MSSP_VAR])
......
{"look_items": {"tavern": "A roughly constructed building."}, "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.", "inventory": {}, "exits": {"tavern": "Tavern"}, "title": "Outside the Tavern"}
\ No newline at end of file
{
"look_items": {
"tavern": "A roughly constructed building."
},
"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.",
"inventory": {},
"exits": {
"Tavern": "town/tavern"
},
"title": "Outside the Tavern"
}
\ No newline at end of file
......
{"look_items": {"wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.", "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", "bar": "The bar is a long wooden plank thrown over roughly hewn barrels."}, "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.", "inventory": {"candle": [{"age": 0}]}, "exits": {"tavern": "Tavern"}, "title": "Behind the bar"}
\ No newline at end of file
{
"look_items": {
"wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.",
"barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.",
"bar": "The bar is a long wooden plank thrown over roughly hewn barrels."
},
"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.",
"inventory": {
"candle": [{
"age": 0
}]
},
"exits": {
"Tavern": "town/tavern"
},
"title": "Behind the bar"
}
\ No newline at end of file
......
{"look_items": {"wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.", "barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.", "bar": "The bar is a long wooden plank thrown over roughly hewn barrels.", "fire": "The fire crackles quietly in the corner providing a small amount of light and heat."}, "description": "You're in a cozy tavern warmed by an open fire.", "inventory": {"candle": [{"age": 0}, {"age": 0}], "sword": [{"expries": 0}]}, "exits": {"behind": "Room001", "dark": "Dark", "outside": "Outside"}, "title": "Tavern"}
\ No newline at end of file
{
"look_items": {
"wooden,oak,plank": "An old solid oak plank that has a large number of chips and drink marks.",
"barrel,barrels": "The old barrels bands are thick with oxidation and stained with the purple of spilled wine.",
"bar": "The bar is a long wooden plank thrown over roughly hewn barrels.",
"fire": "The fire crackles quietly in the corner providing a small amount of light and heat."
},
"description": "You're in a cozy tavern warmed by an open fire.",
"inventory": {
"candle": [{
"age": 0
}, {
"age": 0
}],
"sword": [{
"expries": 0
}]
},
"exits": {
"behind": "Room001",
"dark": "Dark",
"outside": "Outside"
},
"title": "Tavern",
"zone":"town"
}
\ No newline at end of file