Completed
Branch v1.x-dev (59bcf7)
by Benjamin
05:08
created

AbstractPlayerRuleTrait   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 4.41%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
eloc 65
c 1
b 0
f 0
dl 0
loc 155
ccs 3
cts 68
cp 0.0441
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailablePlayerTypes() 0 5 1
B getContextForRoll() 0 22 9
A getAvailablePlayerKeyTypes() 0 3 1
A getAvailableSkillsFor() 0 6 1
A setPlayerDefaultValues() 0 15 1
A playerIsDisposable() 0 3 1
A getInjury() 0 6 2
B getAvailableSkills() 0 35 10
A getSppLevel() 0 10 4
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\Entity\Player;
10
use Obblm\Core\Entity\PlayerVersion;
11
use Obblm\Core\Exception\InvalidArgumentException;
12
use Obblm\Core\Exception\NotFoundKeyException;
13
use Obblm\Core\Helper\CoreTranslation;
14
use Obblm\Core\Helper\Rule\Roster\Roster;
15
16
/*****************
17
 * PLAYER METHODS
18
 ****************/
19
trait AbstractPlayerRuleTrait
20
{
21
    /**
22
     * @param PlayerVersion $playerVersion
23
     * @return bool
24
     */
25
    public function playerIsDisposable(PlayerVersion $playerVersion):bool
26
    {
27
        return in_array('disposable', $playerVersion->getSkills());
28
    }
29
30
    /**
31
     * @param string $rosterKey
32
     * @return array
33
     */
34 1
    public function getAvailablePlayerTypes(string $rosterKey):array
35
    {
36
        /** @var Roster $roster */
37 1
        $roster = $this->getRosters()->get($rosterKey);
0 ignored issues
show
Bug introduced by
It seems like getRosters() 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

37
        $roster = $this->/** @scrutinizer ignore-call */ getRosters()->get($rosterKey);
Loading history...
38 1
        return $roster->getPositions();
39
    }
40
41
    /**
42
     * @param string $roster
43
     * @return array
44
     */
45
    public function getAvailablePlayerKeyTypes(string $roster):array
46
    {
47
        return array_keys($this->getAvailablePlayerTypes($roster));
48
    }
49
50
    /**
51
     * @param PlayerVersion $version
52
     * @param PositionInterface $position
53
     * @return PlayerVersion|null
54
     */
55
    public function setPlayerDefaultValues(PlayerVersion $version, PositionInterface $position): ?PlayerVersion
56
    {
57
        $version->setCharacteristics($position->getCharacteristics())
58
            ->setActions([
59
                'td' => 0,
60
                'cas' => 0,
61
                'pas' => 0,
62
                'int' => 0,
63
                'mvp' => 0,
64
            ])
65
            ->setSkills($position->getSkills())
66
            ->setValue($position->getCost())
67
            ->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

67
            ->setSppLevel(/** @scrutinizer ignore-type */ $this->getSppLevel($version));
Loading history...
68
69
        return $version;
70
    }
71
72
    /**
73
     * @param $key
74
     * @return object|null
75
     * @throws \Exception
76
     */
77
    public function getInjury($key):?object
78
    {
79
        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

79
        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...
80
            throw new NotFoundKeyException($key, 'getInjuries', self::class);
81
        }
82
        return $this->getInjuries()->get($key);
83
    }
84
85
    /**************************
86
     * PLAYER EVOLUTION METHOD
87
     *************************/
88
89
    /**
90
     * @param PlayerVersion $version
91
     * @return string|null
92
     */
93
    public function getSppLevel(PlayerVersion $version):?string
94
    {
95
        if ($version->getSpp() && $version->getSpp() > 0) {
96
            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

96
            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...
97
                return $this->getSppLevels()->get($version->getSpp());
98
            }
99
            return $this->getSppLevels()->last();
100
        }
101
102
        return $this->getSppLevels()->first();
103
    }
104
105
    public function getContextForRoll(array $roll):?array
106
    {
107
        $context = null;
108
        if (isset($roll['d6_1']) && isset($roll['d6_2'])) {
109
            $context = ['single'];
110
            if ($roll['d6_1'] && $roll['d6_2']) {
111
                if ($roll['d6_1'] === $roll['d6_2']) {
112
                    $context[] = 'double';
113
                }
114
                if (($roll['d6_1'] + $roll['d6_2']) == 10) {
115
                    $context[] = 'm_up';
116
                    $context[] = 'av_up';
117
                }
118
                if (($roll['d6_1'] + $roll['d6_2']) == 11) {
119
                    $context[] = 'ag_up';
120
                }
121
                if (($roll['d6_1'] + $roll['d6_2']) == 12) {
122
                    $context[] = 'st_up';
123
                }
124
            }
125
        }
126
        return $context;
127
    }
128
129
    public function getAvailableSkills(?PlayerVersion $version, $context = ['single', 'double']):?ArrayCollection
130
    {
131
        $criteria = Criteria::create();
132
        if ($version) {
133
            if (!$version->getPlayer()) {
134
                throw new InvalidArgumentException();
135
            }
136
            $availableTypes = $this->getAvailableSkillsFor($version->getPlayer());
137
            $filers = [];
138
            if (in_array('single', $context)) {
139
                $filers[] = Criteria::expr()->in('type', $availableTypes['single']);
140
            }
141
            if (in_array('double', $context)) {
142
                $filers[] = Criteria::expr()->in('type', $availableTypes['double']);
143
            }
144
            if (in_array('av_up', $context)) {
145
                $filers[] = Criteria::expr()->eq('key', 'c.armor_increase');
146
            }
147
            if (in_array('m_up', $context)) {
148
                $filers[] = Criteria::expr()->eq('key', 'c.move_increase');
149
            }
150
            if (in_array('ag_up', $context)) {
151
                $filers[] = Criteria::expr()->eq('key', 'c.agility_increase');
152
            }
153
            if (in_array('st_up', $context)) {
154
                $filers[] = Criteria::expr()->eq('key', 'c.strength_increase');
155
            }
156
            if (count($filers) > 0) {
157
                $composite = new CompositeExpression(CompositeExpression::TYPE_OR, $filers);
158
                $criteria->where(Criteria::expr()->orX($composite));
159
            }
160
        }
161
        $criteria->orderBy(['key' => 'asc']);
162
163
        return $this->getSkills()->matching($criteria);
164
    }
165
166
    abstract public function getSkills():ArrayCollection;
167
168
    private function getAvailableSkillsFor(Player $player):array
169
    {
170
        list($ruleKey, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $player->getPosition());
171
        return [
172
            'single' => $this->rule['rosters'][$roster]['players'][$type]['available_skills'],
173
            'double' => $this->rule['rosters'][$roster]['players'][$type]['available_skills_on_double']
174
        ];
175
    }
176
}
177