Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/maintenance/processstats.php
<?php
/**
 * This file updates all player (and defensive unit) stats for a given week
 * 
 * <p>Normally, it runs on the current year and week, only evaluating current games -- that is, games with a start
 * time 5 hours or less before the current system time (adjusted from GMT).
 * These settings can be overridden with the following GET parameters:
 * <br>year=xxxx // year
 * <br>week=xx // week
 * <br>force=1 // overrides time check and parses *all* games for the given week
 * <br>clear=1 // deletes existing records for the specified week / year before parsing
 * <br>For those unfamiliar, GET parameters mean you add them onto the end of the URL as follows:
 * <br>processstats.php?year=2004&week=1&force=1&clear=1</p>
 * 
 * <p>Some stats are still missing: defensive yardage allowed most notably</p>
 * 
 * <p>This file should be run via cron.  I recommend every 10 minutes, as logic is in place to prevent unnecessary computation.
 * Running frequently is therefore the easiest means of accomodating games on odd days like Thanksgiving.
 * <br>that line would look like:
 * <br>0,10,20,30,40,50 * * * * /path/to/php /path/to/processstats.php</p>
 * 
 * <p>alternately, win32 servers can use pycron (http://www.kalab.com/freeware/pycron/pycron.htm)
 * and have a line like
 * <br>0,10,20,30,40,50 * * * * "c:\path\to\php" "c:\path\to\processstats.php"</p>
 * 
 * <p>Since you're not accessing this via the web, you should place it outside of your document root.
 * There are no permission checks; therefore, anyone could process stats if the
 * file is public.  Security through obscurity is a poor substitute.</p>
 * 
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-maint
*/

// For now, I'll echo all this for debugging purposes.  Not computationally significant compared with the rest of the deal.

$pageTitle = "Statistics Processing";
$public = TRUE;
$maintenance = TRUE;
require_once("offlconfig.php");
require_once($DOC_ROOT . "/lib/header.php");

if (!isset($_GET["year"]))
{	$_GET["year"] = getThisYear();	}
if (!isset($_GET["week"]))
{	$_GET["week"] = getCurrentWeek($_GET["year"]);	}

if (isset($_GET["clear"]))
{
	$clr_wk = new OFFL_DBObject();
	$clr_wk->SQLQuery("DELETE FROM playerstats WHERE year=" . $_GET["year"] . " AND week=" . $_GET["week"]);
}

echo "<h2>" . $_GET["year"] . " Week " . $_GET["week"] . " Stats</h2>\n";

$x = new OFFL_NFLGame();
$schedule = $x->getNFLGames($_GET["year"], $_GET["week"]);

$player_lookup = new OFFL_Player();
$playerStat_lookup = new OFFL_PlayerStat();

