Skip to content

Commit

Permalink
another possible change, deformatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChasarooniZ committed Aug 7, 2023
1 parent 58e4ebe commit e8cc14f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 124 deletions.
1 change: 0 additions & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
},
"scripts": [
"scripts/module.js",
"scripts/hooks.js"
],
"languages": [
{
Expand Down
25 changes: 0 additions & 25 deletions scripts/hooks.js

This file was deleted.

217 changes: 119 additions & 98 deletions scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,114 +34,135 @@ class RPGNumbers {
strokeThickness: 5,
}
};
}

extractTerm(term, flavor = '') {
if (term.class === "NumericTerm") {
result.push({ dmg: term.number, type: term.options.flavor ?? flavor });
} else if (term.class === "Die") {
for (const dieResult of term.results) {
result.push({ dmg: dieResult.result, type: term.options.flavor ?? flavor });
}
} else if (term.class === "Grouping") {
this.extractTerm(term.term, term?.options?.flavor ?? flavor);
} else if (term.class === "ArithmeticExpression") {
for (const operand of term?.operands) {
extractTerm(operand, flavor);
}
} else if (!term?.class) {
for (const t of term.rolls) {
for (const tt of term.terms) {
this.extractTerm(tt, tt?.options?.flavor ?? flavor);
}
}
function extractTerm(term, flavor = '') {
if (term.class === "NumericTerm") {
result.push({ dmg: term.number, type: term.options.flavor ?? flavor });
} else if (term.class === "Die") {
for (const dieResult of term.results) {
result.push({ dmg: dieResult.result, type: term.options.flavor ?? flavor });
}
}

/**
* Extracts the list of damage info from pf2e chat message, breaks down into each individual item
* @param {*} rolls
* @returns
*/
//TODO make this work
//TODO add options to toggle doubling amt of numbers on crit etc. or combining them
extractDamageInfo(rolls) {
const result = [];
console.log({ rolls })

for (const inp of rolls) {
for (const term of inp.terms) {
for (const roll of term.rolls) {
const dmg = { type: roll.type, value: roll.total };
result.push(dmg);
//console.log("----dmg----");
//console.log(dmg);
}
//console.log({inp, term})
//this.extractTerm(term, inp?.options?.flavor ?? '');
} else if (term.class === "Grouping") {
this.extractTerm(term.term, term?.options?.flavor ?? flavor);
} else if (term.class === "ArithmeticExpression") {
for (const operand of term?.operands) {
extractTerm(operand, flavor);
}
} else if (!term?.class) {
for (const t of term.rolls) {
for (const tt of term.terms) {
this.extractTerm(tt, tt?.options?.flavor ?? flavor);
}
}

return result;
}

/**
* Extracts the list of damage info from pf2e chat message, only breaks it up between the overarching damage types
* @param {any} rolls Roll value from pf2e chat message
* @returns
*/
extractDamageInfoCombined(rolls) {
const result = [];

for (const inp of rolls) {
for (const term of inp.terms) {
for (const roll of term.rolls) {
const dmg = { type: roll.type, value: roll.total };
result.push(dmg);
}
}

/**
* Extracts the list of damage info from pf2e chat message, breaks down into each individual item
* @param {*} rolls
* @returns
*/
//TODO make this work
//TODO add options to toggle doubling amt of numbers on crit etc. or combining them
function extractDamageInfo(rolls) {
const result = [];
console.log({ rolls })

for (const inp of rolls) {
for (const term of inp.terms) {
for (const roll of term.rolls) {
const dmg = { type: roll.type, value: roll.total };
result.push(dmg);
//console.log("----dmg----");
//console.log(dmg);
}
//console.log({inp, term})
//this.extractTerm(term, inp?.options?.flavor ?? '');
}
return result;
}

/**
* Generates damage scrolling text for a passed in list of damage values
* @param {{type: string, value: string}[]} dmg_list list of type and value
* @param {string[]} targets list of token ids
*/
//TODO settings on visuals (colors)
//TODO settings on size etc.
//TODO add scaling based on % health
//TODO add scaling based on size
generateDamageScroll(dmg_list, targets) {
for (const target_id of targets) {
const tok = game.canvas.tokens.get(target_id);
const size = tok.document.texture.scaleY * tok.document.width;
const topOffset = size / 4;

const seq = new Sequence();
for (const dmg of dmg_list.filter(d => d.value > 0)) {
style.fill = colors?.[dmg.type] ?? 'white';
seq.scrollingText()
.atLocation(tok, { offset: { y: topOffset }, gridUnits: true })
.text(`${dmg.value}`, style)
.jitter(1)
.anchor("TOP")
.waitUntilFinished(-1800)

return result;
}

/**
* Extracts the list of damage info from pf2e chat message, only breaks it up between the overarching damage types
* @param {any} rolls Roll value from pf2e chat message
* @returns
*/
function extractDamageInfoCombined(rolls) {
const result = [];

for (const inp of rolls) {
for (const term of inp.terms) {
for (const roll of term.rolls) {
const dmg = { type: roll.type, value: roll.total };
result.push(dmg);
}
seq.play();
}
}

/**
* Get list of targets based on a passed in chat msg
* @param {any} msg Message data from create Chat Message
* @returns {string[]} A list of all the ids of the targets
*/
getTargetList(msg) {
if (msg.flags?.["pf2e-target-damage"]?.targets) {
return msg.flags.pf2e - target - damage.targets.map(t => t.id);
} else {
return [(await fromUuid(msg.flags.pf2e.target.token)).id];
return result;
}

/**
* Generates damage scrolling text for a passed in list of damage values
* @param {{type: string, value: string}[]} dmg_list list of type and value
* @param {string[]} targets list of token ids
*/
//TODO settings on visuals (colors)
//TODO settings on size etc.
//TODO add scaling based on % health
//TODO add scaling based on size
function generateDamageScroll(dmg_list, targets) {
for (const target_id of targets) {
const tok = game.canvas.tokens.get(target_id);
const size = tok.document.texture.scaleY * tok.document.width;
const topOffset = size / 4;

const seq = new Sequence();
const color = (new RPGNumbers).colors
for (const dmg of dmg_list.filter(d => d.value > 0)) {
style.fill = colors?.[dmg.type] ?? 'white';
seq.scrollingText()
.atLocation(tok, { offset: { y: topOffset }, gridUnits: true })
.text(`${dmg.value}`, style)
.jitter(1)
.anchor("TOP")
.waitUntilFinished(-1800)
}
seq.play();
}
}

/**
* Get list of targets based on a passed in chat msg
* @param {any} msg Message data from create Chat Message
* @returns {string[]} A list of all the ids of the targets
*/
function getTargetList(msg) {
if (msg.flags?.["pf2e-target-damage"]?.targets) {
return msg.flags.pf2e - target - damage.targets.map(t => t.id);
} else {
return [(await fromUuid(msg.flags.pf2e.target.token)).id];
}
}
}

Hooks.on("init", async () => {
console.error("PF2e RPG Numbers is initiated"); if (!game.user.isGM) return;
game.RPGNumbers = new RPGNumbers();
})

Hooks.on("ready", async () => {
if (!game.user.isGM) return;
console.error("PF2e RPG Numbers is ready");
game.ui.notify("PF2e RPG Numbers is ready")
game.RPGNumbers = new RPGNumbers();
})

Hooks.on("createChatMessage", async function (msg, status, id) {
console.log({ msg })
if (msg?.flags?.pf2e?.context?.type !== 'damage-roll') return;
const dmg_list = extractDamageInfoCombined(msg.rolls);
const targets = getTargetList(msg);
generateDamageScroll(dmg_list, targets);
})

0 comments on commit e8cc14f

Please sign in to comment.