From 4b25d42596a8e2be5444d4ba25178ad8cb8d0381 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 7 May 2014 05:18:35 +0100 Subject: [PATCH 1/5] Implement dice rolling in posts: use an email like 'dice XdY+Z' to roll. --- inc/config.php | 4 ++++ post.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/inc/config.php b/inc/config.php index 494ccda1..da6cf8cf 100644 --- a/inc/config.php +++ b/inc/config.php @@ -547,6 +547,10 @@ 'eu' => 'Europe' ); */ + + // 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; /* * ==================== diff --git a/post.php b/post.php index d1c2b086..8b75a112 100644 --- a/post.php +++ b/post.php @@ -482,6 +482,60 @@ if (isset($_POST['delete'])) { wordfilters($post['body']); $post['body'] = escape_markup_modifiers($post['body']); + + /* 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. + */ + if($config['allow_roll'] && 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 + $post['body'] = 'Rolled ' . implode(', ', $dicerolls) . ' = ' . $dicesum . "\r\n" . $post['body']; + } + } if ($mod && isset($post['raw']) && $post['raw']) { $post['body'] .= "\n1"; From a6ccc1872ab3abbfa7731f2f991a1bdd93f49980 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 7 May 2014 05:56:29 +0100 Subject: [PATCH 2/5] Show the modifier in the result string --- post.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/post.php b/post.php index 8b75a112..8e2a5a0f 100644 --- a/post.php +++ b/post.php @@ -533,7 +533,8 @@ if (isset($_POST['delete'])) { } // Prepend the result to the post body - $post['body'] = 'Rolled ' . implode(', ', $dicerolls) . ' = ' . $dicesum . "\r\n" . $post['body']; + $modifier = ($diceZ != 0) ? ((($diceZ < 0) ? ' - ' : ' + ') . abs($diceZ)) : ''; + $post['body'] = 'Rolled ' . implode(', ', $dicerolls) . $modifier . ' = ' . $dicesum . "\r\n" . $post['body']; } } From fa284fc37addb8fec782c0dabec94541e047c479 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 7 May 2014 06:02:10 +0100 Subject: [PATCH 3/5] Don't show dice sum if only 1 die was rolled --- post.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/post.php b/post.php index 8e2a5a0f..c39355d6 100644 --- a/post.php +++ b/post.php @@ -534,7 +534,8 @@ if (isset($_POST['delete'])) { // Prepend the result to the post body $modifier = ($diceZ != 0) ? ((($diceZ < 0) ? ' - ' : ' + ') . abs($diceZ)) : ''; - $post['body'] = 'Rolled ' . implode(', ', $dicerolls) . $modifier . ' = ' . $dicesum . "\r\n" . $post['body']; + $dicesum = ($diceX > 1) ? ' = ' . $dicesum : ''; + $post['body'] = 'Rolled ' . implode(', ', $dicerolls) . $modifier . $dicesum . "\r\n" . $post['body']; } } From cce7df20c77f4a4e2c6b04e019e602b15166f4e4 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Wed, 7 May 2014 06:04:06 +0100 Subject: [PATCH 4/5] Restrict number of dice we roll, and the number of sides, to positive values --- post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/post.php b/post.php index c39355d6..574de077 100644 --- a/post.php +++ b/post.php @@ -523,7 +523,7 @@ if (isset($_POST['delete'])) { $diceZ = intval($diceZ); // Continue only if we have valid values - if($diceX != 0 && $diceY != 0) { + if($diceX > 0 && $diceY > 0) { $dicerolls = array(); $dicesum = $diceZ; for($i = 0; $i < $diceX; $i++) { From 3c70fe78ae64a4f37092ae5b2eefd6c8fa944a02 Mon Sep 17 00:00:00 2001 From: Michael Walker Date: Mon, 19 May 2014 04:16:02 +0100 Subject: [PATCH 5/5] Move rolling into an event handler --- inc/functions.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++ post.php | 56 ------------------------------------------ 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index db9c91b0..2c599ec5 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -240,6 +240,10 @@ function loadConfig() { event_handler('post', 'postHandler'); } + if ($config['allow_roll']) { + event_handler('post', 'diceRoller'); + } + event('load-config'); if ($config['debug']) { @@ -2298,3 +2302,61 @@ 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) { + 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 = 'Rolled ' . implode(', ', $dicerolls) . $modifier . $dicesum . '
' . $post->body; + } + } +} diff --git a/post.php b/post.php index 574de077..d1c2b086 100644 --- a/post.php +++ b/post.php @@ -482,62 +482,6 @@ if (isset($_POST['delete'])) { wordfilters($post['body']); $post['body'] = escape_markup_modifiers($post['body']); - - /* 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. - */ - if($config['allow_roll'] && 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'] = 'Rolled ' . implode(', ', $dicerolls) . $modifier . $dicesum . "\r\n" . $post['body']; - } - } if ($mod && isset($post['raw']) && $post['raw']) { $post['body'] .= "\n1";