foreach ($schedule as $game)
{
	// first, check game time.  don't bother processing if inappropriate (unless some force setting enabled)
	// "continue;" if not met

	if (!isset($_GET["force"])) // if no override is specified, don't parse non-current games
	{
		$timeDiff = time() - $game->getGametime();
		if (($timeDiff < 0) || ($timeDiff > (5*3600))) // before game time or more than 5 hours past
		{	continue;	} // skip game processing, not current
	}
	else
	{
		// if force, only parse games that have started (but regardless of how long ago)
		if (time() < $game->getGametime())
		{	continue;	}
	}

	$hTeam = new OFFL_NFLTeam($game->getHNFLTeamID());
	$vTeam = new OFFL_NFLTeam($game->getVNFLTeamID());

	$hTeamScore = array();
	$vTeamScore = array();
	$playerScore = array();

	#$filename = "http://www.nfl.com/gamecenter/live/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
	$filename = "http://www.sportsline.com/nfl/gamecenter/live/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
	$gamestat = file($filename);
	$gamestring = "";

	echo "<h2><a href=\"$filename\">ORIGINAL STAT PAGE</a></h2>\n\n";

	foreach ($gamestat as $statline)
	{
		$gamestring .= $statline . "\n\n";
	}

        @list ($junk, $gamestring) = explode("LEFT RAIL", $gamestring, 2); //line 2105
        @list ($playerstats_leftrail, $gamestring) = explode ("moreAwayStats", $gamestring, 2); //line 2136

	#list($junk, $gamestring) = explode("US/Eastern", $gamestring, 2);
	list($junk, $vscore, $gamestring) = explode("id=\"tscore", $gamestring, 3);
	list($junk, $vscore) = explode(">", $vscore, 2);
	list($vscore, $junk) = explode("<", $vscore, 2);
	list($junk, $hscore, $gamestring) = explode(">", $gamestring, 3);
	list($hscore, $junk) = explode("<", $hscore, 2);

	$hTeamScore["Pt"] = $vscore;
	$vTeamScore["Pt"] = $hscore;

	echo $hTeam->GetNFLTeamCity() . ": $hscore " . $vTeam->getNFLTeamCity() . ": $vscore<br />";

	@list ($junk, $gamestring) = explode ("SCORING SUMMARY", $gamestring, 2); //line 2600
	@list ($scoringsummary, $gamestring) = explode ("RIGHT RAIL", $gamestring, 2); //line 2610
	@list ($playerstats_rightrail, $junk) = explode ("moreHomeStats", $gamestring, 2); //line 2634
	$playerstats = $playerstats_leftrail . $playerstats_rightrail;

	$scoringitems = array();

	$scoringitems = explode("<tr  height=17", $scoringsummary);

	foreach ($scoringitems as $item)
	{
		if (!isset($hTeamScore["MiscTD"]))
		{	$hTeamScore["MiscTD"] = 0;	}
		if (!isset($vTeamScore["MiscTD"]))
		{	$vTeamScore["MiscTD"] = 0;	}

		@list ($junk, $item) = explode ("/icons/", $item, 2);
		@list ($teamabbv, $item) = explode (".", $item, 2);
		if ($teamabbv != "")
		{
			if (strstr($item, "FG"))
			{
				@list ($junk, $item) = explode ("href=", $item, 2);
				@list ($href, $name) = explode (">", $item, 2);
				@list ($name, $yd) = explode ("<", $name, 2);
				@list ($fname, $lname) = explode (" ", $name, 2);
				@list ($junk, $yd) = explode (">", $yd, 2);
				@list ($yd, $junk) = explode ("Yd", $yd, 2);
				$yd = trim($yd);
				#$href = "http://www.nfl.com" . $href;
                                #comment out next line when switched profile urls to sportsline
				#$href = str_replace('nfl/', '', $href);
				$href = "http://www.sportsline.com" . $href;

				if ($yd < 20)
				{
					if (isset($playerScore[$href]["FG_0_19"]))
						$playerScore[$href]["FG_0_19"]++;
					else
						$playerScore[$href]["FG_0_19"] = 1;
				}
				elseif ($yd < 30)
				{
					if (isset($playerScore[$href]["FG_20_29"]))
						$playerScore[$href]["FG_20_29"]++;
					else
						$playerScore[$href]["FG_20_29"] = 1;
				}
				elseif ($yd < 40)
				{
					if (isset($playerScore[$href]["FG_30_39"]))
						$playerScore[$href]["FG_30_39"]++;
					else
						$playerScore[$href]["FG_30_39"] = 1;
				}
				elseif ($yd < 50)
				{
					if (isset($playerScore[$href]["FG_40_49"]))
						$playerScore[$href]["FG_40_49"]++;
					else
						$playerScore[$href]["FG_40_49"] = 1;
				}
				else
				{
					if (isset($playerScore[$href]["FG_50p"]))
						$playerScore[$href]["FG_50p"]++;
					else
						$playerScore[$href]["FG_50p"] = 1;
				}

				$nfl_player = $player_lookup->getPlayerByNFLProfile($href);
				if ($nfl_player !== FALSE)
					echo "<p><a href=\"" . $nfl_player->getNFLProfile() . "\">" . $nfl_player->getName() . "</a> hit a $yd yd FG.\n\n";
	//			echo "<p><a href=\"$href\">$fname $lname</a> hit a $yd yd FG.\n\n";
			}
			elseif (stristr($item, "interception return"))
			{
				if ($teamabbv == $hTeam->getNFLTeamAbbv())
				{
					if (isset($hTeamScore["IntTD"]))
						$hTeamScore["IntTD"]++;
					else
						$hTeamScore["IntTD"] = 1;
				}
				else
				{
					if (isset($vTeamScore["IntTD"]))
						$vTeamScore["IntTD"]++;
					else
						$vTeamScore["IntTD"] = 1;
				}
				@$defTD[$teamabbv] += 1;
			}
			elseif (stristr($item, "fumble return"))
			{
				if ($teamabbv == $hTeam->getNFLTeamAbbv())
				{
					if (isset($hTeamScore["FmblTD"]))
						$hTeamScore["FmblTD"]++;
					else
						$hTeamScore["FmblTD"] = 1;
				}
				else
				{
					if (isset($vTeamScore["FmblTD"]))
						$vTeamScore["FmblTD"]++;
					else
						$vTeamScore["FmblTD"] = 1;
				}
				@$defTD[$teamabbv] += 1;
			}
			elseif ((stristr($item, "kick return")) || (stristr($item, "punt return")))
			{
				if ($teamabbv == $hTeam->getNFLTeamAbbv())
				{
					if (isset($hTeamScore["RetTD"]))
						$hTeamScore["RetTD"]++;
					else
						$hTeamScore["RetTD"] = 1;
				}
				else
				{
					if (isset($vTeamScore["RetTD"]))
						$vTeamScore["RetTD"]++;
					else
						$vTeamScore["RetTD"] = 1;
				}
				@$defTD[$teamabbv] += 1;
			}
			elseif (stristr($item, "return"))
			{
				// something like a blocked FG returned for a TD
				if ($teamabbv == $hTeam->getNFLTeamAbbv())
				{
					if (isset($hTeamScore["MiscTD"]))
						$hTeamScore["MiscTD"]++;
					else
						$hTeamScore["MiscTD"] = 1;
				}
				else
				{
					if (isset($vTeamScore["MiscTD"]))
						$vTeamScore["MiscTD"]++;
					else
						$vTeamScore["MiscTD"] = 1;
				}
				@$defTD[$teamabbv] += 1;
			}
			elseif (strstr($item, "SAFETY"))
			{
				if ($teamabbv == $hTeam->getNFLTeamAbbv())
				{
					if (isset($hTeamScore["Safety"]))
						$hTeamScore["Safety"]++;
					else
						$hTeamScore["Safety"] = 1;
				}
				else
				{
					if (isset($vTeamScore["Safety"]))
						$vTeamScore["Safety"]++;
					else
						$vTeamScore["Safety"] = 1;
				}
				@$safety[$teamabbv] += 1;
			}
			if (strstr($item, "2 Pt. Conversion"))
			{
				if (stristr($item, "failed") !== FALSE)
					continue; // 2pt conversion failed, do not run
				// need to find all involved players
				list ($junk, $twopt) = explode ("(", $item, 2);
				list ($twopt, $junk) = explode (")", $twopt, 2);
				$twopt_players = explode ("href=", $twopt);

				foreach ($twopt_players as $p=>$player)
				{
					if ($p)
					{
						@list ($href, $name) = explode (">", $player, 2);
						@list ($name, $junk) = explode ("<", $name, 2);
						@list ($fname, $lname) = explode (" ", $name, 2);
						#$href = "http://www.nfl.com" . $href;
						#this next line could be a bug, haven't had a chance to test it yet.... ok, it seems to be working...
						$href = "http://www.sportsline.com" . $href;

						if (isset($playerScore[$href]["TwoPt"]))
							$playerScore[$href]["TwoPt"]++;
						else
							$playerScore[$href]["TwoPt"] = 1;

						$nfl_player = $player_lookup->getPlayerByNFLProfile($href);
						if ($nfl_player !== FALSE)
							echo "<p><a href=\"" . $nfl_player->getNFLProfile() . "\">" . $nfl_player->getName() . "</a> involved in a 2-pt conversion.\n\n";
					//	echo "<p><a href=\"$href\">$fname $lname</a> involved in a 2-pt conversion.\n\n";
					}
				}
			}

	// core detail
	//		list ($junk, $item) = explode ("align=left>", $item, 2);
	//		list ($item, $junk) = explode ("</td>", $item, 2);
	//		echo "<b>" . $teamabbv . "</b>:\n" . $item . "<br>\n\n";

		}
	}

	if (isset($defTD)) foreach ($defTD as $team=>$tds)
	{
		$nfl_team = $player_lookup->getDefenseByNFLTeamAbbv($team);
		echo "<p>" . $nfl_team->getNFLTeamCity() . " " . $nfl_team->getNFLTeamName() . " has $tds defensive / return TDs\n";
	}
	if (isset($safety)) foreach ($safety as $team=>$safetys)
	{
		$nfl_team = $player_lookup->getDefenseByNFLTeamAbbv($team);
		echo "<p>" . $nfl_team->getNFLTeamCity() . " " . $nfl_team->getNFLTeamName() . " has $safetys safetys\n";
	}
	echo "<hr />\n";
	echo "<p><p>\n\n";

// +--------------------------------------+
// | INDIVIDUAL PLAYER STATISTICS SECTION |
// +--------------------------------------+

	@list ($junk, $playerstats) = explode ("stat00", $playerstats, 2);
	$statcats = explode ("stat", $playerstats);

	//echo "\n<!-- ********** -->\n$playerstats\n<!-- *********** -->\n";

	foreach ($statcats as $i=>$stat)
	{
		@list ($junk, $cat) = explode ("colspan=5>", $stat, 2);
		@list ($cat, $stat) = explode ("<", $cat, 2);
		@list ($junk, $stat) = explode ("href=\"", $stat, 2);


		$players = explode ("href=\"", $stat);
		echo "<p>$i ($cat): ";
		foreach ($players as $player)
		{
			@list ($href, $player) = explode ("\">", $player, 2);
			@list ($href, $junk) = explode ("\" ", $href, 2);
			#$href = "http://www.nfl.com" . $href;
			$href = "http://www.sportsline.com" . $href;
			@list ($name, $player) = explode ("</a>", $player, 2);
			@list ($fname, $lname) = explode ("&nbsp;", $name, 2);
			@list ($junk, $player) = explode ("<td", $player, 2);
			$player_stats = explode("<td", $player);

			for ($j = 0; $j < sizeof($player_stats); $j++)
			{
				@list ($junk, $player_stats[$j]) = explode (">", $player_stats[$j]);
				@list ($player_stats[$j], $junk) = explode ("<", $player_stats[$j]);
				$player_stats[$j] = trim($player_stats[$j]);
			}

			#comment the following line out once your nfl profiles in the database have been switched to sportsline
			#$href = str_replace('nfl/', '', $href);
			$nfl_player = $player_lookup->getPlayerByNFLProfile($href);
			if ($nfl_player === FALSE)
			{	$nfl_player = new OFFL_Player(); 	}

			switch (strtoupper($cat))
			{
				case "PASSING":
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";
					@list ($cmp, $att) = explode("/", $player_stats[0]);
					echo $cmp . " of " . $att . " passing for ";
					echo $player_stats[1] . " Yds, ";
					echo $player_stats[2] . " TDs, ";
					echo $player_stats[3] . " Ints<br>\n";
					
					$playerScore[$href]["PassCmp"] = $cmp;
					$playerScore[$href]["PassAtt"] = $att;
					$playerScore[$href]["PassYd"] = $player_stats[1];
					$playerScore[$href]["PassTD"] = $player_stats[2];
					$playerScore[$href]["Int"] = $player_stats[3];

					break;
				case "RUSHING";
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";

					echo $player_stats[0] . " rushes for ";
					echo $player_stats[1] . " Yds, ";
					echo $player_stats[2] . " TDs<br>\n";

					$playerScore[$href]["RushAtt"] = $player_stats[0];
					$playerScore[$href]["RushYd"] = $player_stats[1];
					$playerScore[$href]["RushTD"] = $player_stats[2];

					break;
				case "RECEIVING";
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";

					echo $player_stats[0] . " receptions for ";
					echo $player_stats[1] . " Yds, ";
					echo $player_stats[2] . " TDs<br>\n";

					$playerScore[$href]["RecAtt"] = $player_stats[0];
					$playerScore[$href]["RecYd"] = $player_stats[1];
					$playerScore[$href]["RecTD"] = $player_stats[2];
					
					break;
				case "FUMBLES";
					if (@$player_stats[1] > 0)
					{
						echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";
						echo $player_stats[1] . " Fumbles lost<br>\n";

						$playerScore[$href]["FmblLost"] = $player_stats[1];

						if ($i % 2) // visiting team lost fumble, array index is an odd number
						{
                                                        if (isset($hTeamScore["FmblRec"]))
                                                                $hTeamScore["FmblRec"] += $player_stats[1];
                                                        else
                                                                $hTeamScore["FmblRec"] = $player_stats[1];

						}
						else // home team lost fumble
						{
                                                        if (isset($vTeamScore["FmblRec"]))
                                                                $vTeamScore["FmblRec"] += $player_stats[1];
                                                        else
                                                                $vTeamScore["FmblRec"] = $player_stats[1];

						}
					}

					break;
				case "KICKING";
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";

					@list ($fg, $fga) = explode ("/", $player_stats[0]);
					@list ($xp, $xpa) = explode ("/", $player_stats[2]);

					echo $fg . "/" . $fga . " FGs, ";
					echo $xp . "/" . $xpa . " XPs, <br>";

					
					if ($fga != "")
						$playerScore[$href]["FGA"] = $fga;
					if ($xp != "")
						$playerScore[$href]["XP"] = $xp;
					if ($xpa != "")
						$playerScore[$href]["XPA"] = $xpa;

					break;
				case "PUNTING";
					echo "No relevant stats";

					break;
				case "KICKOFF RETURNS";
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";
					echo $player_stats[0] . " kickoff returns for ";
					echo floor($player_stats[0] * @$player_stats[1]) . " Yds, ";
					echo @$player_stats[2] . " TDs<br>\n";

					if ($player_stats[0] != "")
					{
						$playerScore[$href]["KRetAtt"] = $player_stats[0];
						$playerScore[$href]["KRetYd"] = floor($player_stats[0] * $player_stats[1]);
						$playerScore[$href]["KRetTD"] = $player_stats[2];
					}

					break;
				case "PUNT RETURNS";
					echo "<a href=\"$WEB_ROOT/players.php?player_id=" . $nfl_player->getPlayerID() . "\">" . $nfl_player->getName() . "</a> :";

					echo $player_stats[0] . " punt returns for ";
					echo floor($player_stats[0] * @$player_stats[1]) . " Yds, ";
					echo @$player_stats[2] . " TDs<br>\n";

					if ($player_stats[0] != "")
					{
						$playerScore[$href]["PRetAtt"] = $player_stats[0];
						$playerScore[$href]["PRetYd"] = floor($player_stats[0] * $player_stats[1]);
						$playerScore[$href]["PRetTD"] = $player_stats[2];
					}

					break;
				case "DEFENSE";

					if ($i % 2)
					{	$teamabbv = $hTeam->getNFLTeamAbbv();	}
					else
					{	$teamabbv = $vTeam->getNFLTeamAbbv();	}
					if ($i % 2)
					{
						if (isset($hTeamScore["Sack"]))
							$hTeamScore["Sack"] += $player_stats[1];
						else
							$hTeamScore["Sack"] = $player_stats[1];
						if (isset($hTeamScore["Int"]))
						{
							$hTeamScore["Int"] += $player_stats[2];
						}
						else
							$hTeamScore["Int"] = $player_stats[2];
					}
					else
					{
						if (isset($vTeamScore["Sack"]))
							$vTeamScore["Sack"] += $player_stats[1];
						else
							$vTeamScore["Sack"] = $player_stats[1];
						if (isset($vTeamScore["Int"]))
							$vTeamScore["Int"] += $player_stats[2];
						else
							$vTeamScore["Int"] = $player_stats[2];
					}
					
					if ($player_stats[1] > 0)
					{	@$sacks[$teamabbv] += $player_stats[1];	}

					if ($player_stats[2] > 0)
					{	@$ints[$teamabbv] += $player_stats[2];	}


					break;
			}
		}
		// echo "$stat\n\n";
	}

	if(isset($sacks))
		foreach ($sacks as $team=>$sack)
		{
				$nfl_team = $player_lookup->getDefenseByNFLTeamAbbv($team);
				echo "<p>" . $nfl_team->getNFLTeamCity() . " " . $nfl_team->getNFLTeamName() . " has $sack sacks\n";
		}
	if(isset($ints))
		foreach ($ints as $team=>$int)
		{
			$nfl_team = $player_lookup->getDefenseByNFLTeamAbbv($team);
			echo "<p>" . $nfl_team->getNFLTeamCity() . " " . $nfl_team->getNFLTeamName() . " has $int ints\n";
		}

	// ****************************
	// Now look for blocked kicks

	#$filename = "http://www.nfl.com/gamecenter/playbyplay/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
	$filename = "http://www.sportsline.com/nfl/gamecenter/playbyplay/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
	$gamestat = file($filename);

	$gamestring = "";

	echo "<h2><a href=\"$filename\">ORIGINAL PLAY-BY-PLAY PAGE</a></h2>\n\n";

	foreach ($gamestat as $statline)
	{
		$gamestring .= $statline . "\n\n";
	}

	list ($junk, $gamestring) = explode ("Full Play-by-Play", $gamestring, 2);

	$homepos = explode("class=home", $gamestring);
	for ($i = 0; $i < sizeof($homepos); $i++)
	{	@list ($homepos[$i], $junk) = explode("class=away", $homepos[$i]);	}

	$awaypos = explode("class=away", $gamestring);
	for ($i = 0; $i < sizeof($awaypos); $i++)
	{	@list ($awaypos[$i], $junk) = explode("class=home", $awaypos[$i]);	}

	foreach ($awaypos as $str)
	{
	// echo $str . "\n\n\n<br>\n\n\n";
		if (stristr($str, "blocked"))
		{
			@$blocks[$hTeam->getNFLTeamAbbv()]++;
			if (isset($hTeamScore["BlkKick"]))
				$hTeamScore["BlkKick"]++;
			else
				$TeamScore["BlkKick"] = 1;
		}
	}
	foreach ($homepos as $str)
	{
	// echo $str . "\n\n\n<br>\n\n\n";
		if (stristr($str, "blocked"))
		{
			@$blocks[$vTeam->getNFLTeamAbbv()]++;
			if (isset($vTeamScore["BlkKick"]))
				$vTeamScore["BlkKick"]++;
			else
				$vTeamScore["BlkKick"] = 1;
		}
	}

	if(isset($blocks))
		foreach ($blocks as $team=>$block)
		{
			$nfl_team = $player_lookup->getDefenseByNFLTeamAbbv($team);
			echo "<p>" . $nfl_team->getNFLTeamCity() . " " . $nfl_team->getNFLTeamName() . " has $block blocked kicks\n";
		}

// +--------------------------------------+
// | DEFENSIVE YARDS ALLOWED SECTION      |
// +--------------------------------------+

 	#$filename = "http://www.nfl.com/gamecenter/teamstats/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
 	$filename = "http://www.sportsline.com/nfl/gamecenter/teamstats/NFL_" . date("Ymd", $game->getGametime()) . "_" . $game->getVNFLTeamAbbv() . "@" . $game->getHNFLTeamAbbv();
 	$gamestat = file($filename);

	$gamestring = "";
	
 	echo "<h2><a href=\"$filename\">ORIGINAL TEAM STATS PAGE</a></h2>\n\n";
 	
	foreach ($gamestat as $statline)
	{
		$gamestring .= $statline . "\n\n";
	}

	 list($junk, $gamestring) = explode("Total Net Yards</b></td>", $gamestring, 2);
	 list($junk, $hyards, $gamestring) = explode("align=right", $gamestring, 3);
	 list($junk, $hyards) = explode(">", $hyards, 2);
	 list($hyards, $junk) = explode("<", $hyards, 2);
	 list($junk, $vyards, $gamestring) = explode(">", $gamestring, 3);
	 list($vyards, $junk) = explode("<", $vyards, 2);

	 $vTeamScore["Yd"] = $vyards;
	 $hTeamScore["Yd"] = $hyards;
	 
	 echo "<p>" . $hTeam->GetNFLTeamCity() . " allowed $hyards total net yards<br><br />" . $vTeam->getNFLTeamCity() . " allowed $vyards total net yards<br />";

// now load up the data

	foreach ($playerScore as $href=>$score)
	{
		$player = $player_lookup->getPlayerByNFLProfile($href);
		if ($player === FALSE)
			continue;

		$stat_record = $playerStat_lookup->getStatsByPlayerIDYearWeek($player->getPlayerID(), $_GET["year"], $_GET["week"]);
		if ($stat_record === FALSE)
		{
			$stat_record = new OFFL_PlayerStat();
			$stat_record->setPlayerID($player->getPlayerID());
			$stat_record->setYear($_GET["year"]);
			$stat_record->setWeek($_GET["week"]);
			$stat_record->setNFLTeamID($player->getNFLTeamID());		
		}

		foreach ($score as $cat_name=>$value)
		{
			$stat_record->{"set" . $cat_name}($value);
		}

		$stat_record->save();
	} // end foreach entry in $playerScore

	if(!isset($hTeamScore["Int"]))
	{	$hTeamScore["Int"] = 0;	 }
	if(!isset($hTeamScore["Sack"]))
	{	$hTeamScore["Sack"] = 0;	 }
	if(!isset($vTeamScore["Int"]))
	{	$vTeamScore["Int"] = 0;	 }
	if(!isset($vTeamScore["Sack"]))
	{	$vTeamScore["Sack"] = 0;	 }

	$player = $player_lookup->getDefenseByNFLTeamAbbv($game->getHNFLTeamAbbv());
	$stat_record = $playerStat_lookup->getStatsByPlayerIDYearWeek($player->getPlayerID(), $_GET["year"], $_GET["week"]);
	if ($stat_record === FALSE)
	{
		$stat_record = new OFFL_PlayerStat();
		$stat_record->setPlayerID($player->getPlayerID());
		$stat_record->setYear($_GET["year"]);
		$stat_record->setWeek($_GET["week"]);
		$stat_record->setNFLTeamID($player->getNFLTeamID());
	}
	foreach ($hTeamScore as $cat_name=>$value)
	{
		$stat_record->{"setDEF" . $cat_name}($value);
	}
	$stat_record->save();

	$player = $player_lookup->getDefenseByNFLTeamAbbv($game->getVNFLTeamAbbv());
	$stat_record = $playerStat_lookup->getStatsByPlayerIDYearWeek($player->getPlayerID(), $_GET["year"], $_GET["week"]);
	if ($stat_record === FALSE)
	{
		$stat_record = new OFFL_PlayerStat();
		$stat_record->setPlayerID($player->getPlayerID());
		$stat_record->setYear($_GET["year"]);
		$stat_record->setWeek($_GET["week"]);
		$stat_record->setNFLTeamID($player->getNFLTeamID());
	}
	foreach ($vTeamScore as $cat_name=>$value)
	{
		$stat_record->{"setDEF" . $cat_name}($value);
	}
	$stat_record->save();

	unset ($hTeamScore);
	unset ($vTeamScore);
	unset ($playerScore);

	unset ($sacks);
	unset ($ints);
	unset ($blocks);
	unset ($safety);
	unset ($defTD);

} // end foreach game

