get.txt 3.99 KB
def get(id, params, tokens, players, mud):
    room_name = players[id]["room"]
    if len(tokens) == 0:
        mud.send_message(id, 'What do you want to get?')
        return True

    room_data = utils.load_object_from_file('rooms/' + room_name + '.json')

    container = None
    if any(x in params for x in ['from', 'out of']):
        if 'out of' in params:
            container = params.split('out of')[1].strip()
        else:
            container = params.split('from')[1].strip()
        if room_data["inventory"].get(container) is None and players[id]["inventory"].get(container) is None:
            mud.send_message(id, 'You do not see the container {} here.'.format(container))
            return True

    if container:
        found = False
        if container in players[id]["inventory"]:
            if tokens[0] in players[id]['inventory'][container][0]['inventory']:
                found = True
                if tokens[0] in players[id]['inventory']:
                    print(players[id]['inventory'][container][0]['inventory'][tokens[0]])
                    players[id]['inventory'][tokens[0]].append(players[id]['inventory'][container][0]['inventory'][tokens[0]].pop())
                else:
                    players[id]['inventory'][tokens[0]] = [players[id]['inventory'][container][0]['inventory'][tokens[0]].pop()]
                if len(players[id]['inventory'][container][0]['inventory'][tokens[0]]) <= 0:
                    del players[id]['inventory'][container][0]['inventory'][tokens[0]]
        elif container in room_data["inventory"]:
            if tokens[0] in room_data['inventory'][container][0]['inventory']:
                found = True
                if tokens[0] in players[id]['inventory']:
                    players[id]['inventory'][tokens[0]].append(room_data['inventory'][container][0]['inventory'][tokens[0]].pop())
                else:
                    players[id]['inventory'][tokens[0]] = [room_data['inventory'][container][0]['inventory'][tokens[0]].pop()]
                if len(room_data['inventory'][container][0]['inventory'][tokens[0]]) <= 0:
                    del room_data['inventory'][container][0]['inventory'][tokens[0]]
        if not found:
            mud.send_message(id, 'You do not see a {} in the container {}.'.format(tokens[0], container))
        else:
            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, "{} picked up a {} from a {}.".format(players[id]["name"], tokens[0], container))
            utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
            utils.save_object_to_file(room_data, 'rooms/' + room_name + '.json')
        return True
    else:
        if tokens[0] in room_data.get('inventory'):
            if tokens[0] in players[id]['inventory']:
                players[id]['inventory'][tokens[0]].append(room_data['inventory'][tokens[0]].pop())
            else:
                players[id]['inventory'][tokens[0]] = [room_data['inventory'][tokens[0]].pop()]

            if len(room_data['inventory'][tokens[0]]) <= 0:
                del room_data['inventory'][tokens[0]]
            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, "{} picked up a {}.".format(
                                                players[id]["name"], tokens[0]))
            utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
            utils.save_object_to_file(room_data, 'rooms/' + room_name + '.json')
        else:
            mud.send_message(id, 'There is no {} here to get.'.format(tokens[0]))

get(id, params.lower().strip(), tokens, players, mud)