Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

AbstractPlayerRuleTrait::playerIsDisposable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Traits;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\Common\Collections\Expr\CompositeExpression;
8
use Obblm\Core\Contracts\PositionInterface;
9
use Obblm\Core\Contracts\RosterInterface;
10
use Obblm\Core\DataTransformer\StringToSkill;
11
use Obblm\Core\Entity\Player;
12
use Obblm\Core\Entity\PlayerVersion;
13
use Obblm\Core\Entity\Team;
14
use Obblm\Core\Exception\InvalidArgumentException;
15
use Obblm\Core\Exception\NotFoundKeyException;
16
use Obblm\Core\Helper\Rule\Inducement\StarPlayer;
17
use Obblm\Core\Helper\Rule\Roster\Roster;
18
use Obblm\Core\Validator\Constraints\Team\AdditionalSkills;
19
20
/*****************
21
 * PLAYER METHODS
22
 ****************/
23
trait AbstractPlayerRuleTrait
24
{
25
    abstract public function getRoster(Team $team):RosterInterface;
26
    /**
27
     * @param PlayerVersion $playerVersion
28
     * @return bool
29
     */
30
    public function playerIsDisposable(PlayerVersion $playerVersion):bool
31
    {
32
        return in_array('disposable', $playerVersion->getSkills());
33
    }
34
35
    public function getPlayerPosition(Player $player):PositionInterface
36
    {
37
        try {
38
            return $this->getRoster($player->getTeam())->getPosition($player->getPosition());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRoster(...$player->getPosition()) could return the type null which is incompatible with the type-hinted return Obblm\Core\Contracts\PositionInterface. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $player->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...rRuleTrait::getRoster() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            return $this->getRoster(/** @scrutinizer ignore-type */ $player->getTeam())->getPosition($player->getPosition());
Loading history...
39
        } catch (NotFoundKeyException $e) {
40
            return $this->getStarPlayer($player->getName());
0 ignored issues
show
Bug introduced by
It seems like getStarPlayer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            return $this->/** @scrutinizer ignore-call */ getStarPlayer($player->getName());
Loading history...
41
        }
42
    }
43
44
    /**
45
     * @param string $rosterKey
46
     * @return array
47
     */
48
    public function getAvailablePlayerForTeamCreation(Team $team)
49
    {
50
        /** @var Roster $roster */
51
        $positions = $this->getRoster($team)->getPositions();
52
        $options = $team->getCreationOptions();
53
        if (isset($options['star_players_allowed']) && $options['star_players_allowed']) {
54
            $starPlayers = $this->getAvailableStarPlayers($team);
0 ignored issues
show
Bug introduced by
The method getAvailableStarPlayers() does not exist on Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait. Did you maybe mean getAvailableSkills()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            /** @scrutinizer ignore-call */ 
55
            $starPlayers = $this->getAvailableStarPlayers($team);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            foreach ($starPlayers as $starPlayer) {
56
                if ($starPlayer instanceof StarPlayer) {
57
                    $positions[$starPlayer->getKey()] = $starPlayer;
58
                }
59
            }
60
        }
61
        return $positions;
62
    }
63
64
    /**
65
     * @param string $roster
66
     * @return array
67
     */
68
    public function getAvailablePlayerKeyTypes(string $roster):array
69
    {
70
        return array_keys($this->getRosters()->get($roster)->getPositions());
0 ignored issues
show
Bug introduced by
The method getRosters() does not exist on Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait. Did you maybe mean getRoster()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return array_keys($this->/** @scrutinizer ignore-call */ getRosters()->get($roster)->getPositions());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74
     * @param PlayerVersion $version
75
     * @param PositionInterface $position
76
     * @return PlayerVersion|null
77
     */
78
    public function setPlayerDefaultValues(PlayerVersion $version, PositionInterface $position): ?PlayerVersion
79
    {
80
        $version->setCharacteristics($position->getCharacteristics())
81
            ->setActions([
82
                'td' => 0,
83
                'cas' => 0,
84
                'pas' => 0,
85
                'int' => 0,
86
                'mvp' => 0,
87
            ])
88
            ->setSkills($position->getSkills())
89
            ->setValue($position->getCost())
90
            ->setSppLevel($this->getSppLevel($version));
0 ignored issues
show
Bug introduced by
It seems like $this->getSppLevel($version) can also be of type null; however, parameter $sppLevel of Obblm\Core\Entity\PlayerVersion::setSppLevel() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
            ->setSppLevel(/** @scrutinizer ignore-type */ $this->getSppLevel($version));
Loading history...
91
92
        return $version;
93
    }
94
95
    /**
96
     * @param $key
97
     * @return object|null
98
     * @throws \Exception
99
     */
100
    public function getInjury($key):?object
101
    {
102
        if (!$this->getInjuries()->containsKey($key)) {
0 ignored issues
show
Bug introduced by
The method getInjuries() does not exist on Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait. Did you maybe mean getInjury()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        if (!$this->/** @scrutinizer ignore-call */ getInjuries()->containsKey($key)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            throw new NotFoundKeyException($key, 'getInjuries', self::class);
104
        }
105
        return $this->getInjuries()->get($key);
106
    }
107
108
    /**************************
109
     * PLAYER EVOLUTION METHOD
110
     *************************/
111
112
    /**
113
     * @param PlayerVersion $version
114
     * @return string|null
115
     */
116
    public function getSppLevel(PlayerVersion $version):?string
117
    {
118
        if ($version->getSpp() && $version->getSpp() > 0) {
119
            if ($this->getSppLevels()->containsKey($version->getSpp())) {
0 ignored issues
show
Bug introduced by
The method getSppLevels() does not exist on Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait. Did you maybe mean getSppLevel()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
            if ($this->/** @scrutinizer ignore-call */ getSppLevels()->containsKey($version->getSpp())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
                return $this->getSppLevels()->get($version->getSpp());
121
            }
122
            return $this->getSppLevels()->last();
123
        }
124
125
        return $this->getSppLevels()->first();
126
    }
127
128
    public function getContextForRoll(array $roll):?array
129
    {
130
        $context = null;
131
        if (isset($roll['d6_1']) && isset($roll['d6_2'])) {
132
            $context = ['single'];
133
            if ($roll['d6_1'] && $roll['d6_2']) {
134
                if ($roll['d6_1'] === $roll['d6_2']) {
135
                    $context[] = 'double';
136
                }
137
                if (($roll['d6_1'] + $roll['d6_2']) == 10) {
138
                    $context[] = 'm_up';
139
                    $context[] = 'av_up';
140
                }
141
                if (($roll['d6_1'] + $roll['d6_2']) == 11) {
142
                    $context[] = 'ag_up';
143
                }
144
                if (($roll['d6_1'] + $roll['d6_2']) == 12) {
145
                    $context[] = 'st_up';
146
                }
147
            }
148
        }
149
        return $context;
150
    }
151
152
    public function getAvailableSkills(?PlayerVersion $version, $context = ['single', 'double']):?ArrayCollection
153
    {
154
        $criteria = Criteria::create();
155
        if ($version) {
156
            if (!$version->getPlayer()) {
157
                throw new InvalidArgumentException();
158
            }
159
            $availableTypes = $this->getAvailableSkillsFor($version->getPlayer());
160
            $filers = [];
161
            if (in_array('single', $context)) {
162
                $filers[] = Criteria::expr()->in('type', $availableTypes['single']);
163
            }
164
            if (in_array('double', $context)) {
165
                $filers[] = Criteria::expr()->in('type', $availableTypes['double']);
166
            }
167
            if (in_array('av_up', $context)) {
168
                $filers[] = Criteria::expr()->eq('key', 'armor_increase');
169
            }
170
            if (in_array('m_up', $context)) {
171
                $filers[] = Criteria::expr()->eq('key', 'move_increase');
172
            }
173
            if (in_array('ag_up', $context)) {
174
                $filers[] = Criteria::expr()->eq('key', 'agility_increase');
175
            }
176
            if (in_array('st_up', $context)) {
177
                $filers[] = Criteria::expr()->eq('key', 'strength_increase');
178
            }
179
            if (count($filers) > 0) {
180
                $composite = new CompositeExpression(CompositeExpression::TYPE_OR, $filers);
181
                $criteria->where(Criteria::expr()->orX($composite));
182
            }
183
        }
184
        $criteria->orderBy(['key' => 'asc']);
185
186
        return $this->getSkills()->matching($criteria);
187
    }
188
189
    public function getPlayerVersionExtraCosts(PlayerVersion $version):int
190
    {
191
        $extraCost = 0;
192
        $team = $version->getPlayer()->getTeam();
193
        if (
194
            !$team->getCreationOption('skills_allowed') ||
195
            $team->getCreationOption('skills_allowed') && $team->getCreationOption('skills_allowed.choice') == AdditionalSkills::NOT_FREE
196
        ) {
197
            foreach ($version->getAdditionalSkills() as $skill) {
198
                $extraCost += $this->getSkillCostForPlayerVersion($version, $skill);
199
            }
200
        }
201
        return $extraCost;
202
    }
203
204
    private function getSkillCostForPlayerVersion(PlayerVersion $version, $skill):int
205
    {
206
        if (!$skill) {
207
            return 0;
208
        }
209
210
        $context = $this->getSkillContextForPlayerVersion($version, $skill);
211
212
        $rule = $this->getAttachedRule()->getRule();
0 ignored issues
show
Bug introduced by
It seems like getAttachedRule() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
        $rule = $this->/** @scrutinizer ignore-call */ getAttachedRule()->getRule();
Loading history...
213
214
        if (is_int($rule['experience_value_modifiers'][$context])) {
215
            return $rule['experience_value_modifiers'][$context];
216
        }
217
218
        throw new NotFoundKeyException($context, 'experience_value_modifiers', $this);
0 ignored issues
show
Bug introduced by
$this of type Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait is incompatible with the type string expected by parameter $type of Obblm\Core\Exception\Not...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
        throw new NotFoundKeyException($context, 'experience_value_modifiers', /** @scrutinizer ignore-type */ $this);
Loading history...
219
    }
220
221
    public function getSkillContextForPlayerVersion(PlayerVersion $version, $skill):string
222
    {
223
        if (is_string($skill)) {
224
            $skill = (new StringToSkill($this))->transform($skill);
0 ignored issues
show
Bug introduced by
$this of type Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait is incompatible with the type Obblm\Core\Contracts\RuleHelperInterface expected by parameter $helper of Obblm\Core\DataTransform...gToSkill::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

224
            $skill = (new StringToSkill(/** @scrutinizer ignore-type */ $this))->transform($skill);
Loading history...
225
        }
226
        $position = $this->getPlayerPosition($version->getPlayer());
0 ignored issues
show
Bug introduced by
It seems like $version->getPlayer() can also be of type null; however, parameter $player of Obblm\Core\Helper\Rule\T...it::getPlayerPosition() does only seem to accept Obblm\Core\Entity\Player, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        $position = $this->getPlayerPosition(/** @scrutinizer ignore-type */ $version->getPlayer());
Loading history...
227
228
        if ($skill->getType() == 'c') {
229
            return 'characteristics';
230
        }
231
        if (in_array($skill->getType(), $position->getOption('available_skills_on_double'))) {
232
            return 'double';
233
        }
234
        if (in_array($skill->getType(), $position->getOption('available_skills'))) {
235
            return 'single';
236
        }
237
238
        throw new NotFoundKeyException($skill->getType(), 'skill_types', $this);
0 ignored issues
show
Bug introduced by
$this of type Obblm\Core\Helper\Rule\T...AbstractPlayerRuleTrait is incompatible with the type string expected by parameter $type of Obblm\Core\Exception\Not...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

238
        throw new NotFoundKeyException($skill->getType(), 'skill_types', /** @scrutinizer ignore-type */ $this);
Loading history...
239
    }
240
241
    abstract public function getSkills():ArrayCollection;
242
243
    private function getAvailableSkillsFor(Player $player):array
244
    {
245
        $position = $this->getRoster($player->getTeam())->getPosition($player->getPosition());
0 ignored issues
show
Bug introduced by
It seems like $player->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...rRuleTrait::getRoster() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
        $position = $this->getRoster(/** @scrutinizer ignore-type */ $player->getTeam())->getPosition($player->getPosition());
Loading history...
246
        return [
247
            'single' => $position->getOption('available_skills'),
248
            'double' => $position->getOption('available_skills_on_double')
249
        ];
250
    }
251
}
252