Skip to content

Commit

Permalink
Improved chancerollplugin to add decisions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajch committed Aug 8, 2024
1 parent e6c7345 commit 245a133
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 56 deletions.
4 changes: 1 addition & 3 deletions assets/sampledata.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
<tw-passagedata pid="4" name="99">
You are confused.

You should try to roll three dice.
If your score is 3 or less, you should [[go back to optimism->Next-1]].
If your score is more, you should [[proceed->100]].
Roll three dice. If you roll 12 to 18, you should [[go back to optimism->Next-1]]. If you roll 1 to 10, you should turn to 100. If you roll 11, you are stunned and need to roll again.
</tw-passagedata>
<tw-passagedata pid="5" name="100" tags="tag1 tag2 tag3">
True to form, it is time for a boss fight.
Expand Down
147 changes: 138 additions & 9 deletions src/chancerollplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { BBScannerPlugin } from "./plugin";

import './types'

const chanceRegEx = /(?:[Rr]oll|[Tt]hrow) (\S*?) di(?:c?)e\.(.*?)(?:\n|$)/

export class ChanceRollPlugin extends BBScannerPlugin {
/** @type {DiceBoardPlugin} */
#diceboard

#chanceroll

constructor() {
super('chancerollplugin')
}
Expand All @@ -27,10 +31,92 @@ export class ChanceRollPlugin extends BBScannerPlugin {
}

this.#diceboard.addEventListener('roll', (e) => {
if (this.active) {
this.setCurrentState(e.detail)
if (!this.active) {
return
}

this.setCurrentState(e.detail)

const rollResult = parseInt(e.detail.total)

const chanceroll = this.#chanceroll
if (!chanceroll || !chanceroll.numDice) {
return
}

const view = this.player.view

view.hideSelectedContent('p.cr-result')

let rowToShow
for (let i = 0; i < chanceroll.actions.length; i++) {
const action = chanceroll.actions[i]
// if (
// (!action.rangeEnd && rollResult == action.rangeStart)
// || (rollResult >= action.rangeStart && rollResult <= action.rangeEnd)
// || (rollResult == action.rangeStart || rollResult == action.rangeEnd)
// ) {
if (rollInRange(action, rollResult)) {
console.log(`Going to show ${JSON.stringify(action)}`)
rowToShow = view.content.querySelector(`p.chanceroll-result-${i}`)
break;
}
}

if (rowToShow) {
view.show(rowToShow)
}
})

this.player.addTransformer(
/**
*
* @param {string} input
*/
(input) => {
if (!this.active) {
return input
}

const chanceroll = this.#chanceroll

if (!chanceroll || !chanceroll.numDice) {
return input
}

const diceString = chanceroll.numDice == '1' || chanceroll.numDice === 'one'
? `one die`
: `${chanceroll.numDice} dice`

const currentState = this.getCurrentState()
const introStatement = currentState && currentState.total
? `You had rolled ${diceString} for a result of ${currentState.total}`
: `Roll ${diceString}`

let result = `<div class="chancerollarea">${introStatement}.`
if (chanceroll.restOfParagraph !== '') {
result = result + ` <span style="color: green;">${chanceroll.restOfParagraph}</span>\n`
}
result = result + '</div>'

for (let i = 0; i < chanceroll.actions.length; i++) {
const action = chanceroll.actions[i]
const ishidden = currentState && currentState.total
? rollInRange(action, currentState.total)
? ''
: 'hidden'
: 'hidden'

result = result + `<p class="cr-result chanceroll-result-${i} ${ishidden}">${action.sentence}`
if (action.destination) {
result = result.trimEnd() + ` [[go to ${action.destination}|${action.destination}]]`
}
result = result + '</p>'
}

return input.replace(chanceRegEx, result)
}
)
}

