go.txt 2.5 KB
def go(id, params, players, mud, tokens, command):
    # store the exit name
    if params == '':
        params = command.strip()
    else:
        params = params.strip()

    room_data = utils.load_object_from_file('rooms/' + players[id]["room"] + '.json')
    exits = room_data['exits']
    # if the specified exit is found in the room's exits list

    if params in exits:
        # go through all the players in the game
        for pid, pl in players.items():
            # if player is in the same room and isn't the player
            # sending the command
            if players[pid]["room"] == players[id]["room"] \
                    and pid != id:
                # send them a message telling them that the player
                # left the room
                mud.send_message(pid, "{} left via exit '{}'".format(
                                              players[id]["name"], params))

        # update the player's current room to the one the exit leads to
        
        if room_data['exits'][params] == None:
            mud.send_message(id, "An invisible force prevents you from going in that direction.")
            return None
        else:
            new_room = utils.load_object_from_file('rooms/' + exits[params] + '.json')
            if not new_room:
                mud.send_message(id, "An invisible force prevents you from going in that direction.")
                return None
            else:
                players[id]["room"] = exits[params]
                mud.send_message(id, "You arrive at '{}'".format(new_room['title']))

        # go through all the players in the game
        for pid, pl in players.items():
            # if player is in the same (new) room and isn't the player
            # sending the command
            if players[pid]["room"] == players[id]["room"] \
                    and pid != id:
                # send them a message telling them that the player
                # entered the room
                mud.send_message(pid,
                                 "{} arrived via exit '{}'".format(
                                              players[id]["name"], params))

        # send the player a message telling them where they are now
        tokens = []
        return 'look'
    # the specified exit wasn't found in the current room
    else:
        # send back an 'unknown exit' message
        mud.send_message(id, "Unknown exit '{}'".format(params))
if command is None and cmd is not None:
    command = cmd
next_command = go(id, params, players, mud, tokens, command)
del go