From b8c53fbbcdffe50e1548c38ff764cf4cb11e2c50 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Mon, 14 Oct 2024 12:21:42 +0200 Subject: [PATCH] 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; + } + } +}