/**
Expand All @@ -41,22 +127,59 @@ export class ChanceRollPlugin extends BBScannerPlugin {
scan (passage) {
const passageBody = passage.body

const phrase1 = /[Rr]oll\s{1}(\S*?)\s{1}di(c{0,1})e\./
let match = passageBody.match(phrase1)
// const phrase1 = /[Rr]oll\s{1}(\S*?)\s{1}di(c{0,1})e\./
// let match = passageBody.match(phrase1)

if (!match) {
const phrase2 = /[Tt]hrow\s{1}(\S*?)\s{1}di(c{0,1})e\./
match = passageBody.match(phrase2)
}
// if (!match) {
// const phrase2 = /[Tt]hrow\s{1}(\S*?)\s{1}di(c{0,1})e\./
// match = passageBody.match(phrase2)
// }


const match = passageBody.match(chanceRegEx)

if (!match) {
this.#diceboard.hide('chanceroll')
return false
}

const numdice = match[1].trim().toLowerCase()
this.#diceboard.setDice(numdice)
if (!this.#diceboard.validateDice(numdice)) {
this.#diceboard.hide('chanceroll')
return false
}

const chanceroll = {
numDice: numdice,
actions: [],
restOfParagraph: ''
}

let restOfParagraph = match[2]

const actionRegEx = /If you roll (?:a )?(\d{1,2})( or(?: a)? | to |)(\d{0,2}),([^\.\n]*?)(?: [Tt]urn to (\d{1,3}))?\./g

let destMatch
while ((destMatch = actionRegEx.exec(match[2])) !== null) {
const chanceAction = {
rangeStart: destMatch[1],
rangeEnd: destMatch[3],
rangeOperator: destMatch[2].trim().toLocaleLowerCase(),
sentence: destMatch[4],
destination: destMatch[5]
}
chanceroll.actions.push(chanceAction)
restOfParagraph = restOfParagraph.replace(destMatch[0], '')
}

restOfParagraph = restOfParagraph.trim()
if (restOfParagraph !== '') {
chanceroll.restOfParagraph = restOfParagraph
}

this.#chanceroll = chanceroll

this.#diceboard.setDice(numdice)
this.#diceboard.show('chanceroll')

const currentState = this.getCurrentState()
Expand All @@ -67,3 +190,9 @@ export class ChanceRollPlugin extends BBScannerPlugin {
return true
}
}

const rollInRange = (action, rollResult) => {
return (!action.rangeEnd && rollResult == action.rangeStart)
|| (rollResult >= action.rangeStart && rollResult <= action.rangeEnd)
|| (rollResult == action.rangeStart || rollResult == action.rangeEnd)
}
22 changes: 17 additions & 5 deletions src/defaultview.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class DefaultView {
}



/**
*
* @param {HTMLElement} element
Expand Down Expand Up @@ -70,24 +71,35 @@ export class DefaultView {
element.setAttribute('disabled', 'true')
}

/**
* Hides all elements that match the selector
* @param {string} selector
*/
hideSelectedContent (selector) {
this.#contentelement.querySelectorAll(selector)
.forEach((item) => {
this.hide(item)
})
}

/**
* @returns {HTMLButtonElement}
*/
get backButton() {
get backButton () {
return document.getElementById('backButton')
}

/**
* @returns {HTMLButtonElement}
*/
get forwardButton() {
get forwardButton () {
return document.getElementById('forwardButton')
}

/**
* @returns {HTMLButtonElement}
*/
get restartButton() {
get restartButton () {
return document.getElementById('restartButton')
}

Expand Down Expand Up @@ -163,7 +175,7 @@ export class DefaultView {
* specified classname under section.sidebar-1
* @param {*} panelName
*/
getToolPanel(panelName) {
getToolPanel (panelName) {
return document.querySelector(`section.sidebar-1 div.${panelName}`)
}

Expand All @@ -175,7 +187,7 @@ export class DefaultView {
*
* @param {*} dialogId
*/
getDialog(dialogId) {
getDialog (dialogId) {
return document.getElementById(dialogId)
}
}
32 changes: 23 additions & 9 deletions src/diceboardplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ export class DiceBoardPlugin extends BBScannerPlugin {
rollDice()
})

const matchMap = {
"0": 0,
"2": 2,
"two": 2,
"3": 3,
"three": 3,
"some": 3
}

this.#setdice = (number) => {
number = matchMap[number] ?? 1

Expand Down Expand Up @@ -224,6 +215,20 @@ export class DiceBoardPlugin extends BBScannerPlugin {
console.log(`Dice shown by ${owner}`)
}


/**
* Validates whether the string parameter would correctly translate to
* a non-zero number of dice.
*
* @param {string} number
* @returns {boolean}
*/
validateDice (number) {
return matchMap[number]
? true
: false
}

/**
* Sets up the diceboard to show and roll the specified number of dice.
*
Expand Down Expand Up @@ -253,6 +258,15 @@ export class DiceBoardPlugin extends BBScannerPlugin {
}
}

const matchMap = {
"0": 0,
"2": 2,
"two": 2,
"3": 3,
"three": 3,
"some": 3
}

const dieTemplate = `
<div class="dice dice-{{n}}">
<div class="side one">
Expand Down
5 changes: 2 additions & 3 deletions src/player.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

import { BBPlugin } from "./plugin"
import { processHTML, processTwineLinks, addParagraphTags } from './transformers'
import { Passage } from "./passage"

import './types'


Expand Down Expand Up @@ -88,7 +87,7 @@ export class Player {
// This will read and sanitise any HTML in
// the passage body. From this point, it's
// all unencoded HTML.
// We are not doing this any, for for this
// We are not doing this any more for this
// particular format.
// bodystr = processHTML(bodystr)

Expand Down
27 changes: 0 additions & 27 deletions src/transformers.js

This file was deleted.

1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
* @property {FuncHTMLElementVoid} show
* @property {FuncHTMLElementVoid} disable
* @property {FuncHTMLElementVoid} enable
* @property {FuncStringVoid} hideSelectedContent
* @property {HTMLButtonElement} backButton
* @property {HTMLButtonElement} forwardButton
* @property {HTMLButtonElement} restartButton
Expand Down

0 comments on commit 245a133

Please sign in to comment.