Last active
January 15, 2019 05:46
-
-
Save ak4bento/d53380cfceb6e3e225ec058dac08b5d7 to your computer and use it in GitHub Desktop.
After each game, the clubs records their score with the recordGame function. The club points in a match are calculated using the following logic : - The winner club will receive 3 points. - Both of clubs will receive 1 point, if the game ended in a draw. - The lost club won’t receive and lose any points.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: akill | |
* Date: 2019-01-15 | |
* Time: 11:25 | |
*/ | |
namespace App\Http\Controllers; | |
class LeagueTable | |
{ | |
public $club; | |
public function __construct(array $club) | |
{ | |
foreach ($club as $league) | |
{ | |
$this->club[$league] = 0; | |
} | |
} | |
public function recordGame($home, $away, $score) | |
{ | |
$resultScore = explode(':',$score); | |
if ($this->poin($resultScore) == "3") | |
{ | |
$this->club[$home] = (int)$this->club[$home] + 3; | |
$this->club[$away] = (int)$this->club[$away] + 0; | |
} | |
if ($this->poin($resultScore) == "1") | |
{ | |
$this->club[$home] = (int)$this->club[$home] + 1; | |
$this->club[$away] = (int)$this->club[$away] + 1; | |
} | |
if ($this->poin($resultScore) == "0") | |
{ | |
$this->club[$home] = (int)$this->club[$home] + 0; | |
$this->club[$away] = (int)$this->club[$away] + 3; | |
} | |
} | |
public function poin($result) | |
{ | |
if ((int) $result[0] > (int) $result[1]) | |
return "3"; | |
else if ((int) $result[0] == (int) $result[1]) | |
return "1"; | |
else | |
return "0"; | |
} | |
public function all() | |
{ | |
arsort($this->club); | |
return $this->club; | |
} | |
public function getRank($rank) | |
{ | |
$value = $this->all(); | |
$getKey = array_keys($value); | |
return $getKey[$rank-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment