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