Skip to content

Commit 11876ce

Browse files
Added getExpression() method to DieInterface.
1 parent c8f4a4f commit 11876ce

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

CallOfCthulhu7EdD100Die.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function getValueDescription()
9595
);
9696
}
9797

98+
public function getExpression()
99+
{
100+
return str_pad('C100', 4+count($this->extraTens), ($this->isPenalty ? 'p' : 'b'));
101+
}
102+
98103
protected function getUsedTensDie()
99104
{
100105
if ($this->tens->getValue() == null) {

ConstantDie.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public function getValueDescription()
2424
{
2525
return '#'.$this->value;
2626
}
27+
28+
public function getExpression()
29+
{
30+
return $this->value;
31+
}
2732
}

DieCollection.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,43 @@ public function getValueDescription()
110110

111111
return $description;
112112
}
113+
114+
public function getExpression()
115+
{
116+
return '('.implode(' '.$this->operator.' ', $this->getSubExpressions($this->dice)).')';
117+
}
118+
119+
protected function getSubExpressions($expressions)
120+
{
121+
$newExpressions = [];
122+
for ($i=0; $i < count($expressions); $i++) {
123+
$current = $expressions[$i];
124+
125+
if ($current instanceOf SingleDie) {
126+
$counter = 1;
127+
128+
for ($j=$i+1; $j < count($expressions); $j++) {
129+
$inner = $expressions[$j];
130+
if ($inner instanceOf SingleDie) {
131+
if ($current->getExpression() == $inner->getExpression()) {
132+
$i++;
133+
$counter++;
134+
}
135+
} else {
136+
break;
137+
}
138+
}
139+
140+
if ($counter > 1) {
141+
$newExpressions[] = $counter.$current->getExpression();
142+
} else {
143+
$newExpressions[] = $current->getExpression();
144+
}
145+
} else {
146+
$newExpressions[] = $current->getExpression();
147+
}
148+
}
149+
150+
return $newExpressions;
151+
}
113152
}

DieInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public function roll();
99
public function getValue();
1010

1111
public function getValueDescription();
12+
13+
public function getExpression();
1214
}

SingleDie.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public function getValueDescription()
3030

3131
return sprintf('D%d:*', $this->sides);
3232
}
33+
34+
public function getExpression()
35+
{
36+
return 'D'.$this->sides;
37+
}
3338
}

0 commit comments

Comments
 (0)