summaryrefslogtreecommitdiff
path: root/roll.php
blob: c52f7621a19526fbc2391895d2f03623a094f7db (plain)
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
#!/usr/bin/env php
<?php

function usage() {
	echo "Arguments are in the format [<COUNT>]d<SIZE>[+MOD]\n";
}

function roll($input) {
	preg_match('/([0-9]*)d([0-9]+)([+-][0-9]+)?/', $input, $matches);
	if (sizeof($matches) < 2) {
		usage();
		return;
	}
	$dice = (int)$matches[1];
	$die_size = (int)$matches[2];
	@$mod = (int)$matches[3];
	if ($dice<1) $dice = 1;

	$total = 0;
	for ($i=0; $i < $dice; $i++) {
		$v = mt_rand(1, $die_size);
		echo $v.'+';
		$total += $v;
	}
	$total += $mod;
	echo $mod.' = '.$total."\n";
}

if (sizeof($argv) == 1) {
	usage();
}
array_shift($argv);
foreach ($argv as $arg) {
	roll($arg);
}