1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Lib\DsManager\Helpers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\Lib\DsManager\Models\Orm\LeagueRound; |
8
|
|
|
use App\Lib\DsManager\Models\Orm\Match; |
9
|
|
|
use App\Lib\DsManager\Models\Orm\Match as MatchOrm; |
10
|
|
|
use App\Lib\DsManager\Models\Orm\MatchResult; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MatchSimulator |
14
|
|
|
* @package App\Lib\DsManager\Helpers |
15
|
|
|
*/ |
16
|
|
|
class MatchSimulator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param $roundId |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function simulateRound($roundId) |
23
|
|
|
{ |
24
|
|
|
$result = self::getCompleteRound($roundId); |
25
|
|
|
if (!empty($result) |
26
|
|
|
&& |
27
|
|
|
!$result->simulated |
28
|
|
|
) { |
29
|
|
|
$matches = Match::where( |
30
|
|
|
[ |
31
|
|
|
'league_round_id' => $roundId |
32
|
|
|
] |
33
|
|
|
)->get(); |
34
|
|
|
foreach ($matches as $match) { |
35
|
|
|
self::simulateSimpleResult($match->id)->toArray(); |
36
|
|
|
} |
37
|
|
|
LeagueRound::find($roundId)->update(['simulated' => true]); |
38
|
|
|
$result = self::getCompleteRound($roundId); |
39
|
|
|
} |
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $matchId |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public static function simulateCompleteResult($matchId) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$result = self::getCompleteResult($matchId); |
50
|
|
|
if (!empty($result) |
51
|
|
|
&& !$result->simulated |
|
|
|
|
52
|
|
|
&& self::simulate($matchId) === 1 |
53
|
|
|
) { |
54
|
|
|
$result = self::getCompleteResult($matchId); |
55
|
|
|
} |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $matchId |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public static function simulateSimpleResult($matchId) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$result = self::getSimpleResult($matchId); |
66
|
|
|
if (!empty($result) |
67
|
|
|
&& !$result->simulated |
|
|
|
|
68
|
|
|
&& self::simulate($matchId) === 1 |
69
|
|
|
) { |
70
|
|
|
$result = self::getSimpleResult($matchId); |
71
|
|
|
} |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $matchId |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
private static function simulate($matchId) |
80
|
|
|
{ |
81
|
|
|
$match = \App\Lib\DsManager\Models\Match::fromArray( |
82
|
|
|
MatchOrm::complete() |
|
|
|
|
83
|
|
|
->where( |
84
|
|
|
[ |
85
|
|
|
'id' => $matchId |
86
|
|
|
] |
87
|
|
|
)->first()->toArray() |
88
|
|
|
); |
89
|
|
|
$matchResult = $match->simulate()->toArray(); |
90
|
|
|
$result = MatchResult::where( |
91
|
|
|
[ |
92
|
|
|
'id' => $matchId |
93
|
|
|
] |
94
|
|
|
)->update( |
95
|
|
|
MatchResult::resolveAttributes( |
96
|
|
|
$matchResult, |
97
|
|
|
$matchId |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $matchId |
106
|
|
|
* @return MatchResult |
107
|
|
|
*/ |
108
|
|
|
private static function getCompleteResult($matchId) |
109
|
|
|
{ |
110
|
|
|
return MatchResult::complete()->where( |
111
|
|
|
[ |
112
|
|
|
'id' => $matchId |
113
|
|
|
] |
114
|
|
|
)->first(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $matchId |
119
|
|
|
* @return MatchResult |
120
|
|
|
*/ |
121
|
|
|
private static function getSimpleResult($matchId) |
122
|
|
|
{ |
123
|
|
|
return MatchResult::teams()->where( |
124
|
|
|
[ |
125
|
|
|
'id' => $matchId |
126
|
|
|
] |
127
|
|
|
)->first(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $roundId |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
private static function getCompleteRound($roundId) |
135
|
|
|
{ |
136
|
|
|
return LeagueRound::complete()->where( |
|
|
|
|
137
|
|
|
[ |
138
|
|
|
'id' => $roundId |
139
|
|
|
] |
140
|
|
|
)->first(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.