From fe7a667441df1806c4131b38e95bd14da935baa9 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sat, 12 Oct 2024 16:16:37 +0200 Subject: [PATCH 1/7] style.css: add diceroll styling --- stylesheets/style.css | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/stylesheets/style.css b/stylesheets/style.css index b7c39b26..ef1abf8b 100644 --- a/stylesheets/style.css +++ b/stylesheets/style.css @@ -1042,6 +1042,20 @@ div.boardlist a { cursor: pointer; } +/* Inline dice */ +.dice-option table { + border: 1px dotted black; + margin: 0; + border-collapse: collapse; +} +.dice-option table td { + text-align: center; + border-left: 1px dotted black; + padding-left: 2px; + padding-right: 2px; + padding-bottom: 2px; +} + #youtube-size input { width: 50px; } From 27e4bd833a1ee5dda75188ca2cbb124a38105692 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 12:18:04 +0200 Subject: [PATCH 2/7] config.php: use op-cache friend array syntax for markup config --- inc/config.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/inc/config.php b/inc/config.php index 11ac8809..fbca7447 100644 --- a/inc/config.php +++ b/inc/config.php @@ -783,11 +783,13 @@ * ==================== */ - // "Wiki" markup syntax ($config['wiki_markup'] in pervious versions): - $config['markup'][] = array("/'''(.+?)'''/", "\$1"); - $config['markup'][] = array("/''(.+?)''/", "\$1"); - $config['markup'][] = array("/\*\*(.+?)\*\*/", "\$1"); - $config['markup'][] = array("/^[ |\t]*==(.+?)==[ |\t]*$/m", "\$1"); + $config['markup'] = [ + // "Wiki" markup syntax ($config['wiki_markup'] in pervious versions): + [ "/'''(.+?)'''/", "\$1" ], + [ "/''(.+?)''/", "\$1" ], + [ "/\*\*(.+?)\*\*/", "\$1" ], + [ "/^[ |\t]*==(.+?)==[ |\t]*$/m", "\$1" ], + ]; // Code markup. This should be set to a regular expression, using tags you want to use. Examples: // "/\[code\](.*?)\[\/code\]/is" From b8c53fbbcdffe50e1548c38ff764cf4cb11e2c50 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 12:21:42 +0200 Subject: [PATCH 3/7] dice.php: extract email dice function from functions.php --- inc/functions.php | 61 +----------------------------------------- inc/functions/dice.php | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 inc/functions/dice.php diff --git a/inc/functions.php b/inc/functions.php index 30994b5d..15aa2c0e 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -241,7 +241,7 @@ function loadConfig() { $config['version'] = $__version; if ($config['allow_roll']) { - event_handler('post', 'diceRoller'); + event_handler('post', 'email_dice_roll'); } if (in_array('webm', $config['allowed_ext_files']) || in_array('mp4', $config['allowed_ext_files'])) { @@ -2600,65 +2600,6 @@ function shell_exec_error($command, $suppress_stdout = false) { return $return === 'TB_SUCCESS' ? false : $return; } -/* Die rolling: - * If "dice XdY+/-Z" is in the email field (where X or +/-Z may be - * missing), X Y-sided dice are rolled and summed, with the modifier Z - * added on. The result is displayed at the top of the post. - */ -function diceRoller($post) { - global $config; - if(strpos(strtolower($post->email), 'dice%20') === 0) { - $dicestr = str_split(substr($post->email, strlen('dice%20'))); - - // Get params - $diceX = ''; - $diceY = ''; - $diceZ = ''; - - $curd = 'diceX'; - for($i = 0; $i < count($dicestr); $i ++) { - if(is_numeric($dicestr[$i])) { - $$curd .= $dicestr[$i]; - } else if($dicestr[$i] == 'd') { - $curd = 'diceY'; - } else if($dicestr[$i] == '-' || $dicestr[$i] == '+') { - $curd = 'diceZ'; - $$curd = $dicestr[$i]; - } - } - - // Default values for X and Z - if($diceX == '') { - $diceX = '1'; - } - - if($diceZ == '') { - $diceZ = '+0'; - } - - // Intify them - $diceX = intval($diceX); - $diceY = intval($diceY); - $diceZ = intval($diceZ); - - // Continue only if we have valid values - if($diceX > 0 && $diceY > 0) { - $dicerolls = array(); - $dicesum = $diceZ; - for($i = 0; $i < $diceX; $i++) { - $roll = rand(1, $diceY); - $dicerolls[] = $roll; - $dicesum += $roll; - } - - // Prepend the result to the post body - $modifier = ($diceZ != 0) ? ((($diceZ < 0) ? ' - ' : ' + ') . abs($diceZ)) : ''; - $dicesum = ($diceX > 1) ? ' = ' . $dicesum : ''; - $post->body = '
Dice rollRolled ' . implode(', ', $dicerolls) . $modifier . $dicesum . '

' . $post->body; - } - } -} - function slugify($post) { global $config; diff --git a/inc/functions/dice.php b/inc/functions/dice.php new file mode 100644 index 00000000..f3208b33 --- /dev/null +++ b/inc/functions/dice.php @@ -0,0 +1,61 @@ +email), 'dice%20') === 0) { + $dicestr = str_split(substr($post->email, strlen('dice%20'))); + + // Get params + $diceX = ''; + $diceY = ''; + $diceZ = ''; + + $curd = 'diceX'; + for($i = 0; $i < count($dicestr); $i ++) { + if(is_numeric($dicestr[$i])) { + $$curd .= $dicestr[$i]; + } else if($dicestr[$i] == 'd') { + $curd = 'diceY'; + } else if($dicestr[$i] == '-' || $dicestr[$i] == '+') { + $curd = 'diceZ'; + $$curd = $dicestr[$i]; + } + } + + // Default values for X and Z + if($diceX == '') { + $diceX = '1'; + } + + if($diceZ == '') { + $diceZ = '+0'; + } + + // Intify them + $diceX = intval($diceX); + $diceY = intval($diceY); + $diceZ = intval($diceZ); + + // Continue only if we have valid values + if($diceX > 0 && $diceY > 0) { + $dicerolls = array(); + $dicesum = $diceZ; + for($i = 0; $i < $diceX; $i++) { + $roll = rand(1, $diceY); + $dicerolls[] = $roll; + $dicesum += $roll; + } + + // Prepend the result to the post body + $modifier = ($diceZ != 0) ? ((($diceZ < 0) ? ' - ' : ' + ') . abs($diceZ)) : ''; + $dicesum = ($diceX > 1) ? ' = ' . $dicesum : ''; + $post->body = '
Dice rollRolled ' . implode(', ', $dicerolls) . $modifier . $dicesum . '

' . $post->body; + } + } +} From ceccbfc5b79cdc0851d48877301aed167ded4083 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sat, 12 Oct 2024 16:20:01 +0200 Subject: [PATCH 4/7] config.php: limit the number of dicerolls --- inc/config.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/config.php b/inc/config.php index fbca7447..2e14555d 100644 --- a/inc/config.php +++ b/inc/config.php @@ -712,6 +712,9 @@ ); */ + // Maximum number inline of dice rolls per markup. + $config['max_roll_count'] = 50; + // Allow dice rolling: an email field of the form "dice XdY+/-Z" will result in X Y-sided dice rolled and summed, // with the modifier Z added, with the result displayed at the top of the post body. $config['allow_roll'] = false; From ad1d56d0927f22f924d788ad744eb09b3990ae76 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 12:23:38 +0200 Subject: [PATCH 5/7] dice.php: handle inline dice rolling markup --- inc/functions/dice.php | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/inc/functions/dice.php b/inc/functions/dice.php index f3208b33..558257bf 100644 --- a/inc/functions/dice.php +++ b/inc/functions/dice.php @@ -1,6 +1,11 @@ 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 relative to the root. Null if none. + * @return string The html to replace the original markup with. + */ +function inline_dice_roll_markup(array $matches, ?string $img_path): string { + global $config; + + $dice_count = _get_or_default_int($matches, 1, 1); + $dice_faces = _get_or_default_int($matches, 3, 6); + $dice_offset = _get_or_default_int($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; + } + + $tot = 0; + for ($i = 0; $i < $dice_count; $i++) { + $tot += mt_rand(1, $dice_faces); + } + // Ensure that final result is at least an integer. + $tot = abs((int)($dice_offset + $tot)); + + + if ($img_path !== null) { + $img_text = "dice"; + } else { + $img_text = ''; + } + + if ($dice_offset === 0) { + $dice_offset_text = ''; + } elseif ($dice_offset > 0) { + $dice_offset_text = "+{$dice_offset}"; + } else { + $dice_offset_text = (string)$dice_offset; + } + + return "$img_text {$dice_count}d{$dice_faces}{$dice_offset_text} = $tot"; +} From fba88643ecda698ca3be13be3ca1b7adf4b691a6 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 12:24:44 +0200 Subject: [PATCH 6/7] config.php: add inline dice rolling markup support to the default configuration --- inc/config.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inc/config.php b/inc/config.php index 2e14555d..067bc715 100644 --- a/inc/config.php +++ b/inc/config.php @@ -787,6 +787,8 @@ */ $config['markup'] = [ + // Inline dice roll markup. + [ "/!([-+]?\d+)?([d])([-+]?\d+)([-+]\d+)?/iu", fn($m) => inline_dice_roll_markup($m, 'static/d10.svg') ], // "Wiki" markup syntax ($config['wiki_markup'] in pervious versions): [ "/'''(.+?)'''/", "\$1" ], [ "/''(.+?)''/", "\$1" ], From b67ff982e29759d1349b6f21acc45471d9da158e Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 13:55:58 +0200 Subject: [PATCH 7/7] composer: add dice.php to autoload --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 88ee789d..a2e906fe 100644 --- a/composer.json +++ b/composer.json @@ -34,9 +34,10 @@ "inc/lock.php", "inc/queue.php", "inc/functions.php", + "inc/functions/dice.php", + "inc/functions/format.php", "inc/functions/net.php", "inc/functions/num.php", - "inc/functions/format.php", "inc/functions/theme.php", "inc/service/captcha-queries.php", "inc/context.php"