Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Last active August 29, 2015 14:04
Show Gist options
  • Save Sigmus/0c60f5c9804ee03d8f2c to your computer and use it in GitHub Desktop.
Save Sigmus/0c60f5c9804ee03d8f2c to your computer and use it in GitHub Desktop.
<?php
class Scorer {
private $canonical;
private $game;
public function __construct($game)
{
$this->game = $game;
$this->canonical = Game::whereCanonical(true)
->whereStageId($this->game->stage_id)->first();
}
public function totalScore()
{
$scorableRounds = array(
'rd1', 'rd2', 'rd3', 'rd4', 'rd5', 'qf', 'sf', 'f');
$pointsMatrix = array(
'rd1' => 50, 'rd2' => 100, 'rd3' => 150, 'rd4' => 200,
'rd5' => 300, 'qf' => 450, 'sf' => 800, 'f' => 1000,
);
$points = 0;
foreach ($scorableRounds as $roundSlug)
{
$points += $this->score($roundSlug) * $pointsMatrix[$roundSlug];
}
return $points;
}
public function score($round_slug)
{
$heats_ids = Heat::getByRoundAndStage(
$round_slug, $this->game->stage_id
)->lists('id');
if ($this->canonical == null) {
return 0;
}
$intersect = array_intersect(
$this->surfersByGameAndHeats($this->canonical->id, $heats_ids),
$this->surfersByGameAndHeats($this->game->id, $heats_ids)
);
return count($intersect);
}
private function surfersByGameAndHeats($game_id, $heats_ids)
{
return Ride::whereGameId($game_id)
->whereIn('heat_id', $heats_ids)
->wherePoints(6)
->lists('surfer_id');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment