Location: PHPKode > projects > My Free Fantasy League > alpha1/module/nflgc/include/lib/nflgc/parser.php
<?php
/*%FS%----------------------------------------------------------------
!  Id:    $Id: parser.php,v 1.14 2004/08/21 03:53:34 dantheman Exp $
!
!  File:    parser.php
!
!  Desc:  NFL GameCenter parsing functions.
!
!  Copyright (c) 2004 Dan Mills.
!
!  See license file for information on usage and redistribution of
!  this file and for a DISCLAIMER OF ALL WARRANTIES.
!
!  History:
!   $Log: parser.php,v $
!   Revision 1.14  2004/08/21 03:53:34  dantheman
!   Added scoring categories 37-41
!
!   Revision 1.13  2004/08/19 16:28:23  dantheman
!   Team unit scoring is functional, mebbe sorta.
!
!   Revision 1.12  2004/08/18 18:10:53  dantheman
!   Do not overwrite old player data, if there is no good new data to replace it
!
!   Revision 1.11  2004/08/11 14:03:48  dantheman
!   Modified for 2004 nfl.com input
!
!   Revision 1.10  2004/08/11 13:14:34  dantheman
!   hell if i know
!
!   Revision 1.9  2004/04/14 10:38:34  dantheman
!   began work on team (DEF, etc) fantasy scoring
!
!   Revision 1.8  2004/04/12 12:33:20  dantheman
!   game.php now queries for unknown players automaticaly.
!
!   Revision 1.7  2004/04/09 20:11:50  dantheman
!   Player parser is 95% done
!
!   Revision 1.6  2004/04/09 11:16:38  dantheman
!   More work on player parsing.
!
!   Revision 1.5  2004/04/08 11:04:05  dantheman
!   Beginning player page parser and added nfl team graphics
!
!   Revision 1.4  2004/04/07 11:22:05  dantheman
!   game parser now builds an array suitable for insert into scoringData
!
!   Revision 1.3  2004/04/03 07:07:04  dantheman
!   Split nflgc/parser.php into component files.
!
!   Revision 1.2  2004/04/03 05:22:51  dantheman
!   the game parser is pretty functional
!
!   Revision 1.1.1.1  2004/04/03 02:58:05  dantheman
!   Imported sources
!
!
/ %FE%--------------------------------------------------------------*/

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_FixTheDamnKick
!
!  Description: For reasons I cannot tell, ParseSource.xsl loses a 
!                    space on extra points and conversions.  This is a 
!                    kludgey fix for that and some general formatting.
!
!  Arguments:
!   $str - string - the javascript string to parse.
!
!  Results:
!   A string.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_FixTheDamnKick($str) {
	$str = str_replace("kick is good)", " kick is good)", $str);
	$str = str_replace("failed)", " failed)", $str);
	$str = str_replace("kick Failed)", " kick failed)", $str);
	$str = str_replace("2 Pt. Conversion", " 2 pt conversion", $str);
	$str = str_replace("  ", " ", $str);
	return trim($str);
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_NewPlayerFromSource
!
!  Description: Get player from nfl.com, parse it and insert in DB.
!
!  Arguments:
!   $playerID - string - primary key for nflgc_player table
!   $playerURL - string - URL of nfl.com resource
!
!  Results:
!   An array of player data.
!
!  Errors Thrown:
!    "Player ID '$playerID' not found at nfl.com".
!    "Cannot eval playerSrcArray from script: $php".
!    "Cannot retrieve player image, $playerpicURL".
/ %PE%--------------------------------------------------------------*/
function nflgc_NewPlayerFromSource($playerID, $playerURL, $updateFlag) {
	#--------------------------------------------------------------------
	# Retrieve the source html player data.
	# Convert it to xhtml.
	#--------------------------------------------------------------------
	$html = ffl_GetWebSource($playerURL);
	$xhtml = nflgc_TidySource($html, ' start formfile="nflnorm_player_profile.htm" // -->');
	if ( ($html === false) || ($xhtml == false)) { 
		ffl_Error(FFLERROR, "Player ID '$playerID' not found at nfl.com");
		return false;
	}
	#--------------------------------------------------------------------
	# Use XSLT to transform the xhtml data
	# into an 'eval-able' command.
	#--------------------------------------------------------------------
	$php = nflgc_ParseSource($xhtml, 'player');
	if ( @eval('$playerSrcArray = array('.$php.');') === false ) {
		ffl_Error(FFLERROR, "Cannot eval playerSrcArray from script: $php");
	}
	#--------------------------------------------------------------------
	# Data returned from XSLT can be in two different patterns.
	#--------------------------------------------------------------------
	$name  = explode(
		' ', preg_replace( "/\s+/", " ", $playerSrcArray["jersey_name_position"])
	);
	if ( strpos(implode('',$name), '#') === false) {
		#--------------------------------------------------------------------
		# PATTERN A has no number sign and the position before the name
		#
		# Separate 'jersey_name_position'.
		#--------------------------------------------------------------------
		$jersey = $name[0];
		array_shift($name);
		$position = $name[(count($name)-1)];
		array_pop($name);
		$name = trim(implode(' ', $name));
		#--------------------------------------------------------------------
		# Separate 'height_weight_birth'.
		#--------------------------------------------------------------------
		$height_weight_birth = preg_replace( "/\s+/", " ", $playerSrcArray["height_weight_birth"]);
		list($trash, $height, $trash, $weight, $trash, $birth) = explode(' ', $height_weight_birth);
		#--------------------------------------------------------------------
		# Separate 'college_nflexperience'.
		#--------------------------------------------------------------------
		$college_nflexperience = str_replace( 'College:', "", $playerSrcArray["college_nflexperience"]);
		$college_nflexperience = str_replace( 'NFL Experience:', "", $college_nflexperience);
		$college_nflexperience = preg_replace( "/\s+/", " ", $college_nflexperience);
		$college = explode(' ', $college_nflexperience);
		$nflexperience = $college[(count($college)-1)];
		array_pop($college);
		$college = trim(implode(' ', $college));
	} else {
		#--------------------------------------------------------------------
		# PATTERN B has number sign for jersey which comes after
		# name and no position data jersey_name_position.
		# Also college and experience info is in height_weight_birth
		#
		# Separate 'jersey_name_position'.
		#--------------------------------------------------------------------
		$jersey = substr( $name[(count($name)-1)], 1 );
		array_pop($name);
		$name = trim(implode(' ', $name));
		#--------------------------------------------------------------------
		# Separate 'height_weight_birth'.
		#--------------------------------------------------------------------
		$height_weight_birth = preg_replace( "/\s+/", " ", $playerSrcArray["height_weight_birth"]);
		list(
			$trash, $height, $trash, $weight, $trash, $birth, $trash,
			$college, $trash, $trash, $nflexperience
		) = explode(' ', $height_weight_birth);
		#--------------------------------------------------------------------
		# 'college_nflexperience' has no useful data.
		#--------------------------------------------------------------------
	}
	#--------------------------------------------------------------------
	# Get team (and, if Pattern B, position).
	#--------------------------------------------------------------------
	$rosterURL = trim($playerSrcArray["teamroster"]);
	$trash = explode('/', $rosterURL);
	$team = $trash[(count($trash)-1)];
	if ( empty($position) ) {
		$trash = explode( '/', trim($playerSrcArray["others_at_position"]) );
		$position = $trash[(count($trash)-1)];
		$trash = explode('_', $position);
		$position = $trash[1];
	}
	#--------------------------------------------------------------------
	# Convert $birth to unix time.
	#--------------------------------------------------------------------
	list($birthM, $birthD, $birthY) = explode('/', $birth);
	$birth = mktime(0,0,0,$birthM,$birthD, $birthY);
	if ($birth < 10000) {
		# Born before the unix epoch, make 'em 12/31/1969.
		$birth = 10000;
	}
	#--------------------------------------------------------------------
	# Figure out the first and last name from $name.
	#--------------------------------------------------------------------
	$name = explode( ' ', $name );
	if ( count($name) != 2 ) {
		#--------------------------------------------------------------------
		# If player has more than 2 names, check the nfl.com
		# team roster page to decide whether extra names
		# belong to first or last. 
		# (The roster page lists names "last, first".)
		#--------------------------------------------------------------------
		foreach ( file(MYFFLSRCROOT."/$rosterURL") as $line ) {
			$lastname = $name[count($name)-1];
			if ( strpos($line, "$lastname, ") !== false) {
				$pattern = "/$playerID.>(.+ $lastname, [^<]+)/";
				preg_match($pattern, $line, $matches);
			}
		}
		$name = array_reverse( explode(',', $matches[1]) );
	}
	#--------------------------------------------------------------------
	# Retrieve the player icon.
	#--------------------------------------------------------------------
	$playerpicURL = MYFFLPLAYERPICSRC.$playerID.'.jpg';
	$iconFile = WEBIMAGEDIR.'/'.FFLMODULE.'/nfl/players/'.$playerID.'_icon.jpg';
	if ( $sourcefile_id = @imageCreateFromJPEG($playerpicURL) ) {
		@ffl_ResizePic($sourcefile_id, $iconFile, '24');
	} else {
		ffl_Error(FFLWARNING, "Cannot retrieve player image, $playerpicURL");
		@copy(WEBIMAGEDIR.'/'.FFLMODULE.'/nfl/players/default_icon.jpg', $iconFile);
	}
	#--------------------------------------------------------------------
	# Do a little validation.
	#
	# Known Bug: A player with three names and no jersey
	# number will not be caught here.
	#--------------------------------------------------------------------
	if ( !$updateFlag && empty($name[0]) && empty($name[1]) && $birth==10000 ) {
		# Bad NFL.com player ID (but nfl.com does not return an error).
		ffl_Error(FFLERROR, "Player ID '$playerID' not found at nfl.com");
		return false;
	}
	if ( !$updateFlag && 
		(!is_numeric($jersey) || empty($name[0]) || empty($name[1]))
	) {
		ffl_Error(FFLWARNING, 
			"Player parser yielded questionable data for player $playerID.
Check the player's name ($name[1], $name[0] : $jersey???) in the database.");
	}
	if ( empty($name[0]) && !is_numeric($jersey) ) {
		# Players no longer on roster have no jersey.
		$name[0] = $jersey;
		$jersey = '';
	}
	if ( empty($name[1]) ) {
		# Players no longer on roster often have no position.
		$name[1] = $position;
		$position = '';
	}
	#--------------------------------------------------------------------
	# Update the database.
	#--------------------------------------------------------------------
	$name[0] = addslashes($name[0]);
	$name[1] = addslashes($name[1]);
	$position = addslashes($position);
	$team = addslashes($team);
	$jersey = addslashes($jersey);
	$height = addslashes($height);
	$weight = addslashes($weight);
	$birth = addslashes($birth);
	$college = addslashes($college);
	$nflexperience = addslashes($nflexperience);
	if ($updateFlag) {
		# Update the database, but do not enter any blanks.
		$query = "UPDATE ".FFLDB_MODPRE."player SET ";
		if ( strlen($position) > 0 ) { $query .= "positionID='$position', "; }
		if ( strlen($team) > 0 ) { $query .= "teamID='$team', "; }
		if ( strlen($jersey) > 0 ) { $query .= "jersey='$jersey', "; }
		if ( strlen($height) > 0 ) { $query .= "height='$height', "; }
		if ( strlen($weight) > 0 ) { $query .= "weight='$weight', "; }
		if ( strlen($birth) > 0 ) { $query .= "birth='$birth', "; }
		if ( strlen($college) > 0 ) { $query .= "college='$college', "; }
		if ( strlen($nflexperience) > 0 ) { $query .= "nflexperience='$nflexperience' "; }
		$query .= " WHERE id='$playerID' ";
	} else {
		$query = "INSERT INTO ".FFLDB_MODPRE."player (
			id, lastname, firstname, teamID, positionID, jersey,
			height, weight, birth, college, nflexperience
			) VALUES ( 
			'$playerID', '$name[1]', '$name[0]', '$team',
			'$position', '$jersey', '$height', '$weight', '$birth',
			'$college', '$nflexperience'
			) ";
	}
	if (!$result = mysql_query($query)) {
		ffl_Error( FFLERROR, "Cannot update Player database.\n" ); 
		ffl_Error( FFLDEBUG, "$query\n" ); 
		ffl_Error( FFLDEBUG, mysql_error() ); 
	}
	#--------------------------------------------------------------------
	# Return player array.
	#--------------------------------------------------------------------
	return array( 
		$name[0], $name[1], $position, $team, $jersey, $height,
		$weight, $birth, $college, $nflexperience
	);
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_ParsedGame2ScoringCategories
!
!  Description: .
!
!  Arguments:
!   $source - string - html returned from web resource
!
!  Results:
!   An xhtml string.
!
!  Errors Thrown:
!   Cannot build scoring category array.
/ %PE%--------------------------------------------------------------*/
function nflgc_ParsedGame2ScoringData($gameArray) {
	#--------------------------------------------------------------------
	# Get the individual scoring categories from the database,
	# and convert the data in the $gameArray to an array
	# suitable for insertion into the scoringData table.
	#--------------------------------------------------------------------
	$scCategories = array();
	$scData = array();
	$query = "SELECT id FROM ".FFLDB_MODPRE."scoringCategory
		WHERE count_ind='yes' ";
	if (!$result = @mysql_query($query)) {
		ffl_Error( FFLERROR, "Cannot build scoring category array.\n" ); 
		ffl_Error( FFLDEBUG, mysql_error() ); 
	} else {
		while ( $row = @mysql_fetch_row($result) ) {
			$scCategories[] = $row[0];
		}
	}
	foreach ($scCategories as $category) {
		$scData[] = nflgc_ScoringClass2Category($category, $gameArray);
	}
	#--------------------------------------------------------------------
	# Consolidate the scData array.
	# Basically this just removes an extraneous level from the array.
	#--------------------------------------------------------------------
	$consolidated = array();
	foreach ($scData as $subarray) {
		foreach ($subarray as $subsubarray) {
			$consolidated[] = $subsubarray;
		}
	}
	#--------------------------------------------------------------------
	# Yet again, consolidate the consolidated array.
	# This gives us the final individual scoring stats.
	#--------------------------------------------------------------------
	$rtn = array();
	foreach ( $consolidated as $score ) {
		$player = $score[1];
		$category = $score[0];
		$quantity = $score[2];
		if ( !is_array($rtn["$player"]) ) {
			$rtn["$player"] = array();
		}
		$rtn["$player"]["$category"] = $rtn["$player"]["$category"] + $quantity;
	}
	#--------------------------------------------------------------------
	# Print array if debug set to 10+.
	#--------------------------------------------------------------------
	if ( FFLDEBUGLVL >= 10 ) {
		ffl_Error( FFLDEBUG, "Array returned from
nflgc_ParsedGame2ScoringCategories follows.
It is an array of player IDs whose values is an
array of scoring category ID keys and quantity
values." );
		ffl_Error( FFLDEBUG, print_r($rtn, true) );
	}

	return $rtn;
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_ParseGame
!
!  Description: 
!
!  Arguments:
!   $source - string - html returned from web resource
!
!  Results:
!   An array.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_ParseGame($gameID, $gameURL) {
	#--------------------------------------------------------------------
	# Get the external html resorce ad convert it to xhtml.
	#--------------------------------------------------------------------
	$html = ffl_GetWebSource($gameURL);
	$xhtml = nflgc_TidySource($html, ' START SCOREBOARD -->');
	#echo '<pre>'.htmlspecialchars($xhtml).'</pre>';
	#--------------------------------------------------------------------
	# Get the scoring classes from the database.
	#--------------------------------------------------------------------
	$scClasses = array();
	$query = "SELECT id FROM ".FFLDB_MODPRE."scoringClass";
	if (!$result = mysql_query($query)) {
		ffl_Error( FFLERROR, "Cannot build scoring class array.\n" ); 
		ffl_Error( FFLDEBUG, mysql_error() ); 
	} else {
		while ( $row = mysql_fetch_row($result) ) {
			$scClasses[] = $row[0];
		}
	}
	#--------------------------------------------------------------------
	# Parse the xhtml into a php array of arrays for each
	# scoring class.
	#--------------------------------------------------------------------
	$gameArrayScript .= '$'."gameArray['GAMEID'] = '$gameID';\n\n";
	foreach ( $scClasses as $class ) {
		$gameArrayScript .= '$gameArray["'.$class.'"] = array( '.nflgc_ParseSource($xhtml, $class).' );'."\n\n";
	}
	if ( (@eval($gameArrayScript)) === false ) {
		ffl_Error( FFLWARNING, "Cannot eval parsed source, \"$gameURL\"." );
		ffl_Error( FFLDEBUG, $gameArrayScript );
	};
	#--------------------------------------------------------------------
	# Return the array of arrays.
	#--------------------------------------------------------------------
	return $gameArray;
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_ParseSource
!
!  Description: Extract the data relevant to a given nflgc 
!                      scoring class from an xml source string.
!
!  Arguments:
!   $source - string - the string to parse
!   $class - string - return data relevant to this scoring class.
!
!  Results:
!   An array.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_ParseSource($source, $class) {
	$xml = ffl_Write2TmpFile($source);
	$xsl = LIBDIR.'/'.FFLMODULE;
	$args = array();
	switch ($class) {
		case 'STATE':
			$xsl .= '/ParseSource_STATE.xsl';
			break;
		case 'SC':
			$xsl .= '/ParseSource_SC.xsl';
			break;
		case 'player':
			$xsl .= '/ParsePlayerSource.xsl';
			break;
		default:
			$xsl .= '/ParseSource.xsl';
			$args['classString'] = $class;
	}
	$rtn = ffl_ApplyXSL($xml, $xsl, $args);
	unlink($xml);
	return $rtn;
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_ScoringClass2Category
!
!  Description: Takes an array of game data and build an
!                      array of individual player scoring data for a
!                      given category.
!
!  Arguments:
!   $category - integer - the scoring category ID
!   $srcArr - array - the array of game data
!
!  Results:
!   An array.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_ScoringClass2Category($category, $srcArr) {
	$rtn = array();
	switch ($category) {
		case 1: # OFFENSIVE TOUCHDOWNS
			foreach ( array_merge($srcArr['PS'], $srcArr['RU'], $srcArr['REC']) as $data ) {
				if ( $data['td'] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $data['td'] );
				}
			}
			break;
		case 2: # 2-POINT CONVERSIONS
			foreach ( $srcArr['SC'] as $score ) {
				# Check the part of the description inside the first parens.
				# Look for "2 pt conversion" -- but not accompanied by "failed".
				$descripsplit = explode( '(', $score['descrip']);
				if ( 
					( strcmp($score['type'], 'TD') == 0 ) &&
					( strpos($descripsplit[1], '2 pt conversion') !== false ) &&
					( strpos($descripsplit[1], 'failed') === false )
				) {
					# Who gets credit?
					# If TD was passing player3 does, if rushing player2 does.
					if ( strpos($descripsplit[0], 'pass from') !== false ) {
						$firstcredit = '3';
					} else {
						$firstcredit = '2';
					}
					$rtn[] = array( $category,  $score['player'.$firstcredit], '1' );
					# Look for potential receiver for the 2pt pass.
					if ( strpos($descripsplit[1], 'pass to') !== false ) {
						$rtn[] = array( $category,  $score['player'.($firstcredit+1)], '1' );
					}
				}
			}
			break;
		case 3: # COMPLETIONS
			foreach ( $srcArr['PS'] as $data ) {
				$cp = explode("/", $data['cp_at']);
				if ( $cp[0] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $cp[0] );
				}
			}
			break;
		case 4: # PASSING YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'PS', 'yds');
			break;
		case 5: # THROWN INTERCEPTIONS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'PS', 'int');
			break;
		case 6: # RUSHING YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'RU', 'yds');
			break;
		case 7: # CATCHES
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'REC', 'rec');
			break;
		case 8: # RECEIVING YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'REC', 'yds');
			break;
		case 9: # FUMBLES
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'FUM', 'lost');
			break;
		case 10: # FG LESS THAN 30 YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper2($category, $srcArr, '12');
			break;
		case 11:  # FG 30-39 YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper2($category, $srcArr, '3');
			break;
		case 12:  # FG 40-49 YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper2($category, $srcArr, '4');
			break;
		case 13:  # FG GREATER THAN 50 YARDS
			$rtn = nflgc_ScoringClass2CategoryHelper2($category, $srcArr, '56789');
			break;
		case 14:  # EXTRA POINT
			foreach ( $srcArr['K'] as $data ) {
				$xp = explode("/", $data['xp']);
				if ( $xp[0] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $xp[0] );
				}
			}
			break;
		case 15:  # EXTRA POINT MISSED
			foreach ( $srcArr['K'] as $data ) {
				$xp = explode("/", $data['xp']);
				$misses = $xp[1] - $xp[0];
				if ( $misses > 0 ) {
					$rtn[] = array( $category,  $data['player'], $misses );
				}
			}
			break;
		case 16: # KICKOFF RTN YARDS
			foreach ( $srcArr['KOR'] as $data ) {
				$yards = round($data['no'] * $data['avg']);
				if ( $yards > 0 ) {
					$rtn[] = array( $category,  $data['player'], $yards );
				}
			}
			break;
		case 17: # PUNT RTN YARDS
			foreach ( $srcArr['PTR'] as $data ) {
				$yards = round($data['no'] * $data['avg']);
				if ( $yards > 0 ) {
					$rtn[] = array( $category,  $data['player'], $yards );
				}
			}
			break;
		case 18: # INTERCEPTIONS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'DEF', 'int');
			break;
		case 19: # SACKS
			foreach ( $srcArr["DEF"] as $data ) {
				# To handle 1/2 sacks, sacks are multiplied by 10.
				$sacks = $data['sck'] * 10;
				if ( $sacks > 0 ) {
					$rtn[] = array( $category,  $data['player'], $sacks );
				}
			}
			break;
		case 20: # RECOVERED FUMBLES
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'FUM', 'rec');
			break;
		case 21: # FUMBLES FORCED
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'DEF', 'ff');
			break;
		case 22: # SAFETY
			# Currently, only team defenses are credited with the safety.
			# Game Center's attribution for individuals is too inconsistent to parse.
			# (Individual credit could to given manually of course.)
			break;
		case 23-33: # Categories 23-33 are Team Defense only.
			break;
		case 34: # DEFENSIVE TD
			foreach ( $srcArr['SC'] as $score ) {
				if ( 
					( strcmp($score['type'], 'TD') == 0 ) &&
					( preg_match("/(fumble|interception)\sreturn/i", $score['descrip']) )
				) {
					$rtn[] = array( $category,  $score['player1'], '1' );
				}
			}
			break;
		case 35: # SPECIAL TEAMS TD
			foreach ( array_merge($srcArr['KOR'], $srcArr['PTR']) as $data ) {
				if ( $data['td'] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $data['td'] );
				}
			}
			break;
		case 36: # PUNTS INSIDE 20
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'PNT', 'i20');
			break;
		case 37: # RUSHING ATTEMPTS
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'RU', 'att');
			break;
		case 38:  # MISSED FIELD GOAL
			foreach ( $srcArr['K'] as $data ) {
				$fg = explode("/", $data['fg']);
				$misses = $fg[1] - $fg[0];
				if ( $misses > 0 ) {
					$rtn[] = array( $category,  $data['player'], $misses );
				}
			}
			break;
		case 39:  # TACKLES
			foreach ( $srcArr['DEF'] as $data ) {
				$ta = explode("-", $data['t_a']);
				if ( $ta[0] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $ta[0] );
				}
			}
		case 40:  # ASSISTS
			foreach ( $srcArr['DEF'] as $data ) {
				$ta = explode("-", $data['t_a']);
				if ( $ta[1] > 0 ) {
					$rtn[] = array( $category,  $data['player'], $ta[1] );
				}
			}
			break;
		case 41: # INDIVIDUAL FUMBLE RECOVERY
			$rtn = nflgc_ScoringClass2CategoryHelper1($category, $srcArr, 'FUM', 'rec');
			break;
	}
	return $rtn;
}
function nflgc_ScoringClass2CategoryHelper1($category, $srcArr, $class, $stat) {
	$rtn = array();
	foreach ( $srcArr["$class"] as $data ) {
		if ( $data["$stat"] > 0 ) {
			$rtn[] = array( $category,  $data['player'], $data["$stat"] );
		}
	}
	return $rtn;
}
function nflgc_ScoringClass2CategoryHelper2($category, $srcArr, $digits) {
	$rtn = array();
	foreach ( $srcArr['SC'] as $score ) {
		if ( ( strcmp($score['type'], 'FG') == 0 ) &&
				( preg_match("/[$digits]. yd,/i", $score['descrip']) ) ) {
			$rtn[] = array( $category,  $score['player1'], '1' );
		}
	}
	return $rtn;
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_StripTeamGraphicURI
!
!  Description: Return the team ID portion of a nfl.com image URI.
!
!  Arguments:
!   $uri - string - the string to clean
!
!  Results:
!   A string.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_StripTeamGraphicURI($uri) {
	$uri = explode( '/', $uri);
	$index = count($uri) - 1;
	$image = $uri[$index];
	$image = explode('.', $image);
	return $image[0];
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_StripPlayerURI
!
!  Description: Return the player ID portion of a nfl.com URI.
!
!  Arguments:
!   $uri - string - the string to clean
!
!  Results:
!   A string.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_StripPlayerURI($uri) {
	$uri = explode( '/', $uri);
	$index = count($uri) - 1;
	return $uri[$index];
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_StripTimeJavascript
!
!  Description: Return timestamp portion of the gamecenter
!                      live scoreboard.  This function supports the
!                      ParseSource.xsl stylesheet.
!
!  Arguments:
!   $js - string - the javascript string to parse.
!
!  Results:
!   A string.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_StripTimeJavascript($js) {
	$js = explode("'", $js);
	$timeRemaining = $js[3];
	if ( is_numeric($timeRemaining) ) {
		return date('h:i', $timeRemaining);
	} else {
		return $timeRemaining;
	}
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_TeamUnitScoringData
!
!  Description: 
!
!  Arguments:
!   $category - integer - scoring data table category id.
!   $units - array - assign data to these fantasy team-unit "players".
!   $away - string - away team ID.
!   $home - string - home team ID.
!   $srcArr - array - parsed data from nfl.com resource.
!
!  Results:
!   A array containing the given category, unit name and
!   the quantity of scoring data earned for each of the $units.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_TeamUnitScoringData( $category, $units, $away, $home, $srcArr ) {
	global $args;
	$rtn = array();
	foreach ( $units as $unit ) {
		if ( $category == 20 || $category == 30 ) {
			# Count fumbles lost for other team.
			$queryCategory = 9;
			$teamID_operator = '!=';
		} else {
			$queryCategory = $category;
			$teamID_operator = '=';
		}
		if (
			($category >= 16 && $category <= 21) ||
			($category >= 34 && $category <= 36)
		) {
			foreach ( array($away, $home) as $team ) {
				$query = "SELECT d.quantity 
					FROM nflgc_scoringDataLive AS d, 
					nflgc_player AS p 
					WHERE d.categoryID='$queryCategory' 
						AND d.gameID='".$srcArr['GAMEID']."' 
						AND d.playerID = p.id
						AND p.teamID $teamID_operator '$team' ";
				if (!$result = mysql_query($query)) {
					ffl_Error( FFLWARNING, "Database error in nflgc_TeamUnitScoringData.\n" ); 
					ffl_Error( FFLDEBUG, "Query: \"$query\" returned the following error" ); 
					ffl_Error( FFLDEBUG, mysql_error() ); 
				} else {
					$quantity = 0;
					while ( $row = mysql_fetch_array($result) ) {
						$quantity = $quantity + $row[0];
					}
				}
				if ( $quantity ) {
					$rtn[] = array( $category,  'T'.$unit.'_'.$team, $quantity);
				}
			}
		} elseif ( $category == 22 ) {
			$quantity = array();
			foreach ( $srcArr['SC'] as $score ) {
				if ( strcmp($score['type'], 'SAFETY') == 0 ) {
					$quantity[$score['team']]++ ;
				}
			}
			if ( $quantity[$away] ) {
				$rtn[] = array( $category,  'T'.$unit.'_'.$away, $quantity[$away]);
			}
			if ( $quantity[$home] ) {
				$rtn[] = array( $category,  'T'.$unit.'_'.$home, $quantity[$home]);
			}
		} elseif ( $category >= 23 && $category <= 29 ) {
			# Points Against
			# We really just have to do this once.
			if ( $category != 23 ) { continue; }
			# Do away team
			$pointsAgainst = $srcArr['STATE']['homescore'];
			if ( $pointsAgainst == 0 ) { $newCat = 23; }
			elseif ( $pointsAgainst < 7 ) { $newCat = 24; }
			elseif ( $pointsAgainst < 14 ) { $newCat = 25; }
			elseif ( $pointsAgainst < 21 ) { $newCat = 26; }
			elseif ( $pointsAgainst < 30) { $newCat = 27; }
			elseif ( $pointsAgainst < 40 ) { $newCat = 28; }
			else { $newCat = 29; }
			$rtn[] = array( $newCat,  'T'.$unit.'_'.$away, '1');
			# and same for home team
			$pointsAgainst = $srcArr['STATE']['awayscore'];
			if ( $pointsAgainst == 0 ) { $newCat = 23; }
			elseif ( $pointsAgainst < 7 ) { $newCat = 24; }
			elseif ( $pointsAgainst < 14 ) { $newCat = 25; }
			elseif ( $pointsAgainst < 21 ) { $newCat = 26; }
			elseif ( $pointsAgainst < 30) { $newCat = 27; }
			elseif ( $pointsAgainst < 40 ) { $newCat = 28; }
			else { $newCat = 29; }
			$rtn[] = array( $newCat,  'T'.$unit.'_'.$home, '1');
		} elseif ( $category >= 30 && $category <= 33 ) {
			# Yards Against
			# We really just have to do this once.
			if ( $category != 30 ) { continue; }
			foreach ( array($away, $home) as $team ) {
				$yardsAgainst = 0;
				$query = " SELECT sum(d.quantity) 
					FROM nflgc_scoringDataLive AS d, nflgc_player AS p 
					WHERE (d.categoryID=4 OR d.categoryID=6)
						AND d.gameID= '".$srcArr['GAMEID']."'
						AND d.playerID = p.id AND p.teamID $teamID_operator '$team' ";
				$result = mysql_query($query);
				if ( !$yardsAgainst = mysql_result($result, 0) ) {
					# If mysql error, set yards against in an average range.
					$yardsAgainst = 301;
				}
				if ( $yardsAgainst <= 150 ) { $newCat = 30; }
				elseif ( $yardsAgainst <= 275 ) { $newCat = 31; }
				elseif ( $yardsAgainst <= 400 ) { $newCat = 32; }
				else { $newCat = 33; }
				$rtn[] = array( $newCat,  'T'.$unit.'_'.$team, '1');
			}
		}
	}
	return $rtn;
}

/*%PS%----------------------------------------------------------------
!  Name:    nflgc_TidySource
!
!  Description: Remove extraneous bits from raw web 
!                      resource data and convert it to xhtml.
!
!  Arguments:
!   $source - string - html returned from web resource
!   $garbageEnd - pattern - anything in $source before this string
!                          will be discarded.
!
!  Results:
!   An xhtml string.
!
!  Errors Thrown:
!   None.
/ %PE%--------------------------------------------------------------*/
function nflgc_TidySource($source, $garbageEnd) {
	#--------------------------------------------------------------------
	# Remove useless stuff at the beginning.
	#--------------------------------------------------------------------
	$source = ereg_replace('.+'.$garbageEnd, '', $source);

	#--------------------------------------------------------------------
	# Tidy what's left to get xhtml.
	#--------------------------------------------------------------------
	if ( !$tidyOut = ffl_TidyHtml($source) ) {
		$tidyOut =  "Tidy error in nflgc_TidySource().";
	}
	#--------------------------------------------------------------------
	# We don't need the html or head tags, and they seem
	# to freak out the XSLT engine for some reason, so we
	# remove them here.
	#--------------------------------------------------------------------
	$tidyOut = ereg_replace( '<html .+</head>', '', $tidyOut);
	$tidyOut = str_replace( '</html>', '', $tidyOut);
	#--------------------------------------------------------------------
	# Return.
	#--------------------------------------------------------------------
	return $tidyOut;
}

?>
Return current item: My Free Fantasy League