/******************************
 * 
 *   SCORES PROCESSING MOVED
 *   TO THIS FILE FOR FASTER
 * UPDATES AND NO INCONSISTENT
 *   SCORING DATA DISPLAYED.
 * 
 ******************************/

echo "<h2>" . $_GET["year"] . " Week " . $_GET["week"] . " Scores</h2>\n";

// check to see if this is the current week (and use players for roster)
// or a past week (and use pastrosters for roster)
$curr_year = getThisYear();
$curr_week = getCurrentWeek($curr_year);

$playerStat_lookup = new OFFL_PlayerStat();
$current = FALSE;
if (($curr_week == $_GET["week"]) && ($curr_year == $_GET["year"]))
{	$current = TRUE;	}

// find the final game of the week so I can setFinal() if needed
$x = new OFFL_NFLGame();
$weekGames = $x->getNFLGames($_GET["year"], $_GET["week"]);
$gametime = 0;
foreach ($weekGames as $nfl_game)
{	
	if ($nfl_game->getGametime() > $gametime)
	{	$gametime = $nfl_game->getGametime();	}
}
$final = 0;
if ( (time()-$gametime) > (5*3600) ) // 5 hours past start time of last weekly game
{	$final = 1;	}

// *********************************************
// start checking for new compatibility here!!!
// *********************************************
$x = new OFFL_Game();
$games = $x->getGames(NULL, $_GET["year"], $_GET["week"]);

