Skip to content

Commit

Permalink
Merge pull request #15 from eupolemo/issue-8
Browse files Browse the repository at this point in the history
Added:

    Roll Mode and Types of Rolls
    Deferred Inline Rolls
    closes #14, closes #8
  • Loading branch information
eupolemo authored Jan 6, 2021
2 parents 0a9a8bf + e3ec010 commit 563a44a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 28 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
<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">

# fvtt-l5r4ed-dice-roller

## Description
An implementation of a dice roller to Legends of the five rings 4th Edition game system for Foundry Virtual Tabletop (http://foundryvtt.com).

[Release Notes](#release-notes)

## Roll
It captures chat message and converts to a Foundry VTT roll pattern and shows the result with the Legend of Five Rings text roll.

Example: 10k6 is converted to '/r 10d10k6x10'

### Accepts FVTT Rolls
It can be used with roll, GM roll, self roll, blind roll and deferred inline roll.

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

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

## Release Notes
### 1.0.0
- First release

### 1.0.1
- [Issue #6](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/6) Support to FoundryVTT 0.7.9

### 1.1.0
- [Issue #8](https://github.com/eupolemo/fvtt-l5r4ed-dice-roller/issues/8) Support FVTT rolls (roll, gm roll, self roll, blind roll and inline deferred rolls)
84 changes: 58 additions & 26 deletions dice-roller.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,73 @@
Hooks.on("chatMessage", function (chatlog, message, chatdata) {
let pattern = /^\d+k\d+([+]\d+)?$/;

if ( pattern.test(message) ) {
const pattern = /^\d+k\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}\d+k\d+([+]\d+)?\]\]/

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}`)
return false
}
} else if ( pattern.test(message) ) {
message = roll_parser(message)
roll_parsed = message

chatlog.processMessage(`/r ${message}`)
return false
} else if ( inside_message_roll.test(message) ) {
const deferred_roll_pattern = /\[\[(?:\/r(?:oll)? |\/gmr(?:oll)? |\/b(?:lind)?r(?:oll)? |\/s(?:elf)?r(?:oll)? ){1}(.*?)\]\]/g;
const kxy_pattern = /\d+k\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))
});

chatlog.processMessage(result)
return false
}
});

Hooks.on('renderChatMessage', async (app, html, msg) => {
if ( !app.isRoll || !app.isContentVisible ) return;
let pattern = /^\d+d\d+k\d+(x\d+)?( \+\ \d+)?$/;
if ( app.isRoll ) {
const pattern = /^\d+d\d+k\d+(x\d+)?( \+\ \d+)?$/;

const roll = app.roll
const formula = roll.formula
const die = roll.dice[0]
let bonus = 0
const roll = app.roll
const formula = roll.formula
const die = roll.dice[0]
let bonus = 0

if ( roll.terms.includes('+') ) {
bonus = roll.terms[roll.terms.indexOf('+') + 1]
}
if ( roll.terms.includes('+') ) {
bonus = roll.terms[roll.terms.indexOf('+') + 1]
}

if ( pattern.test(formula) ) {
const b_div_tag = '<div class="dice-formula">'
const e_div_tag = "</div>"
const b_span_tag = '<span class="part-formula">'
const e_span_tag = '</span>'
const regex_div = new RegExp(`${b_div_tag}.*?${e_div_tag}`, 'g')
const regex_span = new RegExp(`${b_span_tag}.*?${e_span_tag}`, 'g')
if ( pattern.test(formula) ) {
const b_div_tag = '<div class="dice-formula">'
const e_div_tag = "</div>"
const b_span_tag = '<span class="part-formula">'
const e_span_tag = '</span>'
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]}${bonus > 0 ? ' + ' + bonus : ''}${die.modifiers.length > 1 ? ' Exploding: ' + die.modifiers[1].replace('x', '') : ''}`
let roll_l5r = `${die.number}${die.modifiers[0]}${bonus > 0 ? ' + ' + bonus : ''}${die.modifiers.length > 1 ? ' Exploding: ' + die.modifiers[1].replace('x', '') : ''}`

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
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
}
} else {
const inside_message_roll = /\d+d\d+k\d+(x\d+)?(\+\d+)?/g
if( !inside_message_roll.test(msg.message.content) || !msg.message.content.includes('data-formula')) 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(/[dkx+-]+/)
let xky = `${dices}k${kept}${bonus > 0 ? ' + ' + bonus : ''}`
child.setAttribute('title', `${xky}`)
child.innerHTML = child.innerHTML.replace(inside_message_roll, `${xky}`)
}
}
})

Expand All @@ -56,7 +88,7 @@ function roll_parser(roll) {
}

function parseIntIfPossible(x) {
var numbers = /^[0-9]+$/;
const numbers = /^[0-9]+$/;
if ( x.match(numbers) ) {
return parseInt(x)
} else {
Expand Down Expand Up @@ -103,7 +135,7 @@ function calculate_keeps({kept, rises} = roll) {
}

function calculate_bonus({rises, bonus} = roll) {
console.log(rises + ' ' + bonus)
// console.log(rises + ' ' + bonus)
bonus += rises * 2;
return bonus
}
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"title": "Dice Roller - Legends of the five rings 4th edition",
"description": "Dice roller using syntax of L5R 4th Edition.",
"author": "Eupolemo Castro",
"version": "1.0.1",
"minimumCoreVersion": "0.6.0",
"version": "1.1.0",
"minimumCoreVersion": "0.7.0",
"compatibleCoreVersion": "0.7.9",
"scripts": [
"./dice-roller.js"
Expand Down
Binary file modified readme-resources/roll-l5r.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 563a44a

Please sign in to comment.