8671340f by Barry

Bug fixes for put and added bag to every player. Added password protection

1 parent d1d1ff4e
......@@ -14,12 +14,14 @@ def put(id, tokens, players, mud):
if dest in players[id]['inventory'] or dest in room_data['inventory']:
if dest in room_data['inventory']:
if subject in room_data['inventory'][dest][0]["inventory"].get(subject):
if len(room_data['inventory'][dest][0]["inventory"]) == 0 and not room_data['inventory'][dest][0]["inventory"].get(subject):
room_data['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()]
else:
room_data['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop())
elif dest in players[id]['inventory']:
if subject in players[id]['inventory'][dest][0]["inventory"].get(subject):
#print(players[id]['inventory'][dest][0]["inventory"])
print(players[id]['inventory'][dest][0]["inventory"].get(subject))
if len(players[id]['inventory'][dest][0]["inventory"]) == 0 and not players[id]['inventory'][dest][0]["inventory"].get(subject):
players[id]['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()]
else:
players[id]['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop())
......
......@@ -2,7 +2,12 @@
"name": null,
"password": null,
"room": null,
"inventory": {},
"inventory": {
"bag": [{
"expires": 0,
"inventory": {}
}]
},
"prompt": "hp %hp mp %mp> ",
"aliases": {},
"hp": 100,
......
......@@ -12,7 +12,7 @@ from sys import platform
from mudserver import MudServer
from commandhandler import CommandHandler
from utils import load_object_from_file, save_object_to_file
from utils import load_object_from_file, save_object_to_file, password_hash
if 'esp' in platform:
from gc import collect, mem_free
......@@ -123,7 +123,7 @@ while True:
for pid, pl in players.items():
# send each player a message to tell them about the diconnected
# player
if players[pid]["name"] is not None:
if players[pid].get("name") is not None:
mud.send_message(pid, "{} quit the game".format(players[pid]["name"]))
# remove the player's entry in the player dictionary
......@@ -141,7 +141,7 @@ 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 players[id].get("name") 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!")
......@@ -149,30 +149,41 @@ while True:
continue
already_logged_in = False
for pid, pl in players.items():
if players[pid]["name"] == command:
if players[pid].get("name") == command:
mud.send_message(id, "{} is already logged in.".format(command))
mud.disconnect_player(id)
already_logged_in = True
if already_logged_in:
continue
players[id]["name"] = command
continue
loaded_player = load_object_from_file("players/{}.json".format(command))
mud.remote_echo(id, False)
mud.send_message(id, "Enter Password: ")
players[id]["name"] = command
if not loaded_player:
# Player does not exist.
mud.send_message(id, "Character does not exist. Please enter a password to create a new character: ")
else:
mud.send_message(id, "Enter Password: ")
elif players[id]["password"] is None:
if players[id].get("retries", 0) > 1:
mud.send_message(id, "Too many attempts")
mud.disconnect_player(id)
continue
if command.strip() == '' or command is None:
players[id]["retries"] = players[id].get("retries", 0) + 1
mud.send_message(id, "Invalid Password")
continue
# TODO: Write password shadow file
loaded_player = load_object_from_file("players/{}.json".format(players[pid]["name"]))
if loaded_player is None:
players[id]["password"] = True
players[id]["password"] = password_hash(players[pid]["name"], command)
players[id]["room"] = "Tavern"
else:
players[id] = loaded_player
if loaded_player["password"] == password_hash(players[pid]["name"], command):
players[id] = loaded_player
else:
players[id]["retries"] = players[id].get("retries", 0) + 1
mud.send_message(id, "Invalid Password")
continue
# go through all the players in the game
for pid, pl in players.items():
......
......@@ -3,7 +3,7 @@ def run_mobs(players, mud):
from utils import load_object_from_file, save_object_to_file, calc_att, get_att
for pid, player in players.items():
if not player['password']:
if not player or not player.get("name") or not player.get('password'):
continue
if player['mp'] < player['maxmp']:
players[pid]['mp'] += player['mpr']
......
{"mpr": 0.25, "aa": "1d2", "password": true, "sp": {"fireball": {"desc": "A fireball blasts from your fingertips striking the target", "cost": 5, "dmg": "2d4"}}, "maxhp": 953, "maxsta": 10, "inventory": {"bag": [{"expires": 0, "inventory": {"candle": [{"expires": 0}, {"expires": 0}]}}], "candle": [{"age": 0}, {"age": 0}]}, "aliases": {}, "prompt": "h %hp m %mp s %st> ", "name": "test", "hp": 74, "mp": 10.0, "maxmp": 10, "weapon": "sword", "sta": 10.00000000000004, "room": "Room001", "at": {"kick": {"desc": "You unleash a powerful kick", "cost": 5, "dmg": "2d4"}}, "star": 0.4}
\ No newline at end of file
{"mpr": 0.25, "aa": "1d2", "password": "6c76899eb15393064b4f4db94805e5862232920b", "sp": {"fireball": {"desc": "A fireball blasts from your fingertips striking the target", "cost": 5, "dmg": "2d4"}}, "maxhp": 953, "maxsta": 10, "inventory": {"bag": [{"expires": 0, "inventory": {"candle": [{"expires": 0}, {"expires": 0}]}}], "candle": [{"age": 0}, {"age": 0}]}, "aliases": {}, "prompt": "h %hp m %mp s %st> ", "name": "test", "hp": 74, "mp": 10.0, "maxmp": 10, "weapon": "sword", "sta": 10.00000000000004, "room": "Room001", "at": {"kick": {"desc": "You unleash a powerful kick", "cost": 5, "dmg": "2d4"}}, "star": 0.4}
\ 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}, {"age": 0}, {"age": 0}, {"age": 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}, {"age": 0}, {"age": 0}]}, "exits": {"behind": "Room001", "dark": "Dark", "outside": "Outside"}, "title": "Tavern"}
\ No newline at end of file
......
......@@ -15,6 +15,15 @@ codes = {'resetall': 0, 'bold': 1, 'underline': 4,
'white': 37}
def password_hash(name, password):
if 'esp' in sys.platform:
import uhashlib
return ''.join(['%.2x' % i for i in uhashlib.sha1(password + 'weemud' + name).digest()])
else:
import hashlib
return ''.join(['%.2x' % i for i in hashlib.sha1(bytearray(password + 'weemud' + name, 'utf-8')).digest()])
def get_color_list():
res = {}
for c in codes:
......