Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/maintenance/processwaivers.php
<?php
/**
 * Processes waiver claims
 *
 * This file should be run via cron.  I recommend once daily on Tues->Sat at 2AM, but you can pick
 * 
 * that line would look like:
 * 
 * 0 2 * * 1-5 /path/to/php /path/to/processwaivers.php
 * 
 * alternately, win32 servers can use pycron (http://www.kalab.com/freeware/pycron/pycron.htm)
 * and have a line like
 * 
 * 0 2 * * 1-5 "c:\path\to\php" "c:\path\to\processwaivers.php"
 * 
 * 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 prematurely execute a waiver claim if the
 * file is public.  Security through obscurity is a poor substitute.
 * 
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-maint
 */

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

/*
	get all waivers, but use only the first legit one found.
*/

$x = new OFFL_League();
$leagues = $x->getAllLeagues();
$waiver_lookup = new OFFL_Waiver();

foreach ($leagues as $league)
{
	do
	{
		$all_waivers = $waiver_lookup->getAllWaivers($league->getLeagueID());
		$waiver_found = 0;
		foreach ($all_waivers as $waiver)
		{
			if (($waiver->getWaiverDeadline() - time()) < 0) // that is, time() is past the set deadline
			{
				$due_waiver = new OFFL_Waiver($waiver->getWaiverID());
				$waiver_found = 1;
				break;
			}
		}

		if (!isset($due_waiver))
		{	break;	}

		$addplayer = new OFFL_Player($due_waiver->getPlayerID(), $league->getLeagueID());
		if ($addplayer->getFFLTeamID() != 0) // player already picked up
		{
			$due_waiver->deleteWaiver();
			continue;
		}
		$addplayer->setStarter(0);
		if ($due_waiver->getDropPlayerID() != 0)
		{
			$dropPlayer = new OFFL_Player($due_waiver->getDropPlayerID(), $league->getLeagueID());
			if ($dropPlayer->getFFLTeamID() != $due_waiver->getFFLTeamID()) // if player to be dropped not on team any more
			{
				$due_waiver->deleteWaiver();
				continue;
			}
		}
		// now we have a potential transaction.  test as in rosters.php


		$thisTeam = new OFFL_FFLTeam($due_waiver->getFFLTeamID());
		$roster_old = $thisTeam->getRoster();
		$roster = array();
		$roster_removed = array(); // catch the players who are dropped

		foreach($roster_old as $player)
		{
			if ($player->getPlayerID() != $due_waiver->getDropPlayerID())
				$roster[] = $player;
			else
				$roster_removed[] = $player;
		}
		$roster[] = $addplayer;
				
		usort($roster, "cmp_players"); // this function is in player.php -- should include correctly.

		$errors = $thisTeam->validateRosterFlex($roster);

		if (!sizeof($errors))
		{
			// success!  process the waiver

			$transaction = new OFFL_Transaction();
			$transaction->setTransType("waiver");
			$transaction->setLeagueID($league->getLeagueID());
			$transaction->save();

			$transItem = new OFFL_TransactionItem();
			$transItem->setTransID($transaction->getTransID());
			$transItem->setTransType("addw");
			$transItem->setPlayerID($due_waiver->getPlayerID());
			$transItem->setToFFLTeamID($due_waiver->getFFLTeamID());
			$transItem->save();
			if ($due_waiver->getDropPlayerID() != 0)
			{
				$transItem = new OFFL_TransactionItem();
				$transItem->setTransID($transaction->getTransID());
				$transItem->setTransType("drop");
				$transItem->setPlayerID($due_waiver->getDropPlayerID());
				$transItem->setFromFFLTeamID($due_waiver->getFFLTeamID());
				$transItem->save();
			}

			// fix the new team roster
			$thisTeam->setRoster($roster);
			$thisTeam->save();

			// and drop the other players appropriately
			foreach ($roster_removed as $player)
			{
				$wp = new OFFL_WaiverPlayer();
				$wp->setLeagueID($league->getLeagueID());
				$wp->setPlayerID($player->getPlayerID());
				$wp->setWaiverDeadline(time());
				$wp->save();
			}
		}


		// team priorities resorted ONLY when a waiver claim is successfully processed.
		// NO PENALTY for failed claims
		$due_waiver->resetTeamPriority($due_waiver->getFFLTeamID());
		$due_waiver->deleteWaiver();

		unset($due_waiver);

	} while ($waiver_found);

	// Now I need to disable waiver status on unclaimed players who came due.

	$wp = new OFFL_WaiverPlayer();
	$waiverplayers = $wp->getAllWaiverPlayers($league->getLeagueID());
	foreach ($waiverplayers as $wp)
	{
		if (($wp->getWaiverDeadline() - time()) < 0) // player due to come off waivers
		{	$wp->deleteWaiverPlayer();	}
	}

} // end foreach $leagues

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