Completed
Push — master ( 01ad84...898da4 )
by Vincenzo
02:22
created
api/Lib/DsManager/Models/MatchResult.php 3 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -31,8 +31,8 @@
 block discarded – undo
31 31
 
32 32
     /**
33 33
      * MatchResult constructor.
34
-     * @param $goalHome
35
-     * @param $goalAway
34
+     * @param integer $goalHome
35
+     * @param integer $goalAway
36 36
      * @param Team $home
37 37
      * @param Team $away
38 38
      */
Please login to merge, or discard this patch.
Unused Use Statements   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@
 block discarded – undo
2 2
 
3 3
 namespace App\Lib\DsManager\Models;
4 4
 
5
-use App\Lib\DsManager\Helpers\Randomizer;
5
+use App\Lib\DsManager\Helpers\Randomizer;
6 6
 use App\Lib\Helpers\Config;
7 7
 
8 8
 
Please login to merge, or discard this patch.
Indentation   +128 added lines, -128 removed lines patch added patch discarded remove patch
@@ -12,141 +12,141 @@
 block discarded – undo
12 12
  */
13 13
 class MatchResult
14 14
 {
15
-    /**
16
-     * @var
17
-     */
18
-    private $goalHome;
19
-    /**
20
-     * @var
21
-     */
22
-    private $goalAway;
23
-    /**
24
-     * @var Team
25
-     */
26
-    private $homeTeam;
27
-    /**
28
-     * @var Team
29
-     */
30
-    private $awayTeam;
15
+	/**
16
+	 * @var
17
+	 */
18
+	private $goalHome;
19
+	/**
20
+	 * @var
21
+	 */
22
+	private $goalAway;
23
+	/**
24
+	 * @var Team
25
+	 */
26
+	private $homeTeam;
27
+	/**
28
+	 * @var Team
29
+	 */
30
+	private $awayTeam;
31 31
 
32
-    /**
33
-     * MatchResult constructor.
34
-     * @param $goalHome
35
-     * @param $goalAway
36
-     * @param Team $home
37
-     * @param Team $away
38
-     */
39
-    public function __construct($goalHome, $goalAway, Team $home, Team $away)
40
-    {
41
-        $this->goalHome = $goalHome;
42
-        $this->goalAway = $goalAway;
43
-        $this->homeTeam = $home;
44
-        $this->awayTeam = $away;
45
-    }
32
+	/**
33
+	 * MatchResult constructor.
34
+	 * @param $goalHome
35
+	 * @param $goalAway
36
+	 * @param Team $home
37
+	 * @param Team $away
38
+	 */
39
+	public function __construct($goalHome, $goalAway, Team $home, Team $away)
40
+	{
41
+		$this->goalHome = $goalHome;
42
+		$this->goalAway = $goalAway;
43
+		$this->homeTeam = $home;
44
+		$this->awayTeam = $away;
45
+	}
46 46
 
47
-    /**
48
-     * @return array
49
-     */
50
-    public function getWinnerLoser()
51
-    {
52
-        $isDraw = false;
53
-        $winner = null;
54
-        $loser = null;
55
-        if ($this->goalAway == $this->goalHome) {
56
-            $isDraw = true;
57
-            $winner = $this->homeTeam;
58
-            $loser = $this->awayTeam;
59
-        } else if ($this->goalHome > $this->goalAway) {
60
-            $winner = $this->homeTeam;
61
-            $loser = $this->awayTeam;
62
-        } else {
63
-            $winner = $this->awayTeam;
64
-            $loser = $this->homeTeam;
65
-        }
47
+	/**
48
+	 * @return array
49
+	 */
50
+	public function getWinnerLoser()
51
+	{
52
+		$isDraw = false;
53
+		$winner = null;
54
+		$loser = null;
55
+		if ($this->goalAway == $this->goalHome) {
56
+			$isDraw = true;
57
+			$winner = $this->homeTeam;
58
+			$loser = $this->awayTeam;
59
+		} else if ($this->goalHome > $this->goalAway) {
60
+			$winner = $this->homeTeam;
61
+			$loser = $this->awayTeam;
62
+		} else {
63
+			$winner = $this->awayTeam;
64
+			$loser = $this->homeTeam;
65
+		}
66 66
 
67
-        return [
68
-            'isDraw' => $isDraw,
69
-            'winner' => $this->cleanTeam($winner->toArray()),
70
-            'loser' => $this->cleanTeam($loser->toArray())
71
-        ];
72
-    }
67
+		return [
68
+			'isDraw' => $isDraw,
69
+			'winner' => $this->cleanTeam($winner->toArray()),
70
+			'loser' => $this->cleanTeam($loser->toArray())
71
+		];
72
+	}
73 73
 
74
-    /**
75
-     * @return array
76
-     */
77
-    public function toArray()
78
-    {
79
-        $result = [];
80
-        $result["goalHome"] = $this->goalHome;
81
-        $result["goalAway"] = $this->goalAway;
82
-        $result['info'] = $this->getWinnerLoser();
83
-        $result['info']['scorers'] = $this->getScorers();
84
-        return $result;
85
-    }
74
+	/**
75
+	 * @return array
76
+	 */
77
+	public function toArray()
78
+	{
79
+		$result = [];
80
+		$result["goalHome"] = $this->goalHome;
81
+		$result["goalAway"] = $this->goalAway;
82
+		$result['info'] = $this->getWinnerLoser();
83
+		$result['info']['scorers'] = $this->getScorers();
84
+		return $result;
85
+	}
86 86
 
87
-    /**
88
-     * @param array $team
89
-     * @return array
90
-     */
91
-    private function cleanTeam(array $team)
92
-    {
93
-        $fieldsToClean = [
94
-            'coach',
95
-            'roster',
96
-        ];
87
+	/**
88
+	 * @param array $team
89
+	 * @return array
90
+	 */
91
+	private function cleanTeam(array $team)
92
+	{
93
+		$fieldsToClean = [
94
+			'coach',
95
+			'roster',
96
+		];
97 97
 
98
-        foreach ($fieldsToClean as $field) {
99
-            if (array_key_exists($field, $team)) {
100
-                unset($team[$field]);
101
-            }
102
-        }
103
-        return $team;
104
-    }
98
+		foreach ($fieldsToClean as $field) {
99
+			if (array_key_exists($field, $team)) {
100
+				unset($team[$field]);
101
+			}
102
+		}
103
+		return $team;
104
+	}
105 105
 
106
-    /**
107
-     * @return array
108
-     */
109
-    private function getScorers()
110
-    {
111
-        $scorers = [
112
-            'home' => [],
113
-            'away' => []
114
-        ];
115
-        for ($i = 0; $i < $this->goalHome; $i++) {
116
-            $scorers['home'][] = $this->pickAScorer($this->homeTeam);
117
-        }
118
-        for ($i = 0; $i < $this->goalAway; $i++) {
119
-            $scorers['away'][] = $this->pickAScorer($this->awayTeam);
120
-        }
121
-        return $scorers;
122
-    }
106
+	/**
107
+	 * @return array
108
+	 */
109
+	private function getScorers()
110
+	{
111
+		$scorers = [
112
+			'home' => [],
113
+			'away' => []
114
+		];
115
+		for ($i = 0; $i < $this->goalHome; $i++) {
116
+			$scorers['home'][] = $this->pickAScorer($this->homeTeam);
117
+		}
118
+		for ($i = 0; $i < $this->goalAway; $i++) {
119
+			$scorers['away'][] = $this->pickAScorer($this->awayTeam);
120
+		}
121
+		return $scorers;
122
+	}
123 123
 
124
-    /**
125
-     * @param Team $team
126
-     * @return Player
127
-     */
128
-    private function pickAScorer(Team $team)
129
-    {
130
-        $player = null;
131
-        if (Randomizer::boolOnPercentage(70)) {
132
-            $roles = Config::get('modules.roles');
133
-            $forwards = array_splice($roles, count($roles) / 2);
134
-            $pos = array_rand($forwards);
135
-            unset($forwards[$pos]);
136
-            $player = $team->getBestPlayerForRole($pos);
137
-            while (empty($player)) {
138
-                if (!empty($forwards)) {
139
-                    $pos = array_rand($forwards);
140
-                    unset($forwards[$pos]);
141
-                    $player = $team->getBestPlayerForRole($pos);
142
-                } else {
143
-                    $player = $team->roster[array_rand($team->roster)];
144
-                }
145
-            }
146
-        } else {
147
-            $player = $team->roster[array_rand($team->roster)];
148
-        }
149
-        return $player;
150
-    }
124
+	/**
125
+	 * @param Team $team
126
+	 * @return Player
127
+	 */
128
+	private function pickAScorer(Team $team)
129
+	{
130
+		$player = null;
131
+		if (Randomizer::boolOnPercentage(70)) {
132
+			$roles = Config::get('modules.roles');
133
+			$forwards = array_splice($roles, count($roles) / 2);
134
+			$pos = array_rand($forwards);
135
+			unset($forwards[$pos]);
136
+			$player = $team->getBestPlayerForRole($pos);
137
+			while (empty($player)) {
138
+				if (!empty($forwards)) {
139
+					$pos = array_rand($forwards);
140
+					unset($forwards[$pos]);
141
+					$player = $team->getBestPlayerForRole($pos);
142
+				} else {
143
+					$player = $team->roster[array_rand($team->roster)];
144
+				}
145
+			}
146
+		} else {
147
+			$player = $team->roster[array_rand($team->roster)];
148
+		}
149
+		return $player;
150
+	}
151 151
 
152 152
 }
