Skip to content

Instantly share code, notes, and snippets.

@fmonaca
Created November 15, 2013 07:46
Show Gist options
  • Save fmonaca/7480640 to your computer and use it in GitHub Desktop.
Save fmonaca/7480640 to your computer and use it in GitHub Desktop.
Turn Tracker for the HERO system, showing by phase
var iii = iii || {};
var iTurn = iTurn || {};
var combatBegun = combatBegun || {};
var aChars = new Array();
var aSPDchart = new Array();
aSPDchart = ['','12','6,12','4,8,12','3,6,9,12','3,5,8,10,12','2,4,6,8,10,12','2,4,6,7,9,11,12','2,3,5,6,8,9,11,12','2,3,4,6,7,8,10,11,12','2,3,4,5,6,8,9,10,11,12','2,3,4,5,6,7,8,9,10,11,12','1,2,3,4,5,6,7,8,9,10,11,12'];
function recoveryAll(tokens) {
for(var idCounter=0; idCounter<tokens.length; idCounter++){
var currID = tokens[idCounter][0];
var currChar = findObjs({
_type: "graphic",
_id: currID,
_pageid: Campaign().get("playerpageid"),
});
var oCharacter = getObj("character", currChar[0].get("represents"));
var oHealth = findObjs({name: "REC",_type: "attribute", _characterid: oCharacter.id}, {caseInsensitive: true})[0];
if (oHealth != undefined ) { var rec = parseInt(oHealth.get('current')); }
var curEnd = parseInt(currChar[0].get('bar2_value'));
var curStun = parseInt(currChar[0].get('bar1_value'));
var maxEnd = parseInt(currChar[0].get('bar2_max'));
var maxStun = parseInt(currChar[0].get('bar1_max'));
if(curEnd + rec > maxEnd){
currChar[0].set('bar2_value', maxEnd);
}else{
currChar[0].set('bar2_value', (curEnd + rec));
}
if(curStun + rec > maxStun){
currChar[0].set('bar1_value', maxStun);
}else{
currChar[0].set('bar1_value', (curStun + rec));
}
}
}
on('chat:message', function(msg) {
var command = msg.content.toLowerCase();
if (msg.type == "api" && (command == "!combatstart" || command == "!nextphase")){
if(command == "!combatstart"){
cambatBegun = true;
iii = 1;
iTurn = 1;
var selected = msg.selected;
var nCount = 0;
_.each(selected, function(obj) {
if(obj._type != 'graphic') return;
var token = getObj("graphic", obj._id);
var oCharacter = getObj("character", token.get("represents"));
if (oCharacter != undefined ) { // gets SPD, DEX, Phases from character's sheet and put them into the array together with tokenen's ID
aChars[nCount] = new Array(2);
aChars[nCount][0] = token.get('_id');
var oSPD = findObjs({name: "SPD",_type: "attribute", _characterid: oCharacter.id}, {caseInsensitive: true})[0];
if (oSPD != undefined )
aChars[nCount][1] = parseInt(oSPD.get('current'));
var oDEX = findObjs({name: "DEX",_type: "attribute", _characterid: oCharacter.id}, {caseInsensitive: true})[0];
if (oDEX != undefined )
aChars[nCount][2] = parseInt(oDEX.get('current'));
nCount++;
}
});
aChars = aChars.sort(function(a,b) { // sorts characters by SPD and then DEX
if (a[1] < b[1]) return 1;
if (a[1] > b[1]) return -1;
if (a[2] < b[2]) return 1;
if (a[2] > b[2]) return -1;
return 0;
});
if(aChars.length>0) {sendChat("GM", "Combat begins!");}
else{sendChat("GM", "No tokens selected!");}
}
var currSPD = 0;
var needle = "";
if(Campaign().get("turnorder") == "") {
turnorder = [];
} else turnorder = JSON.parse(Campaign().get("turnorder"));
if (msg.type == "api" && command == "!nextphase" && combatBegun){
turnorder.length = 0;
Campaign().set("turnorder", JSON.stringify(turnorder));
iii++;
if(iii>12) {iii=1; sendChat("GM", "Recovery time!"); recoveryAll(aChars);}
if(iii==1) {iTurn++; sendChat("GM", "New Turn!");}
}
nextPhase(iii);
function nextPhase(phase){
if(aChars.length>0){
var header = true;
do{
if(phase<3) {needle = phase+",";} else {needle = phase;}
for (var a = 0; a < aChars.length; a++) { // cycles through the characters to see if they act during this phase
currSPD = aChars[a][1];
var phases = aSPDchart[currSPD];
var pos = phases.indexOf(needle);
if(pos>-1){
if(header){
sendChat("GM", "Phase " + phase + " begins!");
turnorder.push({
id: "-1",
pr: " ",
custom: "TURN " + iTurn + " - Phase " + phase
});
header = false;
}
turnorder.push({
id: aChars[a][0],
pr: aChars[a][2],
custom: ""
});
}
}
if(header){
phase++;
if(phase>12) {phase=1; sendChat("GM", "Recovery time!"); recoveryAll(aChars);}
if(phase==1) {iTurn++; sendChat("GM", "New Turn!");}
iii = phase;
}
} while (header)
}
Campaign().set("turnorder", JSON.stringify(turnorder));
}
}
if (msg.type == "api" && command == "!combatend"){
turnorder.length = 0;
Campaign().set("turnorder", JSON.stringify(turnorder));
sendChat("GM", "Combat ends.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment