Passed
Push — master ( 32373f...f18c17 )
by Benjamin
05:50 queued 01:43
created

AbstractPlayerRuleTrait   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 31.58%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
eloc 73
c 1
b 0
f 0
dl 0
loc 169
ccs 24
cts 76
cp 0.3158
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 getInjury() 0 6 2
B getAvailableSkills() 0 35 10
A setPlayerDefaultValues() 0 30 1
A playerIsDisposable() 0 3 1
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\Entity\Player;
9
use Obblm\Core\Entity\PlayerVersion;
10
use Obblm\Core\Exception\InvalidArgumentException;
11
use Obblm\Core\Exception\NotFoundKeyException;
12
use Obblm\Core\Helper\CoreTranslation;
13
use Obblm\Core\Helper\Rule\Roster\Roster;
14
15
/*****************
16
 * PLAYER METHODS
17
 ****************/
18
trait AbstractPlayerRuleTrait
19
{
20
    /**
21
     * @param PlayerVersion $playerVersion
22
     * @return bool
23
     */
24 1
    public function playerIsDisposable(PlayerVersion $playerVersion):bool
25
    {
26 1
        return in_array('disposable', $playerVersion->getSkills());
27
    }
28
29
    /**
30
     * @param string $rosterKey
31
     * @return array
32
     */
33 1
    public function getAvailablePlayerTypes(string $rosterKey):array
34
    {
35
        /** @var Roster $roster */
36 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

36
        $roster = $this->/** @scrutinizer ignore-call */ getRosters()->get($rosterKey);
Loading history...
37 1
        return $roster->getPlayerTypes();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $roster->getPlayerTypes() could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
40
    /**
41
     * @param string $roster
42
     * @return array
43
     */
44
    public function getAvailablePlayerKeyTypes(string $roster):array
45
    {
46
        return array_keys($this->getAvailablePlayerTypes($roster));
47
    }
48
49
    /**
50
     * @param PlayerVersion $version
51
     * @return PlayerVersion|null
52
     */
53 1
    public function setPlayerDefaultValues(PlayerVersion $version): ?PlayerVersion
54
    {
55
        /**
56
         * -characteristics: []
57
         * -skills: []
58
         * -spp_level: null
59
         * -value: null
60
         */
61 1
        list($ruleKey, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $version->getPlayer()->getType());
62 1
        $types = $this->getAvailablePlayerTypes($roster);
63 1
        $base = $types[$type];
64 1
        $characteristics = $base['characteristics'];
65 1
        $version->setCharacteristics([
66 1
            'ma' => $characteristics['ma'],
67 1
            'st' => $characteristics['st'],
68 1
            'ag' => $characteristics['ag'],
69 1
            'av' => $characteristics['av']
70
        ])
71 1
            ->setActions([
72 1
                'td' => 0,
73
                'cas' => 0,
74
                'pas' => 0,
75
                'int' => 0,
76
                'mvp' => 0,
77
            ])
78 1
            ->setSkills(($base['skills'] ?? []))
79 1
            ->setValue($base['cost'])
80 1
            ->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

80
            ->setSppLevel(/** @scrutinizer ignore-type */ $this->getSppLevel($version));
Loading history...
81
82 1
        return $version;
83
    }
84
85
    /**
86
     * @param $key
87
     * @return object|null
88
     * @throws \Exception
89
     */
90
    public function getInjury($key):?object
91
    {
92
        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

92
        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...
93
            throw new NotFoundKeyException($key, 'getInjuries', self::class);
94
        }
95
        return $this->getInjuries()->get($key);
96
    }
97
98
    /**************************
99
     * PLAYER EVOLUTION METHOD
100
     *************************/
101
102
    /**
103
     * @param PlayerVersion $version
104
     * @return string|null
105
     */
106 1
    public function getSppLevel(PlayerVersion $version):?string
107
    {
108 1
        if ($version->getSpp() && $version->getSpp() > 0) {
109
            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

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