@ 2020-01-21T12:54:52Z <?php
function parseRollCode($rollCode) {
$cleanRollCode = preg_replace('/\s+/', '', strtolower($rollCode));
$intro = "<strong>Dice Roll:</strong> {$cleanRollCode}";
$error = "Invalid Roll Code";
$result = [
'input' => $rollCode,
'total' => 0,
'detailText' => $intro
];
/*
$out...
[0] => roll tokens (optional leading sign)
[1] => optional signs
[2] => number&type die segments
[3] => reroll threshold segments
[4] => keep setting segments
[5] => constant segments
*/
if (!preg_match_all('~\G(^-?|[+-])(?:(\d+d\d+)(r\d+)?([v^]\d+)?|(\d+))(?=[+-]|$)~x', $cleanRollCode, $out)) {
$result['detailText'] .= "\n{$error}";
return $result;
}
foreach ($out[0] as $index => $segment) {
$result['detailText'] .= "\n<strong>{$segment} Results:</strong>";
if (!empty($out[5][$index])) {
$result['detailText'] .= ' (subtotal: ' . ((int)$segment) . ')';
$result['total'] += $segment;
} else {
// dice count & die type
[$diceCount, $dieType] = explode('d', $out[2][$index], 2);
// positive or negative arithmetic
$signFactor = (!empty($out[1][$index][0]) && $out[1][$index][0] == '-' ? -1 : 1);
// reroll threshold
$rerollThreshold = (int)ltrim($out[3][$index] ?? 0, 'r');
if ($rerollThreshold >= $dieType) {
return ['input' => $rollCode, 'total' => 0, 'detailText' => "{$intro}\n{$error}"];
}
$rollResults = [];
for ($r = 0; $r < $diceCount; ++$r) {
$rollResults[] = rand($rerollThreshold + 1, $dieType);
}
// keep settings
if (!empty($out[4][$index])) {
$keepCount = ltrim($out[4][$index], '^v');
if ($keepCount > $diceCount) {
return ['input' => $rollCode, 'total' => 0, 'detailText' => "{$intro}\n{$error}"];
}
if ($out[4][$index][0] == '^') {
arsort($rollResults);
} else {
asort($rollResults);
}
$keep = array_slice($rollResults, 0, $keepCount, true);
$subtotal = array_sum($keep) * $signFactor;
for ($i = 0, $len = count($rollResults); $i < $len; ++$i) {
$result['detailText'] .= ' ' . ($keep[$i] ?? "<s>{$rollResults[$i]}</s>");
}
$result['detailText'] .= " (subtotal: {$subtotal})";
$result['total'] += $subtotal;
} else {
$subtotal = array_sum($rollResults) * $signFactor;
$result['detailText'] .= " " . implode(" ", $rollResults) . " (subtotal: {$subtotal})";
$result['total'] += $subtotal;
}
}
}
return $result;
}
$rolls = ["1 d2 0", "-1d20+5", "d20", "2d20^1", "4d6r1^3", "5d20^3", "5d8v2", "2d20v1", "5+3d4", "3d20^1",
"3d6r1 + 1d4r2", "-1d20", "1d20+5", "1d20 + 5", "1d-20", "1d20+1d6", "5d20-3d6", "1d6r2", "5d6r-6", "3d20^1v1",
"1d20r30", "1d20r20", "1d20r19", "1d20v2", "1d20v1", "1d20^2", "1d20+5d8r2^3-5-1d6+3d4+2-4+6-8"];
foreach ($rolls as $roll) {
$rollResult = parseRollCode($roll);
echo "{$rollResult['detailText']}\n<strong>Total:</strong> {$rollResult['total']}\n---\n";
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.0.1 0.018 0.003 17.29 8.0.0 0.010 0.010 17.53 7.4.14 0.017 0.000 16.76 7.4.13 0.008 0.011 16.62 7.4.12 0.011 0.008 16.94 7.4.11 0.010 0.010 16.73 7.4.10 0.004 0.015 16.68 7.4.9 0.006 0.013 16.98 7.4.8 0.006 0.016 16.85 7.4.7 0.012 0.006 16.63 7.4.6 0.018 0.003 16.74 7.4.5 0.014 0.006 16.57 7.4.4 0.008 0.011 16.69 7.4.3 0.006 0.012 16.85 7.4.2 0.022 0.000 16.50 7.4.1 0.017 0.005 16.75 7.4.0 0.011 0.008 16.68 7.3.26 0.003 0.016 16.65 7.3.25 0.013 0.006 16.57 7.3.24 0.007 0.011 16.59 7.3.23 0.013 0.005 16.49 7.3.22 0.013 0.005 16.48 7.3.21 0.006 0.019 16.60 7.3.20 0.012 0.009 17.00 7.3.19 0.005 0.013 16.65 7.3.18 0.007 0.011 16.72 7.3.17 0.012 0.006 16.57 7.3.16 0.009 0.009 16.72 7.3.15 0.011 0.008 16.77 7.3.14 0.015 0.006 16.77 7.3.13 0.011 0.010 16.55 7.3.12 0.005 0.016 16.74 7.3.11 0.012 0.011 16.64 7.3.10 0.009 0.010 16.43 7.3.9 0.012 0.008 16.85 7.3.8 0.012 0.010 16.69 7.3.7 0.013 0.007 16.57 7.3.6 0.010 0.008 16.60 7.3.5 0.011 0.009 16.57 7.3.4 0.010 0.010 16.62 7.3.3 0.009 0.010 16.62 7.3.2 0.010 0.009 16.71 7.3.1 0.013 0.009 16.57 7.3.0 0.012 0.009 16.62 7.2.34 0.010 0.013 17.00 7.2.33 0.021 0.007 16.84 7.2.32 0.019 0.011 16.90 7.2.31 0.025 0.004 16.93 7.2.30 0.019 0.014 16.90 7.2.29 0.017 0.013 16.81 7.2.28 0.020 0.014 16.98 7.2.27 0.026 0.007 17.11 7.2.26 0.019 0.010 16.86 7.2.25 0.018 0.007 16.94 7.2.24 0.019 0.006 16.85 7.2.23 0.018 0.008 16.85 7.2.22 0.020 0.003 16.96 7.2.21 0.014 0.012 16.83 7.2.20 0.024 0.002 16.89 7.2.19 0.015 0.009 16.91 7.2.18 0.017 0.008 16.92 7.2.17 0.014 0.010 16.71 7.2.16 0.011 0.016 16.96 7.2.15 0.014 0.013 17.04 7.2.14 0.019 0.008 16.85 7.2.13 0.021 0.007 16.91 7.2.12 0.019 0.006 16.78 7.2.11 0.021 0.006 16.91 7.2.10 0.020 0.014 16.92 7.2.9 0.017 0.013 16.95 7.2.8 0.019 0.009 16.86 7.2.7 0.036 0.011 16.82 7.2.6 0.019 0.007 16.92 7.2.5 0.020 0.005 16.87 7.2.4 0.021 0.009 17.02 7.2.3 0.018 0.010 17.03 7.2.2 0.018 0.008 16.85 7.2.1 0.013 0.015 16.86 7.2.0 0.013 0.017 16.83
preferences:dark mode live preview ace vim emacs key bindings
28.08 ms | 403 KiB | 5 Q