look.txt 3.02 KB
def look(id, mud, players, tokens):
    room_name = players[id]["room"]
    room_data = utils.load_object_from_file('rooms/' + room_name + '.json')
    room_monsters = utils.load_object_from_file('rooms/' + room_name + '_monsters.json')
    mud.send_message(id, "")
    if len(tokens) > 0 and tokens[0] == 'at':
        del tokens[0]
    if len(tokens) == 0:
        mud.send_message(id, room_data.get('title'), color=['bold', 'green'])
        mud.send_message(id, room_data.get('description'), color='green')
    else:
        subject = tokens[0]
        items = room_data.get('look_items')
        for item in items:
            if subject in item:
                mud.send_message(id, items[item])
                return True
        if subject in room_data['inventory']:
            item = utils.load_object_from_file('inventory/' + subject + '.json')
            mud.send_message(id, 'You see {}'.format(item.get('description')))
            return True
        if subject in players[id]['inventory']:
            item = utils.load_object_from_file('inventory/' + subject + '.json')
            mud.send_message(id, 'You check your inventory and see {}'.format(item.get('description')))
            return True
        if subject in room_monsters:
            if len(room_monsters[subject]['active']) > 0:
                monster_template = utils.load_object_from_file('mobs/{}.json'.format(subject))
                if monster_template:
                    mud.send_message(id, 'You see {}'.format(monster_template.get('desc').lower()))
                    return True
        mud.send_message(id, "That doesn't seem to be here.")
        return

    playershere = []
    # go through every player in the game
    for pid, pl in players.items():
        # if they're in the same room as the player
        if players[pid]["room"] == players[id]["room"]:
            # ... and they have a name to be shown
            if players[pid]["name"] is not None and players[id]["name"] != players[pid]["name"]:
                # add their name to the list
                playershere.append(players[pid]["name"])

    # send player a message containing the list of players in the room
    if len(playershere) > 0:
        mud.send_message(id, "%boldPlayers here:%reset {}".format(", ".join(playershere)))

    # send player a message containing the list of exits from this room
    mud.send_message(id, "%boldObvious Exits:%reset {}".format(", ".join(room_data.get('exits'))))

    # send player a message containing the list of exits from this room
    if len(room_data.get('inventory')) > 0:
        mud.send_message(id, "%boldItems here:%reset {}".format(", ".join(room_data.get('inventory').keys())))

    # send player a message containing the list of players in the room
    for mon_name, monster in room_monsters.items():
        count = len(monster['active'])
        if count > 1:
            mud.send_message(id, "{} {} are here.".format(count, mon_name))
        elif count > 0:
            mud.send_message(id, "a {} is here.".format(mon_name))

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