foreach ($games as $game)
{
#if ($game->getHFFLTeamID() == 13) {

	$hTeam = new OFFL_FFLTeam($game->getHFFLTeamID());
	$vTeam = new OFFL_FFLTeam($game->getVFFLTeamID());
	if ($current)
	{
		$hRosterPlayers = $hTeam->getRoster(TRUE); // get starters
		$vRosterPlayers = $vTeam->getRoster(TRUE);
		$hRoster = array();
		$vRoster = array();
		foreach ($hRosterPlayers as $player)
		{	$hRoster[] = $player->getPlayerID();	}
		foreach ($vRosterPlayers as $player)
		{	$vRoster[] = $player->getPlayerID();	}
	}
	else
	{
		$hPastRoster = new OFFL_PastRoster($_GET["year"], $_GET["week"], $game->getHFFLTeamID());
		$hRoster = array();
		$hR = $hPastRoster->getPlayerIDArray();
		$hS = $hPastRoster->getStarterArray();
		foreach ($hR as $i=>$player_id)
		{
			if ($hS[$i])
			{	$hRoster[] = $player_id;	}
		}
		$vPastRoster = new OFFL_PastRoster($_GET["year"], $_GET["week"], $game->getVFFLTeamID());
		$vRoster = array();
		$vR = $vPastRoster->getPlayerIDArray();
		$vS = $vPastRoster->getStarterArray();
		foreach ($vR as $i=>$player_id)
		{
			if ($vS[$i])
			{	$vRoster[] = $player_id;	}
		}
	}

	$hScore = 0;
	$vScore = 0;
	foreach ($hRoster as $player_id)
	{
		$stat = $playerStat_lookup->getStatsByPlayerIDYearWeek($player_id, $_GET["year"], $_GET["week"]);
		if ($stat !== FALSE)
		{
			$stat->setLeagueID($game->getLeagueID());
			$hScore += $stat->calculatePoints();
		}
	}
	foreach ($vRoster as $player_id)
	{
		$stat = $playerStat_lookup->getStatsByPlayerIDYearWeek($player_id, $_GET["year"], $_GET["week"]);
		if ($stat !== FALSE)
		{
			$stat->setLeagueID($game->getLeagueID());
			$vScore += $stat->calculatePoints();
		}
	}

	$game->setHFFLTeamScore($hScore);
	$game->setVFFLTeamScore($vScore);

	echo "<p>" . $vTeam->getFFLTeamFullName() . " " . $vScore . " at " . $hTeam->getFFLTeamFullName() . " " . $hScore;
	if ($final)
	{	echo " (Final)";	}
	else
	{	echo " (In Progress)";	}
	echo "</p>\n";

	$game->setFinal($final);

	$game->save();
}



require($DOC_ROOT . "/lib/footer.php"); ?>
Return current item: Online Fantasy Football League