1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MeadSteve\DiceApi\Dice; |
4
|
|
|
|
5
|
|
|
use MeadSteve\DiceApi\BasicDice; |
6
|
|
|
use MeadSteve\DiceApi\Dice; |
7
|
|
|
|
8
|
|
|
class DiceGenerator |
9
|
|
|
{ |
10
|
|
|
public function diceFromUrlString($urlString) |
11
|
|
|
{ |
12
|
|
|
$parts = explode("/", $urlString); |
13
|
|
|
$parts = array_filter($parts, [$this, 'notBlank']); |
14
|
|
|
$diceSets = array_map([$this, 'getDiceForPart'], $parts); |
15
|
|
|
return $this->flattenDiceSets($diceSets); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $part |
20
|
|
|
* @return Dice[] |
21
|
|
|
*/ |
22
|
|
|
private function getDiceForPart($part) |
23
|
|
|
{ |
24
|
|
|
$data = $this->parseDiceString($part); |
25
|
|
|
$type = $data["type"]; |
26
|
|
|
$diceCount = $data["count"]; |
27
|
|
|
|
28
|
|
|
if (is_numeric($type)) { |
29
|
|
|
return $this->buildBasicNumericDice($type, $diceCount); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
switch (strtolower($type)) { |
33
|
|
|
case "steve": |
34
|
|
|
return $this->buildSteveDice($type, $diceCount); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new UncreatableDiceException("No idea how to make a d{$type}"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $size |
42
|
|
|
* @return Dice |
43
|
|
|
*/ |
44
|
|
|
private function newDiceOfSize($size) |
45
|
|
|
{ |
46
|
|
|
if ($size == 0) { |
47
|
|
|
return new ZeropointDice(); |
48
|
|
|
} |
49
|
|
|
return new BasicDice($size); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array[] |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function flattenDiceSets($diceSets) |
57
|
|
|
{ |
58
|
|
|
$dice = []; |
59
|
|
|
foreach ($diceSets as $set) { |
60
|
|
|
$dice = array_merge($dice, $set); |
61
|
|
|
} |
62
|
|
|
return $dice; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $part |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
private function parseDiceString($part) |
70
|
|
|
{ |
71
|
|
|
$data = []; |
72
|
|
|
$valid = preg_match("/(?P<count>[0-9]+)?d(?P<type>[^\/]+)/i", $part, $data); |
73
|
|
|
if (!$valid) { |
74
|
|
|
throw new UncreatableDiceException("Problem creating dice from incorrectly formated data: " + $part); |
75
|
|
|
} |
76
|
|
|
if (!$data["count"]) { |
77
|
|
|
$data["count"] = 1; |
78
|
|
|
} |
79
|
|
|
return $data; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function notBlank($string) |
83
|
|
|
{ |
84
|
|
|
return $string !== ""; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function buildBasicNumericDice($size, $diceCount) : array |
88
|
|
|
{ |
89
|
|
|
$newDice = []; |
90
|
|
|
if ((strlen($size) > 4) || ($size > 9000)) { |
91
|
|
|
throw new UncreatableDiceException("Only dice with a power level less than 9000 can be created."); |
92
|
|
|
} |
93
|
|
|
for ($i = 0; $i < $diceCount; $i++) { |
94
|
|
|
$newDice[] = $this->newDiceOfSize($size); |
95
|
|
|
} |
96
|
|
|
return $newDice; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function buildSteveDice($_type, $diceCount) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$newDice = []; |
102
|
|
|
for ($i = 0; $i < $diceCount; $i++) { |
103
|
|
|
$newDice[] = new SteveDice(); |
104
|
|
|
} |
105
|
|
|
return $newDice; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.