From 82cbf292dfdde9acf4b43cfc0a7fc7b051e20431 Mon Sep 17 00:00:00 2001 From: "Aaron C. Meadows" Date: Sun, 1 Jun 2025 11:19:03 -0500 Subject: [PATCH] Updated MonsterHitDice to v0.3.13 --- MonsterHitDice/0.3.13/MonsterHitDice.js | 248 ++++++++++++++++++++++++ MonsterHitDice/MonsterHitDice.js | 27 +-- MonsterHitDice/script.json | 3 +- 3 files changed, 266 insertions(+), 12 deletions(-) create mode 100644 MonsterHitDice/0.3.13/MonsterHitDice.js diff --git a/MonsterHitDice/0.3.13/MonsterHitDice.js b/MonsterHitDice/0.3.13/MonsterHitDice.js new file mode 100644 index 000000000..03c3f895f --- /dev/null +++ b/MonsterHitDice/0.3.13/MonsterHitDice.js @@ -0,0 +1,248 @@ +// Github: https://github.com/shdwjk/Roll20API/blob/master/MonsterHitDice/MonsterHitDice.js +// By: The Aaron, Arcane Scriptomancer +// Contact: https://app.roll20.net/users/104025/the-aaron +var API_Meta = API_Meta||{}; //eslint-disable-line no-var +API_Meta.MonsterHitDice={offset:Number.MAX_SAFE_INTEGER,lineCount:-1}; +{try{throw new Error('');}catch(e){API_Meta.MonsterHitDice.offset=(parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/,'$1'),10)-6);}} + +var globalconfig = globalconfig || undefined; //eslint-disable-line no-var +const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars + + const version = '0.3.13'; + API_Meta.MonsterHitDice.version = version; + const lastUpdate = 1748794696; + const schemaVersion = 0.3; + + let tokenIds = []; + + const checkGlobalConfig = () => { + const s=state.MonsterHitDice; + const g = (globalconfig && globalconfig.monsterhitdice); + if(g && g.lastsaved && g.lastsaved > s.globalconfigCache.lastsaved){ + log(' > Updating from Global Config < ['+(new Date(g.lastsaved*1000))+']'); + s.config.bar = parseInt(g.Bar.match(/\d+/)[0]); + switch(g.Sheet){ + case "DnD 5e - Community Contributed": + s.config.hitDiceAttribute = 'npc_HP_hit_dice'; + s.config.findSRDFormula = false; + s.config.HDis1eD8s = false; + s.config.useConBonus = true; + s.config.conBonusAttribute = 'npc_constitution'; + s.config.conBonusIsStat = true; + break; + + case "DnD 5e - Shaped v2": + s.config.hitDiceAttribute = 'npc_hpformula'; + s.config.findSRDFormula = true; + s.config.HDis1eD8s = false; + s.config.useConBonus = false; + s.config.conBonusAttribute = ''; + s.config.conBonusIsStat = false; + break; + + case "DnD 5e - OGL by roll20": + s.config.hitDiceAttribute = 'npc_hpformula'; + s.config.findSRDFormula = false; + s.config.HDis1eD8s = false; + s.config.useConBonus = false; + s.config.conBonusAttribute = ''; + s.config.conBonusIsStat = false; + break; + + case "Advanced 1st Edition": + s.config.hitDiceAttribute = 'hitdice'; + s.config.findSRDFormula = false; + s.config.HDis1eD8s = true; + s.config.useConBonus = false; + s.config.conBonusAttribute = ''; + s.config.conBonusIsStat = false; + break; + + + case "Custom - (Use settings below)": + s.config.hitDiceAttribute = g['HitDice Attribute']; + s.config.findSRDFormula = 'findSRDFormula' === g['SRD Embedded Formula']; + s.config.HDis1eD8s = 'HDis1eD8s' === g['1st Edition HD in Attribute (d8)']; + s.config.useConBonus = 'useSeparateCon' === g['Separate Constitution Modifier']; + s.config.conBonusAttribute = g['Constitution Attribute']; + s.config.conBonusIsStat = 'conIsStat' === g['Constitution is Stat']; + break; + } + state.MonsterHitDice.globalconfigCache=globalconfig.monsterhitdice; + } + }; + + const checkInstall = () => { + log('-=> MonsterHitDice v'+version+' <=- ['+(new Date(lastUpdate*1000))+']'); + + if( ! _.has(state,'MonsterHitDice') || state.MonsterHitDice.version !== schemaVersion) { + log(' > Updating Schema to v'+schemaVersion+' <'); + switch(state.MonsterHitDice && state.MonsterHitDice.version) { + + case 0.1: + delete state.MonsterHitDice.globalConfigCache; + state.MonsterHitDice.globalconfigCache = {lastsaved:0}; + + /* falls through */ + case 0.2: + state.MonsterHitDice.config.findSRDFormula = state.MonsterHitDice.findSRDFormula; + delete state.MonsterHitDice.findSRDFormula; + + /* falls through */ + case 'UpdateSchemaVersion': + state.MonsterHitDice.version = schemaVersion; + break; + + default: + state.MonsterHitDice = { + version: schemaVersion, + globalconfigCache: {lastsaved:0}, + config: { + bar: 3, + hitDiceAttribute: 'npc_hpformula', + findSRDFormula: false, + HDis1eD8s: false, + useConBonus: false, + conBonusAttribute: '', + conBonusIsStat: false + } + }; + } + } + checkGlobalConfig(); + }; + + const handleInput = (msg) => { + + if (msg.type === "api" && /^!mhd(\b|$)/i.test(msg.content) && playerIsGM(msg.playerid) ) { + let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname'); + let count = 0; + (msg.selected || []) + .map(o=>getObj('graphic',o._id)) + .filter(g=>undefined !== g) + .forEach( o => { + ++count; + tokenIds.push(o.id); + rollHitDice(o); + }) + ; + sendChat('',`/w "${who}" Rolling hit dice for ${count} token(s).`); + } + }; + + const findSRDRoll = (txt) => { + return txt.match(/\d+d\d+(\+\d+)?/)[0]||0; + }; + + const buildHD1eExpression = (exp) => { + let m = `${exp}`.match(/(\d+)([+-]\d+)?/); + if(m){ + return `${m[1]}d8${ m[2] ? m[2] : ''}`; + } + return 0; + }; + + const rollHitDice = (obj) => { + let sets = {}; + const bar = 'bar'+state.MonsterHitDice.config.bar; + let hdAttrib; + let conAttrib; + let hdExpression = 0; + let conExpression = 0; + let bonus = 0; + + if(_.contains(tokenIds,obj.id)){ + tokenIds=_.without(tokenIds,obj.id); + + if( 'graphic' === obj.get('type') && + 'token' === obj.get('subtype') && + '' !== obj.get('represents') + ) { + if( obj && '' === obj.get(bar+'_link') ) { + hdAttrib = findObjs({ + type: 'attribute', + characterid: obj.get('represents'), + name: state.MonsterHitDice.config.hitDiceAttribute + })[0]; + conAttrib = findObjs({ + _type: 'attribute', + _characterid:obj.get('represents'), + name: state.MonsterHitDice.config.conBonusAttribute + })[0]; + + if( hdAttrib ) { + hdExpression = state.MonsterHitDice.config.findSRDFormula + ? findSRDRoll(hdAttrib.get('current')) + : hdAttrib.get('current') + ; + + hdExpression = state.MonsterHitDice.config.HDis1eD8s + ? buildHD1eExpression(hdExpression) + : hdExpression + ; + if( state.MonsterHitDice.config.useConBonus && conAttrib ) { + conExpression = state.MonsterHitDice.config.conBonusIsStat + ? Math.round((conAttrib.get('current')-10)/2) + : conAttrib.get('current') + ; + + bonus = conExpression * _.reduce(hdExpression.match(/(\d+)d\d+/g),(m,die) => { + m+=parseInt(die.match(/(\d+)d\d+/)[1],10); + return m; + },0); + } + + sendChat('','/r '+hdExpression+'+'+bonus,(r) => { + let hp=0; + _.each(r,(subr) => { + let val=JSON.parse(subr.content); + if(_.has(val,'total')) + { + hp+=val.total; + } + }); + sets[bar+"_value"] = Math.max(hp,1); + sets[bar+"_max"] = Math.max(hp,1); + obj.set(sets); + }); + } + } + } + } + }; + + const saveTokenId = (obj) => { + tokenIds.push(obj.id); + + setTimeout(((id) => { + return () => { + let token=getObj('graphic',id); + if(token){ + rollHitDice(token); + } + }; + })(obj.id),100); + }; + + + const registerEventHandlers = () => { + on('chat:message', handleInput); + on('add:graphic', saveTokenId); + on('change:graphic', rollHitDice); + }; + + + on('ready',() => { + checkInstall(); + registerEventHandlers(); + }); + + + + return { + /* public interface */ + }; + +})(); + +{try{throw new Error('');}catch(e){API_Meta.MonsterHitDice.lineCount=(parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/,'$1'),10)-API_Meta.MonsterHitDice.offset);}} diff --git a/MonsterHitDice/MonsterHitDice.js b/MonsterHitDice/MonsterHitDice.js index 5f0d4fbc8..03c3f895f 100644 --- a/MonsterHitDice/MonsterHitDice.js +++ b/MonsterHitDice/MonsterHitDice.js @@ -8,9 +8,9 @@ API_Meta.MonsterHitDice={offset:Number.MAX_SAFE_INTEGER,lineCount:-1}; var globalconfig = globalconfig || undefined; //eslint-disable-line no-var const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars - const version = '0.3.12'; + const version = '0.3.13'; API_Meta.MonsterHitDice.version = version; - const lastUpdate = 1748112536; + const lastUpdate = 1748794696; const schemaVersion = 0.3; let tokenIds = []; @@ -120,10 +120,10 @@ const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars (msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) - .forEach( async o => { + .forEach( o => { ++count; tokenIds.push(o.id); - await rollHitDice(o); + rollHitDice(o); }) ; sendChat('',`/w "${who}" Rolling hit dice for ${count} token(s).`); @@ -142,9 +142,10 @@ const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars return 0; }; - const rollHitDice = async (obj) => { + const rollHitDice = (obj) => { let sets = {}; const bar = 'bar'+state.MonsterHitDice.config.bar; + let hdAttrib; let conAttrib; let hdExpression = 0; let conExpression = 0; @@ -158,17 +159,21 @@ const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars '' !== obj.get('represents') ) { if( obj && '' === obj.get(bar+'_link') ) { - let hdHitDiceFormula = await getSheetItem(obj.get('represents'), state.MonsterHitDice.config.hitDiceAttribute); + hdAttrib = findObjs({ + type: 'attribute', + characterid: obj.get('represents'), + name: state.MonsterHitDice.config.hitDiceAttribute + })[0]; conAttrib = findObjs({ _type: 'attribute', _characterid:obj.get('represents'), name: state.MonsterHitDice.config.conBonusAttribute })[0]; - if( hdHitDiceFormula ) { + if( hdAttrib ) { hdExpression = state.MonsterHitDice.config.findSRDFormula - ? findSRDRoll(hdHitDiceFormula) - : hdHitDiceFormula + ? findSRDRoll(hdAttrib.get('current')) + : hdAttrib.get('current') ; hdExpression = state.MonsterHitDice.config.HDis1eD8s @@ -210,10 +215,10 @@ const MonsterHitDice = (() => { // eslint-disable-line no-unused-vars tokenIds.push(obj.id); setTimeout(((id) => { - return async () => { + return () => { let token=getObj('graphic',id); if(token){ - await rollHitDice(token); + rollHitDice(token); } }; })(obj.id),100); diff --git a/MonsterHitDice/script.json b/MonsterHitDice/script.json index d0eb9b802..0c06f7090 100644 --- a/MonsterHitDice/script.json +++ b/MonsterHitDice/script.json @@ -1,7 +1,7 @@ { "name": "MonsterHitDice", "script": "MonsterHitDice.js", - "version": "0.3.12", + "version": "0.3.13", "description": "Automatically fills in the hit points for a characters dragged onto the table top.\r\rThe default token for the character needs to represent that creature but **should not** have the bar used for hit points linked to an attribute. Use the settings below to configure the script for how you calculate hit points in your game. There are several sheets for which the configurations are built in or you can use the more detailed settings. Details about each setting are included along with it.\r\r## Commands\r\r```\r!mhd\r```\r\rThis will reroll hitpoints for all selected tokens.", "authors": "The Aaron", "roll20userid": "104025", @@ -83,6 +83,7 @@ "previousversions": [ "0.3.10", "0.3.11", + "0.3.12", "0.3.3", "0.3.4", "0.3.5",