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