Skip to content

Instantly share code, notes, and snippets.

@Geritz
Last active October 31, 2021 21:55
Show Gist options
  • Save Geritz/7a92163c1a937670bd47706c523a3843 to your computer and use it in GitHub Desktop.
Save Geritz/7a92163c1a937670bd47706c523a3843 to your computer and use it in GitHub Desktop.
Game Master Toolkit - Timers
//GmtkTimers.js by Spencer Moran
// This script adds functions and events for keeping track of the myriad of conditions imposed over the course of a game.
// Note, this does not use the state object and will reset if the script is relaunched or the game session is closed.
//
//Functions:
// AddTurnCounter - Adds a turn counter that updates whenever the turn order is changed.
// Initialize - Adds a macro to query the important details of the AddTurnCounter function.
//
//Single-use spin up actions
on('ready', function(){
//"use strict";
if(!GmtkTimers.Roll20ObjContains('GMTK-Timers','handout')){
createObj("handout",{
name: 'GMTK-Timers',
showplayers: false,
});
}
let handoutobj = findObjs({_type: "handout", name: "GMTK-Timers"})[0];
let content = "MACRO: !gmtk_timers_init";
handoutobj.get('notes', notes => handoutobj.set('notes', content));
});
//Command parser
on('chat:message', function(msg){
if('api' !== msg.type) {
return;
}
var args = msg.content.split(/\s+/);
let stragg = function(startpos=1){
let aggstring = ""
for (let i = startpos; i < args.length; i++)
{
aggstring += args[i]
if (i+1 !== args.length){
aggstring += " ";
}
}
return aggstring.toLowerCase();
};
switch (args[0]){
case '!gmtk_timers_addturncounter':
//name, level, duration
GmtkTimers.AddCounterOnTokenTurn(msg,stragg(3),args[1],args[2]);
break;
case '!gmtk_timers_modifycounterlevel':
//id, value, name
GmtkTimers.ModifyCounterLevel(args[1],args[2],stragg(3));
break;
case '!gmtk_timers_deletecounter':
//id, name
GmtkTimers.RemoveEffectOnTokenTurn(args[1],stragg(2));
break;
case '!gmtk_timers_init':
GmtkTimers.Initialize(msg);
break;
default:
break;
}
return;
});
on('change:campaign:turnorder', function(){
GmtkTimers.DisplayCountersOnCurrentTurn();
});
var GmtkTimersTurnStatus = {};
var GmtkTimers = {
'Roll20ObjContains' : function(value, type='macro'){
let test = findObjs({
_type: type,
name: value
});
return ((test.length <= 0)? false : true);
},
'GetTokenId' : function(msg){
if (typeof msg.selected === 'undefined') {
sendChat("GMTK", "&{template:default}{{name=ERROR}}{{No token selected}}")
return;
}
return msg.selected[0]._id;
},
'AddCounterOnTokenTurn' : function(msg,name,level,duration){
if (typeof msg.selected === 'undefined') {
sendChat("GMTK", "&{template:default}{{name=ERROR}}{{No token selected}}")
return;
}
let turnorder = Campaign().get("turnorder");
if (turnorder == "[]") return;
turnorder = JSON.parse(turnorder);
if(typeof GmtkTimersTurnStatus[this.GetTokenId(msg)] === 'undefined') GmtkTimersTurnStatus[this.GetTokenId(msg)] = {};
GmtkTimersTurnStatus[this.GetTokenId(msg)][name] = {level:parseInt(level), duration:parseInt(duration), name:name};
sendChat("Turn Counters", "&{template:default}{{name=Turn Counter Updated}}{{NOTIFY=Added turn counter: "+name+" with level: "+level+" and duration: "+duration+"}}");
},
'ModifyCounterLevel' : function (id,value,name){
let turnorder = Campaign().get("turnorder");
if (turnorder == "") return;
turnorder = JSON.parse(turnorder);
if(typeof GmtkTimersTurnStatus[id] === 'undefined') return;
if(typeof GmtkTimersTurnStatus[id][name] === 'undefined') return;
GmtkTimersTurnStatus[id][name].level += parseInt(value);
sendChat("GMTK", "&{template:default}{{name=Condition Modified}}{{"+name+" = "+value+"}}");
return;
},
'RemoveEffectOnTokenTurn' : function(id,name){
let turnorder = Campaign().get("turnorder");
if (turnorder == "") return;
turnorder = JSON.parse(turnorder);
if(typeof GmtkTimersTurnStatus[id] === 'undefined') return;
delete GmtkTimersTurnStatus[id][name];
sendChat("GMTK", "&{template:default}{{name=Condition Cleared}}{{"+name+"}}");
return;
},
'DisplayCountersOnCurrentTurn' : function(){
turnorder = Campaign().get("turnorder");
if (turnorder == "") return;
turnorder = JSON.parse(turnorder);
if (typeof turnorder[0] === 'undefined') return;
let effects = GmtkTimersTurnStatus[turnorder[0].id];
let tok_id = turnorder[0].id;
let entryname = getObj('graphic',turnorder[0].id).get('name');
let chatmsg = "&{template:default}{{name=Counters for "+entryname+"}}";
let chatcontent = "";
for (effect in effects){
let lev = (effects[effect].level < 1)? "" : effects[effect].level;
let nam = effects[effect].name;
let dur = (effects[effect].duration < 0)? "indf" : effects[effect].duration-- +" Turns";
chatcontent += "{{" + effect + " "+lev+" ("+ dur +")=[-](!gmtk_timers_modifycounterlevel "+tok_id+" -?{Reduce By|} "+nam+")[+](!gmtk_timers_modifycounterlevel "+tok_id+" ?{Increase By|} "+nam+")[clear](!gmtk_timers_deletecounter "+tok_id+" "+nam+")}}";
if (effects[effect].duration == 0) delete effects[effect];
}
if(chatcontent ==="") return;
sendChat("Turn Counters", chatmsg+chatcontent);
return;
},
'Initialize' : function(msg){
state.lncrgmtk = {
"turns": {}
};
let chatstring = "/w " + msg.who + " &{template:default}{{name=Generating GMTK-Timer Macro...}}";
let numMacros = 0;
if (!playerIsGM(msg.playerid)){
return;
}
if (!this.Roll20ObjContains("Add-Turn-Counter")){
createObj('macro', {
_playerid: msg.playerid,
name: "Add-Turn-Counter",
action: "!gmtk_timers_addturncounter ?{level|} ?{duration|} ?{name|}",
visibleto: '',
istokenaction: false
});
chatstring += "{{Add-Turn-Counter}}";
numMacros++;
}
if (numMacros == 0) {
chatstring += "{{All macros are already defined}}";
}
sendChat("GMTK-Timers",chatstring);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment