Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Rollable Table Macros: Display both the !rtm result and the item itself

July 26 (8 years ago)
First of all, I want to thank Nathanael W. for this useful script. 

I would like to know if there is a way of displaying both the !rtm result and the corresponding table item itself.

I had previously found the issue that if there is an icon for a given item in a rollable table, then only the icon will be shown, not the title. The title will only be displayed when there is no icon. Then I started editing the icons so that they include the text I want to be displayed. Now, if it were possible to show both icon and text, AND roll the corresponding dice, it would be really great.

I was wondering if there is an easy way to modify the script.

Thanks in advance for any suggestions.
July 26 (8 years ago)
The Aaron
Pro
API Scripter
I'm not overly familiar with Rollable Table Macros / !rtm and have confused it with my own RecursiveTable / !rt script.  I've been meaning to add image support to RecursiveTable, so possibly there is a solution there.

Can you give a link to Rollable Table macros and a few screen shots and commands? I can take a quick look at the code...

July 26 (8 years ago)
Thank you very much, The Aaron

The script is quite simple. It takes a rollable table, converts the titles into strings, makes a list of those strings accounting for weight, and selects an item from the list. Then what comes to the chat is a string, so inline rolls are possible.

The only command is the one doing that (there are two others to change the "from" of the chat input).

So I think if the script somehow preserved the identity of the items of the rollable table when making the list, it could call the proper item later to be imputed along with the text.

https://wiki.roll20.net/Script:Rollable_Table_Macr...

https://github.com/Roll20/roll20-api-scripts/blob/...
July 26 (8 years ago)
The Aaron
Pro
API Scripter
Hmm.  I don't actually see a way that it could display the table item's avatar image.  Can you screenshot?
July 26 (8 years ago)

Edited July 26 (8 years ago)
The table is opened so you can see the icons and titles. In the chat are results of repeatedly calling !rtm for that table. Notice how inline rolls on titles are actually rolled in the chat.



July 26 (8 years ago)
The Aaron
Pro
API Scripter
So, to be clear, this script has never output the images, and that's what you'd like?
July 26 (8 years ago)
Yes, exactly. Do you think it would be possible?

Also, is there an explanation of your RecursiveTable script? I can only get the code itself...
July 26 (8 years ago)
The Aaron
Pro
API Scripter
RecursiveTable: https://app.roll20.net/forum/post/4954818/script-u...

It is possible, and is a feature I want to add to RecursiveTable, so I'd most likely add it there and suggest using it, but if that doesn't work for you, I can probably hack it into !rtm.
July 26 (8 years ago)
As I see it, if the script takes the title of each item and makes a list of the strings, could it not take the items identity as well and make a list of two-item lists ([item title], [item id])? Then it can take the first item for !rtm purposes, and just call the second to the chat at the end.

I still get lost with node. Do not know how to get item identities nor how to work with them.
July 26 (8 years ago)
Great! Thanks! I will take a look at Recursive Table (but only tomorrow). If it does not fulfill my needs, I will let you know so maybe you can try to modify !rtm.
July 26 (8 years ago)
The Aaron
Pro
API Scripter
Actually, I just hacked it into RTM for now:
// Rollable Table Macros
// API Commands:
// !rtm table-name(required) myself/from-name(optional)


var RollableTableMacros = RollableTableMacros || ( function() {
    'use strict';
    
    var commandListener = function() {
        // Listens for API command
        on( 'chat:message', function( msg ) {
            if( msg.type === 'api' && !msg.rolltemplate ) {
                var params = msg.content.substring( 1 ).split( ' ' ),
                    command = params[0].toLowerCase();
                
                if( command === 'rtm' ) {
                    var tableName = params[1] ? params[1].toLowerCase() : '',
                        msgFrom = getFrom( params, msg.playerid );
                    findTable( msgFrom, tableName );
                }
            }
        });
    },
    
    getFrom = function( params, playerId ) {
        // Determine the sender of the messages. Defaults to table name (formatted to title case).
        var msgFrom = titleCase( params[1].replace( /-/g, ' ' ) );
        
        if( params.length > 2 ) {
            // If the optional third paramater was passed, assign sender to that string or original sender ('myself')
            msgFrom = params.splice( 2 ).join( ' ' );
            msgFrom = msgFrom.toLowerCase() == 'myself' ? ( 'player|' + playerId ) : msgFrom;
        }
        
        return msgFrom;
    },
    
    findTable = function( msgFrom, tableName ) {
        // Finds the corresponding table, outputs error message to chat if not found
        var tables = findObjs({ type: 'rollabletable', name: tableName }, { caseInsensitive: true });
        
        if( tables.length < 1 ) {
            sendChat( msgFrom, 'No such table exists.' );
        } else {
            rollTable( tables[0].id, msgFrom );
        }
    },
    
    rollTable = function( tableId, msgFrom ) {
        // Picks an item from the table
        var items = findObjs({ type: 'tableitem', rollabletableid: tableId }),
            weightedList = [];
        
        if( items.length > 0 ) {
            _.each( items, function( item ){
                // Build a weighted list to draw from
                let weight = item.get( 'weight' );
                _( weight ).times(function( ){
                    weightedList.push( item.id );
                });
            });
        
            var chosenItem = getObj( 'tableitem', weightedList[ randomInteger( weightedList.length ) - 1 ] );
        
            sendChat( msgFrom, handleImage( handleMacro( chosenItem.get( 'name' ) ), chosenItem.get( 'avatar') ) );
        } else {
            sendChat( msgFrom, 'No items on this table.' );
        }
    },
    
    handleImage = function ( message, image){
        return /^https?:\/\//.test(image) 
            ? `<div><img style="max-height: 3em; max-width: 6em; float: left;" src="${image}">${message}<div style="clear:both;"></div>`
            : message
            ;
    },


    handleMacro = function( resultText ) {
        // Recursively handles any macro calls
        var resultLines = resultText.split( "\n" );
        
        _.each( resultLines, function( line, index, resultArray ){
            var lineArray = line.split( ' ' );
            
            _.each( lineArray, function( word, index, parentArray ){
                if( word[0] === '#' ) {
                    var macro = findObjs({ type: 'macro', name: word.substring( 1 ) });
                
                    parentArray[ index ] = macro.length > 0 ? handleMacro( macro[0].get( 'action' ) ) : word;
                }
            });
            
            resultArray[ index ] = lineArray.join( ' ' );
        });
        
        return resultLines.join( "\n" );
    },
    
    titleCase = function(str) {
        // returns the string in title case (each word capitalized)
        return str.toLowerCase().replace(/(^| )(\w)/g, function(x) {
            return x.toUpperCase();
        });
    };
    
    return {
        CommandListener: commandListener
    };
    
}());


on( 'ready', function(){
   'use strict';
   
   RollableTableMacros.CommandListener();
});



July 26 (8 years ago)
The Aaron
Pro
API Scripter
I'm still going to add this into RecursiveTable, but for now... =D
July 26 (8 years ago)
Wow, thanks! I will try it now :-)
July 26 (8 years ago)
Works perfectly. Also, I will compare both versions and understand a bit how all this is working.

Thanks again!