1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace App\Lib\DsManager\Models;
|
4
|
|
|
|
5
|
|
|
use App\Lib\DsManager\Helpers\Randomizer;
|
6
|
|
|
use App\Lib\Helpers\Config;
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* Class MatchResult
|
11
|
|
|
* @package App\Lib\DsManager\Models
|
12
|
|
|
*/
|
13
|
|
|
class MatchResult
|
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;
|
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
|
|
|
}
|
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
|
|
|
}
|
66
|
|
|
|
67
|
|
|
return [
|
68
|
|
|
'isDraw' => $isDraw,
|
69
|
|
|
'winner' => $this->cleanTeam($winner->toArray()),
|
70
|
|
|
'loser' => $this->cleanTeam($loser->toArray())
|
71
|
|
|
];
|
72
|
|
|
}
|
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
|
|
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* @param array $team
|
89
|
|
|
* @return array
|
90
|
|
|
*/
|
91
|
|
|
private function cleanTeam(array $team)
|
92
|
|
|
{
|
93
|
|
|
$fieldsToClean = [
|
94
|
|
|
'coach',
|
95
|
|
|
'roster',
|
96
|
|
|
];
|
97
|
|
|
|
98
|
|
|
foreach ($fieldsToClean as $field) {
|
99
|
|
|
if (array_key_exists($field, $team)) {
|
100
|
|
|
unset($team[$field]);
|
101
|
|
|
}
|
102
|
|
|
}
|
103
|
|
|
return $team;
|
104
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
}
|
151
|
|
|
|
152
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.