-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallOfCthulhu7EdD100Die.php
159 lines (124 loc) · 3.89 KB
/
CallOfCthulhu7EdD100Die.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
153
154
155
156
157
158
159
<?php
namespace Fervo\Rollo;
class CallOfCthulhu7EdD100Die implements DieInterface
{
protected $tens;
protected $units;
protected $extraTens = [];
protected $isPenalty = false;
public function __construct($bonus = 0, $penalty = 0)
{
$this->tens = new SingleDie(10);
$this->units = new SingleDie(10);
if ($bonus > $penalty) {
$bonusDice = $bonus - $penalty;
for ($i=0; $i < $bonusDice; $i++) {
$this->extraTens[] = new SingleDie(10);
}
} else {
$penaltyDice = $penalty - $bonus;
for ($i=0; $i < $penaltyDice; $i++) {
$this->extraTens[] = new SingleDie(10);
$this->isPenalty = true;
}
}
}
public function roll()
{
$this->tens->roll();
$this->units->roll();
foreach ($this->extraTens as $theDie) {
$theDie->roll();
}
}
public function getValue()
{
$tens = $this->getUsedTensDie();
return $this->doGetDiceValue($tens, $this->units);
}
protected function doGetDiceValue(SingleDie $tens, SingleDie $units)
{
if ($tens->getValue() === null || $units->getValue() === null) {
return null;
}
$value = $this->getTensValue($tens) + $this->getUnitsValue($units);
if ($value == 0) {
return 100;
}
return $value;
}
public function getValueDescription()
{
$total = $this->getValue();
if ($total === null) {
$total = '*';
}
$usedTens = $this->getUsedTensDie();
$unusedTens = $this->getUnusedTensDice();
$unusedDiceDescriptions = array_map(function($theDie) {
return sprintf(
"%s%s",
($this->isPenalty ? 'P' : 'B'),
($theDie->getValue() == null ? '*' : $this->getTensValue($theDie))
);
}, $unusedTens);
$unused = implode(',', $unusedDiceDescriptions);
if (strlen($unused) > 0) {
$unused .= ',';
}
return sprintf(
"D100:{%s%s+%s}=%s",
$unused,
$this->getTensValueDescription($usedTens),
$this->getUnitsValueDescription($this->units),
$total
);
}
public function getExpression()
{
return str_pad('C100', 4+count($this->extraTens), ($this->isPenalty ? 'p' : 'b'));
}
protected function getUsedTensDie()
{
if ($this->tens->getValue() == null) {
return $this->tens;
}
$currentDie = $this->tens;
foreach ($this->extraTens as $theDie) {
$currentValue = $this->doGetDiceValue($currentDie, $this->units);
$newValue = $this->doGetDiceValue($theDie, $this->units);
if ((!$this->isPenalty && $currentValue > $newValue) || ($this->isPenalty && $currentValue < $newValue)) {
$currentDie = $theDie;
}
}
return $currentDie;
}
protected function getUnusedTensDice()
{
$dice = array_merge([$this->tens], $this->extraTens);
$usedTens = $this->getUsedTensDie();
return array_filter($dice, function ($theDie) use ($usedTens) { return $theDie !== $usedTens; });
}
protected function getTensValue(SingleDie $tens)
{
return ($tens->getValue() - 1) * 10;
}
protected function getUnitsValue(SingleDie $units)
{
return $units->getValue() - 1;
}
protected function getTensValueDescription(SingleDie $tens)
{
if ($tens->getValue() == null) {
return '*';
}
return $this->getTensValue($tens);
}
protected function getUnitsValueDescription(SingleDie $units)
{
if ($units->getValue() == null) {
return '*';
}
return $this->getUnitsValue($units);
}
}