Skip to content

Instantly share code, notes, and snippets.

@shdwjk
Forked from Kurohyou/Page Navigation
Created May 12, 2016 23:34
Show Gist options
  • Save shdwjk/3df42a56f8ec959e48d9c7f64450d500 to your computer and use it in GitHub Desktop.
Save shdwjk/3df42a56f8ec959e48d9c7f64450d500 to your computer and use it in GitHub Desktop.
/*Script goal: to simplify moving between maps in Roll20.
Specific Goals:
1. Moving a token to a designated location on a map should move the player(s) to the associated map if GM permission is set to true
2. GM should be able to enter in a specific map to travel to via API command dialog.
3. I think that's it.
*/
var INTERACTIVEMAP = INTERACTIVEMAP || (function(){
'use strict';
var version = '0.0.1',
lastUpdate = 1463096043,
schemaVersion = 0.1,
defaults = {
css: {
button: {
'border': '1px solid #cccccc',
'border-radius': '1em',
'background-color': '#006dcc',
'margin': '0 .1em',
'font-weight': 'bold',
'padding': '.1em 1em',
'color': 'white'
}
}
},
templates = {},
ch = function (c) {
var entities = {
'<' : 'lt',
'>' : 'gt',
"'" : '#39',
'@' : '#64',
'{' : '#123',
'|' : '#124',
'}' : '#125',
'[' : '#91',
']' : '#93',
'"' : 'quot',
'-' : 'mdash',
' ' : 'nbsp'
};
if(_.has(entities,c) ){
return ('&'+entities[c]+';');
}
return '';
},
buildTemplates = function() {
templates.cssProperty =_.template(
'<%=name %>: <%=value %>;'
);
templates.style = _.template(
'style="<%='+
'_.map(css,function(v,k) {'+
'return templates.cssProperty({'+
'defaults: defaults,'+
'templates: templates,'+
'name:k,'+
'value:v'+
'});'+
'}).join("")'+
' %>"'
);
templates.button = _.template(
'<a <%= templates.style({'+
'defaults: defaults,'+
'templates: templates,'+
'css: _.defaults(css,defaults.css.button)'+
'}) %> href="<%= command %>"><%= label||"Button" %></a>'
);
},
makeButton = function(command, label, backgroundColor, color){
return templates.button({
command: command,
label: label,
templates: templates,
defaults: defaults,
css: {
color: color,
'background-color': backgroundColor
}
});
},
checkInstall = function() {
log('-=> Interactive Map v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
if( ! _.has(state,'INTERACTIVEMAP') || state.INTERACTIVEMAP.version !== schemaVersion) {
log(' > Updating Schema to v'+schemaVersion+' <');
switch(state.INTERACTIVEMAP && state.INTERACTIVEMAP.version) {
case 0.2:
state.INTERACTIVEMAP.config.labelGranted = false;
/* break; */
/* falls through */
case 'UpdateSchemaVersion':
state.INTERACTIVEMAP.version = schemaVersion;
break;
default:
state.INTERACTIVEMAP = {
version: schemaVersion,
};
break;
}
}
//getDestinations();
},
movePlayer = function(destination, player){
//Campaign().set({playerspecificpages: player.id: destination[0].id})
},
moveCurrentParty = function(destination){
//Campaign().set({playerpageid: destination[0].id});
},
moveAllPlayers = function(destination){
//Campaign().set({playerpageid: destination[0].id, playerspecificpages: false});
},
getDestinationCollisions = function(token) {
var pageId,
destinations,
collisions,
tokenControl,
msg = '',
lastCollision;
pageId = token.get('_pageid');
destinations = getDestinations(pageId);
collisions = TokenCollisions.getCollisions(token, destinations);
lastCollision = _.last(collisions);
tokenControl = token.get('controlledby').split(",");
if(lastCollision && _.contains(tokenControl, 'all')){
//return lastCollision;
msg += makeButton(
'!nav all ' + lastCollision,
'ALL', '#faa732'
);
msg += makebutton(
'!nav current ' + lastCollision,
'CURRENT PARTY', '#5bb75b'
);
} else if(lastCollision && !_.contains(tokenControl, 'all')){
msg += makeButton(
'!nav all ' + lastCollision,
'ALL', '#faa732'
);
msg += makeButton(
'!nav current ' + lastCollision,
'CURRENT PARTY', '#5bb75b'
);
msg += makeButton(
'!nav player ' + lastCollision + ' ' + tokenControl[0],
'SPECIFIC PLAYER');
//call make buttons for all, current party, and player
}
},
getDestinations = function(pageId){
return findObjs({
pageid: pageId,
type: 'graphic',
layer: /*'map' ||*/ 'gmlayer',
statusmarkers: 'flying-flag'
});
},
handleInput = function(msg_orig){
var msg = _.clone(msg_orig),
args,
who,
page,
player;
if (msg.type !== "api" || !playerIsGM(msg.playerid)) {
return;
}
who=getObj('player',msg.playerid).get('_displayname');
args = msg.content.split(/\s+--/);
switch(args[0]) {
case '!nav':
if(0 === args.length || _.contains(args,'help')) {
showHelp(who);
return;
}
if(args[2] && args[3]){
page = getObj('page', args[2]);
player = getObj('player', args[3]);
}
switch(args[1]){
case 'all':
if(page){
moveAllPlayers(page);
}else{
sendChat('','/w "'+who+'" '+
'<div><b>No pageid associated with that destination:</div> '+a+'</div>'
);
}
break;
case 'current':
if(page){
moveCurrentParty(page);
} else{
sendChat('','/w "'+who+'" '+
'<div><b>No pageid associated with that destination:</div> '+a+'</div>'
);
}
break;
case 'player':
if(page && player){
movePlayer(page, player);
}else{
sendChat('','/w "'+who+'" '+
'<div><b>No pageid associated with that destination or no specific player named:</div> '+a+'</div>'
);
}
break;
default:
sendChat('','/w "'+who+'" '+
'<div><b>Unsupported Option:</div> '+a+'</div>'
);
}
break;
}
},
RegisterEventHandlers = function() {
on('change:graphic', getDestinationCollisions);
//on('chat:message', handleInput);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: RegisterEventHandlers
};
}());
on("ready",function(){
'use strict';
INTERACTIVEMAP.CheckInstall();
INTERACTIVEMAP.RegisterEventHandlers();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment