Skip to content

Commit

Permalink
Merge pull request #22 from eupolemo/1.4.0
Browse files Browse the repository at this point in the history
1.4.0
  • Loading branch information
eupolemo authored Feb 20, 2022
2 parents 1e54b68 + 3a25720 commit 7acb657
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 62 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/eupolemo/fvtt-l5r4ed-dice-roller?style=for-the-badge"> <img alt="GitHub Releases" src="https://img.shields.io/github/downloads/eupolemo/fvtt-l5r4ed-dice-roller/latest/total?style=for-the-badge">
<img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/eupolemo/fvtt-l5r4ed-dice-roller?style=for-the-badge"> <img alt="GitHub Releases" src="https://img.shields.io/github/downloads/eupolemo/fvtt-l5r4ed-dice-roller/latest/l5r4ed-dice-roller.zip?color=3CB371&label=DOWNLOADS&style=for-the-badge">

# fvtt-l5r4ed-dice-roller

Expand Down Expand Up @@ -38,11 +38,12 @@ It can be used with roll, GM roll, self roll, blind roll and deferred inline rol

Example:

- /r or /roll 6k5;
- /r or /roll 6k5[describing dice]#describing roll;
- /gmr or /gmroll 6k5;
- /sr or /selfroll 6k5;
- /br or /broll or /blindroll 6k5;
- Message [[/r 6k5]] works well;
- Message [[6k5]] works well;

<img src="readme-resources/roll-l5r.gif"/>

Expand All @@ -68,3 +69,10 @@ Example:

