-
Notifications
You must be signed in to change notification settings - Fork 0
/
DieCollection.php
152 lines (123 loc) · 3.81 KB
/
DieCollection.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Fervo\Rollo;
use Doctrine\Common\Collections\ArrayCollection;
/**
*
*/
class DieCollection implements DieInterface
{
const OPERATOR_ADDITION = '+';
const OPERATOR_SUBTRACTION = '-';
protected $operator;
protected $dice;
public function __construct(array $dice = [], $operator = self::OPERATOR_ADDITION)
{
$this->operator = $operator;
$this->dice = new ArrayCollection();
$this->addDice($dice);
}
public function getOperator()
{
return $this->operator;
}
public function getDice()
{
return $this->dice->toArray();
}
public function replaceDieWithDice(DieInterface $oldDie, array $dice)
{
// @todo optimize
$index = $this->dice->indexOf($oldDie);
if ($index === false) {
throw new \RuntimeException("Old die to be replaced doesn't exist in collection");
}
if ($index === 0) {
$left = [];
} else {
$left = $this->dice->slice(0, $index);
}
$right = $this->dice->slice($index + 1);
$this->dice = new ArrayCollection($left);
$this->addDice($dice);
$this->addDice($right);
}
public function addDice(array $dice)
{
foreach ($dice as $theDie) {
$this->addDie($theDie);
}
}
public function addDie(DieInterface $theDie)
{
if ($this->dice->indexOf($theDie) !== false) {
throw new NotUniqueException($theDie);
}
$this->dice[] = $theDie;
}
public function roll()
{
foreach ($this->dice as $theDie) {
$theDie->roll();
}
}
public function getValue()
{
// @todo optimize
foreach ($this->dice as $theDie) {
if ($theDie->getValue() === null) {
return null;
}
}
$total = $this->dice->first()->getValue();
foreach ($this->dice->slice(1) as $theDie) {
if ($this->operator == self::OPERATOR_ADDITION) {
$total += $theDie->getValue();
} else {
$total -= $theDie->getValue();
}
}
return $total;
}
public function getValueDescription()
{
// @todo optimize
$diceDescriptions = $this->dice->map(function($theDie) { return $theDie->getValueDescription(); });
$description = '[';
$description .= implode(' '.$this->operator.' ', $diceDescriptions->getValues());
$description .= ']='.($this->getValue() === null ? '*' : $this->getValue());
return $description;
}
public function getExpression()
{
return '('.implode(' '.$this->operator.' ', $this->getSubExpressions($this->dice)).')';
}
protected function getSubExpressions($expressions)
{
$newExpressions = [];
for ($i=0; $i < count($expressions); $i++) {
$current = $expressions[$i];
if ($current instanceOf SingleDie) {
$counter = 1;
for ($j=$i+1; $j < count($expressions); $j++) {
$inner = $expressions[$j];
if ($inner instanceOf SingleDie) {
if ($current->getExpression() == $inner->getExpression()) {
$i++;
$counter++;
}
} else {
break;
}
}
if ($counter > 1) {
$newExpressions[] = $counter.$current->getExpression();
} else {
$newExpressions[] = $current->getExpression();
}
} else {
$newExpressions[] = $current->getExpression();
}
}
return $newExpressions;
}
}