<?php
#awards - more complicaded...
include_once('./queries/getBadgeByID.php');
include_once('./queries/getMedalByID.php');
include_once('./queries/getRibbonByID.php');
include_once('./queries/getSFRibbonsByID.php');
include_once('./queries/getSFMedalByID.php'); #добавил Excavator_red
include_once('./queries/getSFBadgeByID.php');


define('AWD', 0);
define('LEVEL', 1);
define('EARNED', 2);
define('FIRST', 3);
define('NAME', 4);

function achieved($has, $value)
{
	if ($has == $value) return 'achieved';
	else return 'notachieved';
}

function earned($date)
{
	if ($date > 0)
		return ' (<i>'.date('Y-m-d H:i:s', $date).'</i>)';
}

function getBadgeLevel($value)
{
	for ($LEVEL=3; $LEVEL>=0; $LEVEL--)
	{
		if ($value[$LEVEL][EARNED] > 0)
		{
			return $LEVEL;
		}
		if ($value[$LEVEL][EARNED] == $LEVEL) // both is zero
			return 0;
	}
}

function getRibbonLevel($value)
{
	for ($LEVEL=2; $LEVEL>=0; $LEVEL--)
	{
		if ($value[$LEVEL][EARNED] > 0)
		{
			return $LEVEL;
		}
		if ($value[$LEVEL][EARNED] == $LEVEL) // both is zero
			return 0;
	}
}

function getAwardByPID_and_AWD($PID, $AWD, $LEVEL)
{
	include('./queries/getAwardByPID_and_AWD.php'); // imports the correct sql statement
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	
	$awards = array();
	$idx = 0;
	while ($row = mysql_fetch_assoc($result)) {
		$awards[$idx] = $row;
		$idx++;
	}
	// Free resultset
	mysql_free_result($result);
	return $awards;
}

function getAwardByPID_and_AWD_NOLEVEL($PID, $AWD)
{
	include('./queries/getAwardByPID_and_AWD_NOLEVEL.php'); // imports the correct sql statement
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	
	$awards = array();
	$idx = 0;
	while ($row = mysql_fetch_assoc($result)) {
		$awards[$idx] = $row;
		$idx++;
	}
	// Free resultset
	mysql_free_result($result);
	return $awards;
}


function getAwardsByPID($PID)
{
	$PlayerAwards = array();
	# grab all badges
	# $PlayerAwards[$awd]
	# $PlayerAwards[$awd][$level]
	# $PlayerAwards[$awd][$level]['earned']
	# $PlayerAwards[$awd][$level]['first']
	


	$PlayerAwards = array();

	#get badges
	$count = getBadgeCount();
	for ($i=0; $i<$count; $i++)
	{
		$AWD = trim(getBadge($i));
		for ($LEVEL=0; $LEVEL<4; $LEVEL++) // levels!
		{
			$award = getAwardByPID_and_AWD($PID, $AWD, $LEVEL);
			$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
			$PlayerAwards[$i][$LEVEL][LEVEL] = $LEVEL;
			$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
			$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
			$PlayerAwards[$i][$LEVEL][NAME] = getBadgeByID($AWD);		
		}
	}
	
	#append next after those
	#get Medals
	$oldcount = $count;
	$count = $oldcount+getMedalCount();
	for ($i=$oldcount; $i<$count; $i++)
	{
		$AWD = trim(getMedal($i-$oldcount));
		$LEVEL=0; // levels!
		$award = getAwardByPID_and_AWD_NOLEVEL($PID, $AWD);
		$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
		$PlayerAwards[$i][$LEVEL][LEVEL] = isset($award[0]['level']) ? $award[0]['level'] : 0;
		$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
		$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
		$PlayerAwards[$i][$LEVEL][NAME] = getMedalByID($AWD);		
	}	
	#append next after those
	#get Ribbons
	$oldcount = $count;
	$count = $oldcount+getRibbonCount();
	for ($i=$oldcount; $i<$count; $i++)
	{
		$AWD = trim(getRibbon($i-$oldcount));
		$LEVEL=0; // levels!
		$award = getAwardByPID_and_AWD_NOLEVEL($PID, $AWD);
		$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
		$PlayerAwards[$i][$LEVEL][LEVEL] = isset($award[0]['level']) ? $award[0]['level'] : 0;
		$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
		$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
		$PlayerAwards[$i][$LEVEL][NAME] = getRibbonByID($AWD);		
	}		
	
	#append next after those	
	#get SFbadges
	$oldcount = $count;
	$count = $oldcount+getSFBadgeCount();
	for ($i=$oldcount; $i<$count; $i++)
	{
		$AWD = trim(getSFBadge($i-$oldcount));
		for ($LEVEL=0; $LEVEL<4; $LEVEL++) // levels!
		{
			$award = getAwardByPID_and_AWD($PID, $AWD, $LEVEL);
			$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
			$PlayerAwards[$i][$LEVEL][LEVEL] = $LEVEL;
			$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
			$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
			$PlayerAwards[$i][$LEVEL][NAME] = getSFBadgeByID($AWD);	
		}
	}
#####################################добавил Excavator_red######################################## 	
    #append next after those
	#get SFMedals
	$oldcount = $count;
	$count = $oldcount+getSFMedalCount();
	for ($i=$oldcount; $i<$count; $i++)
	{
		$AWD = trim(getSFMedal($i-$oldcount));
		$LEVEL=0; // levels!
		$award = getAwardByPID_and_AWD_NOLEVEL($PID, $AWD);
		$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
		$PlayerAwards[$i][$LEVEL][LEVEL] = isset($award[0]['level']) ? $award[0]['level'] : 0;
		$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
		$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
		$PlayerAwards[$i][$LEVEL][NAME] = getSFMedalByID($AWD);		
	}	
####################################################################################################	
	
	#append next after those
	#get SFRibbons
	$oldcount = $count;
	$count = $oldcount+getSFRibbonCount();
	for ($i=$oldcount; $i<$count; $i++)
	{
		$AWD = trim(getSFRibbon($i-$oldcount));
		$LEVEL=0; // levels!
		$award = getAwardByPID_and_AWD_NOLEVEL($PID, $AWD);
		$PlayerAwards[$i][$LEVEL][AWD] = isset($AWD) ? $AWD : 0;
		$PlayerAwards[$i][$LEVEL][LEVEL] = isset($award[0]['level']) ? $award[0]['level'] : 0;
		$PlayerAwards[$i][$LEVEL][EARNED] = isset($award[0]['earned']) ? $award[0]['earned'] : 0;
		$PlayerAwards[$i][$LEVEL][FIRST] = isset($award[0]['first']) ? $award[0]['first'] : 0;
		$PlayerAwards[$i][$LEVEL][NAME] = getSFRibbonByID($AWD);		
	}			
	
	return $PlayerAwards;
}


# returns the path to the award
#NOT FINISHED YET
function getBadgeStatus($awards, $awardID)
{
	foreach ($awards as $key => $value)
	{
		if ($awards['awd'] == $awardID)
		{
			$ROOT."game-images/awards/front/".$awardID.'_'.$awards['level'].'.png';	
		}
		else
			return $ROOT."game-images/awards/locked/".$awardID.'_0.png';
	}
}

?>