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

[Script] Ammo - Automated ammunition management in a character attribute.

1417021801
The Aaron
Roll20 Production Team
API Scripter
JonMichael (Vante) said: As I said before, I'm tracking a lot of stuff. Just some examples: Format: Items (who I want to whisper) Arrows (to ME) Spell_slots_l1 (To GM) I have a macro for short rest and long rest that replenishes the items. But I didn't want to spam the GM with all of it when I use it. Honestly, this is a minor issue and I feel like I'm hassling you, so if it takes major thought and changes don't worry about it. You've been awesome! This will probably have to wait on my to overhaul ammo. (or possibly provide another script that is similar to TokenMod but for Attributes)
The Aaron said: JonMichael (Vante) said: As I said before, I'm tracking a lot of stuff. Just some examples: Format: Items (who I want to whisper) Arrows (to ME) Spell_slots_l1 (To GM) I have a macro for short rest and long rest that replenishes the items. But I didn't want to spam the GM with all of it when I use it. Honestly, this is a minor issue and I feel like I'm hassling you, so if it takes major thought and changes don't worry about it. You've been awesome! This will probably have to wait on my to overhaul ammo. (or possibly provide another script that is similar to TokenMod but for Attributes) No worries! It was a long shot on my part. Our GM will just have to deal with it! HAHA! :D
The Aaron said: Oh! misunderstood. though you were saying you wanted to adjust the maximum to a new maximum. } else if( adj > max) { sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '') +'<div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;">' +'<b>'+chr.get('name') + '</b> does not have enough storage space for ammo. Needs '+adj+', but only has ' +'<span style="color: #ff0000;">'+max+'</span>.' +'<span style="font-weight:normal;color:#708090;>[Attribute: '+attr.get('name')+']</span>' +'</div>' ); adj = max; } Just change the valid = false; to adj = max; Thanks a million Aaron!
1417026288
The Aaron
Roll20 Production Team
API Scripter
=D
1417234150

Edited 1417234388
Hey Aaron, I'm getting an error now. I need help with it, because I have no idea what it is asking. ReferenceError: playerid is not defined at HandleInput (evalmachine.&lt;anonymous&gt;:791:31) at eval ( <a href="https://gist.github.com/anonymous/61b90e25ccee81216c7b#file-ammo-js" rel="nofollow">https://gist.github.com/anonymous/61b90e25ccee81216c7b#file-ammo-js</a>
1417235842

