<?PHP 

// do I need to do additional security checks to make sure the script is being called appropriately?

include("config.php");

if(is_logged_in($user)){
  $username = base64_decode($_SESSION['user']);
  if($username != "Slowman" && $username != "Rappstar" && $username != "Herbert") { 
    header('Location:https://slowtwitch.com/calendar/');
    exit();
  }
} else { 
  header('Location:https://slowtwitch.com/calendar/');
  exit(); 
}

// set the page title
$pagetitle = "Admin: Fix Points";

// set meta tags
$meta_keywords = "";
$meta_description = "";
?>

<? include("include_common_head.php"); ?>
<? include("include_common_content.php"); ?>

<!-- Start outer -->
<div id="outer">

<!-- BODY -->
<div class="content">
<!-- LEFT COLUMN -->

<!-- End left column -->
<!-- Content col -->

<h1 class="content-title"><strong>Checking Points&hellip;</strong></h1>
<div class="indent">
<?
// select any races that we haven't cleared that are past due.
$sql = "SELECT uid, name, swim, bike, run, onetype, twotype, threetype, oneunit, twounit, threeunit, points FROM gforum_Triathlons WHERE date > '$min_date'";
			  
$results = mysql_query($sql) or die(mysql_error());

echo "Processing races&hellip;<br />";

$arr_Races = array("");
$i = 0;
while($row = mysql_fetch_array($results)) {
	  $uid = $row[uid];
	  $name = $row[name];
	  $swim = $row[swim];
	  $bike = $row[bike];
	  if ($row[run] != "") { $run = $row[run]; } else { $run = 0; }
	  $oneunit = $row[oneunit];
	  $twounit = $row[twounit];
	  $threeunit = $row[threeunit];
	  $onetype = $row[onetype];
	  $twotype = $row[twotype];
	  $threetype = $row[threetype];
	 
	  $one_points = 0;
	  $two_points = 0;
	  $three_points = 0;
	
 	  $arrSwimPoints = array(1 => 17.6, 2 => 0.011, 3 => 0.01, 4 => 11);
	  $arrBikePoints = array(1 => 1, 2 => 0.00062, 3 => 0.00057, 4=> .62);
	  $arrRunPoints = array(1 => 4, 2 => 0.0025, 3 => 0.0023, 4 => 2.5);
	  $arrPaddlePoints = array(1 => 2.6667, 2 => 0.00167, 3 => 0.00153, 4 => 1.6667);
	  $arrSkiPoints = array(1 => 2.75, 2 => 0.00172, 3 => 0.00158, 4 => 1.72);
	  $arrMtbPoints = array(1 => 2, 2 => 0.00124, 3 => 0.00114, 4=> 1.24);
	  $arrPoints = array(1 => $arrSwimPoints, 2=> $arrBikePoints, 3 => $arrRunPoints, 4 => $arrPaddlePoints, 5 => $arrSkiPoints); 
	  
	  $one_points = $swim*$arrPoints[$onetype][$oneunit];
	  $two_points = $bike*$arrPoints[$twotype][$twounit];
	  $three_points = $run*$arrPoints[$threetype][$threeunit];
	  
      $points = $one_points + $two_points + $three_points;
      
	  $points = round($points, 0);
	  
	  //echo ("Race ID: $uid - $one_points / $two_points / $three_points -- $points<br />");
	  
	  // throw an error on bad units
	  if ($points > 260) {
		   echo ("<strong>Excessive Points.</strong> RACE_UID: <a href=\"https://slowtwitch.com/calendar/individual.php?uid=".$uid."\" target=\"_blank\">".$uid.". ".$name." [".$points." Points]</a><br />");
	  }
	  

	  if ($points != $row[points]) {	
		  if ($points < 40) { $pointclass = 1; 
		  } elseif ($points >= 40 AND $points < 100) { $pointclass = 2; 
		  } elseif ($points >= 100 AND $points < 180) { $pointclass = 3; 
		  } else { $pointclass = 4; } 
	
		  $sql_Fix = "UPDATE ".$prefix."Triathlons
					  SET one_points = ".($one_points).",
						  two_points = ".($two_points).",
						  three_points = ".($three_points).",
						  points = ".($points).",
						  pointclass = ".($pointclass)."
						  WHERE uid = ".$uid;
						  
		  mysql_query($sql_Fix) OR die(mysql_error());
		  echo ("Race #".$uid." updated.<br />");
       }
}

echo "Done processing races.<br />";

//header('Location: https://slowtwitch.com/calendar/?tags_cleared='.$tags_cleared.'.php');

?>

</div>
<!-- End content col -->

</div>
<!-- End BODY -->
</div>
<!-- End Outer -->

<? include("include_common_footer.php"); ?>