Skip to content

Commit

Permalink
Moving order of teams to TeamTable, as well as small tables
Browse files Browse the repository at this point in the history
  • Loading branch information
WildaSoftware committed Dec 4, 2022
1 parent c3329c5 commit 069c944
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 40 deletions.
72 changes: 35 additions & 37 deletions Group.php
@@ -1,6 +1,7 @@
<?php
require_once('Team.php');
require_once('MatchDTO.php');
require_once('TeamTable.php');

class Group {

Expand All @@ -19,7 +20,7 @@ public function __construct(string $matchesFile) {
$this->matches = json_decode($fileContent, true);
}

public function sumResults($restrictedTeamSymbols = []) {
public function sumResults($restrictedTeamSymbols = []): TeamTable {
$teams = [];
foreach($this->matches as $match) {
$matchDto = new MatchDTO($match);
Expand All @@ -45,45 +46,44 @@ public function sumResults($restrictedTeamSymbols = []) {
$teams[$b]->addMatchResult($teamB, $teamA);
}

return $teams;
return new TeamTable(array_keys($teams), array_values($teams));
}

public function calculateOrder($teams, $verifyFairPlay = false) {
$order = array_keys($teams);
public function calculateOrder(TeamTable $table, bool $verifyFairPlay = false) {
$smallTables = [];
for($i = 0; $i < count($order) - 1; ++$i) {
for($j = $i + 1; $j < count($order); ++$j) {

for($i = 0; $i < count($table->getTeams()) - 1; ++$i) {
for($j = $i + 1; $j < count($table->getTeams()); ++$j) {
if($i == $j) continue;

$team1 = $teams[$order[$i]];
$team2 = $teams[$order[$j]];
$team1 = $table->getTeams()[$i];
$team2 = $table->getTeams()[$j];

if($team2->points > $team1->points) {
$this->swapOrder($order, $i, $j);
$table->swapOrder($i, $j);
}
elseif($team2->points == $team1->points) {
if($team2->goalsBalance > $team1->goalsBalance) {
$this->swapOrder($order, $i, $j);
$table->swapOrder($i, $j);
}
elseif($team2->goalsBalance == $team1->goalsBalance) {
if($team2->scoredGoals > $team1->scoredGoals) {
$this->swapOrder($order, $i, $j);
$table->swapOrder($i, $j);
}
elseif($team2->scoredGoals == $team1->scoredGoals) {
if($verifyFairPlay) {
if($team2->fairPlay > $team1->fairPlay) {
$this->swapOrder($order, $i, $j);
$table->swapOrder($i, $j);
}
}
else {
$hash = md5($team2->points.$team2->goalsBalance.$team2->scoredGoals);
if(!array_key_exists($hash, $smallTables)) {
$smallTables[$hash] = [];
$smallTables[$hash] = new TeamTable();
}

$smallTables[$hash][] = $team1->symbol;
$smallTables[$hash][] = $team2->symbol;
$smallTables[$hash]->addTeam($team1);
$smallTables[$hash]->addTeam($team2);
}
}
}
Expand All @@ -92,52 +92,50 @@ public function calculateOrder($teams, $verifyFairPlay = false) {
}

if(!empty($smallTables)) {
foreach($smallTables as $hash => $table) {
$subTeams = $this->sumResults($table);
$newOrder = $this->calculateOrder($subTeams, true);
$smallTables[$hash] = $newOrder;
foreach($smallTables as $hash => $smallTable) {
$restrictedTable = $this->sumResults($smallTable->getTeamSymbols());
$orderedSmallTableSymbols = $this->calculateOrder($restrictedTable, true);
$smallTables[$hash]->reorderBySymbols($orderedSmallTableSymbols);
}
}

$result = [];
$visitedTeams = [];
for($i = 0; $i < count($order); ++$i) {
if(in_array($order[$i], $visitedTeams)) continue;
$visitedSymbols = [];
$teams = $table->getTeams();

for($i = 0; $i < count($teams); ++$i) {
if(in_array($teams[$i]->symbol, $visitedSymbols)) continue;

$smallTableIncludingThisTeam = null;
foreach($smallTables as $smallTable) {
if(in_array($order[$i], $smallTable)) {
if(in_array($teams[$i]->symbol, $smallTable->getTeamSymbols())) {
$smallTableIncludingThisTeam = $smallTable;
break;
}
}

if(!empty($smallTableIncludingThisTeam)) {
foreach($smallTableIncludingThisTeam as $team) {
if(in_array($team, $visitedTeams)) continue;
foreach($smallTableIncludingThisTeam->getTeamSymbols() as $symbol) {
if(in_array($symbol, $visitedSymbols)) continue;

$result[] = $team;
$visitedTeams[] = $team;
$result[] = $symbol;
$visitedSymbols[] = $symbol;
}
}
else {
$result[] = $order[$i];
$visitedTeams[] = $order[$i];
$result[] = $teams[$i]->symbol;
$visitedTeams[] = $teams[$i]->symbols;
}
}

return $result;
}

public function printResults($teams, $order) {
public function printResults($teamTable, $order) {
$i = 1;
foreach($order as $idx) {
$team = $teams[$idx];
foreach($order as $symbol) {
$team = $teamTable->getTeamBySymbol($symbol);
echo ($i++).". $team->symbol - $team->points pkt., $team->scoredGoals-$team->concededGoals ($team->fairPlay)\n";
}
}

private function swapOrder(&$array, $i, $j) {
list($array[$i], $array[$j]) = [$array[$j], $array[$i]];
}
}
52 changes: 52 additions & 0 deletions TeamTable.php
@@ -0,0 +1,52 @@
<?php
require_once('Team.php');

class TeamTable {

private $teamSymbols = [];
private $teams = [];

public function __construct(array $teamSymbols = [], array $teams = []) {
$this->teamSymbols = $teamSymbols;
$this->teams = $teams;
}

public function addTeam(Team $team) {
$this->teamSymbols[] = $team->symbol;
$this->teams[] = $team;
}

public function getTeamSymbols() {
return $this->teamSymbols;
}

public function getTeams() {
return $this->teams;
}

public function getTeamBySymbol(string $symbol): Team {
for($i = 0; $i < count($this->teamSymbols); ++$i) {
if($this->teamSymbols[$i] == $symbol) {
return $this->teams[$i];
}
}

return null;
}

public function swapOrder($i, $j) {
list($this->teamSymbols[$i], $this->teamSymbols[$j]) = [$this->teamSymbols[$j], $this->teamSymbols[$i]];
list($this->teams[$i], $this->teams[$j]) = [$this->teams[$j], $this->teams[$i]];
}

public function reorderBySymbols(array $symbols) {
$this->teamSymbols = $symbols;
$newTeams = [];

foreach($symbols as $symbol) {
$newTeams[] = $this->getTeamBySymbol($symbol);
}

$this->teams[] = $newTeams;
}
}
6 changes: 3 additions & 3 deletions index.php
Expand Up @@ -7,9 +7,9 @@
}

$group = new Group($argv[1]);
$teams = $group->sumResults();
$order = $group->calculateOrder($teams);
$group->printResults($teams, $order);
$teamTable = $group->sumResults();
$order = $group->calculateOrder($teamTable);
$group->printResults($teamTable, $order);
}
catch(Exception $e) {
echo $e->getMessage()."\n";
Expand Down

0 comments on commit 069c944

Please sign in to comment.