CalcDice::rolltype()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 15
nc 5
nop 1
1
<?php
2
3
namespace DiceCalc;
4
5
/**
6
 * Class CalcDice
7
 *
8
 * @package DiceCalc
9
 * @author  Owen Winkler <[email protected]>
10
 * @license MIT http://opensource.org/licenses/MIT
11
 */
12
class CalcDice extends CalcSet
13
{
14
    protected $saved_values = [];
15
16
    public function __construct($set_value)
17
    {
18
        $this->values = [];
19
        $this->label = $set_value;
20
        preg_match('/' . Calc::DICE_REGEX . '/ix', $set_value, $matches);
21
        if (intval($matches['multiple']) == 0 && $matches['multiple'] != '0') {
22
            $matches['multiple'] = 1;
23
        }
24
        $matches += ['matches'=>'', 'openroll' => '', 'reroll' => '', 'keep' => ''];
25
        for ($z = 0; $z < $matches['multiple']; $z++) {
26
            $keep = true;
27
28
            $newval = $this->rolltype($matches['dietype']);
29
30
            if ($matches['reroll'] != '') {
31
                $gtlt = $matches['rerolleval'];
32
                $range = intval($matches['rerolllimit']);
33
                if ($gtlt == '<' && $newval < $range) {
34
                    $keep = false;
35
                    $z--;
36
                }
37
                if ($gtlt == '>' && $newval > $range) {
38
                    $keep = false;
39
                    $z--;
40
                }
41
            }
42
43
            if ($matches['openroll'] != '') {
44
                $gtlt = $matches['openrolleval'];
45
                $range = intval($matches['openrolllimit']);
46
                $addvals = [$newval];
47
                $addval = $newval;
48
49
                $evals = [
50
                    '<' => function($addval, $range) { return $addval < $range; },
51
                    '>' => function($addval, $range) { return $addval > $range; },
52
                    '=' => function($addval, $range) { return $addval == $range; },
53
                ];
54
55
                while(isset($evals[$gtlt]) && $evals[$gtlt]($addval, $range)) {
56
                    $addval = $this->rolltype($matches['dietype']);
57
                    $addvals[] = $addval;
58
                }
59
60
                $newval = new Calc(implode('+', $addvals));
61
                $newval = $newval();
62
            }
63
64
            if ($keep) {
65
                $this->values['_' . count($this->values)] = $newval;
66
            }
67
        }
68
69
        $this->saved_values = $this->values;
70
71
        if ($matches['keep'] != '') {
72
            $gtlt = $matches['keepeval'];
73
            $range = intval($matches['keeprange']);
74
            foreach ($this->values as $k => $set_value) {
75
                $av = $set_value instanceof Calc ? $set_value() : $set_value;
76
                if ($gtlt == '>' && $av <= $range) {
77
                    unset($this->values[$k]);
78
                }
79
                if ($gtlt == '<' && $av >= $range) {
80
                    unset($this->values[$k]);
81
                }
82
            }
83
        }
84
85
        asort($this->values);
86 View Code Duplication
        if (isset($matches['highdice']) && $matches['highdice'] != '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            $this->values = array_slice($this->values, -intval($matches['highdice']), null, true);
88
        }
89 View Code Duplication
        if (isset($matches['lowdice']) && $matches['lowdice'] != '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $this->values = array_slice($this->values, 0, intval($matches['lowdice']), true);
91
        }
92
    }
93
94
    public function rolltype($dietype)
95
    {
96
        if (is_numeric($dietype)) {
97
            $newval = Random::get(1, $dietype);
98
        } elseif ($dietype == 'f') {
99
            $newval = Random::get(-1, 1);
100
        } elseif ($dietype == '%') {
101
            $newval = Random::get(1, 100);
102
        } elseif ($dietype[0] == '[') {
103
            $dietype = trim($dietype, '[]');
104
            $opts = explode(',', $dietype);
105
            $newval = $opts[Random::get(0, count($opts)-1)];
106
        } else {
107
            var_dump($dietype);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($dietype); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
108
            $newval = 'unknown';
109
        }
110
111
        return $newval;
112
    }
113
114
    public function __toString()
115
    {
116
        $out = [];
117
        foreach (array_keys($this->saved_values) as $key) {
118
            $vout = $this->saved_values[$key];
119
            if ($vout === true) {
120
                $vout = '<span class="true">true</span>';
121
            }
122
            if ($vout === false) {
123
                $vout = '<span class="false">false</span>';
124
            }
125
            if (isset($this->values[$key])) {
126
                $out[] = $vout;
127
            } else {
128
                if ($vout instanceof Calc) {
129
                    $out[] = '<s>' . $vout() . '</s>';
130
                } else {
131
                    $out[] = '<s>' . $vout . '</s>';
132
                }
133
            }
134
        }
135
136
        $result = '[' . $this->label . ':';
137
        $comma = '';
138
        foreach ($out as $o) {
139
            if ($o instanceof Calc) {
140
                $result .= $comma . $o();
141
            } else {
142
                $result .= $comma . $o;
143
            }
144
            $comma = ' + ';
145
        }
146
        $result .= ']';
147
148
        return $result;
149
    }
150
}
151