- [Issue #7](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/7) Discard excess dices from keep when have less than 10 dices on roll
- [Issue #16](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/17) Added untrained and emphasis roll

### 1.4.0

- [Issue #11](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/11) Immediate Inline Rolls added
- [Issue #12](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/12) Describing roll added
- [Issue #13](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/13) Describing dice added
- [Issue #24](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/24) Now bonuses are shown correctly
164 changes: 107 additions & 57 deletions dice-roller.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
Hooks.on("chatMessage", function (chatlog, message, chatdata) {
// const pattern = /^\d+k\d+x\d+([+]\d+)?$/;
const pattern = /^(u|e)?\d+k\d+(x\d+)?([+]\d+)?$/;
const pattern = /^(u|e)?\d+k\d+(x\d+)?([+]\d+)?(\[.+\])?(\#(.*))?$/;
const roll_pattern = /^(\/r(?:oll)? |\/gmr(?:oll)? |\/b(?:lind)?r(?:oll)? |\/s(?:elf)?r(?:oll)? ){1}/;
const inside_message_roll = /\[\[(\/r(?:oll)? |\/gmr(?:oll)? |\/b(?:lind)?r(?:oll)? |\/s(?:elf)?r(?:oll)? ){1}(u|e)?\d+k\d+(x\d+)?([+]\d+)?\]\]/;

const deferred_inline_roll_pattern = /\[\[(\/r(?:oll)? |\/gmr(?:oll)? |\/b(?:lind)?r(?:oll)? |\/s(?:elf)?r(?:oll)? ){1}(u|e)?\d+k\d+(x\d+)?([+]\d+)?(\[.+\])?\]\]/;
const immediate_message_roll_pattern = new RegExp(/\[\[(u|e)?\d+k\d+(x\d+)?([+]\d+)?(\[.+\])?(\#(.*))?\]\]/)
const inside_message_roll_pattern = new RegExp("(" + immediate_message_roll_pattern.source + ")|(" + deferred_inline_roll_pattern.source + ")")
if (roll_pattern.test(message)) {
let parts = message.split(" ");
// console.log(parts)

if (pattern.test(parts[1])) {
let roll_parsed = roll_parser(parts[1]);
chatlog.processMessage(`${parts[0]} ${roll_parsed}`);
const describing_dice_pattern = /\[.*\]*$/;
const describing_dice = parts[1].match(describing_dice_pattern);
let message_without_describing = parts[1].replace(describing_dice_pattern, "");

const describing_roll_pattern = /(\#(.*))*$/;
let describing_roll;
if(describing_roll_pattern.test(message_without_describing)) {
describing_roll = message_without_describing.match(describing_roll_pattern);
message_without_describing = message_without_describing.replace(describing_roll_pattern, "");
}

let roll_parsed = roll_parser(message_without_describing);
chatlog.processMessage(`${parts[0]} ${roll_parsed}${describing_dice ? describing_dice : ""}${describing_roll ? describing_roll[0] : ""}`);
return false;
}
} else if (pattern.test(message)) {
message = roll_parser(message);
const describing_dice_pattern = /\[.*\]*$/;
const describing_dice = message.match(describing_dice_pattern);
let message_without_describing = message.replace(describing_dice_pattern, "");

const describing_roll_pattern = /(\#(.*))*$/;
let describing_roll;
if(describing_roll_pattern.test(message_without_describing)) {
describing_roll = message_without_describing.match(describing_roll_pattern);
message_without_describing = message_without_describing.replace(describing_roll_pattern, "");
}

chatlog.processMessage(`/r ${message}`);
message = roll_parser(message_without_describing);
chatlog.processMessage(`/r ${message}${describing_dice && describing_dice.length > 0 ? describing_dice[0] : ""}${describing_roll ? describing_roll[0] : ""}`);
return false;
} else if (inside_message_roll.test(message)) {
} else if (inside_message_roll_pattern.test(message)) {
const deferred_roll_pattern = /\[\[(?:\/r(?:oll)? |\/gmr(?:oll)? |\/b(?:lind)?r(?:oll)? |\/s(?:elf)?r(?:oll)? ){1}(.*?)\]\]/g;
const kxy_pattern = /(u|e)?\d+k\d+(x\d+)?([+]\d+)?/;
let result = message.replace(
deferred_roll_pattern,
function (match, token) {
if (!inside_message_roll.test(match)) return match;
return match.replace(kxy_pattern, roll_parser(token));
}
);

let result = message;

const inline_message_pattern = /\[\[((u|e)?\d+k\d+(x\d+)?([+]\d+)?(\[.+\])?(\#(.*))?){1}\]\]/g

if( deferred_roll_pattern.test(message))
result = message.replace(
deferred_roll_pattern,
function (match, token) {
if (!deferred_roll_pattern.test(match)) return match;
return match.replace(kxy_pattern, roll_parser(token));
}
);
else if ( inline_message_pattern.test(message))
result = message.replace(
inline_message_pattern,
function (match, token) {
if (!inline_message_pattern.test(match)) return match;
return match.replace(kxy_pattern, roll_parser(token));
}
);
chatlog.processMessage(result);
return false;
}
});

Hooks.on("renderChatMessage", async (app, html, msg) => {
if (app.isRoll) {
const pattern = /^\d+d\d+(r1)?k\d+(x(>=)?\d+)?( \+\ \d+)?$/;
const pattern = /^\d+d\d+(r1)?k\d+(x(>=)?\d+)?( \+\ \d+)?(\[.+\])?$/;

const roll = app.roll;
const formula = roll.formula;
Expand All @@ -51,9 +87,11 @@ Hooks.on("renderChatMessage", async (app, html, msg) => {
const e_div_tag = "</div>";
const b_span_tag = '<span class="part-formula">';
const e_span_tag = "</span>";
const b_flavor_tag = '<div class="part-flavor">';
const regex_div = new RegExp(`${b_div_tag}.*?${e_div_tag}`, "g");
const regex_span = new RegExp(`${b_span_tag}.*?${e_span_tag}`, "g");


let roll_l5r = `${die.number}${
die.modifiers[0] === "r1" ? die.modifiers[1] : die.modifiers[0]
}${bonus > 0 ? " + " + bonus : ""}${
Expand All @@ -66,58 +104,70 @@ Hooks.on("renderChatMessage", async (app, html, msg) => {
: " Untrained"
}`;

const describing_dice_pattern = /\[.*\]*$/;
const describing_dice = formula.match(describing_dice_pattern);
let flavor = "";
if( describing_dice ) {
flavor = describing_dice.length > 0 ? describing_dice[0] : "";
}

msg.message.content = msg.message.content
.replace(regex_div, `${b_div_tag} ${roll_l5r} ${e_div_tag}`)
.replace(regex_span, `${b_span_tag} ${roll_l5r} ${e_span_tag}`);
html.find(".dice-formula")[0].innerHTML = roll_l5r;
html.find(".part-formula")[0].innerHTML = roll_l5r;
.replace(regex_div, `${b_div_tag} ${roll_l5r}{flavor} ${e_div_tag}`)
.replace(regex_span, `${b_span_tag} ${roll_l5r} ${e_span_tag}`)
html.find(".dice-formula")[0].innerHTML = roll_l5r + flavor;
let part_formula = html.find(".part-formula")[0];
part_formula.innerHTML = roll_l5r;

const flavor_pattern = /\[(.*)\]/;
if(flavor_pattern.test(flavor)) {
$(`${b_flavor_tag}${flavor.match(flavor_pattern)[1]}${e_span_tag}`).insertAfter(part_formula)
}

}
} else {
const inside_message_roll = /\d+d\d+(r1)?k\d+(x(>=)?\d+)?(\+\d+)?/g;
if (
!inside_message_roll.test(msg.message.content) ||
!msg.message.content.includes("data-formula")
!msg.message.content.match(inside_message_roll)
)
return;

const roll = msg.message.content.match(inside_message_roll);
for (var child of html.find(".message-content")[0].children) {
const roll = child.getAttribute("title").match(inside_message_roll).pop();
let [dices, , kept_explode_bonus] = roll.split(/[dk]+/);
let kept,
explode_bonus = 0,
bonus = 0;
let explode = 11;
if (kept_explode_bonus.toString().includes("x")) {
[kept, explode_bonus] = kept_explode_bonus.split(/[x>=]+/);
} else if (kept_explode_bonus.includes("+")) {
[kept, bonus] = kept_explode_bonus.split(/[+]+/);
}
if (explode_bonus.toString().includes("+")) {
[explode, bonus = 0] = explode_bonus.split(/[+]+/);
}

let xky = `${dices}k${kept}${bonus > 0 ? " + " + bonus : ""}${
explode <= 10
? " Exploding " +
explode +
(roll.includes("r1") ? " with Emphasis" : "")
: " Untrained"
}`;
child.setAttribute("title", `${xky}`);
child.childNodes.forEach((element) => {
let a = 0;
if (element.nodeValue === null) {
return;
if( inside_message_roll.test(child.getAttribute("title")) ) {
const roll = child.getAttribute("title").match(inside_message_roll).pop();
let [dices, , kept_explode_bonus] = roll.split(/[dk]+/);
let kept,
explode_bonus = 0,
bonus = 0;
let explode = 11;
if (kept_explode_bonus.toString().includes("x")) {
[kept, explode_bonus] = kept_explode_bonus.split(/[x>=]+/);
} else if (kept_explode_bonus.includes("+")) {
[kept, bonus] = kept_explode_bonus.split(/[+]+/);
}
element.nodeValue = element.nodeValue.replace(
inside_message_roll,
`${xky}`
);
});
// child.innerHTML = child.innerHTML
// .text()
// .replace(inside_message_roll, `${xky}`);
if (explode_bonus.toString().includes("+")) {
[explode, bonus = 0] = explode_bonus.split(/[+]+/);
}

let xky = `${dices}k${kept}${bonus > 0 ? " + " + bonus : ""}${
explode <= 10
? " Exploding " +
explode +
(roll.includes("r1") ? " with Emphasis" : "")
: " Untrained"
}`;
child.setAttribute("title", `${xky}`);
child.childNodes.forEach((element) => {
let a = 0;
if (element.nodeValue === null) {
return;
}
element.nodeValue = element.nodeValue.replace(
inside_message_roll,
`${xky}`
);
});
}
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"title": "Dice Roller - Legends of the five rings 4th edition",
"description": "Dice roller using syntax of L5R 4th Edition.",
"author": "Eupolemo Castro",
"version": "1.3.0",
"minimumCoreVersion": "0.7.0",
"compatibleCoreVersion": "0.7.9",
"version": "1.4.0",
"minimumCoreVersion": "0.8.8",
"compatibleCoreVersion": "9.249",
"scripts": ["./dice-roller.js"],
"url": "https://github.com/eupolemo/fvtt-l5r4ed-dice-roller",
"manifest": "https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/raw/main/module.json",
Expand Down

0 comments on commit 7acb657

Please sign in to comment.