Created
March 14, 2017 00:02
-
-
Save chartjes/7849bf48580c67462041530d3ecec769 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class Game extends Model { | |
public $name = 'Game'; | |
public $belongsTo = array( | |
'HomeTeam' => array( | |
'className' => 'Franchise', | |
), | |
'AwayTeam' => array( | |
'className' => 'Franchise') | |
); | |
public $sequence = 'games_id_seq'; | |
public $actAs = array('Containable'); | |
/** | |
* Build result set for editing in the admin | |
*/ | |
public function getByWeek($week) | |
{ | |
$this->scheduleTable = Configure::read('Schedule.table'); | |
$this->teamsTable = Configure::read('Team.table'); | |
$sql = " | |
SELECT | |
Schedule.*, | |
ScheduleHomeTeam.ibl AS HomeTeamIbl, | |
ScheduleHomeTeam.code AS HomeTeamCode, | |
ScheduleAwayTeam.ibl AS AwayTeamIbl, | |
ScheduleAwayTeam.code AS AwayTeamCode | |
FROM {$this->scheduleTable} AS Schedule | |
LEFT JOIN {$this->teamsTable} AS ScheduleHomeTeam ON ScheduleHomeTeam.code = Schedule.home | |
LEFT JOIN {$this->teamsTable} AS ScheduleAwayTeam ON ScheduleAwayTeam.code = Schedule.away | |
WHERE Schedule.week = {$week} | |
ORDER BY ScheduleHomeTeam.ibl, ScheduleAwayTeam.ibl"; | |
$results = $this->query($sql); | |
/** | |
* Loop through the schedue for the week and grab scores | |
* if they already exist | |
*/ | |
foreach ($results as $idx => $result) { | |
// Need the ID's of the teams | |
$sql = "SELECT id FROM franchises WHERE nickname = '{$result[0]['hometeamibl']}' LIMIT 1"; | |
$data = $this->query($sql); | |
$homeTeamId = $data[0][0]['id']; | |
$sql = "SELECT id FROM franchises WHERE nickname = '{$result[0]['awayteamibl']}' LIMIT 1"; | |
$data = $this->query($sql); | |
$awayTeamId = $data[0][0]['id']; | |
$gameResults = $this->find('all', array( | |
'conditions' => array( | |
'Game.week' => $week, | |
'Game.home_team_id' => $homeTeamId, | |
'Game.away_team_id' => $awayTeamId), | |
'fields' => array( | |
'Game.home_score', | |
'Game.away_score'), | |
'recursive' => -1 | |
)); | |
if ($gameResults) { | |
$awayScores = array(); | |
$homeScores = array(); | |
foreach ($gameResults as $gameResult) { | |
$awayScores[] = $gameResult['Game']['away_score']; | |
$homeScores[] = $gameResult['Game']['home_score']; | |
} | |
$results[$idx]['gamescores'] = array( | |
'awayScores' => $awayScores, | |
'homeScores' => $homeScores, | |
'awayTeamId' => $awayTeamId, | |
'homeTeamId' => $homeTeamId | |
); | |
} | |
} | |
return $results; | |
} | |
/** | |
* Return maximum week from database | |
* | |
* @return int | |
*/ | |
public function getMaxWeek() | |
{ | |
$result = $this->query(' | |
SELECT MAX(week) AS week | |
FROM GAMES' | |
); | |
$maxWeek = 1; | |
if (isset($result[0][0]['week'])) { | |
$maxWeek = $result[0][0]['week']; | |
} | |
return $maxWeek; | |
} | |
/** | |
* Group games according to series | |
* | |
* @param integer $week | |
* @return array | |
*/ | |
public function getSeries($week, $awayTeam, $homeTeam) | |
{ | |
$this->recursive = -1; | |
$results = $this->find('all', array('week' => $week, 'home_team_id' => $homeTeam,'away_team_id' => $awayTeam), array('id', 'home_score', 'away_score') | |
); | |
$homeScores = array(); | |
$awayScores = array(); | |
$gameId = array(); | |
$idx = 1; | |
foreach ($results as $game) { | |
$homeScores[$idx] = $game['Game']['home_score']; | |
$awayScores[$idx] = $game['Game']['away_score']; | |
$gameId[$idx] = $game['Game']['id']; | |
$idx++; | |
} | |
return array( | |
'week' => $week, | |
'gameId' => $gameId, | |
'homeTeam' => $homeTeam, | |
'awayTeam' => $awayTeam, | |
'homeScores' => $homeScores, | |
'awayScores' => $awayScores | |
); | |
} | |
public function getWeeklyResult($week) | |
{ | |
return array_merge($this->_weeklyResults($week, 'AC'), $this->_weeklyResults($week, 'NC')); | |
} | |
/** | |
* Get series that are missing from a particular week | |
* | |
* @param integer $week | |
* @return array | |
*/ | |
public function getMissing($week) | |
{ | |
$results = $this->query("SELECT f.nickname FROM franchises AS f, games AS g WHERE f.id NOT IN (SELECT home_team_id FROM games WHERE week = {$week}) GROUP BY f.nickname ORDER BY f.nickname"); | |
$teams = array(); | |
foreach ($results as $result) { | |
$teams[] = $result[0]['nickname']; | |
} | |
return implode(', ', $teams); | |
} | |
public function _weeklyResults($week, $conference) | |
{ | |
$conditions = array('Game.week' => $week, 'HomeTeam.conference' => $conference); | |
$order = array('HomeTeam.nickname', 'Game.id' => 'ASC'); | |
$results = $this->find('all', compact('conditions', 'order')); | |
$home_scores = array(); | |
$away_scores = array(); | |
foreach ($results as $result) { | |
$teams = $result['AwayTeam']['nickname'] . '@' . $result['HomeTeam']['nickname']; | |
if (!isset($home_scores[$teams])) { | |
$home_scores[$teams] = ''; | |
} | |
if (!isset($away_scores[$teams])) { | |
$away_scores[$teams] = ''; | |
} | |
$home_scores[$teams] .= str_pad($result['Game']['home_score'], 5, ' ', STR_PAD_LEFT); | |
$away_scores[$teams] .= str_pad($result['Game']['away_score'], 5, ' ', STR_PAD_LEFT); | |
} | |
$series = array(); | |
foreach ($home_scores as $teams => $home_score) { | |
list($away_team, $home_team) = split('@', $teams); | |
$series[] = array('home_team' => $home_team, | |
'away_team' => $away_team, | |
'home_scores' => $home_score, | |
'away_scores' => $away_scores[$teams]); | |
} | |
return $series; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment