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

Need one table... with one item....

1453422923
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Need one table... with one item.... Want to make sure it exists and no others with the same properties… And it has one item with the right avatar. Can this be imporved upon? check_rollabletable = function() { var table = findObjs(rollabletableprop)[0] || createObj('rollabletable', rollabletableprop), tableItem; _.each( findObjs(rollabletableprop) , function( eachTable ) { if( eachTable.get('id') !== table.get('id') ) { eachTable.remove(); } }); tableItem = findObjs({type: 'tableitem', rollabletableid: table.get('id') })[0] || createObj('tableitem', {rollabletableid: table.get('id') , avatar: rollabletableavatar}); _.each( findObjs({type: 'tableitem', rollabletableid: table.get('id') }) , function( eachItem ) { if( eachItem.get('id') !== tableItem.get('id') ) { eachItem.remove(); } }); },
1453435045

Edited 1453435225
Lithl
Pro
Sheet Author
API Scripter
function checkRollableTable(properties, options) { var matchingTables = findObjs(properties), table = matchingTables[0] || createObj('rollabletable', properties), matchingTableItems = findObjs({ type: 'tableitem', rollabletableid: table.id }); if (!matchingTableItems[0]) { createObj('tableitem', { rollabletableid: table.id, avatar: options.img || '' }); } if (options.delete) { _.each(matchingTableItems, function(item, index) { if (index === 0) return; item.remove(); } } else if (matchingTableItems.length > 1) { log('Matching table contains multiple table items'); log(table); return; } if (options.delete) { _.each(matchingTables, function(item, index) { if (index === 0) return; item.remove(); }); } else if (matchingTables.length > 1) { log('Multiple tables match given properties'); log(properties); return; } } Cuts two calls to findObjs, which is a slow function Replaces calls to get('id') with direct access of id property (only the id property can do this) Filters the first element of _.each by referencing the index of the array, rather than the ids of every object (the table/item we're skipping is the first one by definition) Does not store the table item as an unused variable Uses parameters rather than relying of variables outside function scope Includes options parameter to supply image for table item and choose between deleting extra tables/table items or simply logging the existence of extras Edit: I'm not certain whether table#remove will cause orphaned tableitems. If it does, code needs to be added after line 12 to prevent orphaned objects.
1453458785

Edited 1453458842
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Brian said: Cuts two calls to findObjs, which is a slow function (Thanks! I felt that could be avoided.) Replaces calls to get('id') with direct access of id property (only the id property can do this) (Didn't know this!!!!!!!) Filters the first element of _.each by referencing the index of the array, rather than the ids of every object (the table/item we're skipping is the first one by definition) (Good point) Does not store the table item as an unused variable Uses parameters rather than relying of variables outside function scope ( :P ) Includes options parameter to supply image for table item and choose between deleting extra tables/table items or simply logging the existence of extras check_rollabletable = function(tableProperties,itemProperties) { var matchingTables = findObjs(tableProperties), table = matchingTables[0] || createObj('rollabletable', tableProperties), tableItems = findObjs({type: 'tableitem', rollabletableid: table.id }), item = tableItems[0] || createObj('tableitem', _.extend({rollabletableid: table.id}, itemProperties) ); _.each( matchingTables , function( eachTable, index ) {  if( 0 !== index ){ eachTable.remove();  } }); _.each( tableItems , function( eachItem, index ) {  if( 0 !== index ) {  eachItem.remove(); } }); return { table: table, item: item }; }, EDIT: I don't think you can have orphaned tableitems to a table.... pretty postive on that.