1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-26 08:20:58 +01:00

Implement dice rolling in posts: use an email like 'dice XdY+Z' to roll.

This commit is contained in:
Michael Walker 2014-05-07 05:18:35 +01:00
parent 9df6ca8ddd
commit 4b25d42596
2 changed files with 58 additions and 0 deletions

View File

@ -548,6 +548,10 @@
);
*/
// 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;
/*
* ====================
* Ban settings

View File

@ -483,6 +483,60 @@ if (isset($_POST['delete'])) {
$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'] .= "\n<tinyboard raw html>1</tinyboard>";
}