drop.txt 1.25 KB
def drop(id, tokens, players, mud):
    room_name = players[id]["room"]
    if len(tokens) == 0:
        mud.send_message(id, 'What do you want to drop?')
        return True
    room_data = utils.load_object_from_file('rooms/' + room_name + '.txt')
    if tokens[0] in players[id]['inventory']:
        players[id]['inventory'][tokens[0]] -= 1
        if players[id]['inventory'][tokens[0]] <= 0:
            del players[id]['inventory'][tokens[0]]
        if tokens[0] in room_data['inventory']:
          room_data['inventory'][tokens[0]] += 1
        else:
          room_data['inventory'][tokens[0]] = 1
        utils.save_object_to_file(room_data, 'rooms/' + room_name + '.txt')
        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, "{} dropped a {}.".format(
                                            players[id]["name"], tokens[0]))
        utils.save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
    else:
        mud.send_message(id, 'You do not have {} in your inventory.'.format(tokens[0]))

drop(id, tokens, players, mud)