put.txt 2.41 KB
def put(id, tokens, players, mud):
    if len(tokens) == 0:
        mud.send_message(id, 'What do you want to put and where do you want to put it?')
        return True
    subject = tokens[0]
    if 'in' == tokens[1]:
        del tokens[1]
    dest = tokens[1]

    room_name = players[id]["room"]
    room_data = utils.load_object_from_file('rooms/' + room_name + '.json')

    if subject in players[id]['inventory']:
        if dest in players[id]['inventory'] or dest in room_data['inventory']:

            if dest in room_data['inventory']:
                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']:
                #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())
            if len(players[id]['inventory'][tokens[0]]) <= 0:
                del players[id]['inventory'][tokens[0]]

            utils.save_object_to_file(room_data, 'rooms/' + room_name + '.json')
            for pid, pl in players.items():
                # if they're in the same room as the player
                if players[pid]["room"] == players[id]["room"]:
                    # send them a message telling them what the player said
                    mud.send_message(pid, "{} put a {} in a {}.".format(players[id]["name"], subject, dest))
            utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
        else:
            mud.send_message(id, 'You cannot put {} in {} since it is not here.'.format(subject, dest))
    else:
        mud.send_message(id, 'You do not have {} in your inventory.'.format(subject))

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