Skip to content

Commit

Permalink
partial fix for multiple edge or trouble rolls
Browse files Browse the repository at this point in the history
closes #47 #47
  • Loading branch information
mjording committed Sep 25, 2024
1 parent 926c846 commit 3479e82
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ If either of these conditions are not met on the commit that tag points at, the
- The `system.json` file's `download` url must match the expected outcome of the release CI artifact. This should simply be changing version numbers in the url to match the release version.

```text
https://github.com/mjording/marvel-multiverse/releases/download/release-1.7.2/marvel-multiverse-1.7.2.zip
https://github.com/mjording/marvel-multiverse/releases/download/release-1.7.3/marvel-multiverse-1.7.3.zip
└─ Tag Name ──┘ └─ V ─┘ (version)
```

Expand Down
49 changes: 33 additions & 16 deletions module/documents/chat-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -357,51 +357,68 @@ export class ChatMessageMarvel extends ChatMessage {
if (!(rollTerm.rolls.length === 3 && (rollTerm.rolls[1].terms[0] instanceof game.MarvelMultiverse.dice.MarvelDie))) return;

const targetRoll = rollTerm.rolls[dieIndex];
const targetDie = targetRoll.terms[0];
const formulaReg = /(?<number>\d)d(?<dieType>\d|m).*/;
const formulaGroups = formulaReg.exec(targetRoll._formula)?.groups;

const targetReg = /\d(.*)/;
const targetFormula = targetRoll._formula.replace(targetReg, '2$1');
const formulaNumber = formulaGroups.number;
const formulaDie = formulaGroups.dieType;

targetDie.number += 1;

const targetFormula = `${targetDie.number}d${formulaDie}`;

targetRoll._formula = `${targetFormula}${modifier}`;
targetRoll.number = 2;


rollTerm.terms[dieIndex] = targetRoll._formula
const targetDie = targetRoll.terms[0];
targetDie.number = 2;
targetDie.modifiers?.push(modifier);

if (!targetDie.modifiers.includes(modifier)) {
targetDie.modifiers?.push(modifier)
}

const newRoll = new MarvelMultiverseRoll(targetRoll._formula, {...targetRoll.data});
await newRoll.roll();

const old_result = targetDie.results.find((r) => r.active).result

targetDie.results.push(newRoll.terms[0].results[0]);

const resultResults = targetDie.results.map((result) => result.result);

let activeResult;

const discardResult = targetDie.results[resultResults.indexOf(modifier === 'kh' ? Math.min.apply(Math, resultResults) : Math.max.apply(Math, resultResults))];
const activeResult = targetDie.results[resultResults.indexOf(modifier === 'kh' ? Math.max.apply(Math, resultResults) : Math.min.apply(Math, resultResults))];

if ( targetDie instanceof game.MarvelMultiverse.dice.MarvelDie && resultResults.indexOf(1) > -1 ){
activeResult = targetDie.results[resultResults.indexOf(modifier === 'kh' ? 1 : Math.max.apply(Math, resultResults))];
} else {
activeResult = targetDie.results[resultResults.indexOf(modifier === 'kh' ? Math.max.apply(Math, resultResults) : Math.min.apply(Math, resultResults))];
}
activeResult.active = true;
delete activeResult.discarded;

discardResult.active = false;
discardResult.discarded = true;
targetDie.results.filter((r) => r !== activeResult).forEach((result, i) => {
result.active = false;
result.discarded = true;
});


const re = /(\(?{)(\dd\d),(\ddm),(\dd\d)(}.*)/;

let replacedFormula;
switch (dieIndex) {
case 0: {
replacedFormula = roll.formula.replace(re, "$12d6" + modifier + ",$3,$4$5");
replacedFormula = roll.formula.replace(re, "$1"+ targetDie.number +"d6" + modifier + ",$3,$4$5");
}
case 1: {
replacedFormula = roll.formula.replace(re, "$1$2,2dm"+ modifier +",$4$5");
replacedFormula = roll.formula.replace(re, "$1$2,"+ targetDie.number +"dm"+ modifier +",$4$5");
}
case 2: {
replacedFormula = roll.formula.replace(re, "$1$2,$3,2d6"+ modifier +"$5");
replacedFormula = roll.formula.replace(re, "$1$2,$3,"+ targetDie.number +"d6"+ modifier +"$5");
}
}

roll._formula = replacedFormula;
roll._total = roll.total - discardResult.result + activeResult.result;
const resultAct = targetDie instanceof game.MarvelMultiverse.dice.MarvelDie && resultResults.indexOf(1) > -1 ? 6 : activeResult.result;
roll._total = roll.total - old_result + resultAct;

let update = await roll.toMessage({flavor: flavor}, {create: false});
update = foundry.utils.mergeObject(chatMessage.toJSON(), update);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marvel-multiverse",
"version": "1.7.2",
"version": "1.7.3",
"description": "CSS compiler for the MarvelMultiverse system",
"scripts": {
"build:css": "sass src/scss/marvel-multiverse.scss css/marvel-multiverse.css --style=expanded --no-source-map",
Expand Down
4 changes: 2 additions & 2 deletions system.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"thumbnail": "systems/marvel-multiverse/ui/official/mmrpg-setup-thumbnail.png"
}
],
"version": "1.7.2",
"version": "1.7.3",
"compatibility": {
"minimum": 12,
"verified": "12.328"
Expand Down Expand Up @@ -116,7 +116,7 @@
],
"socket": true,
"manifest": "https://raw.githubusercontent.com/mjording/marvel-multiverse/main/system.json",
"download": "https://github.com/mjording/marvel-multiverse/releases/download/release-1.7.2/marvel-multiverse-release-1.7.2.zip",
"download": "https://github.com/mjording/marvel-multiverse/releases/download/release-1.7.3/marvel-multiverse-release-1.7.3.zip",
"background": "systems/marvel-multiverse/ui/official/mmrpg-background.webp",
"grid": {
"distance": 5,
Expand Down

0 comments on commit 3479e82

Please sign in to comment.