Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_game.php
<?php
/**
 * Defines the {@link OFFL_Game} class.
 *
 * @package offl
 * @author Stephen Rochelle <hide@address.com>
 */

if (strtr(__FILE__, "\\", "/") == $_SERVER["SCRIPT_FILENAME"])
{	die ("Cannot access file directly!");	}

require_once($DOC_ROOT . "/lib/functions.php");
require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
/**
 * Defines interfaces for games between {@link OFFL_FFLTeam FFL teams}.
 *
 * {@link getCurrentWeek()} should probably be moved to {@link functions.php}, as it's not really a class function.
 *
 * @package offl
 */
class OFFL_Game extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_game_id = NULL;

	/**
	 * @var integer
	 */
	var $_year = NULL;

	/**
	 * @var integer
	 */
	var $_week = NULL;

	/**
	 * @var integer
	 */
	var $_league_id = NULL;

	/**
	 * @var integer
	 */
	var $_v_fflteam_id = NULL;

	/**
	 * Scores can be floats
	 * @var number
	 */
	var $_v_fflteam_score=NULL;

	/**
	 * @var integer
	 */
	var $_h_fflteam_id = NULL;

	/**
	 * Scores can be floats
	 * @var number
	 */
	var $_h_fflteam_score=NULL;

	/**
	 * @var boolean Uses integral boolean values (0/1)
	 */
	var $_final = 0;

	/**
	 * Constructor
	 *
	 * @param integer $game_id Optional: If set, loads game from database
	 */
	function OFFL_Game ($game_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if (!is_null($game_id))
		{
			$this->_game_id = $game_id;
			$this->populate();
		}
	}

	/**
	 * Yanks info from database.
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_game_id))
		{
			$this->_emsg = "\$_game_id is not set.	Cannot populate Game object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT * FROM games WHERE game_id=" . $this->_game_id;

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No person records found for game_id $this->_game_id.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_year = mysql_result($result,0,"year");
		$this->_week = mysql_result($result,0,"week");
		$this->_league_id = mysql_result($result,0,"league_id");
		$this->_v_fflteam_id = mysql_result($result,0,"v_fflteam_id");
		$this->_h_fflteam_id = mysql_result($result,0,"h_fflteam_id");
		$this->_v_fflteam_score = mysql_result($result,0,"v_fflteam_score");
		$this->_h_fflteam_score = mysql_result($result,0,"h_fflteam_score");
		$this->_final = mysql_result($result,0,"final");
		mysql_free_result($result);

		$control = new OFFL_Control($this->_league_id);
		if ($control->getValue("SCORING_FRAC_POINTS") == "No")
		{
			if (!is_null($this->_v_fflteam_score))
			{	$this->_v_fflteam_score = (int)$this->_v_fflteam_score;	 }
			if (!is_null($this->_h_fflteam_score))
			{	$this->_h_fflteam_score = (int)$this->_h_fflteam_score;	 }
		}
	}

	/**
	 * Saves info into database.
	 *
	 * Determines INSERT vs SELECT automagically.
	 */
	function save ()
	{
		if(is_null($this->_h_fflteam_id))
		{	$this->_h_fflteam_id = "NULL";	}
		if(is_null($this->_v_fflteam_id))
		{	$this->_v_fflteam_id = "NULL";	}
		if(is_null($this->_game_id))
		{
			// insert
			$sql = "insert into games (year, week, league_id, v_fflteam_id, h_fflteam_id, final) values (" . $this->_year . ", " . $this->_week . ", " . $this->_league_id . ", " . $this->_v_fflteam_id . ", " . $this->_h_fflteam_id . ", " . $this->_final . ")";
		}
		else
		{
			// update
			$sql = "update games set year=" . $this->_year . ", week=" . $this->_week . ", league_id=" . $this->_league_id . ", v_fflteam_id=" . $this->_v_fflteam_id . ", h_fflteam_id=" . $this->_h_fflteam_id . ", final=" . $this->_final;

			// Only set scores when both scores are given, otherwise null
			if(!is_null($this->_v_fflteam_score) && !is_null($this->_h_fflteam_score))
				$sql .= ", v_fflteam_score=" . $this->_v_fflteam_score . ", h_fflteam_score=" . $this->_h_fflteam_score;
			else
				$sql .= ", v_fflteam_score=NULL, h_fflteam_score=NULL";

			$sql .=" where game_id=" . $this->_game_id;
		}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		return $result;
	}

	/**
	 * @return integer
	 */
	function getGameID()
	{
		return $this->_game_id;
	}

	/**
	 * @param integer $year
	 */
	function setYear($year)
	{
		$this->_year = $year; 
	}

	/**
	 * @return integer
	 */
	function getYear()
	{
		return $this->_year;
	}

	/**
	 * @param integer $week
	 */
	function setWeek($week)
	{
		$this->_week = $week; 
	}

	/**
	 * @return integer
	 */
	function getWeek()
	{
		return $this->_week;
	}

	/**
	 * @param integer $league_id
	 */
	function setLeagueID($league_id)
	{
		$this->_league_id = $league_id; 
	}

	/**
	 * @return integer
	 */
	function getLeagueID()
	{
		return $this->_league_id;
	}

	/**
	 * @param integer $v_fflteam_id
	 */
	function setVFFLTeamID($v_fflteam_id)
	{
		$this->_v_fflteam_id = $v_fflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getVFFLTeamID()
	{
		return $this->_v_fflteam_id;
	}

	/**
	 * @param integer $h_fflteam_id
	 */
	function setHFFLTeamID($h_fflteam_id)
	{
		$this->_h_fflteam_id = $h_fflteam_id; 
	}

	/**
	 * @return integer
	 */
	function getHFFLTeamID()
	{
		return $this->_h_fflteam_id;
	}

	/**
	 * @param number $h_fflteam_score
	 */
	function setHFFLTeamScore($h_fflteam_score)
	{
		$this->_h_fflteam_score = $h_fflteam_score; 
	}

	/**
	 * @return number
	 */
	function getHFFLTeamScore()
	{
		return $this->_h_fflteam_score;
	}

	/**
	 * @param number $v_fflteam_score
	 */
	function setVFFLTeamScore($v_fflteam_score)
	{
		$this->_v_fflteam_score = $v_fflteam_score; 
	}

	/**
	 * @return number
	 */
	function getVFFLTeamScore()
	{
		return $this->_v_fflteam_score;
	}
	
	/**
	 * @param boolean $final Takes boolean values
	 */
	function setFinal($final)
	{
		if ($final)
		{	$this->_final = 1;	}
		else
		{	$this->_final = 0;	}
	}

	/**
	 * @return boolean Uses integral boolean values (1/0)
	 */
	function getFinal()
	{
		return $this->_final;
	}

// *************
// PROCESSING FUNCTIONS
// *************

	/**
	 * Finds a team's score for an arbitrary game
	 * @param integer $year
	 * @param integer $week
	 * @param integer $fflteam_id
	 * @return number Returns the score for the given combination of year, week, and fflteam_id, or NULL if not found
	 */
	function getGameScore($year, $week, $fflteam_id)
	{
		$sql = "SELECT game_id FROM games WHERE year=" . $year . " AND week=" . $week . " AND (h_fflteam_id=" . $fflteam_id . " OR v_fflteam_id=" . $fflteam_id . ")";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if (mysql_num_rows($result) == 0)
		{	return NULL;	}

		$game = new OFFL_Game(mysql_result($result,0,"game_id"));
		if ($fflteam_id == $game->getHFFLTeamID())
		{	return $game->getHFFLTeamScore();	}

		return $game->getVFFLTeamScore();
	}

	/**
	 * Finds OFFL_Game objects with optional criteria
	 *
	 * @param integer $league_id Optional: set for a particular league
	 * @param integer $year Optional: set for a particular year
	 * @param integer $week Optional: set for a particular week
	 * @return array Returns an array of OFFL_Game objects fitting the given criteria
	 */
	function getGames($league_id=NULL, $year=NULL, $week=NULL)
	{
		$retArr = array();
		$sql = "SELECT game_id FROM games";
		if(isset($league_id))
		{
			$sql .= " WHERE league_id=" . $league_id;
			if(isset($year))
			{
				$sql .= " AND year=" . $year;
				if(isset($week))
				{	$sql .= " AND week=" . $week;	}
			}
		}
		else
		if(isset($year))
		{
			$sql .= " WHERE year=" . $year;
			if(isset($week))
			{	$sql .= " AND week=" . $week;	}
		}
		$sql .= " ORDER BY league_id, year, week";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$game = new OFFL_Game(mysql_result($result,$i,"game_id"));
			array_push($retArr, $game);
		}
	
		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Finds years with associated OFFL_Games, modified by {@link $_league_id} criteria if set
	 *
	 * @return array Returns an array of integral years (2003, 2004, 2005, etc) matching the criteria
	 */
	function getGameYears()
	{
		$retArr = array();
		$sql = "SELECT DISTINCT year FROM games";
		if (isset($this->_league_id))
		{	$sql .= " WHERE league_id=" . $this->_league_id;	}
		$sql .= " ORDER BY year";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
			array_push($retArr, mysql_result($result,$i,"year"));

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Finds weeks with associated OFFL_Games, modified by {@link $_year} and optional {@link $_league_id} criteria
	 *
	 * @return array Returns an array of integral weeks (1, 2, 3, etc) matching the criteria.
	 */
	function getGameWeeks()
	{
		$retArr = array();
		if (is_null($this->_year))
		{	return $retArr;	}
		$sql = "SELECT DISTINCT week FROM games WHERE year=" . $this->_year;
		if (isset($this->_league_id))
		{	$sql .= " AND league_id=" . $this->_league_id;	}
		$sql .= " ORDER BY week";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
			array_push($retArr, mysql_result($result,$i,"week"));

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * Finds the max (usually assumed current) year associated with OFFL_Games, modified by optional league_id criteria
	 *
	 * Currently returns {@link $_year} if nothing found in database.  Seems risky to me.  May swap over to current year based on date detection?
	 *
	 * @param integer
	 * @return integer
	 */
	function getMaxGameYear($league_id=NULL)
	{
		$sql = "SELECT MAX(year) FROM games";
		if(isset($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		$maxyear = mysql_result($result,0,0);
		mysql_free_result($result);

		if($maxyear < 1900)
		{	$maxyear = getThisYear() - 1;	}

		return $maxyear;
	}

	/**
	 * Creates and saves OFFL_Game objects to match a schedule.
	 *
	 * This function needs revision, IMO, but it's functional for now.
	 *
	 * @param integer $league_id Index of {@link OFFL_League} for schedule being created
	 * @param integer $year Year for schedule being created
	 * @param integer $numgames Number of games per week (normally number of teams halved and floored)
	 * @param integer $numweeks Number of regular-season weeks
	 * @param integer $numrounds Number of playoff weeks.  Assumes single-elimination bracket with no bye weeks.
	 */
	function createSchedule($league_id, $year, $numgames, $numweeks, $numrounds)
	{
		// Regular Season
		for($i=1; $i<=$numweeks; $i++)
		{
			for($j=0; $j<$numgames; $j++)
			{
				$p = new OFFL_Game();
				$p->setLeagueID($league_id);
				$p->setYear($year);
				$p->setWeek($i);
				// $p->setFFLTeamID($teams[$j]);
				$p->save();
			}
		}

		// Playoffs
		for($i=1; $i<=$numrounds; $i++)
		{
			// Need to factor in some binary action
			for($j=0; $j<pow(2,intval($numrounds-1-$i)); $j++)
			{
				$p = new OFFL_Game();
				$p->setLeagueID($league_id);
				$p->setYear($year);
				$p->setWeek($numweeks+$i);
				$p->save();
			}
		}
	}

} // end OFFL_Game class

?>
Return current item: Online Fantasy Football League