Evaluator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 151
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A process() 0 8 1
A findPotentialLevels() 0 14 2
A testCP() 0 9 1
A testHP() 0 4 1
A findPotentialHPIVs() 0 16 4
B findPotentialIVs() 0 16 5
1
<?php
2
3
namespace IVCalculator;
4
5
use Illuminate\Support\Collection;
6
use IVCalculator\Entities\Evaluation;
7
use IVCalculator\Entities\HPIV;
8
use IVCalculator\Entities\IV;
9
use IVCalculator\Entities\Level;
10
use IVCalculator\Entities\Pokemon;
11
use IVCalculator\Traits\InteractsWithFiles;
12
13
class Evaluator
14
{
15
    use InteractsWithFiles;
16
17
    /**
18
     * @var Collection
19
     */
20
    private $potentialLevels;
21
22
    /**
23
     * @var Collection
24
     */
25
    private $potentialHPIVs;
26
27
    /**
28
     * @var Collection
29
     */
30
    private $potentialIVs;
31
32
    /**
33
     * @var Collection
34
     */
35
    protected $levelUpData;
36
37
    public function __construct()
38
    {
39
        $this->levelUpData = $this->loadDataFile('level_up.json');
40
41
        $this->potentialLevels = new Collection();
42
        $this->potentialHPIVs = new Collection();
43
        $this->potentialIVs = new Collection();
44
    }
45
46
    /**
47
     * @param Pokemon $pokemon
48
     * @param $cp
49
     * @param $hp
50
     * @param $dustCost
51
     * @param bool $neverUpgraded
52
     *
53
     * @return Evaluation
54
     */
55
    public function process(Pokemon $pokemon, $cp, $hp, $dustCost, $neverUpgraded = false)
56
    {
57
        $this->findPotentialLevels($dustCost, $neverUpgraded)
58
            ->findPotentialHPIVs($pokemon, $hp)
59
            ->findPotentialIVs($pokemon, $cp);
60
61
        return new Evaluation($this->potentialIVs);
62
    }
63
64
    /**
65
     * @param $dustCost
66
     * @param $neverUpgraded
67
     *
68
     * @return $this
69
     */
70
    private function findPotentialLevels($dustCost, $neverUpgraded)
71
    {
72
        $this->potentialLevels = $this->levelUpData
73
            ->where('dust', $dustCost)
74
            ->sortBy('level');
75
76
        if ($neverUpgraded) {
77
            $this->potentialLevels->filter(function ($potentialLevel) {
78
                return $potentialLevel->level % 2 === 0;
79
            });
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param Pokemon $pokemon
87
     * @param $hp
88
     *
89
     * @return $this
90
     */
91
    private function findPotentialHPIVs(Pokemon $pokemon, $hp)
92
    {
93
        foreach ($this->potentialLevels as $data) {
94
            $level = new Level($data->level, $data->dust, $data->candy, $data->cpScalar);
95
96
            foreach (range(0, 15) as $staminaIV) {
97
                if ($this->testHP($pokemon, $level, $hp, $staminaIV)) {
98
                    $hpiv = new HPIV($level, $staminaIV);
99
100
                    $this->potentialHPIVs->push($hpiv);
101
                }
102
            }
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param Pokemon $pokemon
110
     * @param $cp
111
     *
112
     * @return $this
113
     */
114
    private function findPotentialIVs(Pokemon $pokemon, $cp)
115
    {
116
        foreach ($this->potentialHPIVs as $hpiv) {
117
            foreach (range(0, 15) as $attackIV) {
118
                foreach (range(0, 15) as $defenseIV) {
119
                    if ($this->testCP($pokemon, $hpiv->level, $cp, $attackIV, $defenseIV, $hpiv->staminaIV)) {
120
                        $iv = new IV($attackIV, $defenseIV, $hpiv->staminaIV, $hpiv->level->level);
121
122
                        $this->potentialIVs->push($iv);
123
                    }
124
                }
125
            }
126
        }
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param Pokemon $pokemon
133
     * @param Level   $level
134
     * @param $cp
135
     * @param $attackIV
136
     * @param $defenseIV
137
     * @param $staminaIV
138
     *
139
     * @return bool
140
     */
141
    private function testCP(Pokemon $pokemon, Level $level, $cp, $attackIV, $defenseIV, $staminaIV)
142
    {
143
        $attackFactor = $pokemon->attack + $attackIV;
144
        $defenseFactor = pow($pokemon->defense + $defenseIV, 0.5);
145
        $staminaFactor = pow($pokemon->stamina + $staminaIV, 0.5);
146
        $scalarFactor = pow($level->cpScalar, 2);
147
148
        return $cp == floor($attackFactor * $defenseFactor * $staminaFactor * $scalarFactor / 10);
149
    }
150
151
    /**
152
     * @param Pokemon $pokemon
153
     * @param Level   $level
154
     * @param $hp
155
     * @param $staminaIV
156
     *
157
     * @return bool
158
     */
159
    private function testHP(Pokemon $pokemon, Level $level, $hp, $staminaIV)
160
    {
161
        return $hp == (int) floor(($pokemon->stamina + $staminaIV) * $level->cpScalar);
162
    }
163
}
164