-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll.php
48 lines (38 loc) · 1.01 KB
/
roll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
$die = strtolower($argv[1]);
$exploded = explode('d', $die);
if(empty($exploded[0])) {
$rolls = 1;
}else{
$rolls = (int)$exploded[0];
}
$right_side = $exploded[1];
$mod_plus = strpos($right_side,'+');
$mod_minus = strpos($right_side,'-');
if ($mod_plus == TRUE) {
$r_exploded = explode('+',$right_side);
$modifier = (int)$r_exploded[1];
$modifier_operation = '+';
$sides = (int)$r_exploded[0];
} elseif ($mod_minus == TRUE) {
$r_exploded = explode('-',$right_side);
$modifier = (int)$r_exploded[1];
$modifier_operation = '-';
$sides = (int)$r_exploded[0];
} else {
$modifier = 0;
$modifier_operation = '+';
$sides = (int)$right_side;
}
$total = 0;
$roll = 0;
while($roll < $rolls) {
$total += random_int(1,$sides);
$roll++;
}
if ($modifier_operation == '+') {
$total += $modifier;
} elseif ($modifier_operation == '-') {
$total -= $modifier;
}
echo $total."\n";