|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use App\Events\MatchWasFinished; |
|
6
|
|
|
use App\Listeners\Match\UpdateResultType; |
|
7
|
|
|
use App\Match; |
|
8
|
|
|
use App\Tournament; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use Illuminate\Support\Facades\Validator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class MatchUpdate |
|
15
|
|
|
* @package App\Http\Requests |
|
16
|
|
|
*/ |
|
17
|
|
|
class MatchUpdate extends Request |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Determine if the user is authorized to make this request. |
|
21
|
|
|
* |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
|
|
public function authorize() |
|
25
|
|
|
{ |
|
26
|
|
|
return Auth::check(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the validation rules that apply to the request. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function rules() |
|
35
|
|
|
{ |
|
36
|
|
|
$matchId = $this->route('matchId'); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Copy of match row |
|
40
|
|
|
* |
|
41
|
|
|
* @var $match Match |
|
42
|
|
|
*/ |
|
43
|
|
|
$match = Match::findOrFail($matchId)->replicate(); |
|
44
|
|
|
$tournament = $match->tournament()->get()->first(); |
|
45
|
|
|
|
|
46
|
|
|
Validator::extend('round_active', function ($attribute, $value, $parameters) use ($match, $tournament) { |
|
|
|
|
|
|
47
|
|
|
return $this->isRoundActive($match, $tournament); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
Validator::extend( |
|
51
|
|
|
'round_finished_for_pair', |
|
52
|
|
|
function ($attribute, $value, $parameters) use ($match, $tournament) { |
|
|
|
|
|
|
53
|
|
|
$match->status = $value; |
|
|
|
|
|
|
54
|
|
|
return $this->isRoundFinishedForPair($match, $tournament); |
|
55
|
|
|
} |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$statuses = implode(',', Match::getAvailableStatuses()); |
|
59
|
|
|
|
|
60
|
|
|
return [ |
|
61
|
|
|
'match.homeScore' => 'required|integer', |
|
62
|
|
|
'match.awayScore' => 'required|integer', |
|
63
|
|
|
'match.status' => 'required|in:' . $statuses . '|round_active|round_finished_for_pair' |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $match |
|
69
|
|
|
* @param $tournament |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function isRoundActive($match, $tournament) |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
// rule is not applied for Leagues |
|
76
|
|
|
if (Tournament::TYPE_LEAGUE === $tournament->type) { |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// rule is not applied for group stage matches |
|
81
|
|
|
if (Match::GAME_TYPE_GROUP_STAGE === $match->gameType) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $match->round === $tournament->getCurrentRound(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $match |
|
90
|
|
|
* @param $tournament Tournament |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function isRoundFinishedForPair($match, $tournament) |
|
94
|
|
|
{ |
|
95
|
|
|
// rule is not applied for Leagues |
|
96
|
|
|
if (Tournament::TYPE_LEAGUE === $tournament->type) { |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// rule is not applied for group stage matches |
|
101
|
|
|
if (Match::GAME_TYPE_GROUP_STAGE === $match->gameType) { |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$match->fill(array_get($this->only(['match.homeScore', 'match.awayScore', 'match.status']), 'match')); |
|
106
|
|
|
|
|
107
|
|
|
$event = new MatchWasFinished($match); |
|
108
|
|
|
|
|
109
|
|
|
$listener = new UpdateResultType(); |
|
110
|
|
|
$listener->handle($event); |
|
111
|
|
|
|
|
112
|
|
|
$secondMatchForPair = Match::where([ |
|
113
|
|
|
'round' => $match->round, |
|
114
|
|
|
'homeTournamentTeamId' => $match->awayTournamentTeamId, |
|
115
|
|
|
'awayTournamentTeamId' => $match->homeTournamentTeamId |
|
116
|
|
|
])->first(); |
|
117
|
|
|
|
|
118
|
|
|
$matches = new Collection([$event->match, $secondMatchForPair]); |
|
119
|
|
|
|
|
120
|
|
|
// all matches in pair should have `finished` status |
|
121
|
|
|
if ($matches->where('status', Match::STATUS_FINISHED)->count() < 2) { |
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$score = $tournament->getScore($matches); |
|
126
|
|
|
|
|
127
|
|
|
return 2 === $score->unique('position')->count(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.