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): ...@@ -14,12 +14,14 @@ def put(id, tokens, players, mud):
14 if dest in players[id]['inventory'] or dest in room_data['inventory']: 14 if dest in players[id]['inventory'] or dest in room_data['inventory']:
15 15
16 if dest in room_data['inventory']: 16 if dest in room_data['inventory']:
17 if subject in room_data['inventory'][dest][0]["inventory"].get(subject): 17 if len(room_data['inventory'][dest][0]["inventory"]) == 0 and not room_data['inventory'][dest][0]["inventory"].get(subject):
18 room_data['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()] 18 room_data['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()]
19 else: 19 else:
20 room_data['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop()) 20 room_data['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop())
21 elif dest in players[id]['inventory']: 21 elif dest in players[id]['inventory']:
22 if subject in players[id]['inventory'][dest][0]["inventory"].get(subject): 22 #print(players[id]['inventory'][dest][0]["inventory"])
23 print(players[id]['inventory'][dest][0]["inventory"].get(subject))
24 if len(players[id]['inventory'][dest][0]["inventory"]) == 0 and not players[id]['inventory'][dest][0]["inventory"].get(subject):
23 players[id]['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()] 25 players[id]['inventory'][dest][0]["inventory"][subject] = [players[id]['inventory'][subject].pop()]
24 else: 26 else:
25 players[id]['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop()) 27 players[id]['inventory'][dest][0]["inventory"][subject].append(players[id]['inventory'][subject].pop())
......
...@@ -2,7 +2,12 @@ ...@@ -2,7 +2,12 @@
2 "name": null, 2 "name": null,
3 "password": null, 3 "password": null,
4 "room": null, 4 "room": null,
5 "inventory": {}, 5 "inventory": {
6 "bag": [{
7 "expires": 0,
8 "inventory": {}
9 }]
10 },
6 "prompt": "hp %hp mp %mp> ", 11 "prompt": "hp %hp mp %mp> ",
7 "aliases": {}, 12 "aliases": {},
8 "hp": 100, 13 "hp": 100,
......
...@@ -12,7 +12,7 @@ from sys import platform ...@@ -12,7 +12,7 @@ from sys import platform
12 12
13 from mudserver import MudServer 13 from mudserver import MudServer
14 from commandhandler import CommandHandler 14 from commandhandler import CommandHandler
15 from utils import load_object_from_file, save_object_to_file 15 from utils import load_object_from_file, save_object_to_file, password_hash
16 16
17 if 'esp' in platform: 17 if 'esp' in platform:
18 from gc import collect, mem_free 18 from gc import collect, mem_free
...@@ -123,7 +123,7 @@ while True: ...@@ -123,7 +123,7 @@ while True:
123 for pid, pl in players.items(): 123 for pid, pl in players.items():
124 # send each player a message to tell them about the diconnected 124 # send each player a message to tell them about the diconnected
125 # player 125 # player
126 if players[pid]["name"] is not None: 126 if players[pid].get("name") is not None:
127 mud.send_message(pid, "{} quit the game".format(players[pid]["name"])) 127 mud.send_message(pid, "{} quit the game".format(players[pid]["name"]))
128 128
129 # remove the player's entry in the player dictionary 129 # remove the player's entry in the player dictionary
...@@ -141,7 +141,7 @@ while True: ...@@ -141,7 +141,7 @@ while True:
141 141
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].get("name") is None:
145 if command.strip() == '' or len(command) > 12 or not command.isalnum() 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!") 147 print("Bad guy!")
...@@ -149,30 +149,41 @@ while True: ...@@ -149,30 +149,41 @@ while True:
149 continue 149 continue
150 already_logged_in = False 150 already_logged_in = False
151 for pid, pl in players.items(): 151 for pid, pl in players.items():
152 if players[pid]["name"] == command: 152 if players[pid].get("name") == command:
153 mud.send_message(id, "{} is already logged in.".format(command)) 153 mud.send_message(id, "{} is already logged in.".format(command))
154 mud.disconnect_player(id) 154 mud.disconnect_player(id)
155 already_logged_in = True
156 if already_logged_in:
157 continue 155 continue
158 players[id]["name"] = command 156 loaded_player = load_object_from_file("players/{}.json".format(command))
159
160 mud.remote_echo(id, False) 157 mud.remote_echo(id, False)
158 players[id]["name"] = command
159 if not loaded_player:
160 # Player does not exist.
161 mud.send_message(id, "Character does not exist. Please enter a password to create a new character: ")
162 else:
161 mud.send_message(id, "Enter Password: ") 163 mud.send_message(id, "Enter Password: ")
162 164
163 elif players[id]["password"] is None: 165 elif players[id]["password"] is None:
166 if players[id].get("retries", 0) > 1:
167 mud.send_message(id, "Too many attempts")
168 mud.disconnect_player(id)
169 continue
170
164 if command.strip() == '' or command is None: 171 if command.strip() == '' or command is None:
172 players[id]["retries"] = players[id].get("retries", 0) + 1
165 mud.send_message(id, "Invalid Password") 173 mud.send_message(id, "Invalid Password")
166 continue 174 continue
167 175
168 # TODO: Write password shadow file
169
170 loaded_player = load_object_from_file("players/{}.json".format(players[pid]["name"])) 176 loaded_player = load_object_from_file("players/{}.json".format(players[pid]["name"]))
171 if loaded_player is None: 177 if loaded_player is None:
172 players[id]["password"] = True 178 players[id]["password"] = password_hash(players[pid]["name"], command)
173 players[id]["room"] = "Tavern" 179 players[id]["room"] = "Tavern"
174 else: 180 else:
181 if loaded_player["password"] == password_hash(players[pid]["name"], command):
175 players[id] = loaded_player 182 players[id] = loaded_player
183 else:
184 players[id]["retries"] = players[id].get("retries", 0) + 1
185 mud.send_message(id, "Invalid Password")
186 continue
176 187
177 # go through all the players in the game 188 # go through all the players in the game
178 for pid, pl in players.items(): 189 for pid, pl in players.items():
......
...@@ -3,7 +3,7 @@ def run_mobs(players, mud): ...@@ -3,7 +3,7 @@ def run_mobs(players, mud):
3 3
4 from utils import load_object_from_file, save_object_to_file, calc_att, get_att 4 from utils import load_object_from_file, save_object_to_file, calc_att, get_att
5 for pid, player in players.items(): 5 for pid, player in players.items():
6 if not player['password']: 6 if not player or not player.get("name") or not player.get('password'):
7 continue 7 continue
8 if player['mp'] < player['maxmp']: 8 if player['mp'] < player['maxmp']:
9 players[pid]['mp'] += player['mpr'] 9 players[pid]['mp'] += player['mpr']
......
1 {"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 ...\ No newline at end of file
1 {"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 ...\ No newline at end of file
......
1 {"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 ...\ No newline at end of file
1 {"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 ...\ No newline at end of file
......
...@@ -15,6 +15,15 @@ codes = {'resetall': 0, 'bold': 1, 'underline': 4, ...@@ -15,6 +15,15 @@ codes = {'resetall': 0, 'bold': 1, 'underline': 4,
15 'white': 37} 15 'white': 37}
16 16
17 17
18 def password_hash(name, password):
19 if 'esp' in sys.platform:
20 import uhashlib
21 return ''.join(['%.2x' % i for i in uhashlib.sha1(password + 'weemud' + name).digest()])
22 else:
23 import hashlib
24 return ''.join(['%.2x' % i for i in hashlib.sha1(bytearray(password + 'weemud' + name, 'utf-8')).digest()])
25
26
18 def get_color_list(): 27 def get_color_list():
19 res = {} 28 res = {}
20 for c in codes: 29 for c in codes:
......