|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Tournament |
|
10
|
|
|
* @package App |
|
11
|
|
|
*/ |
|
12
|
|
|
class Tournament extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $table = 'tournaments'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $fillable = ['name', 'description', 'status', 'type', 'membersType']; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public $timestamps = true; |
|
28
|
|
|
|
|
29
|
|
|
const MEMBERS_TYPE_SINGLE = 'single'; |
|
30
|
|
|
const MEMBERS_TYPE_DOUBLE = 'double'; |
|
31
|
|
|
|
|
32
|
|
|
const TYPE_LEAGUE = 'league'; |
|
33
|
|
|
const TYPE_KNOCK_OUT = 'knock_out'; |
|
34
|
|
|
const TYPE_MULTISTAGE = 'multistage'; |
|
35
|
|
|
|
|
36
|
|
|
const STATUS_DRAFT = 'draft'; |
|
37
|
|
|
const STATUS_STARTED = 'started'; |
|
38
|
|
|
const STATUS_COMPLETED = 'completed'; |
|
39
|
|
|
const STATUS_DELETED = 'deleted'; |
|
40
|
|
|
|
|
41
|
|
|
const MIN_TEAMS_AMOUNT = 2; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
45
|
|
|
*/ |
|
46
|
|
|
public function tournamentTeams() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->hasMany(TournamentTeam::class, 'tournamentId'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
53
|
|
|
*/ |
|
54
|
|
|
public function matches() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->hasMany(Match::class, 'tournamentId'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getAvailableMembersType() |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
self::MEMBERS_TYPE_SINGLE, |
|
66
|
|
|
self::MEMBERS_TYPE_DOUBLE, |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getAvailableTypes() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
self::TYPE_LEAGUE, |
|
77
|
|
|
self::TYPE_KNOCK_OUT, |
|
78
|
|
|
self::TYPE_MULTISTAGE |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function getAvailableStatuses() |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
self::STATUS_DRAFT, |
|
89
|
|
|
self::STATUS_STARTED, |
|
90
|
|
|
self::STATUS_COMPLETED, |
|
91
|
|
|
self::STATUS_DELETED |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Group matches in tournament into pairs |
|
97
|
|
|
* |
|
98
|
|
|
* @return Collection |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getPairs() |
|
101
|
|
|
{ |
|
102
|
|
|
// get pairs of current round matches |
|
103
|
|
|
$pairs = new Collection(); |
|
104
|
|
|
|
|
105
|
|
|
$this->matches->map(function ($match) use ($pairs) { |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$pairId = [$match->homeTournamentTeam->id, $match->awayTournamentTeam->id]; |
|
108
|
|
|
sort($pairId); |
|
109
|
|
|
$pairId = implode('-', $pairId); |
|
110
|
|
|
|
|
111
|
|
|
$pair = $pairs->pull($pairId); |
|
112
|
|
|
|
|
113
|
|
|
if (!$pair) { |
|
114
|
|
|
$teams = new Collection([ |
|
115
|
|
|
$match->homeTournamentTeam->with('Team')->get(), |
|
116
|
|
|
$match->awayTournamentTeam->with('Team')->get() |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
|
|
$pair = new Collection([ |
|
120
|
|
|
'id' => $pairId, |
|
121
|
|
|
'round' => $match->round, |
|
122
|
|
|
'teams' => $teams, |
|
123
|
|
|
'matches' => new Collection() |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$pair->get('matches')->push($match); |
|
128
|
|
|
|
|
129
|
|
|
$pairs->put($pairId, $pair); |
|
130
|
|
|
}); |
|
131
|
|
|
|
|
132
|
|
|
return $pairs; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function matchScore($match, $homeTeam, $awayTeam) |
|
136
|
|
|
{ |
|
137
|
|
|
if (Match::STATUS_FINISHED == $match->status) { |
|
138
|
|
|
$homeTeam['matches']++; |
|
139
|
|
|
$awayTeam['matches']++; |
|
140
|
|
|
$homeTeam['goalsScored'] += $match->homeScore; |
|
141
|
|
|
$homeTeam['goalsAgainsted'] += $match->awayScore; |
|
142
|
|
|
$homeTeam['goalsDifference'] = ($homeTeam['goalsScored'] - $homeTeam['goalsAgainsted']); |
|
143
|
|
|
|
|
144
|
|
|
$awayTeam['goalsScored'] += $match->awayScore; |
|
145
|
|
|
$awayTeam['goalsAgainsted'] += $match->homeScore; |
|
146
|
|
|
$awayTeam['goalsDifference'] = ($awayTeam['goalsScored'] - $awayTeam['goalsAgainsted']); |
|
147
|
|
|
|
|
148
|
|
|
switch ($match->resultType) { |
|
149
|
|
View Code Duplication |
case Match::RESULT_TYPE_HOME_WIN: |
|
|
|
|
|
|
150
|
|
|
$homeTeam['wins']++; |
|
151
|
|
|
$homeTeam['points'] += Match::POINTS_WIN; |
|
152
|
|
|
$awayTeam['losts']++; |
|
153
|
|
|
|
|
154
|
|
|
break; |
|
155
|
|
View Code Duplication |
case Match::RESULT_TYPE_AWAY_WIN: |
|
|
|
|
|
|
156
|
|
|
$awayTeam['wins']++; |
|
157
|
|
|
$homeTeam['losts']++; |
|
158
|
|
|
$awayTeam['points'] += Match::POINTS_WIN; |
|
159
|
|
|
|
|
160
|
|
|
break; |
|
161
|
|
View Code Duplication |
case Match::RESULT_TYPE_DRAW: |
|
|
|
|
|
|
162
|
|
|
$homeTeam['draws']++; |
|
163
|
|
|
$awayTeam['draws']++; |
|
164
|
|
|
$homeTeam['points'] += Match::POINTS_DRAW; |
|
165
|
|
|
$awayTeam['points'] += Match::POINTS_DRAW; |
|
166
|
|
|
|
|
167
|
|
|
break; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return ['homeTeam' => $homeTeam, 'awayTeam' => $awayTeam]; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @todo Make a refactoring for `pair` entity |
|
176
|
|
|
* @name getScore |
|
177
|
|
|
* @param Collection $matches |
|
178
|
|
|
* @return Collection|static |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getScore(Collection $matches) |
|
181
|
|
|
{ |
|
182
|
|
|
$score = new Collection(); |
|
183
|
|
|
|
|
184
|
|
|
$matches->map(function ($match) use ($score) { |
|
185
|
|
|
|
|
186
|
|
|
$homeTeam = $score->pull($match->homeTournamentTeam->id); |
|
187
|
|
|
$awayTeam = $score->pull($match->awayTournamentTeam->id); |
|
188
|
|
|
|
|
189
|
|
|
$defaultTeamData = [ |
|
190
|
|
|
'matches' => 0, |
|
191
|
|
|
'position' => 0, |
|
192
|
|
|
'wins' => 0, |
|
193
|
|
|
'draws' => 0, |
|
194
|
|
|
'losts' => 0, |
|
195
|
|
|
'points' => 0, |
|
196
|
|
|
'goalsScored' => 0, |
|
197
|
|
|
'goalsAgainsted' => 0, |
|
198
|
|
|
'goalsDifference' => 0, |
|
199
|
|
|
]; |
|
200
|
|
|
|
|
201
|
|
|
if (!$homeTeam) { |
|
202
|
|
|
$homeTeam = array_merge( |
|
203
|
|
|
[ |
|
204
|
|
|
'teamId' => $match->homeTournamentTeam->id, |
|
205
|
|
|
'name' => $match->homeTournamentTeam->team->name, |
|
206
|
|
|
], |
|
207
|
|
|
$defaultTeamData |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
if (!$awayTeam) { |
|
212
|
|
|
$awayTeam = array_merge( |
|
213
|
|
|
[ |
|
214
|
|
|
'teamId' => $match->awayTournamentTeam->id, |
|
215
|
|
|
'name' => $match->awayTournamentTeam->team->name, |
|
216
|
|
|
], |
|
217
|
|
|
$defaultTeamData |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$teams =$this->matchScore($match, $homeTeam, $awayTeam); |
|
222
|
|
|
$score->put($match->homeTournamentTeam->id, $teams['homeTeam']); |
|
223
|
|
|
$score->put($match->awayTournamentTeam->id, $teams['awayTeam']); |
|
224
|
|
|
}); |
|
225
|
|
|
|
|
226
|
|
|
// sort by points and goal difference |
|
227
|
|
|
$score = $score->sort(function ($a, $b) { |
|
228
|
|
|
if ($b['points'] === $a['points']) { |
|
229
|
|
|
return $b['goalsDifference'] - $a['goalsDifference']; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $b['points'] - $a['points']; |
|
233
|
|
|
}); |
|
234
|
|
|
|
|
235
|
|
|
$previousRow = null; |
|
236
|
|
|
$position = 1; |
|
237
|
|
|
$score = $score->map(function ($row) use (&$previousRow, &$position) { |
|
238
|
|
|
if ($previousRow |
|
239
|
|
|
&& $previousRow['points'] > 0 |
|
240
|
|
|
&& $previousRow['points'] == $row['points'] |
|
241
|
|
|
&& $previousRow['goalsDifference'] == $row['goalsDifference'] |
|
242
|
|
|
&& $previousRow['goalsScored'] == $row['goalsScored'] |
|
243
|
|
|
) { |
|
244
|
|
|
$row['position'] = $previousRow['position']; |
|
245
|
|
|
} else { |
|
246
|
|
|
$row['position'] = $position; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
$position++; |
|
250
|
|
|
|
|
251
|
|
|
$previousRow = $row; |
|
252
|
|
|
|
|
253
|
|
|
return $row; |
|
254
|
|
|
}); |
|
255
|
|
|
|
|
256
|
|
|
// alphabetical sort for teams on the same position |
|
257
|
|
|
$score = $score->sortBy(function ($team) { |
|
258
|
|
|
return $team['position'] . '-' . $team['name']; |
|
259
|
|
|
}, SORT_NUMERIC); |
|
260
|
|
|
|
|
261
|
|
|
return $score; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @name getWinner |
|
266
|
|
|
* @param Collection $matches |
|
267
|
|
|
* @return mixed |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getWinner(Collection $matches) |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->getScore($matches)->first(); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @return int |
|
276
|
|
|
*/ |
|
277
|
|
|
public function getCurrentRound() |
|
278
|
|
|
{ |
|
279
|
|
|
$matches = $this->matches()->get(); |
|
280
|
|
|
|
|
281
|
|
|
if ($matches->count() === 0) { |
|
282
|
|
|
return 0; |
|
283
|
|
|
} else { |
|
284
|
|
|
return $matches->pluck('round')->max(); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.