Edited 1417238056
The Aaron
Roll20 Production Team
API Scripter
In handleInput() , any where you refer to playerid , it needs to be msg.playerid . In the other functions, I'm passing it in as an argument, and so it arrives with the name playerid . In handleInput() , it is on the msg object, so you have to refer to it there. You will also need to add the who variable in, being sure to reference the playerid off of the msg object there as well. That should clear it up. Here is a version with those changes made: // GIST: <a href="https://gist.github.com/XXXXXXXXXXXXXXXXX" rel="nofollow">https://gist.github.com/XXXXXXXXXXXXXXXXX</a> var Ammo = Ammo || (function() { 'use strict'; var version = 0.11, adjustAmmo = function (playerid,attr,amount) { var val = parseInt(attr.get('current'),10)||0, max = parseInt(attr.get('max'),10)||10000, adj = (val+amount), who = getObj('player',playerid).get('_displayname').split(' ')[0], chr = getObj('character',attr.get('characterid')), valid = true; if(adj &lt; 0 ) { sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'&lt;b&gt;'+chr.get('name') + '&lt;/b&gt; does not have enough '+attr.get('name')+'. Needs '+Math.abs(amount)+', but only has ' +'&lt;span style="color: #ff0000;"&gt;'+val+'&lt;/span&gt;.' +'&lt;span style="font-weight:normal;color:#708090;&gt;[Attribute: '+attr.get('name')+']&lt;/span&gt;' +'&lt;/div&gt;' ); valid = false; } else if( adj &gt; max) { sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'&lt;b&gt;'+chr.get('name') + '&lt;/b&gt; does not have enough storage space for more '+attr.get('name')+'s. Needs '+adj+', but only has space for ' +'&lt;span style="color: #ff0000;"&gt;'+max+'&lt;/span&gt;.' +'&lt;span style="font-weight:normal;color:#708090;&gt;[Attribute: '+attr.get('name')+']&lt;/span&gt;' +'&lt;/div&gt;' ); adj = max; } if( isGM(playerid) || valid ) { attr.set({current: adj}); sendChat('Ammo', (isGM(playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'&lt;b&gt;'+chr.get('name') + '&lt;/b&gt; '+( (adj&lt;val) ? 'uses' : 'gains' )+' '+Math.abs(amount)+' '+attr.get('name')+' and has '+adj+' remaining.' +'&lt;/div&gt;' ); if(!valid) { sendChat('Ammo', '/w gm ' +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'Ignoring warnings and applying adjustment anyway. Was: '+val+'/'+max+' Now: '+adj+'/'+max +'&lt;/div&gt;' ); } } }, showHelp = function(playerid) { var who=getObj('player',playerid).get('_displayname').split(' ')[0]; sendChat('', '/w '+who+' ' +'&lt;div style="border: 1px solid black; background-color: white; padding: 3px 3px;"&gt;' +'&lt;div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;"&gt;' +'Ammo v'+version +'&lt;/div&gt;' +'&lt;div style="padding-left:10px;margin-bottom:3px;"&gt;' +'&lt;p&gt;Ammo provides inventory management for ammunition stored in a character' +'attribute. If the adjustment would change the attribute to be below 0 or above' +'it\'s maximum value, a warning will be issued and the attribute will not be' +'changed.&lt;/p&gt;' +( (isGM(playerid)) ? '&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; As the GM, bounds will not be ' +'enforced for you. You will be whispered the warnings, but the operation ' +'will succeed. You will also be told the previous and current state in case ' +'you want to revert the change.' : '') +'&lt;/div&gt;' +'&lt;b&gt;Commands&lt;/b&gt;' +'&lt;div style="padding-left:10px;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;!ammo &lt;id&gt; &lt;attribute&gt; &lt;amount&gt; &lt;/span&gt;&lt;/b&gt;' +'&lt;div style="padding-left: 10px;padding-right:20px"&gt;' +'This command requires 3 parameters:' +'&lt;ul&gt;' +'&lt;li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;id&lt;/span&gt;&lt;/b&gt; -- The id of the character which has the attribute. You can pass this as @{selected|token_id} and the character id will be pulled from represents field of the token.' +'&lt;/li&gt; ' +'&lt;li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;attribute&lt;/span&gt;&lt;/b&gt; -- The name of the attribute representing ammunition.' +'&lt;/li&gt; ' +'&lt;li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;amount&lt;/span&gt;&lt;/b&gt; -- The change to apply to the current quantity of ammo. Use negative numbers to decrease the amount, and possitive numbers to increase it.' +'&lt;/li&gt; ' +'&lt;/ul&gt;' +'&lt;/div&gt;' +'&lt;/div&gt;' +'&lt;div style="padding-left:10px;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;!get-represents [token_id]&lt;/span&gt;&lt;/b&gt;' +'&lt;div style="padding-left: 10px;padding-right:20px"&gt;' +'This command will show the character id for the supplied token id, or will list the character ids for all the selected tokens.' +'&lt;ul&gt;' +'&lt;li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;"&gt;' +'&lt;b&gt;&lt;span style="font-family: serif;"&gt;token_id&lt;/span&gt;&lt;/b&gt; -- The id of the token to lookup, usually supplied with: @{selected|token_id}' +'&lt;/li&gt; ' +'&lt;/ul&gt;' +'&lt;/div&gt;' +'&lt;/div&gt;' +'&lt;/div&gt;' ); }, HandleInput = function(msg_orig) { var msg = _.clone(msg_orig), args,attr,amount,chr,token,text='', who ; if (msg.type !== "api") { return; } if(_.has(msg,'inlinerolls')){ msg.content = _.chain(msg.inlinerolls) .reduce(function(m,v,k){ m['$[['+k+']]']=v.results.total || 0; return m; },{}) .reduce(function(m,v,k){ return m.replace(k,v); },msg.content) .value(); } who = getObj('player',msg.playerid).get('_displayname').split(' ')[0]; args = msg.content.split(" "); switch(args[0]) { case '!ammo': if(args.length &gt; 1) { chr = getObj('character', args[1]); if( ! chr ) { token = getObj('graphic', args[1]); if(token) { chr = getObj('character', token.get('represents')); } } if(chr) { if(! isGM(msg.playerid) && ! _.contains(chr.get('controlledby').split(','),msg.playerid) && ! _.contains(chr.get('controlledby').split(','),'all') ) { sendChat('Ammo', '&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'You do not control the specified character: '+chr.id +'&lt;/div&gt;' ); sendChat('Ammo', '/w gm &lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'&lt;b&gt;'+getObj('player',msg.playerid).get('_displayname')+'&lt;/b&gt; attempted to adjust attribute &lt;b&gt;'+args[2]+'&lt;/b&gt; on character &lt;b&gt;'+chr.get('name')+'&lt;/b&gt;.' +'&lt;/div&gt;' ); return; } attr = findObjs({_type: 'attribute', _characterid: chr.id, name: args[2]})[0]; } amount=parseInt(args[3],10); if(attr && amount) { adjustAmmo(msg.playerid,attr,amount); } else { if(attr) { sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'Amount ['+args[3]+'] is not correct. Please specify a positive or negative integer value like -1 or 4.' +'&lt;/div&gt;' ); } else { if(chr) { sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +' Attribute ['+args[2]+'] was not found. Please verify that you have the right name.' +'&lt;/div&gt;' ); } else { sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '/w '+who+' ') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +( (undefined !== token) ? ('Token id ['+args[1]+'] does not represent a character. ') : ('Character/Token id ['+args[1]+'] is not valid. ') ) +'Please be sure you are specifying it correctly, either with @{selected|token_id} or copying the character id from: !get-represents @{selected|token_id} ' +'&lt;/div&gt;' ); } } } } else { showHelp(msg.playerid); } break; case '!get-represents': if(args.length &gt; 1) { chr = getObj('character', args[1]); if( ! chr ) { token = getObj('graphic', args[1]); if(token) { sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'The specified token represents the following character:' +'&lt;ul&gt;&lt;li&gt;'+ ( ('' !== token.get('name')) ? token.get('name') : 'BLANK' )+' -&gt; '+ ( ('' !== token.get('represents')) ? token.get('represents') : 'NOTHING' ) + '&lt;/li&gt;&lt;/ul&gt;' +'&lt;/div&gt;' ); } else { sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +' Token id ['+args[1]+'] is not valid.' +'&lt;/div&gt;' ); } } } else if (msg.selected && msg.selected.length) { _.each(msg.selected, function(s) { token = getObj('graphic', s._id); if(token) { text += '&lt;li&gt;'+ ( ('' !== token.get('name')) ? token.get('name') : 'BLANK' )+' -&gt; '+ ( ('' !== token.get('represents')) ? token.get('represents') : 'NOTHING' ) + '&lt;/li&gt;'; } }); sendChat('Ammo', (isGM(msg.playerid) ? '/w gm ' : '') +'&lt;div style="padding:1px 3px;border: 1px solid #8B4513;background: #eeffee; color: #8B4513; font-size: 80%;"&gt;' +'The selected tokens represent the following characters:' +'&lt;ul&gt;' + text + '&lt;/ul&gt;' +'&lt;/div&gt;' ); } else { showHelp(msg.playerid); } break; } }, RegisterEventHandlers = function() { on('chat:message', HandleInput); }; return { RegisterEventHandlers: RegisterEventHandlers }; }()); on("ready",function(){ 'use strict'; var Has_IsGM=false; try { _.isFunction(isGM); Has_IsGM=true; } catch (err) { log('--------------------------------------------------------------'); log('Ammo requires the isGM module to work.'); log('isGM GIST: <a href="https://gist.github.com/shdwjk/8d5bb062abab18463625" rel="nofollow">https://gist.github.com/shdwjk/8d5bb062abab18463625</a> '); log('--------------------------------------------------------------'); } if( Has_IsGM ) { Ammo.RegisterEventHandlers(); } }); Hope that helps!
DOH! I copied and paste what you have there. It broke, says "Unexpected identifier" right after I save it.
1417238119
The Aaron
Roll20 Production Team
API Scripter
Fixed it above. The editor had eaten the '); on the end of that log('isGM GIST: ... line.
Awesome as always man! Thanks yet again!
1417268574
The Aaron
Roll20 Production Team
API Scripter
No worries!