1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Lib\DsManager\Models\Orm; |
4
|
|
|
|
5
|
|
|
use App\Lib\DsManager\Helpers\Randomizer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MatchResult |
9
|
|
|
* @package App\Lib\DsManager\Models\Orm |
10
|
|
|
*/ |
11
|
|
|
class MatchResult extends Match |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $fillable = [ |
17
|
|
|
'goal_home', |
18
|
|
|
'goal_away', |
19
|
|
|
'simulated', |
20
|
|
|
'winner_id', |
21
|
|
|
'loser_id', |
22
|
|
|
'is_draw' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
protected $hidden = [ |
26
|
|
|
'home_team_id', |
27
|
|
|
'away_team_id', |
28
|
|
|
'winner_id', |
29
|
|
|
'loser_id', |
30
|
|
|
'is_draw', |
31
|
|
|
'created_at', |
32
|
|
|
'updated_at' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $casts = [ |
39
|
|
|
'simulated' => 'boolean' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public static function resolveAttributes(array $attributes, $matchId) |
43
|
|
|
{ |
44
|
|
|
self::generateAppearances( |
45
|
|
|
[ |
46
|
|
|
$attributes['home_team_id'], |
47
|
|
|
$attributes['away_team_id'] |
48
|
|
|
], |
49
|
|
|
$matchId |
50
|
|
|
); |
51
|
|
|
$attributes = self::setResult($attributes); |
52
|
|
|
if (array_key_exists('info', $attributes)) { |
53
|
|
|
if (array_key_exists('scorers', $attributes['info'])) { |
54
|
|
|
foreach ($attributes['info']['scorers']['home'] as $scorerHome) { |
55
|
|
|
self::addScorer($matchId, $attributes['home_team_id'], $scorerHome->id); |
56
|
|
|
} |
57
|
|
|
foreach ($attributes['info']['scorers']['away'] as $scorerAway) { |
58
|
|
|
self::addScorer($matchId, $attributes['away_team_id'], $scorerAway->id); |
59
|
|
|
} |
60
|
|
|
unset($attributes['info']['scorers']); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
unset($attributes['info']); |
64
|
|
|
return $attributes; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private static function addScorer($matchId, $teamId, $playerId) |
68
|
|
|
{ |
69
|
|
|
$scorer = MatchPlayer::where( |
70
|
|
|
[ |
71
|
|
|
'match_id' => $matchId, |
72
|
|
|
'team_id' => $teamId, |
73
|
|
|
'player_id' => $playerId |
74
|
|
|
] |
75
|
|
|
)->first(); |
76
|
|
|
if (!empty($scorer)) { |
77
|
|
|
$scorer->goals = $scorer->goals + 1; |
78
|
|
|
$scorer->vote = $scorer->vote <= 9 ? $scorer->vote + rand(0, 1) : $scorer->vote; |
79
|
|
|
$scorer->save(); |
80
|
|
|
} else { |
81
|
|
|
MatchPlayer::create( |
82
|
|
|
[ |
83
|
|
|
'match_id' => $matchId, |
84
|
|
|
'team_id' => $teamId, |
85
|
|
|
'player_id' => $playerId, |
86
|
|
|
'goals' => 1 |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private static function generateAppearances( |
93
|
|
|
$teamIds, |
94
|
|
|
$matchId |
95
|
|
|
) |
96
|
|
|
{ |
97
|
|
|
foreach ($teamIds as $id) { |
98
|
|
|
$players = Player::where('team_id', $id)->get()->random(11); |
99
|
|
|
foreach ($players as $player) { |
100
|
|
|
MatchPlayer::create( |
101
|
|
|
[ |
102
|
|
|
'match_id' => $matchId, |
103
|
|
|
'team_id' => $id, |
104
|
|
|
'player_id' => $player->id, |
105
|
|
|
'vote' => Randomizer::voteFromSkill($player->skillAvg) |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private static function setResult($attributes) |
113
|
|
|
{ |
114
|
|
|
$toExtract = [ |
115
|
|
|
'winner_id' => 'winner_id', |
116
|
|
|
'loser_id' => 'loser_id', |
117
|
|
|
'is_draw' => 'is_draw' |
118
|
|
|
]; |
119
|
|
|
if (array_key_exists('info', $attributes)) { |
120
|
|
|
foreach ($toExtract as $key => $attr) { |
121
|
|
|
$attributes[$attr] = $attributes['info'][$key]; |
122
|
|
|
unset($attributes['info'][$key]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $attributes; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function scorers() |
129
|
|
|
{ |
130
|
|
|
return $this->belongsToMany( |
131
|
|
|
Player::class, |
132
|
|
|
'match_players', |
133
|
|
|
'match_id' |
134
|
|
|
)->withPivot( |
135
|
|
|
'team_id', |
136
|
|
|
'goals' |
137
|
|
|
)->where( |
138
|
|
|
'goals', '>', 0 |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function scopeComplete($query) |
143
|
|
|
{ |
144
|
|
|
return parent::scopeComplete($query)->with('scorers'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
} |