@@ -50,7 +50,7 @@ |
||
| 50 | 50 | |
| 51 | 51 | $this->splitTeams($round1); |
| 52 | 52 | |
| 53 | - if (count($this->getTeams()) % 4 == 2) { |
|
| 53 | + if (count($this->getTeams())%4 == 2) { |
|
| 54 | 54 | $group_top = $round2->group([ |
| 55 | 55 | 'name' => 'TOP', |
| 56 | 56 | 'type' => \TWO_TWO |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | $groupIds = []; |
| 32 | 32 | $allGroups = []; |
| 33 | 33 | |
| 34 | - for ($i=1; $i <= $startGroups; $i++) { |
|
| 34 | + for ($i = 1; $i <= $startGroups; $i++) { |
|
| 35 | 35 | $g = $startRound->group([ |
| 36 | 36 | 'name' => 'Start group - '.$i, |
| 37 | 37 | 'inGame' => 2, |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | // SPLIT TEAMS EVENLY |
| 46 | 46 | $this->splitTeams(); |
| 47 | 47 | |
| 48 | - for ($r=2; $r <= $roundsNum-1; $r++) { |
|
| 48 | + for ($r = 2; $r <= $roundsNum-1; $r++) { |
|
| 49 | 49 | $groups = []; |
| 50 | 50 | $losingGroups = []; |
| 51 | 51 | $round = $this->round('Round '.$r); |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | private function calcByes(int $countTeams, int &$nextPow) { |
| 90 | 90 | $byes = 0; |
| 91 | 91 | $nextPow = $countTeams; |
| 92 | - if ( !\TournamentGenerator\isPowerOf2($countTeams) ) { |
|
| 92 | + if (!\TournamentGenerator\isPowerOf2($countTeams)) { |
|
| 93 | 93 | $nextPow = bindec(str_pad(1, strlen(decbin($countTeams))+1, 0, STR_PAD_RIGHT)); |
| 94 | 94 | $byes = $nextPow-$countTeams; |
| 95 | 95 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | private function generateWinSide(int &$r, int &$byes, int &$countTeams, \TournamentGenerator\Round &$round, array &$allGroups, array &$groups, \TournamentGenerator\Group &$lastWinningGroup = null, array &$previousGroups = []) { |
| 99 | 99 | $order = 1; |
| 100 | - for ($g=1; $g <= (($countTeams+$byes)/pow(2, $r)); $g++) { |
|
| 100 | + for ($g = 1; $g <= (($countTeams+$byes)/pow(2, $r)); $g++) { |
|
| 101 | 101 | $group = $round->group([ |
| 102 | 102 | 'name' => 'Round '.$r.' - win '.$g, |
| 103 | 103 | 'inGame' => 2, |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $losingGroupTeamsCount = count($previousLosingGroups)+count($previousGroups); |
| 118 | 118 | $order = 2; |
| 119 | 119 | if (\TournamentGenerator\isPowerOf2($losingGroupTeamsCount)) { // IF THE NUMBER OF TEAMS IS A POWER OF 2, GENERATE GROUPS WITHOUT BYES |
| 120 | - for ($g=1; $g <= $losingGroupTeamsCount/2; $g++) { |
|
| 120 | + for ($g = 1; $g <= $losingGroupTeamsCount/2; $g++) { |
|
| 121 | 121 | $group = $round->group([ |
| 122 | 122 | 'name' => 'Round '.$r.' - loss '.$g, |
| 123 | 123 | 'inGame' => 2, |
@@ -145,11 +145,11 @@ discard block |
||
| 145 | 145 | $n = (floor(count($previousLosingGroups)/2)+$losingByes); |
| 146 | 146 | $byesGroupsNums = []; |
| 147 | 147 | $byesProgressed = 0; |
| 148 | - for ($i=0; $i < $losingByes; $i++) { |
|
| 148 | + for ($i = 0; $i < $losingByes; $i++) { |
|
| 149 | 149 | $byesGroupsNums[] = $n-($i*2); |
| 150 | 150 | } |
| 151 | 151 | $lastGroup = 0; |
| 152 | - for ($g=1; $g <= ((count($previousLosingGroups)/2)+$losingByes); $g++) { |
|
| 152 | + for ($g = 1; $g <= ((count($previousLosingGroups)/2)+$losingByes); $g++) { |
|
| 153 | 153 | $group = $round->group([ |
| 154 | 154 | 'name' => 'Round '.$r.' - loss '.$g, |
| 155 | 155 | 'inGame' => 2, |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | else { |
| 168 | 168 | $previousLosingGroups[$lastGroup]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE |
| 169 | - if (isset($previousLosingGroups[$lastGroup + 1])) $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); // PROGREESS FROM LOSING GROUP BEFORE |
|
| 169 | + if (isset($previousLosingGroups[$lastGroup+1])) $previousLosingGroups[$lastGroup+1]->progression($group, 0, 1); // PROGREESS FROM LOSING GROUP BEFORE |
|
| 170 | 170 | $lastGroup += 2; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -13,7 +13,9 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | $countTeams = count($this->getTeams()); |
| 15 | 15 | |
| 16 | - if ($countTeams < 3) throw new \Exception('Double elimination is possible for minimum of 3 teams - '.$countTeams.' teams given.'); |
|
| 16 | + if ($countTeams < 3) { |
|
| 17 | + throw new \Exception('Double elimination is possible for minimum of 3 teams - '.$countTeams.' teams given.'); |
|
| 18 | + } |
|
| 17 | 19 | |
| 18 | 20 | |
| 19 | 21 | // CALCULATE BYES |
@@ -131,15 +133,19 @@ discard block |
||
| 131 | 133 | if ($r === 2) { // FIRST LOSING ROUND |
| 132 | 134 | $previousGroups[2*($g-1)]->progression($group, 1, 1); // PROGRESS FROM STARTING GROUP |
| 133 | 135 | $previousGroups[(2*($g-1))+1]->progression($group, 1, 1); // PROGREESS FROM STARTING GROUP |
| 134 | - } |
|
| 135 | - elseif ($losingGroupTeamsCount >= 2) { |
|
| 136 | + } elseif ($losingGroupTeamsCount >= 2) { |
|
| 136 | 137 | $previousLosingGroups[$g-1]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE |
| 137 | - if (isset(array_reverse($previousGroups)[$g-1])) array_reverse($previousGroups)[$g-1]->progression($group, 1, 1); // PROGREESS FROM WINNING GROUP BEFORE |
|
| 138 | - else $previousLosingGroups[$g]->progression($group, 0, 1); // PROGRESS OTHER TEAM FROM LOSING GROUP BEEFORE |
|
| 138 | + if (isset(array_reverse($previousGroups)[$g-1])) { |
|
| 139 | + array_reverse($previousGroups)[$g-1]->progression($group, 1, 1); |
|
| 140 | + } |
|
| 141 | + // PROGREESS FROM WINNING GROUP BEFORE |
|
| 142 | + else { |
|
| 143 | + $previousLosingGroups[$g]->progression($group, 0, 1); |
|
| 144 | + } |
|
| 145 | + // PROGRESS OTHER TEAM FROM LOSING GROUP BEEFORE |
|
| 139 | 146 | } |
| 140 | 147 | } |
| 141 | - } |
|
| 142 | - else { // IF THE NUMBER OF TEAMS IS NOT A POWER OF 2, GENERATE GROUPS WITH BYES |
|
| 148 | + } else { // IF THE NUMBER OF TEAMS IS NOT A POWER OF 2, GENERATE GROUPS WITH BYES |
|
| 143 | 149 | // LOOK FOR THE CLOSEST LOWER POWER OF 2 |
| 144 | 150 | $losingByes = $losingGroupTeamsCount-bindec(str_pad(1, strlen(decbin($losingGroupTeamsCount)), 0, STR_PAD_RIGHT)); |
| 145 | 151 | $n = (floor(count($previousLosingGroups)/2)+$losingByes); |
@@ -163,10 +169,12 @@ discard block |
||
| 163 | 169 | if (in_array($g, $byesGroupsNums) && isset($previousGroups[$byesProgressed])) { // EMPTY GROUP FROM BYE |
| 164 | 170 | $previousGroups[$byesProgressed]->progression($group, 1, 1); // PROGRESS FROM WINNING GROUP BEFORE |
| 165 | 171 | $byesProgressed++; |
| 166 | - } |
|
| 167 | - else { |
|
| 172 | + } else { |
|
| 168 | 173 | $previousLosingGroups[$lastGroup]->progression($group, 0, 1); // PROGRESS FROM LOSING GROUP BEFORE |
| 169 | - if (isset($previousLosingGroups[$lastGroup + 1])) $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); // PROGREESS FROM LOSING GROUP BEFORE |
|
| 174 | + if (isset($previousLosingGroups[$lastGroup + 1])) { |
|
| 175 | + $previousLosingGroups[$lastGroup + 1]->progression($group, 0, 1); |
|
| 176 | + } |
|
| 177 | + // PROGREESS FROM LOSING GROUP BEFORE |
|
| 170 | 178 | $lastGroup += 2; |
| 171 | 179 | } |
| 172 | 180 | } |
@@ -44,11 +44,19 @@ discard block |
||
| 44 | 44 | private $groups = []; |
| 45 | 45 | |
| 46 | 46 | function __construct(string $what = 'points', string $how = '>', $val = 0, $groups = []){ |
| 47 | - if (in_array(strtolower($what), ['points', 'score', 'wins', 'draws', 'losses', 'second', 'third', 'team', 'notprogressed', 'progressed'])) $this->what = strtolower($what); |
|
| 48 | - if (in_array($how, ['>', '<', '>=', '<=', '=', '!='])) $this->how = $how; |
|
| 49 | - if ((gettype($val) === 'integer' && strtolower($what) !== 'team') || ($val instanceof Team && strtolower($what) === 'team')) $this->val = $val; |
|
| 47 | + if (in_array(strtolower($what), ['points', 'score', 'wins', 'draws', 'losses', 'second', 'third', 'team', 'notprogressed', 'progressed'])) { |
|
| 48 | + $this->what = strtolower($what); |
|
| 49 | + } |
|
| 50 | + if (in_array($how, ['>', '<', '>=', '<=', '=', '!='])) { |
|
| 51 | + $this->how = $how; |
|
| 52 | + } |
|
| 53 | + if ((gettype($val) === 'integer' && strtolower($what) !== 'team') || ($val instanceof Team && strtolower($what) === 'team')) { |
|
| 54 | + $this->val = $val; |
|
| 55 | + } |
|
| 50 | 56 | foreach ($groups as $group) { |
| 51 | - if ($group instanceof Group) $this->groups[] = $group->id; |
|
| 57 | + if ($group instanceof Group) { |
|
| 58 | + $this->groups[] = $group->id; |
|
| 59 | + } |
|
| 52 | 60 | } |
| 53 | 61 | } |
| 54 | 62 | public function __toString() { |
@@ -56,14 +64,14 @@ discard block |
||
| 56 | 64 | } |
| 57 | 65 | |
| 58 | 66 | public function validate(Team $team, $groupsId, string $operation = 'sum', Group $from = null) { |
| 59 | - if (count($this->groups) > 0) $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
| 67 | + if (count($this->groups) > 0) { |
|
| 68 | + $groupsId = array_unique(array_merge($this->groups, (gettype($groupsId) === 'array' ? $groupsId : [$groupsId])), SORT_REGULAR); |
|
| 69 | + } |
|
| 60 | 70 | if ($this->what == 'team') { |
| 61 | 71 | return $this->validateTeam($team); |
| 62 | - } |
|
| 63 | - elseif ($this->what == 'notprogressed') { |
|
| 72 | + } elseif ($this->what == 'notprogressed') { |
|
| 64 | 73 | return !$this->validateProgressed($team, $from); |
| 65 | - } |
|
| 66 | - elseif ($this->what == 'progressed') { |
|
| 74 | + } elseif ($this->what == 'progressed') { |
|
| 67 | 75 | return $this->validateProgressed($team, $from); |
| 68 | 76 | } |
| 69 | 77 | return $this->validateCalc($team, $groupsId, $operation, $from); |
@@ -72,30 +80,45 @@ discard block |
||
| 72 | 80 | private function validateTeam(Team $team) { |
| 73 | 81 | switch ($this->how) { |
| 74 | 82 | case '=': |
| 75 | - if ($this->val === $team) return true; |
|
| 83 | + if ($this->val === $team) { |
|
| 84 | + return true; |
|
| 85 | + } |
|
| 76 | 86 | break; |
| 77 | 87 | case '!=': |
| 78 | - if ($this->val !== $team) return true; |
|
| 88 | + if ($this->val !== $team) { |
|
| 89 | + return true; |
|
| 90 | + } |
|
| 79 | 91 | break; |
| 80 | 92 | } |
| 81 | 93 | return false; |
| 82 | 94 | } |
| 83 | 95 | private function validateProgressed(Team $team, Group $from) { |
| 84 | - if ($from === null) throw new \Exception('Group $from was not defined.'); |
|
| 96 | + if ($from === null) { |
|
| 97 | + throw new \Exception('Group $from was not defined.'); |
|
| 98 | + } |
|
| 85 | 99 | return $from->isProgressed($team); |
| 86 | 100 | } |
| 87 | 101 | private function validateCalc(Team $team, $groupsId, string $operation = 'sum', Group $from = null) { |
| 88 | - if (gettype($groupsId) === 'array' && !in_array(strtolower($operation), ['sum', 'avg', 'max', 'min'])) throw new \Exception('Unknown operation of '.$operation.'. Only "sum", "avg", "min", "max" possible.'); |
|
| 102 | + if (gettype($groupsId) === 'array' && !in_array(strtolower($operation), ['sum', 'avg', 'max', 'min'])) { |
|
| 103 | + throw new \Exception('Unknown operation of '.$operation.'. Only "sum", "avg", "min", "max" possible.'); |
|
| 104 | + } |
|
| 89 | 105 | $comp = 0; |
| 90 | 106 | if (gettype($groupsId) === 'array' && count($groupsId) > 0) { |
| 91 | 107 | $sum = 0; |
| 92 | 108 | $max = null; |
| 93 | 109 | $min = null; |
| 94 | 110 | foreach ($groupsId as $id) { |
| 95 | - if (!isset($team->groupResults[$id])) continue; // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP |
|
| 111 | + if (!isset($team->groupResults[$id])) { |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | + // IF TEAM DIDN'T PLAY IN THAT GROUP -> SKIP |
|
| 96 | 115 | $sum += $team->groupResults[$id][$this->what]; |
| 97 | - if ($team->groupResults[$id][$this->what] > $max || $max === null) $max = $team->groupResults[$id][$this->what]; |
|
| 98 | - if ($team->groupResults[$id][$this->what] < $min || $min === null) $min = $team->groupResults[$id][$this->what]; |
|
| 116 | + if ($team->groupResults[$id][$this->what] > $max || $max === null) { |
|
| 117 | + $max = $team->groupResults[$id][$this->what]; |
|
| 118 | + } |
|
| 119 | + if ($team->groupResults[$id][$this->what] < $min || $min === null) { |
|
| 120 | + $min = $team->groupResults[$id][$this->what]; |
|
| 121 | + } |
|
| 99 | 122 | } |
| 100 | 123 | switch (strtolower($operation)) { |
| 101 | 124 | case 'sum': |
@@ -111,9 +134,11 @@ discard block |
||
| 111 | 134 | $comp = $min; |
| 112 | 135 | break; |
| 113 | 136 | } |
| 137 | + } elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) { |
|
| 138 | + $comp = $team->groupResults[$groupsId][$this->what]; |
|
| 139 | + } else { |
|
| 140 | + throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); |
|
| 114 | 141 | } |
| 115 | - elseif (gettype($groupsId) === 'string' && isset($team->groupResults[$groupsId])) $comp = $team->groupResults[$groupsId][$this->what]; |
|
| 116 | - else throw new \Exception("Couldn't find group of id ".print_r($groupsId, true)); |
|
| 117 | 142 | |
| 118 | 143 | switch ($this->how) { |
| 119 | 144 | case '>': return ($comp > $this->val); |