Completed
Push — master ( a440a9...78d742 )
by Jérémy
03:36
created

Helpers::calculateCP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 5
1
<?php
2
3
/*
4
 * (c) Jérémy Marodon <[email protected]>
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Th3Mouk\PokemonGoIVCalculator\Calculator;
10
11
use Th3Mouk\PokemonGoIVCalculator\Entities\Level;
12
use Th3Mouk\PokemonGoIVCalculator\Extractors\LevelExtractor;
13
14
class Helpers
15
{
16
    /**
17
     * Method to calculate how many dusts are necessary to max a pokemon
18
     * @param  float $pokemon the level of the pokemon
19
     * @param  float $trainer the level of the the trainer
20
     * @return int   dusts to max the pokemon
21
     */
22 View Code Duplication
    public static function dustsToMax(float $pokemon, float $trainer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
23
    {
24
        $trainer += 1.5;
25
        $loop = $pokemon;
26
        $dusts = 0;
27
28
        if ($pokemon >= $trainer) {
29
            return 0;
30
        }
31
32
        if ($trainer > 40) {
33
            $trainer = 40;
34
        }
35
36
        $levels = (new LevelExtractor())
37
            ->getIntervalLevelFiltered($pokemon, $trainer);
38
39
        $levels
40
            ->each(function ($level) use (&$dusts, &$loop, $trainer) {
41
                $loop += 0.5;
42
                $dusts += $level->dust * 2;
43
            });
44
45
        if ($pokemon*2 % 2 === 1) {
46
            $dusts -= $levels->first()->dust;
47
        }
48
49
        $dusts -= $levels->last()->dust;
50
51
        return $dusts;
52
    }
53
54
    /**
55
     * Method to calculate how many candies are necessary to max a pokemon
56
     * @param  float $pokemon the level of the pokemon
57
     * @param  float $trainer the level of the the trainer
58
     * @return int   dusts to max the pokemon
59
     */
60 View Code Duplication
    public static function candiesToMax(float $pokemon, float $trainer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        $trainer += 1.5;
63
        $loop = $pokemon;
64
        $candies = 0;
65
66
        if ($pokemon >= $trainer) {
67
            return 0;
68
        }
69
70
        if ($trainer > 40) {
71
            $trainer = 40;
72
        }
73
74
        $levels = (new LevelExtractor())
75
            ->getIntervalLevelFiltered($pokemon, $trainer);
76
77
        $levels
78
            ->each(function ($level) use (&$candies, &$loop, $trainer) {
79
                $loop += 0.5;
80
                $candies += $level->candy * 2;
81
            });
82
83
        if ($pokemon*2 % 2 === 1) {
84
            $candies -= $levels->first()->candy;
85
        }
86
87
        $candies -= $levels->last()->candy;
88
89
        return $candies;
90
    }
91
92
    /**
93
     * Method to calculate the CP with stats
94
     * @param  int  $attack
95
     * @param  int  $defense
96
     * @param  int  $stamina
97
     * @param  int  $level
98
     * @param  bool $upgraded
99
     * @return int
100
     */
101
    public static function calculateCP(int $attack, int $defense, int $stamina, int $level, bool $upgraded = false)
102
    {
103
        $attackFactor = $attack;
104
        $defenseFactor = pow($defense, 0.5);
105
        $staminaFactor = pow($stamina, 0.5);
106
107
        $datas = (new LevelExtractor())->getExactLevel($level);
108
        $level = new Level(
109
            $datas->level, $datas->dust, $datas->cpScalar, $upgraded
110
        );
111
112
        $scalarFactor = pow($level->getCpScalar(), 2);
113
114
        return floor($attackFactor * $defenseFactor * $staminaFactor * $scalarFactor / 10);
115
    }
116
}
117