equip.txt 2.09 KB
def equip(id, players, tokens, mud):
    if len(tokens) == 0:
        mud.send_message(id, "  %green+-Item---------------------=Equipment=------------------Loc----+", nowrap=True)
        if players[id].get('equipment'):
            for item, item_list in players[id]['equipment'].items():
                if isinstance(item_list, list):
                    vals = [val for val in item_list if val]
                    if len(vals) > 0:
                        mud.send_message(id, '  %green|%reset {} {} %green|'.format("%-51s" % ', '.join(vals), item.rjust(8, ' ')))
                    else:
                        mud.send_message(id, '  %green|%reset {} {} %green|'.format("%-51s" % 'None', item.rjust(8, ' ')))
                else:
                    mud.send_message(id, '  %green|%reset {} {} %green|'.format("%-51s" % item_list or "None", item.rjust(8, ' ')))
            mud.send_message(id, '  %green|%reset {} {} %green|'.format("%-51s" % players[id]['weapon'] or "None", 'weapon'.rjust(8, ' ')))
        mud.send_message(id, "  %green+--------------------------------------------------------------+", nowrap=True)
    else:
        if tokens[0] not in players[id]["inventory"]:
            mud.send_message(id, 'You do not have {} in your inventory.'.format(tokens[0]))
        else:
            gear = utils.load_object_from_file('inventory/' + tokens[0] + '.json')
            if gear["type"] == "weapon":
                mud.send_message(id, 'That is a weapon and must be "wield".')
            elif gear["type"] != "armor":
                mud.send_message(id, 'That cannot be worn.')
            else:
                players[id]["equipment"][gear['loc']] = tokens[0]
                mud.send_message(id, 'You wear the {} on your {}.'.format(tokens[0], gear['loc']))
                players[id]["inventory"][tokens[0]].pop()
                if len(players[id]["inventory"][tokens[0]]) == 0:
                    del players[id]["inventory"][tokens[0]]
                    
                utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))


equip(id, players, tokens, mud)
del equip