Skip to content

Instantly share code, notes, and snippets.

@birdbrainiac
Forked from fmonaca/turnorder
Created September 14, 2018 08:37
Show Gist options
  • Save birdbrainiac/c3f0bd097ba0f5d6a79f86f903725e6e to your computer and use it in GitHub Desktop.
Save birdbrainiac/c3f0bd097ba0f5d6a79f86f903725e6e to your computer and use it in GitHub Desktop.
Automatic Turn Order in Turn Tracker for HERO system campaigns
on('chat:message', function(msg) {
var command = msg.content.toLowerCase();
if (msg.type == "api" && command == "!combatstart"){
sendChat("GM", "Combat begins!");
var selected = msg.selected;
var aChars = new Array();
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;
});
var aSPDchart = new Array();
var currSPD = 0;
var needle = '';
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'];
if(Campaign().get("turnorder") == "") {
turnorder = [];
} else turnorder = JSON.parse(Campaign().get("turnorder"));
for (var i=1;i<13;i++)
{ if(i<3){needle = i+",";} else{needle = i;}
var header = true;
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>=0){
if(header){
turnorder.push({
id: "-1",
pr: " ",
custom: "PHASE " + i
});
header = false;
}
turnorder.push({
id: aChars[a][0],
pr: aChars[a][2],
custom: ""
});
}
}
}
Campaign().set("turnorder", JSON.stringify(turnorder));
}
if (msg.type == "api" && command == "!combatend"){
Campaign().set("turnorder", "");
sendChat("GM", "Combat ends.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment