-
Notifications
You must be signed in to change notification settings - Fork 53
/
SingleEliminationFight.php
52 lines (48 loc) · 1.76 KB
/
SingleEliminationFight.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Xoco70\LaravelTournaments\Models;
class SingleEliminationFight extends Fight
{
public function __construct(Fight $fight = null)
{
parent::__construct();
if ($fight != null) {
$this->id = $fight->id;
$this->short_id = $fight->short_id;
$this->fighters_group_id = $fight->fighters_group_id;
$this->c1 = $fight->c1;
$this->c2 = $fight->c2;
}
}
/**
* @param Championship $championship
*/
public static function saveFights(Championship $championship, $fromRound = 1)
{
// dd("generating fights");
$round = [];
$groupsFromRound = $championship->groupsFromRound($fromRound)->get();
foreach ($groupsFromRound as $group) {
$fighters = parent::getFightersWithByes($group);
$away = $fighters->splice(count($fighters) / 2); // 2
$home = $fighters; // 1
$countHome = count($home);
$countAway = count($away);
for ($i = 0; $i < $countHome + $countAway - 1; $i++) {
for ($j = 0; $j < $countHome; $j++) {
$round[$i][$j]['Home'] = $home[$j];
$round[$i][$j]['Away'] = $away[$j];
$fight = new Fight();
$fight->fighters_group_id = $group->id;
$fight->c1 = $round[$i][$j]['Home']->id;
$fight->c2 = $round[$i][$j]['Away']->id;
$fight->area = $group->area;
$fight->save();
}
if ($countHome + $countAway - 1 > 2) {
$away->prepend($home->splice(1, 1)->shift());
$home->push($away->pop());
}
}
}
}
}