Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrD2LinkedTenacity patch 0.1.1 #1969

Merged
merged 4 commits into from
Sep 2, 2024
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
92 changes: 92 additions & 0 deletions DrD2LinkedTenacity/0.1.1/DrD2LinkedTenacity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Github: TBD
// By: nesuprachy
// Contact: https://app.roll20.net/users/11071738/nesuprachy
//
// This script tracks changes made to the `tenacity_current` attribute and propagates them to other sheets linked via the `npc_owner` attribute.
// Uses ChatSetAttr mod to change attributes from chat https://github.com/Roll20/roll20-api-scripts/tree/master/ChatSetAttr#readme

var DrD2LinkedTenacity = DrD2LinkedTenacity || (function() {
'use strict';

const version = '0.1.1';
const lastUpdate = 1725266705016;

checkInstall = function () {
log(`-=> DrD2LinkedTenacity v${version} <=- [${new Date(lastUpdate)}]`);
},

handleTenacityAttribute = function (obj, prev, isMax) {
if(obj.get('name') === 'tenacity_current') {
var prevVal, newVal = 0;
if(isMax){
prevVal = parseInt(prev.max)||0;
newVal = parseInt(obj.get('max'))||0;
}else {
prevVal = parseInt(prev.current)||0;
newVal = parseInt(obj.get('current'))||0;
}
if(newVal !== prevVal) {
var targetChars = [];
var allCharacters = findObjs({
_type: 'character',
archived: false
}, {caseInsensitive: true});
var originChar = getObj('character', obj.get('_characterid'));
var originCharName = originChar.get('name');
var sheetType = getAttrByName(originChar.id, 'sheet_type');
var npcOwner = getAttrByName(originChar.id, 'npc_owner');

if((sheetType === 'pc') && originCharName) {
// If PC, add ID of every character owned by this PC to target characters array
_.each(allCharacters, function(obj){
if(getAttrByName(obj.id, 'npc_owner') === originCharName) {targetChars.push(obj.id)}
});
}else if ((sheetType === 'npc') && npcOwner) {
// If NPC, add ID of every character with same 'npc_owner' to target characters array
_.each(allCharacters, function(obj) {
if(getAttrByName(obj.id, 'npc_owner') === npcOwner) {targetChars.push(obj.id)}
});
// Add sheets where 'character_name' equals 'npc_owner' to target characters array
_.each(allCharacters, function(obj) {
if(obj.get('name') === npcOwner) {targetChars.push(obj.id)}
});
}

/*log(`sheet_type = \'${sheetType}\', npc_owner = \'${npcOwner}\'`);
if(isMax) {
log(`\'${obj.get('name')}\' (max) of character \'${originCharName}\' changed from ${prevVal} to ${newVal}`);
}else {
log(`\'${obj.get('name')}\' of character \'${originCharName}\' changed from ${prevVal} to ${newVal}`);
}*/

if(Array.isArray(targetChars) && targetChars.length){
if(isMax){
//log(`!setattr --charid ${targetChars} --tenacity_current||${newVal} --silent --nocreate`);
sendChat('API', `!setattr --charid ${targetChars} --tenacity_current||${newVal} --silent --nocreate`, null, {noarchive:true} );
}else {
//log(`!setattr --charid ${targetChars} --tenacity_current|${newVal} --silent --nocreate`);
sendChat('API', `!setattr --charid ${targetChars} --tenacity_current|${newVal} --silent --nocreate`, null, {noarchive:true} );
}
}
}
}
},

registerEventHandlers = function () {
on('change:attribute:current', function(obj, prev){handleTenacityAttribute(obj, prev, false)});
on('change:attribute:max', function(obj, prev){handleTenacityAttribute(obj, prev, true)});
};

return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};

}());

on('ready', () => {
'use strict';

DrD2LinkedTenacity.CheckInstall();
DrD2LinkedTenacity.RegisterEventHandlers();
});
14 changes: 7 additions & 7 deletions DrD2LinkedTenacity/DrD2LinkedTenacity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
// By: nesuprachy
// Contact: https://app.roll20.net/users/11071738/nesuprachy
//
// This script tracks changes made to the 'tenacity_current' attribute and propagates them to other sheets linked via the 'npc_owner' attribute.
// This script tracks changes made to the `tenacity_current` attribute and propagates them to other sheets linked via the `npc_owner` attribute.
// Uses ChatSetAttr mod to change attributes from chat https://github.com/Roll20/roll20-api-scripts/tree/master/ChatSetAttr#readme

var DrD2LinkedTenacity = DrD2LinkedTenacity || (function() {
'use strict';

const version = '0.1.0';
const lastUpdate = '1715082359599';
const version = '0.1.1';
const lastUpdate = 1725266705016;

checkInstall = function () {
log(`-=> DrD2LinkedTenacity v${version} <=- [${new Date(lastUpdate*1000)}]`);
log(`-=> DrD2LinkedTenacity v${version} <=- [${new Date(lastUpdate)}]`);
},

handleAttribute = function (obj, prev, isMax) {
handleTenacityAttribute = function (obj, prev, isMax) {
if(obj.get('name') === 'tenacity_current') {
var prevVal, newVal = 0;
if(isMax){
Expand Down Expand Up @@ -73,8 +73,8 @@ var DrD2LinkedTenacity = DrD2LinkedTenacity || (function() {
},

registerEventHandlers = function () {
on('change:attribute:current', function(obj, prev){handleAttribute(obj, prev, false)});
on('change:attribute:max', function(obj, prev){handleAttribute(obj, prev, true)});
on('change:attribute:current', function(obj, prev){handleTenacityAttribute(obj, prev, false)});
on('change:attribute:max', function(obj, prev){handleTenacityAttribute(obj, prev, true)});
};

return {
Expand Down
4 changes: 2 additions & 2 deletions DrD2LinkedTenacity/script.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "DrD2LinkedTenacity",
"script": "DrD2LinkedTenacity.js",
"version": "0.1.0",
"version": "0.1.1",
"previousversions": [],
"description": "Designed for use only with the Draci Doupe II sheet.\n\nThis script tracks changes made to the 'tenacity_current' attribute and propagates them to other sheets linked via the 'npc_owner' attribute.",
"description": "Designed for use only with the Draci Doupe II sheet.\n\nThis script tracks changes made to the `tenacity_current` attribute and propagates them to other sheets linked via the `npc_owner` attribute.",
"authors": "nesuprachy",
"roll20userid": "11071738",
"useroptions": [],
Expand Down
Loading