-
-
Save palikhov/93bb9493622cd7bf13d8ccb7aa1cee12 to your computer and use it in GitHub Desktop.
Roll20.net Script to change images on a multi-sided token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TokenRoller | |
a Roll20 API Script by Tsolval | |
Change, increment or decrement sides on selected rollable tokens. | |
Command - Action | |
!rt+ - Switch to the next side (increment) | |
!rt- - Switch to the previous side (decrement) | |
!rt* - Switch to a random side (randomize) | |
!rt# - Choose a specific side with, where # is the image index. Remember | |
that indexes start at 0. | |
*/ | |
var TokenSwitch = TokenSwitch || (function() { | |
'use strict'; | |
var parseMessage = function(msg) { | |
if (msg.type == "api" && msg.content.match(/^!rt[0-9*+\-]/)) { | |
if (msg.selected === undefined) { | |
report("You must select at least one token."); | |
} else { | |
let fs = msg.content.charAt(3); | |
msg.selected.forEach(function(entry) { | |
if (entry._type === 'graphic') { | |
var obj = getObj(entry._type, entry._id); | |
if ('+' === fs) { | |
increment(obj); | |
} else if ('-' === fs) { | |
decrement(obj); | |
} else if ('*' === fs) { | |
randomize(obj); | |
} else if (fs.match(/[0-9]/)) { | |
change(obj, fs); | |
} | |
} | |
}); | |
} | |
} | |
}, | |
getCleanImgSrc = function(imgsrc) { | |
var parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/); | |
if(parts) { | |
return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`); | |
} | |
return; | |
}, | |
increment = function(token) { | |
let allSides = token.get('sides').split('|'); | |
let currSide = token.get('currentSide'); | |
let nextSide = (currSide + 1) % allSides.length; | |
token.set({ | |
currentSide: nextSide, | |
imgsrc: getCleanImgSrc(decodeURIComponent(allSides[nextSide])) | |
}); | |
}, | |
decrement = function(token) { | |
let allSides = token.get('sides').split('|'); | |
let currSide = token.get('currentSide'); | |
let prevSide = (currSide + allSides.length - 1) % allSides.length; | |
token.set({ | |
currentSide: prevSide, | |
imgsrc: getCleanImgSrc(decodeURIComponent(allSides[prevSide])) | |
}); | |
}, | |
randomize = function(token) { | |
let allSides = token.get('sides').split('|'); | |
let randSide = (randomInt(allSides.length)) | |
token.set({ | |
currentSide: randSide, | |
imgsrc: getCleanImgSrc(decodeURIComponent(allSides[randSide])) | |
}); | |
}, | |
change = function(token, side) { | |
let allSides = token.get('sides').split('|'); | |
let newSide = side % allSides.length; | |
token.set({ | |
currentSide: newSide, | |
imgsrc: getCleanImgSrc(decodeURIComponent(allSides[newSide])) | |
}); | |
}, | |
randomInt = function(max) { | |
return Math.floor(Math.random() * max); | |
}, | |
report = function(text) { | |
sendChat('TokenSwitch',`/w gm <div>${text}</div>`); | |
}, | |
registerEventHandlers = function() { | |
on('chat:message', parseMessage); | |
}; | |
return { | |
RegisterEventHandlers: registerEventHandlers, | |
}; | |
}()); | |
on("ready",function(){ | |
'use strict'; | |
TokenSwitch.RegisterEventHandlers(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment