Completed
Push — master ( c1ba47...3b776f )
by Benjamin
10:05 queued 04:46
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\Exception\InvalidArgumentException;
11
use Obblm\Core\Helper\CoreTranslation;
12
use Obblm\Core\Helper\Rule\Roster\Roster;
13
14
/*****************
15
 * PLAYER METHODS
16
 ****************/
17
trait AbstractPlayerRuleTrait
18
{
19
    /**
20
     * @param PlayerVersion $playerVersion
21
     * @return bool
22
     */
23
    public function playerIsDisposable(PlayerVersion $playerVersion):bool
24
    {
25
        return in_array('disposable', $playerVersion->getSkills());
26
    }
27
28
    /**
29
     * @param string $rosterKey
30
     * @return array
31
     */
32
    public function getAvailablePlayerTypes(string $rosterKey):array
33
    {
34
        /** @var Roster $roster */
35
        $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

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

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

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

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