Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 166 additions & 65 deletions TokenLock/TokenLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,64 @@
var TokenLock = TokenLock || (function() {
'use strict';

var version = 0.11,
schemaVersion = 0.1,
var version = '0.2.2',
lastUpdate = 1428859122,
schemaVersion = 0.2,

performLock = function() {
if( ! state.TokenLock.locked ) {
state.TokenLock.locked = true;
}
sendChat('TokenLock','/w gm '
+'<div style="border: 1px solid #666666; background: #ffffee;">'
+'Tokens are now <span style="color: #990000; font-weight: bold;">Locked</span>.'
+'</div>'
);
},
performUnlock = function() {
if( state.TokenLock.locked ) {
state.TokenLock.locked = false;
ch = function (c) {
var entities = {
'<' : 'lt',
'>' : 'gt',
"'" : '#39',
'@' : '#64',
'{' : '#123',
'|' : '#124',
'}' : '#125',
'[' : '#91',
']' : '#93',
'"' : 'quot',
'-' : 'mdash',
' ' : 'nbsp'
};

if(_.has(entities,c) ){
return ('&'+entities[c]+';');
}
sendChat('TokenLock','/w gm '
+'<div style="border: 1px solid #666666; background: #ffffee;">'
+'Tokens are now <span style="color: #009900; font-weight: bold;">Unlocked</span>.'
+'</div>'
);
return '';
},
showHelp = function() {

getCommandOption_ToggleLock = function() {
var text = (state.TokenLock.locked ? '<span style="color: #990000;">Locked</span>' : '<span style="color: #009900;">Unlocked</span>' );
return '<div>'
+'Tokens are now <b>'
+text
+'</b>. '
+'<a href="!tl --toggle-lock">'
+'Toggle'
+'</a>'
+'</div>';

},

getConfigOption_AllowMoveOnTurn = function() {
var text = (state.TokenLock.config.allowMoveOnTurn ? 'On' : 'Off' );
return '<div>'
+'Allow Move on Turn is currently <b>'
+text
+'</b> '
+'<a href="!tl-config --toggle-allowmoveonturn">'
+'Toggle'
+'</a>'
+'</div>';

},

showHelp = function(who) {
var stateColor = (state.TokenLock.locked) ? ('#990000') : ('#009900'),
stateName = (state.TokenLock.locked) ? ('Locked') : ('Unlocked');

sendChat('',
'/w gm '
'/w '+who+' '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;">'
+'<div style="float:right;width:90px;border:1px solid black;background-color:#ffc;text-align:center;font-size: 70%;"><span style="color: '+stateColor+'; font-weight:bold; padding: 0px 4px;">'+stateName+'</span></div>'
Expand Down Expand Up @@ -65,40 +94,114 @@ var TokenLock = TokenLock || (function() {
+'</ul>'
+'</div>'
+'</div>'
+getCommandOption_ToggleLock()
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!tl-config ['+ch('<')+'Options'+ch('>')+'|--help]</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>Swaps the selected Tokens for their counterparts on the other layer.</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--help</span></b> '+ch('-')+' Shows the Help screen'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--toggle-allowmoveonturn</span></b> '+ch('-')+' Sets whether tokens can be moved if they are at the top of the turn order.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+getConfigOption_AllowMoveOnTurn()
+'</div>'
);
},

HandleInput = function(msg) {
var args;
handleInput = function(msg) {
var args,who;

if (msg.type !== "api" || !isGM(msg.playerid) ) {
if (msg.type !== "api" || !playerIsGM(msg.playerid) ) {
return;
}
who=getObj('player',msg.playerid).get('_displayname').split(' ')[0];


args = msg.content.split(" ");
switch(args[0]) {
args = msg.content.split(/\s+/);
switch(args.shift()) {
case '!tl':
switch(args[1]) {
if(_.contains(args,'--help')) {
showHelp(who);
return;
}
switch(args.shift()) {
case 'lock':
performLock();
state.TokenLock.locked=true;
sendChat('TokenLock','/w gm '
+getCommandOption_ToggleLock()
);

break;

case 'unlock':
performUnlock();
state.TokenLock.locked=false;
sendChat('TokenLock','/w gm '
+getCommandOption_ToggleLock()
);
break;

case '--toggle-lock':
state.TokenLock.locked= !state.TokenLock.locked;
sendChat('TokenLock','/w gm '
+getCommandOption_ToggleLock()
);
break;

default:
showHelp();
showHelp(who);
break;
}
break;

case '!tl-config':
if(_.contains(args,'--help')) {
showHelp(who);
return;
}
if(!args.length) {
sendChat('','/w '+who+' '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;">'
+'TokenLock v'+version
+'</div>'
+getConfigOption_AllowMoveOnTurn()
+'</div>'
);
return;
}
_.each(args,function(a){
var opt=a.split(/\|/);

switch(opt.shift()) {
case '--toggle-allowmoveonturn':
state.TokenLock.config.allowMoveOnTurn = !state.TokenLock.config.allowMoveOnTurn;
sendChat('','/w '+who+' '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+getConfigOption_AllowMoveOnTurn()
+'</div>'
);
break;

default:
sendChat('','/w '+who+' '
+'<div><b>Unsupported Option:</div> '+a+'</div>'
);
}
});
break;
}

},

HandleMove = function(obj,prev) {
handleMove = function(obj,prev) {
if(state.TokenLock.locked
&& 'token' === obj.get('subtype')
&& ( !state.TokenLock.config.allowMoveOnTurn || (( (JSON.parse(Campaign().get('turnorder'))||[{id:false}])[0].id) !== obj.id) )
&& ( obj.get('left') !== prev.left || obj.get('top') !== prev.top || obj.get('rotation') !== prev.rotation )
) {
if('' !== obj.get('controlledby')) {
Expand All @@ -112,47 +215,45 @@ var TokenLock = TokenLock || (function() {
}
},

CheckInstall = function() {
if( ! _.has(state,'TokenLock') || state.TokenLock.version !== TokenLock.schemaVersion)
{
/* Default Settings stored in the state. */
state.TokenLock = {
version: TokenLock.schemaVersion,
locked: false
};
}
checkInstall = function() {
log('-=> TokenLock v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');

if( ! _.has(state,'TokenLock') || state.TokenLock.version !== schemaVersion) {
log(' > Updating Schema to v'+schemaVersion+' <');
switch(state.TokenLock && state.TokenLock.version) {
case 0.1:
state.TokenLock.config={
allowMoveOnTurn: false
};
state.TokenLock.version=schemaVersion;
break;

default:
state.TokenLock = {
version: schemaVersion,
config: {
allowMoveOnTurn: false
},
locked: false
};
}
}
},

RegisterEventHandlers = function() {
on('chat:message', HandleInput);
on('change:graphic', HandleMove);
registerEventHandlers = function() {
on('chat:message', handleInput);
on('change:graphic', handleMove);
};

return {
RegisterEventHandlers: RegisterEventHandlers,
CheckInstall: CheckInstall
RegisterEventHandlers: registerEventHandlers,
CheckInstall: checkInstall
};
}());

on("ready",function(){
'use strict';

var Has_IsGM=false;
try {
_.isFunction(isGM);
Has_IsGM=true;
}
catch (err)
{
log('--------------------------------------------------------------');
log('TokenLock requires the isGM module to work.');
log('isGM GIST: https://gist.github.com/shdwjk/8d5bb062abab18463625');
log('--------------------------------------------------------------');
}

if( Has_IsGM )
{
TokenLock.CheckInstall();
TokenLock.RegisterEventHandlers();
}
TokenLock.CheckInstall();
TokenLock.RegisterEventHandlers();
});
37 changes: 17 additions & 20 deletions TokenLock/package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
{
"name": "TokenLock",
"version": "0.11",
"description": "Allows GMs to selectively lock the movement of Player Tokens.",
"authors": "The Aaron",
"roll20userid": "104025",
"dependencies": {
"isGMModule": "0.x"
},
"modifies": {
"state.TokenLock": "read,write",
"character.controlledby": "read",
"graphic.controlledby": "read",
"graphic.left": "read,write",
"graphic.represents": "read",
"graphic.rotation": "read,write",
"graphic.subtype": "read",
"graphic.top": "read,write"
},
"conflicts": [
]
"name": "TokenLock",
"version": "0.2.2",
"description": "Allows GMs to selectively lock the movement of Player Tokens.",
"authors": "The Aaron",
"roll20userid": "104025",
"dependencies": [],
"modifies": {
"state.TokenLock": "read,write",
"character.controlledby": "read",
"graphic.controlledby": "read",
"graphic.left": "read,write",
"graphic.represents": "read",
"graphic.rotation": "read,write",
"graphic.subtype": "read",
"graphic.top": "read,write"
},
"conflicts": []
}