diff --git a/inc/config.php b/inc/config.php index 746fcad6..ec73226d 100644 --- a/inc/config.php +++ b/inc/config.php @@ -681,6 +681,9 @@ // with the modifier Z added, with the result displayed at the top of the post body. $config['allow_roll'] = false; + // Maximum number of dice rolls per post. + $config['max_roll_count'] = 24; + // Use semantic URLs for threads, like /b/res/12345/daily-programming-thread.html $config['slugify'] = false; @@ -740,11 +743,16 @@ * ==================== */ - // "Wiki" markup syntax ($config['wiki_markup'] in pervious versions): - $config['markup'][] = array("/'''(.+?)'''/", "\$1"); - $config['markup'][] = array("/''(.+?)''/", "\$1"); - $config['markup'][] = array("/\*\*(.+?)\*\*/", "\$1"); - $config['markup'][] = array("/==(.+?)==/", "\$1"); + + $config['markup'] = [ + // Dice Roll Markup. + [ "/!([-+]?\d+)?([d])([-+]?\d+)([-+]\d+)?/iu", fn($m) => handle_dice_roll_markup($m, 'static/d10.svg') ], + // "Wiki" markup syntax ($config['wiki_markup'] in pervious versions): + [ "/'''(.+?)'''/", "\$1" ], + [ "/''(.+?)''/", "\$1" ], + [ "/\*\*(.+?)\*\*/", "\$1" ], + [ "/==(.+?)==/", "\$1" ] + ]; // Code markup. This should be set to a regular expression, using tags you want to use. Examples: // "/\[code\](.*?)\[\/code\]/is" diff --git a/inc/functions.php b/inc/functions.php index 644cefbe..bca8d1d1 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -2786,6 +2786,51 @@ function diceRoller($post) { } } +/** + * Rolls a dice and generates the appropiate html from the markup. + * @param array $matches The array of the matches according to the default configuration. + * 1 -> The number of dices to roll. + * 3 -> The number faces of the dices. + * 4 -> The offset to apply to the dice. + * @param string $img_path Path to the image to use. Null if none. + * @return string The html to replace the original markup with. + */ +function handle_dice_roll_markup(array $matches, ?string $img_path): string { + function get_or_default(array $arr, int $index, int $default) { + return (isset($arr[$index]) && is_numeric($arr[$index])) ? (int)$arr[$index] : $default; + } + global $config; + + $dice_count = get_or_default($matches, 1, 1); + $dice_faces = get_or_default($matches, 3, 6); + $dice_offset = get_or_default($matches, 4, 0); + + // Clamp between 1 and max_roll_count. + $dice_count = max(min($dice_count, $config['max_roll_count']), 1); + // Must be at least 2. + if ($dice_faces < 2) { + $dice_faces = 6; + } + + $acc = ''; + $text = "$dice_count d $dice_faces + $dice_offset"; + for ($i = 0; $i < $dice_count; $i++) { + $res = $dice_offset + mt_rand(0, $dice_faces); + if ($img_path === null) { + $fmt = "$text $res"; + } else { + $fmt = "$text $res"; + } + if (empty($acc)) { + $acc = $fmt; + } else { + $acc .= " $fmt"; + } + } + + return $acc; +} + function slugify($post) { global $config; diff --git a/stylesheets/style.css b/stylesheets/style.css index 63de8117..4b71246e 100644 --- a/stylesheets/style.css +++ b/stylesheets/style.css @@ -1487,6 +1487,12 @@ li.mix { display: inline-block; } +/* Dice roll support */ +.inline-dice { + width: 1em; + height: 1em; +} + /* Mona Font */ .aa { font-family: Mona, "MS PGothic", "MS Pゴシック", sans-serif; diff --git a/templates/themes/faq/index.html b/templates/themes/faq/index.html index 66a8a88f..6450fd21 100644 --- a/templates/themes/faq/index.html +++ b/templates/themes/faq/index.html @@ -77,6 +77,7 @@

==text== makes it a heading: text

**text** makes it spoiler: text

[code]text[/code] makes it code:

text

+

[dice:XdY+Z] rolls a X dices with Y faces, adding Z to the result: 1 d 2 + 0

Why are my posts being changed / word-filtered ?