|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Xoco70\KendoTournaments\Models; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use App\User; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class Fight extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Fight constructor. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct($userId1 = null, $userId2 = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->c1 = $userId1; |
|
19
|
|
|
$this->c2 = $userId2; |
|
20
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected $table = 'fight'; |
|
24
|
|
|
public $timestamps = true; |
|
25
|
|
|
|
|
26
|
|
|
protected $fillable = [ |
|
27
|
|
|
'tree_id', |
|
28
|
|
|
'c1', |
|
29
|
|
|
'c2' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Championship $championship |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
private static function getActorsToFights(Championship $championship, Round $round = null) |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
if ($championship->category->isTeam) { |
|
40
|
|
|
$fighters = $round->teams; |
|
41
|
|
|
if (sizeof($fighters) % 2 != 0) { |
|
42
|
|
|
$fighters->push(new Team(['name' => "BYE"])); |
|
43
|
|
|
} |
|
44
|
|
|
} else { |
|
45
|
|
|
$fighters = $round->competitors; |
|
46
|
|
|
if (sizeof($fighters) % 2 != 0) { |
|
47
|
|
|
$fighters->push(new Competitor()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
return $fighters; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get First Fighter |
|
55
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
56
|
|
|
*/ |
|
57
|
|
|
public function competitor1() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->belongsTo(Competitor::class, 'c1', 'id'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get Second Fighter |
|
64
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
65
|
|
|
*/ |
|
66
|
|
|
public function competitor2() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->belongsTo(Competitor::class, 'c2', 'id'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get First Fighter |
|
73
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
74
|
|
|
*/ |
|
75
|
|
|
public function team1() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->belongsTo(Team::class, 'c1', 'id'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get Second Fighter |
|
82
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
83
|
|
|
*/ |
|
84
|
|
|
public function team2() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->belongsTo(Team::class, 'c2', 'id'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Save a Fight. |
|
91
|
|
|
* @param $rounds |
|
92
|
|
|
* @param int $numRound |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function savePreliminaryFightRound($rounds, $numRound = 1) |
|
95
|
|
|
{ |
|
96
|
|
|
|
|
97
|
|
|
$c1 = $c2 = $c3 = null; |
|
|
|
|
|
|
98
|
|
|
$order = 0; |
|
99
|
|
|
|
|
100
|
|
|
foreach ($rounds as $round) { |
|
101
|
|
|
if ($round->championship->isTeam) { |
|
102
|
|
|
$fighter1 = isset($round->teams[0]) ? $round->teams[0] : null; |
|
103
|
|
|
$fighter2 = isset($round->teams[1]) ? $round->teams[1] : null; |
|
104
|
|
|
$fighter3 = isset($round->teams[2]) ? $round->teams[2] : null; |
|
105
|
|
|
} else { |
|
106
|
|
|
$fighter1 = isset($round->competitors[0]) ? $round->competitors[0] : null; |
|
107
|
|
|
$fighter2 = isset($round->competitors[1]) ? $round->competitors[1] : null; |
|
108
|
|
|
$fighter3 = isset($round->competitors[2]) ? $round->competitors[2] : null; |
|
109
|
|
|
} |
|
110
|
|
|
switch ($numRound) { |
|
111
|
|
|
case 1: |
|
112
|
|
|
$c1 = $fighter1; |
|
113
|
|
|
$c2 = $fighter2; |
|
114
|
|
|
break; |
|
115
|
|
|
case 2: |
|
116
|
|
|
$c1 = $fighter2; |
|
117
|
|
|
$c2 = $fighter3; |
|
118
|
|
|
break; |
|
119
|
|
|
case 3: |
|
120
|
|
|
$c1 = $fighter3; |
|
121
|
|
|
$c2 = $fighter1; |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
$fight = new Fight(); |
|
125
|
|
|
$fight->round_id = $round->id; |
|
126
|
|
|
$fight->c1 = $c1 != null ? $c1->id : null; |
|
127
|
|
|
$fight->c2 = $c2 != null ? $c2->id : null; |
|
128
|
|
|
$fight->order = $order++; |
|
129
|
|
|
$fight->area = $round->area; |
|
130
|
|
|
$fight->save(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
// public static function saveRoundRobinFight(Championship $championship, $tree) |
|
|
|
|
|
|
136
|
|
|
// { |
|
137
|
|
|
// $championship->category->isTeam |
|
138
|
|
|
// ? $fighters = $championship->teams |
|
139
|
|
|
// : $fighters = $championship->users; |
|
140
|
|
|
// |
|
141
|
|
|
// $reverseFighters = $fighters->reverse(); |
|
142
|
|
|
// $order = 0; |
|
143
|
|
|
// foreach ($reverseFighters as $reverse) { |
|
144
|
|
|
// foreach ($fighters as $fighter) { |
|
145
|
|
|
// if ($fighter->id != $reverse->id) { |
|
146
|
|
|
// $fight = new Fight(); |
|
147
|
|
|
// $fight->tree_id = $tree[0]->id; |
|
148
|
|
|
// $fight->c1 = $fighter->id; |
|
149
|
|
|
// $fight->c2 = $reverse->id; |
|
150
|
|
|
// $fight->order = $order++; |
|
151
|
|
|
// $fight->area = 1; |
|
152
|
|
|
// $fight->save(); |
|
153
|
|
|
// $order++; |
|
154
|
|
|
// } |
|
155
|
|
|
// } |
|
156
|
|
|
// } |
|
157
|
|
|
// } |
|
158
|
|
|
|
|
159
|
|
|
public static function saveRoundRobinFights(Championship $championship, $rounds) |
|
160
|
|
|
{ |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
foreach ($rounds as $round2) { |
|
164
|
|
|
|
|
165
|
|
|
$fighters = self::getActorsToFights($championship, $round2); |
|
166
|
|
|
|
|
167
|
|
|
$away = $fighters->splice(sizeof($fighters) / 2); // 2 |
|
168
|
|
|
|
|
169
|
|
|
$home = $fighters; // 1 |
|
170
|
|
|
|
|
171
|
|
|
$order = 1; |
|
172
|
|
|
|
|
173
|
|
|
for ($i = 0; $i < sizeof($home) + sizeof($away) - 1; $i++) { // 0 -> 2 |
|
|
|
|
|
|
174
|
|
|
for ($j = 0; $j < sizeof($home); $j++) { // 1 no mas |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
$round[$i][$j]["Home"] = $home[$j]; |
|
|
|
|
|
|
177
|
|
|
$round[$i][$j]["Away"] = $away[$j]; |
|
|
|
|
|
|
178
|
|
|
$fight = new Fight(); |
|
179
|
|
|
$fight->round_id = $rounds[0]->id; |
|
180
|
|
|
$fight->c1 = $round[$i][$j]["Home"]->id; |
|
181
|
|
|
$fight->c2 = $round[$i][$j]["Away"]->id; |
|
182
|
|
|
$fight->order = $order++; |
|
183
|
|
|
$fight->area = 1; |
|
184
|
|
|
|
|
185
|
|
|
// We ommit fights that have a BYE in Round robins, but not in Preliminary |
|
186
|
|
|
|
|
187
|
|
|
if ($fight->c1 != null && $fight->c2 != null || !$championship->isRoundRobinType()) { |
|
188
|
|
|
$fight->save(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
if (sizeof($home) + sizeof($away) - 1 > 2) { |
|
192
|
|
|
$away->prepend($home->splice(1, 1)->shift()); |
|
193
|
|
|
$home->push($away->pop()); |
|
194
|
|
|
$order++; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
// return $round; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.