153 153
\ No newline at end of file
Please login to merge, or discard this patch.
api/tests/ModelsTest.php 1 patch
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -8,222 +8,222 @@
 block discarded – undo
8 8
 {
9 9
 
10 10
 
11
-    /**
12
-     * @group Player
13
-     */
14
-    public function testGetRandomPlayer()
15
-    {
16
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
17
-        $player = $rndF->getPlayer(null, $rndF->getLocale());
18
-        $array = $player->toArray();
19
-        $this->assertNotEmpty($array);
20
-
21
-        $newPlayer = \App\Lib\DsManager\Models\Player::fromArray($array);
22
-        $this->assertNotEmpty($newPlayer->toArray());
23
-    }
24
-
25
-    /**
26
-     * @group Coach
27
-     */
28
-    public function testGetRandomCoach()
29
-    {
30
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
31
-        $coach = $rndF->getCoach();
32
-        $this->assertNotEmpty($coach->toArray());
33
-    }
34
-
35
-    /**
36
-     * @group Coaches
37
-     */
38
-    public function testGetRandomCoaches()
39
-    {
40
-        foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
41
-            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
42
-            $coach = $rndF->getCoach();
43
-            $this->assertNotEmpty($coach->toArray());
44
-        }
45
-    }
46
-
47
-    /**
48
-     * @group Players
49
-     */
50
-    public function testGetRandomPlayers()
51
-    {
52
-        foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
53
-            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
54
-            $player = $rndF->getPlayer();
55
-            $this->assertNotEmpty($player->toArray());
56
-        }
57
-    }
58
-
59
-    /**
60
-     * @group Team
61
-     */
62
-    public function testGetRandomTeam()
63
-    {
64
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
65
-        $team = $rndF->getTeam();
66
-        $this->assertNotEmpty($team);
67
-        $this->assertNotEmpty($team->name);
68
-        $this->assertNotEmpty($team->getAvgSkill());
69
-
70
-        //After Adding a player
71
-        $player = $rndF->getPlayer();
72
-        $this->assertNotEmpty($player->toArray());
73
-        $team->roster[] = $player;
74
-        $this->assertNotEmpty($team->getAvgSkill());
75
-        $this->assertNotEmpty($team->getAvgAge());
76
-
77
-        $this->assertNotEmpty($team->coach->toArray());
78
-
79
-        $teamArray = $team->toArray();
80
-        $this->assertNotEmpty($teamArray);
81
-
82
-        $newTeam = \App\Lib\DsManager\Models\Team::fromArray($teamArray);
83
-        $this->assertNotEmpty($newTeam->toArray());
84
-
85
-    }
86
-
87
-    /**
88
-     * @group Teams
89
-     */
90
-    public function testGetRandomTeams()
91
-    {
92
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
93
-
94
-        for ($i = 1; $i <= 20; $i++) {
95
-            $team = $rndF->getTeam();
96
-            $this->assertNotEmpty($team->name);
97
-            $this->assertNotEmpty($team->nationality);
98
-            $this->assertNotEmpty($team->getAvgSkill());
99
-            $this->assertNotEmpty($team->getAvgAge());
100
-        }
101
-    }
102
-
103
-    /**
104
-     * @group Match
105
-     */
106
-    public function testGetRandomMatch()
107
-    {
108
-        for ($i = 1; $i <= 30; $i++) {
109
-            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
110
-            $spanish = $rndF->getTeam();
111
-            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
112
-            $italian = $rndF->getTeam();
113
-            $this->assertNotEmpty($spanish);
114
-            $this->assertNotEmpty($italian);
115
-            $this->assertNotEmpty($italian->name);
116
-            $this->assertNotEmpty($spanish);
117
-            $this->assertNotEmpty($spanish->name);
118
-
119
-            $this->assertNotEmpty($italian->getAvgSkill());
120
-            $this->assertNotEmpty($spanish->getAvgSkill());
121
-            $result = (new \App\Lib\DsManager\Models\Match($italian, $spanish))->simulate()->toArray();
122
-            $this->assertNotEmpty($result);
123
-            $this->assertGreaterThanOrEqual(0, $result['goalHome']);
124
-            $this->assertGreaterThanOrEqual(0, $result['goalAway']);
125
-
126
-        }
127
-    }
128
-
129
-    /**
130
-     * @group Match
131
-     * @group MatchResult
132
-     * @group matchresultoutput
133
-     */
134
-    public function testMatchFromExistingTeams()
135
-    {
136
-        $teamHome = 1;
137
-        $teamAway = 2;
138
-        $homeOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamHome])->first();
139
-        $awayOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamAway])->first();
140
-        $home = \App\Lib\DsManager\Models\Team::fromArray($homeOrm->toArray());
141
-        $away = \App\Lib\DsManager\Models\Team::fromArray($awayOrm->toArray());
142
-
143
-        $match = new \App\Lib\DsManager\Models\Match($home, $away);
144
-        $result = $match->simulate()->toArray();
145
-        $this->assertNotEmpty($result);
146
-        $this->assertGreaterThanOrEqual(0, $result['goalAway']);
147
-        $this->assertGreaterThanOrEqual(0, $result['goalHome']);
148
-        if ($result['goalHome'] > 0) {
149
-            $this->assertNotEmpty($result['info']['scorers']['home']);
150
-            foreach ($result['info']['scorers']['home'] as $scorerHome) {
151
-                $this->assertEquals($scorerHome->team_id, $teamHome);
152
-            }
153
-        } else {
154
-            $this->assertEmpty($result['info']['scorers']['home']);
155
-        }
156
-        if ($result['goalAway'] > 0) {
157
-            $this->assertNotEmpty($result['info']['scorers']['away']);
158
-            foreach ($result['info']['scorers']['away'] as $scorerAway) {
159
-                $this->assertEquals($scorerAway->team_id, $teamAway);
160
-            }
161
-        } else {
162
-            $this->assertEmpty($result['info']['scorers']['away']);
163
-        }
164
-        if ($result['goalHome'] == $result['goalAway']) {
165
-            $this->assertTrue($result['info']['isDraw']);
166
-        } else {
167
-            $this->assertFalse($result['info']['isDraw']);
168
-        }
169
-    }
170
-
171
-    /**
172
-     * @group Matches
173
-     */
174
-    public function testGetRandomMatchesOneTeam()
175
-    {
176
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
177
-        $myTeam = $rndF->getTeam();
178
-        $win = 0;
179
-        $lost = 0;
180
-        $draw = 0;
181
-
182
-        for ($i = 1; $i <= 2; $i++) {
183
-
184
-            $randomLocale = \App\Lib\Helpers\Config::get('generic.localesSmall');
185
-            shuffle($randomLocale);
186
-            $randomLocale = $randomLocale[0];
187
-
188
-            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($randomLocale);
189
-            $opponent = $rndF->getTeam();
190
-            $result = (new \App\Lib\DsManager\Models\Match($opponent, $myTeam))->simulate()->toArray();
191
-            $this->assertNotEmpty($result);
192
-            $result = $result['info'];
193
-            if (!$result['isDraw']) {
194
-                if ($result['winner']['name'] == $myTeam->name) {
195
-                    $win++;
196
-                } else {
197
-                    $lost++;
198
-                }
199
-            } else {
200
-                $draw++;
201
-            }
202
-        }
203
-        $this->assertGreaterThanOrEqual(0, $win);
204
-        $this->assertGreaterThanOrEqual(0, $lost);
205
-        $this->assertGreaterThanOrEqual(0, $draw);
206
-    }
207
-
208
-    /**
209
-     * @group Module
210
-     */
211
-    public function testModule()
212
-    {
213
-        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
214
-        $team = $rndF->getTeam();
215
-
216
-        $modules = \App\Lib\Helpers\Config::get("modules.modules");
217
-        $modules = array_keys($modules);
218
-        foreach ($modules as $mod) {
219
-            $module = new \App\Lib\DsManager\Models\Module($mod);
220
-            $this->assertNotEmpty($module);
221
-            $this->assertNotNull($module->isDefensive());
222
-            $this->assertNotNull($module->isBalanced());
223
-            $this->assertNotNull($module->isOffensive());
224
-            $this->assertTrue(is_array($module->getRoleNeeded()));
225
-        }
226
-        $this->assertGreaterThan(0, $team->playersPerRoleArray());
227
-    }
11
+	/**
12
+	 * @group Player
13
+	 */
14
+	public function testGetRandomPlayer()
15
+	{
16
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
17
+		$player = $rndF->getPlayer(null, $rndF->getLocale());
18
+		$array = $player->toArray();
19
+		$this->assertNotEmpty($array);
20
+
21
+		$newPlayer = \App\Lib\DsManager\Models\Player::fromArray($array);
22
+		$this->assertNotEmpty($newPlayer->toArray());
23
+	}
24
+
25
+	/**
26
+	 * @group Coach
27
+	 */
28
+	public function testGetRandomCoach()
29
+	{
30
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
31
+		$coach = $rndF->getCoach();
32
+		$this->assertNotEmpty($coach->toArray());
33
+	}
34
+
35
+	/**
36
+	 * @group Coaches
37
+	 */
38
+	public function testGetRandomCoaches()
39
+	{
40
+		foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
41
+			$rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
42
+			$coach = $rndF->getCoach();
43
+			$this->assertNotEmpty($coach->toArray());
44
+		}
45
+	}
46
+
47
+	/**
48
+	 * @group Players
49
+	 */
50
+	public function testGetRandomPlayers()
51
+	{
52
+		foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
53
+			$rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
54
+			$player = $rndF->getPlayer();
55
+			$this->assertNotEmpty($player->toArray());
56
+		}
57
+	}
58
+
59
+	/**
60
+	 * @group Team
61
+	 */
62
+	public function testGetRandomTeam()
63
+	{
64
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
65
+		$team = $rndF->getTeam();
66
+		$this->assertNotEmpty($team);
67
+		$this->assertNotEmpty($team->name);
68
+		$this->assertNotEmpty($team->getAvgSkill());
69
+
70
+		//After Adding a player
71
+		$player = $rndF->getPlayer();
72
+		$this->assertNotEmpty($player->toArray());
73
+		$team->roster[] = $player;
74
+		$this->assertNotEmpty($team->getAvgSkill());
75
+		$this->assertNotEmpty($team->getAvgAge());
76
+
77
+		$this->assertNotEmpty($team->coach->toArray());
78
+
79
+		$teamArray = $team->toArray();
80
+		$this->assertNotEmpty($teamArray);
81
+
82
+		$newTeam = \App\Lib\DsManager\Models\Team::fromArray($teamArray);
83
+		$this->assertNotEmpty($newTeam->toArray());
84
+
85
+	}
86
+
87
+	/**
88
+	 * @group Teams
89
+	 */
90
+	public function testGetRandomTeams()
91
+	{
92
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
93
+
94
+		for ($i = 1; $i <= 20; $i++) {
95
+			$team = $rndF->getTeam();
96
+			$this->assertNotEmpty($team->name);
97
+			$this->assertNotEmpty($team->nationality);
98
+			$this->assertNotEmpty($team->getAvgSkill());
99
+			$this->assertNotEmpty($team->getAvgAge());
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * @group Match
105
+	 */
106
+	public function testGetRandomMatch()
107
+	{
108
+		for ($i = 1; $i <= 30; $i++) {
109
+			$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
110
+			$spanish = $rndF->getTeam();
111
+			$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
112
+			$italian = $rndF->getTeam();
113
+			$this->assertNotEmpty($spanish);
114
+			$this->assertNotEmpty($italian);
115
+			$this->assertNotEmpty($italian->name);
116
+			$this->assertNotEmpty($spanish);
117
+			$this->assertNotEmpty($spanish->name);
118
+
119
+			$this->assertNotEmpty($italian->getAvgSkill());
120
+			$this->assertNotEmpty($spanish->getAvgSkill());
121
+			$result = (new \App\Lib\DsManager\Models\Match($italian, $spanish))->simulate()->toArray();
122
+			$this->assertNotEmpty($result);
123
+			$this->assertGreaterThanOrEqual(0, $result['goalHome']);
124
+			$this->assertGreaterThanOrEqual(0, $result['goalAway']);
125
+
126
+		}
127
+	}
128
+
129
+	/**
130
+	 * @group Match
131
+	 * @group MatchResult
132
+	 * @group matchresultoutput
133
+	 */
134
+	public function testMatchFromExistingTeams()
135
+	{
136
+		$teamHome = 1;
137
+		$teamAway = 2;
138
+		$homeOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamHome])->first();
139
+		$awayOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamAway])->first();
140
+		$home = \App\Lib\DsManager\Models\Team::fromArray($homeOrm->toArray());
141
+		$away = \App\Lib\DsManager\Models\Team::fromArray($awayOrm->toArray());
142
+
143
+		$match = new \App\Lib\DsManager\Models\Match($home, $away);
144
+		$result = $match->simulate()->toArray();
145
+		$this->assertNotEmpty($result);
146
+		$this->assertGreaterThanOrEqual(0, $result['goalAway']);
147
+		$this->assertGreaterThanOrEqual(0, $result['goalHome']);
148
+		if ($result['goalHome'] > 0) {
149
+			$this->assertNotEmpty($result['info']['scorers']['home']);
150
+			foreach ($result['info']['scorers']['home'] as $scorerHome) {
151
+				$this->assertEquals($scorerHome->team_id, $teamHome);
152
+			}
153
+		} else {
154
+			$this->assertEmpty($result['info']['scorers']['home']);
155
+		}
156
+		if ($result['goalAway'] > 0) {
157
+			$this->assertNotEmpty($result['info']['scorers']['away']);
158
+			foreach ($result['info']['scorers']['away'] as $scorerAway) {
159
+				$this->assertEquals($scorerAway->team_id, $teamAway);
160
+			}
161
+		} else {
162
+			$this->assertEmpty($result['info']['scorers']['away']);
163
+		}
164
+		if ($result['goalHome'] == $result['goalAway']) {
165
+			$this->assertTrue($result['info']['isDraw']);
166
+		} else {
167
+			$this->assertFalse($result['info']['isDraw']);
168
+		}
169
+	}
170
+
171
+	/**
172
+	 * @group Matches
173
+	 */
174
+	public function testGetRandomMatchesOneTeam()
175
+	{
176
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
177
+		$myTeam = $rndF->getTeam();
178
+		$win = 0;
179
+		$lost = 0;
180
+		$draw = 0;
181
+
182
+		for ($i = 1; $i <= 2; $i++) {
183
+
184
+			$randomLocale = \App\Lib\Helpers\Config::get('generic.localesSmall');
185
+			shuffle($randomLocale);
186
+			$randomLocale = $randomLocale[0];
187
+
188
+			$rndF = new \App\Lib\DsManager\Helpers\RandomFiller($randomLocale);
189
+			$opponent = $rndF->getTeam();
190
+			$result = (new \App\Lib\DsManager\Models\Match($opponent, $myTeam))->simulate()->toArray();
191
+			$this->assertNotEmpty($result);
192
+			$result = $result['info'];
193
+			if (!$result['isDraw']) {
194
+				if ($result['winner']['name'] == $myTeam->name) {
195
+					$win++;
196
+				} else {
197
+					$lost++;
198
+				}
199
+			} else {
200
+				$draw++;
201
+			}
202
+		}
203
+		$this->assertGreaterThanOrEqual(0, $win);
204
+		$this->assertGreaterThanOrEqual(0, $lost);
205
+		$this->assertGreaterThanOrEqual(0, $draw);
206
+	}
207
+
208
+	/**
209
+	 * @group Module
210
+	 */
211
+	public function testModule()
212
+	{
213
+		$rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
214
+		$team = $rndF->getTeam();
215
+
216
+		$modules = \App\Lib\Helpers\Config::get("modules.modules");
217
+		$modules = array_keys($modules);
218
+		foreach ($modules as $mod) {
219
+			$module = new \App\Lib\DsManager\Models\Module($mod);
220
+			$this->assertNotEmpty($module);
221
+			$this->assertNotNull($module->isDefensive());
222
+			$this->assertNotNull($module->isBalanced());
223
+			$this->assertNotNull($module->isOffensive());
224
+			$this->assertTrue(is_array($module->getRoleNeeded()));
225
+		}
226
+		$this->assertGreaterThan(0, $team->playersPerRoleArray());
227
+	}
228 228
 
