Completed
Branch develop (598d0f)
by Benjamin
03:23
created

AbstractPlayerRuleTrait::getContextForRoll()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 14
c 1
b 0
f 0
nc 18
nop 1
dl 0
loc 22
rs 8.0555
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\Helper\CoreTranslation;
11
use Obblm\Core\Helper\Rule\Roster\Roster;
12
13
/*****************
14
 * PLAYER METHODS
15
 ****************/
16
trait AbstractPlayerRuleTrait
17
{
18
    /**
19
     * @param PlayerVersion $playerVersion
20
     * @return bool
21
     */
22
    public function playerIsDisposable(PlayerVersion $playerVersion):bool
23
    {
24
        return in_array('disposable', $playerVersion->getSkills());
25
    }
26
27
    /**
28
     * @param string $roster_key
29
     * @return array
30
     */
31
    public function getAvailablePlayerTypes(string $roster_key):array
32
    {
33
        /** @var Roster $roster */
34
        $roster = $this->getRosters()->get($roster_key);
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

34
        $roster = $this->/** @scrutinizer ignore-call */ getRosters()->get($roster_key);
Loading history...
35
        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...
36
    }
37
38
    /**
39
     * @param string $roster
40
     * @return array
41
     */
42
    public function getAvailablePlayerKeyTypes(string $roster):array
43
    {
44
        return array_keys($this->getAvailablePlayerTypes($roster));
45
    }
46
47
    /**
48
     * @param PlayerVersion $version
49
     * @return PlayerVersion|null
50
     */
51
    public function setPlayerDefaultValues(PlayerVersion $version): ?PlayerVersion
52
    {
53
        /**
54
         * -characteristics: []
55
         * -skills: []
56
         * -spp_level: null
57
         * -value: null
58
         */
59
        list($rule_key, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $version->getPlayer()->getType());
60
        $types = $this->getAvailablePlayerTypes($roster);
61
        $base = $types[$type];
62
        $characteristics = $base['characteristics'];
63
        $version->setCharacteristics([
64
            'ma' => $characteristics['ma'],
65
            'st' => $characteristics['st'],
66
            'ag' => $characteristics['ag'],
67
            'av' => $characteristics['av']
68
        ])
69
            ->setActions([
70
                'td' => 0,
71
                'cas' => 0,
72
                'pas' => 0,
73
                'int' => 0,
74
                'mvp' => 0,
75
            ])
76
            ->setSkills(($base['skills'] ?? []))
77
            ->setValue($base['cost'])
78
            ->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 $spp_level 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

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

90
        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...
91
            throw new \Exception('No Injury found for ' . $key);
92
        }
93
        return $this->getInjuries()->get($key);
94
    }
95
96
    /**************************
97
     * PLAYER EVOLUTION METHOD
98
     *************************/
99
100
    /**
101
     * @param PlayerVersion $version
102
     * @return string|null
103
     */
104
    public function getSppLevel(PlayerVersion $version):?string
105
    {
106
        if ($version->getSpp() && $version->getSpp() > 0) {
107
            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

107
            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...
108
                return $this->getSppLevels()->get($version->getSpp());
109
            }
110
            return $this->getSppLevels()->last();
111
        }
112
113
        return $this->getSppLevels()->first();
114
    }
115
116
    public function getContextForRoll(array $roll):?array
117
    {
118
        $context = null;
119
        if (isset($roll['d6_1']) && isset($roll['d6_2'])) {
120
            $context = ['single'];
121
            if ($roll['d6_1'] && $roll['d6_2']) {
122
                if ($roll['d6_1'] === $roll['d6_2']) {
123
                    $context[] = 'double';
124
                }
125
                if (($roll['d6_1'] + $roll['d6_2']) == 10) {
126
                    $context[] = 'm_up';
127
                    $context[] = 'av_up';
128
                }
129
                if (($roll['d6_1'] + $roll['d6_2']) == 11) {
130
                    $context[] = 'ag_up';
131
                }
132
                if (($roll['d6_1'] + $roll['d6_2']) == 12) {
133
                    $context[] = 'st_up';
134
                }
135
            }
136
        }
137
        return $context;
138
    }
139
140
    public function getAvailableSkills(?PlayerVersion $version, $context = ['single', 'double']):?ArrayCollection
141
    {
142
        $criteria = Criteria::create();
143
        if ($version) {
144
            $available_types = $this->getAvailableSkillsFor($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...getAvailableSkillsFor() 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

144
            $available_types = $this->getAvailableSkillsFor(/** @scrutinizer ignore-type */ $version->getPlayer());
Loading history...
145
            $filers = [];
146
            if (in_array('single', $context)) {
147
                $filers[] = Criteria::expr()->in('type', $available_types['single']);
148
            }
149
            if (in_array('double', $context)) {
150
                $filers[] = Criteria::expr()->in('type', $available_types['double']);
151
            }
152
            if (in_array('av_up', $context)) {
153
                $filers[] = Criteria::expr()->eq('key', 'c.armor_increase');
154
            }
155
            if (in_array('m_up', $context)) {
156
                $filers[] = Criteria::expr()->eq('key', 'c.move_increase');
157
            }
158
            if (in_array('ag_up', $context)) {
159
                $filers[] = Criteria::expr()->eq('key', 'c.agility_increase');
160
            }
161
            if (in_array('st_up', $context)) {
162
                $filers[] = Criteria::expr()->eq('key', 'c.strength_increase');
163
            }
164
            if (count($filers) > 0) {
165
                $composite = new CompositeExpression(CompositeExpression::TYPE_OR, $filers);
166
                $criteria->where(Criteria::expr()->orX($composite));
167
            }
168
        }
169
        $criteria->orderBy(['key' => 'asc']);
170
171
        return $this->getSkills()->matching($criteria);
0 ignored issues
show
Bug introduced by
It seems like getSkills() 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

171
        return $this->/** @scrutinizer ignore-call */ getSkills()->matching($criteria);
Loading history...
172
    }
173
174
    private function getAvailableSkillsFor(Player $player):array
175
    {
176
        list($rule_key, $roster, $type) = explode(CoreTranslation::TRANSLATION_GLUE, $player->getType());
177
        return [
178
            'single' => $this->rule['rosters'][$roster]['players'][$type]['available_skills'],
179
            'double' => $this->rule['rosters'][$roster]['players'][$type]['available_skills_on_double']
180
        ];
181
    }
182
}
183