229 229
 }
Please login to merge, or discard this patch.
api/Lib/DsManager/Models/Match.php 1 patch
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -10,143 +10,143 @@
 block discarded – undo
10 10
  */
11 11
 class Match
12 12
 {
13
-    /**
14
-     * @var Team
15
-     */
16
-    private $homeTeam;
17
-    /**
18
-     * @var Team
19
-     */
20
-    private $awayTeam;
13
+	/**
14
+	 * @var Team
15
+	 */
16
+	private $homeTeam;
17
+	/**
18
+	 * @var Team
19
+	 */
20
+	private $awayTeam;
21 21
 
22
-    /**
23
-     * Match constructor.
24
-     * @param Team $home
25
-     * @param Team $away
26
-     */
27
-    public function __construct(Team $home, Team $away)
28
-    {
29
-        $this->homeTeam = $home;
30
-        $this->awayTeam = $away;
31
-    }
22
+	/**
23
+	 * Match constructor.
24
+	 * @param Team $home
25
+	 * @param Team $away
26
+	 */
27
+	public function __construct(Team $home, Team $away)
28
+	{
29
+		$this->homeTeam = $home;
30
+		$this->awayTeam = $away;
31
+	}
32 32
 
33
-    /**
34
-     * @return MatchResult
35
-     */
36
-    public function simulate()
37
-    {
38
-        $homePoints = $this->homeTeam->getAvgSkill();
39
-        $awayPoints = $this->awayTeam->getAvgSkill();
33
+	/**
34
+	 * @return MatchResult
35
+	 */
36
+	public function simulate()
37
+	{
38
+		$homePoints = $this->homeTeam->getAvgSkill();
39
+		$awayPoints = $this->awayTeam->getAvgSkill();
40 40
 
41
-        $homePoints += $this->malusModule(
42
-            $this->homeTeam->coach->favouriteModule,
43
-            $this->homeTeam->playersPerRoleArray()
44
-        );
45
-        $awayPoints += $this->malusModule(
46
-            $this->awayTeam->coach->favouriteModule,
47
-            $this->awayTeam->playersPerRoleArray()
48
-        );
41
+		$homePoints += $this->malusModule(
42
+			$this->homeTeam->coach->favouriteModule,
43
+			$this->homeTeam->playersPerRoleArray()
44
+		);
45
+		$awayPoints += $this->malusModule(
46
+			$this->awayTeam->coach->favouriteModule,
47
+			$this->awayTeam->playersPerRoleArray()
48
+		);
49 49
 
50
-        $goalHome = 0;
51
-        $goalAway = 0;
50
+		$goalHome = 0;
51
+		$goalAway = 0;
52 52
 
53
-        if (Randomizer::boolOnPercentage(80)) {
54
-            if (($homePoints - $awayPoints) < 0) {
55
-                $goalAway = ($awayPoints - $homePoints) % 6;
56
-                $goalHome += $this->chance();
57
-                $goalAway += $this->chance();
58
-                $goalHome += $this->bonusHome();
59
-            } else {
60
-                $goalHome = ($homePoints - $awayPoints) % 6;
61
-                $goalAway += $this->chance();
62
-                $goalHome += $this->bonusHome();
63
-            }
64
-        } else {
65
-            $goalHome += $this->chance();
66
-            $goalAway += $this->chance();
67
-            $goalHome += $this->bonusHome();
68
-        }
69
-        $goalHome += $this->bonusAge($this->homeTeam);
70
-        $goalAway += $this->bonusAge($this->awayTeam);
53
+		if (Randomizer::boolOnPercentage(80)) {
54
+			if (($homePoints - $awayPoints) < 0) {
55
+				$goalAway = ($awayPoints - $homePoints) % 6;
56
+				$goalHome += $this->chance();
57
+				$goalAway += $this->chance();
58
+				$goalHome += $this->bonusHome();
59
+			} else {
60
+				$goalHome = ($homePoints - $awayPoints) % 6;
61
+				$goalAway += $this->chance();
62
+				$goalHome += $this->bonusHome();
63
+			}
64
+		} else {
65
+			$goalHome += $this->chance();
66
+			$goalAway += $this->chance();
67
+			$goalHome += $this->bonusHome();
68
+		}
69
+		$goalHome += $this->bonusAge($this->homeTeam);
70
+		$goalAway += $this->bonusAge($this->awayTeam);
71 71
 
72
-        //Bonus on Good GoalKeeper
73
-        $goalies = $this->homeTeam->getBestPlayerForRole("GK");
74
-        $goalAway -= $this->bonusGoalKeeper($goalies);
75
-        $goalies = $this->awayTeam->getBestPlayerForRole("GK");
76
-        $goalHome -= $this->bonusGoalKeeper($goalies);
77
-        //
78
-        $homeModule = new Module($this->homeTeam->coach->favouriteModule);
79
-        $awayModule = new Module($this->awayTeam->coach->favouriteModule);
80
-        if ($homeModule->isOffensive()) {
81
-            $goalHome += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
82
-            $goalAway += Randomizer::boolOnPercentage(20) ? 1 : 0;
83
-        }
84
-        if ($awayModule->isOffensive()) {
85
-            $goalAway += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
86
-            $goalHome += Randomizer::boolOnPercentage(20) ? 1 : 0;
87
-        }
88
-        if ($awayModule->isDefensive()) {
89
-            $goalHome -= Randomizer::boolOnPercentage(50) ? 1 : 0;
90
-        }
91
-        if ($homeModule->isDefensive()) {
92
-            $goalAway -= Randomizer::boolOnPercentage(50) ? 1 : 0;
93
-        }
94
-        $goalHome = $goalHome < 0 ? 0 : $goalHome;
95
-        $goalAway = $goalAway < 0 ? 0 : $goalAway;
96
-        return new MatchResult($goalHome, $goalAway, $this->homeTeam, $this->awayTeam);
97
-    }
72
+		//Bonus on Good GoalKeeper
73
+		$goalies = $this->homeTeam->getBestPlayerForRole("GK");
74
+		$goalAway -= $this->bonusGoalKeeper($goalies);
75
+		$goalies = $this->awayTeam->getBestPlayerForRole("GK");
76
+		$goalHome -= $this->bonusGoalKeeper($goalies);
77
+		//
78
+		$homeModule = new Module($this->homeTeam->coach->favouriteModule);
79
+		$awayModule = new Module($this->awayTeam->coach->favouriteModule);
80
+		if ($homeModule->isOffensive()) {
81
+			$goalHome += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
82
+			$goalAway += Randomizer::boolOnPercentage(20) ? 1 : 0;
83
+		}
84
+		if ($awayModule->isOffensive()) {
85
+			$goalAway += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
86
+			$goalHome += Randomizer::boolOnPercentage(20) ? 1 : 0;
87
+		}
88
+		if ($awayModule->isDefensive()) {
89
+			$goalHome -= Randomizer::boolOnPercentage(50) ? 1 : 0;
90
+		}
91
+		if ($homeModule->isDefensive()) {
92
+			$goalAway -= Randomizer::boolOnPercentage(50) ? 1 : 0;
93
+		}
94
+		$goalHome = $goalHome < 0 ? 0 : $goalHome;
95
+		$goalAway = $goalAway < 0 ? 0 : $goalAway;
96
+		return new MatchResult($goalHome, $goalAway, $this->homeTeam, $this->awayTeam);
97
+	}
98 98
 
99
-    /**
100
-     * @param Team $team
101
-     * @return int
102
-     */
103
-    private function bonusAge(Team $team)
104
-    {
105
-        if ($team->getAvgAge() > 29 || $team->getAvgAge() < 24) {
106
-            return $this->chance();
107
-        }
108
-        return 0;
109
-    }
99
+	/**
100
+	 * @param Team $team
101
+	 * @return int
102
+	 */
103
+	private function bonusAge(Team $team)
104
+	{
105
+		if ($team->getAvgAge() > 29 || $team->getAvgAge() < 24) {
106
+			return $this->chance();
107
+		}
108
+		return 0;
109
+	}
110 110
 
111
-    /**
112
-     * @param $goalkeeper
113
-     * @return int
114
-     */
115
-    private function bonusGoalKeeper($goalkeeper)
116
-    {
117
-        $skillGoalkeeper = empty($goalkeeper) ? 1 : $goalkeeper->skillAvg;
118
-        return (Randomizer::boolOnPercentage($skillGoalkeeper) ? 1 : 0);
119
-    }
111
+	/**
112
+	 * @param $goalkeeper
113
+	 * @return int
114
+	 */
115
+	private function bonusGoalKeeper($goalkeeper)
116
+	{
117
+		$skillGoalkeeper = empty($goalkeeper) ? 1 : $goalkeeper->skillAvg;
118
+		return (Randomizer::boolOnPercentage($skillGoalkeeper) ? 1 : 0);
119
+	}
120 120
 
121
-    /**
122
-     * @return int
123
-     */
124
-    private function chance()
125
-    {
126
-        return rand(0, 3);
127
-    }
121
+	/**
122
+	 * @return int
123
+	 */
124
+	private function chance()
125
+	{
126
+		return rand(0, 3);
127
+	}
128 128
 
129
-    /**
130
-     * @return int
131
-     */
132
-    private function bonusHome()
133
-    {
134
-        return Randomizer::boolOnPercentage(66) ? 1 : 0;
135
-    }
129
+	/**
130
+	 * @return int
131
+	 */
132
+	private function bonusHome()
133
+	{
134
+		return Randomizer::boolOnPercentage(66) ? 1 : 0;
135
+	}
136 136
 
137
-    /**
138
-     * @param $moduleString
139
-     * @param $playersRoleArray
140
-     * @return int
141
-     */
142
-    private function malusModule($moduleString, $playersRoleArray)
143
-    {
144
-        $module = new Module($moduleString);
145
-        if ($module->isApplicableToArray($playersRoleArray)) {
146
-            return rand(1, 10);
147
-        } else {
148
-            return (-1) * rand(1, 10);
149
-        }
150
-    }
137
+	/**
138
+	 * @param $moduleString
139
+	 * @param $playersRoleArray
140
+	 * @return int
141
+	 */
142
+	private function malusModule($moduleString, $playersRoleArray)
143
+	{
144
+		$module = new Module($moduleString);
145
+		if ($module->isApplicableToArray($playersRoleArray)) {
146
+			return rand(1, 10);
147
+		} else {
148
+			return (-1) * rand(1, 10);
149
+		}
150
+	}
151 151
 
152 152
 }
153 153
\ No newline at end of file
Please login to merge, or discard this patch.