Source

Selador / php / war_functions.php

Full commit
<?php

/* war_functions.php - Back-end code for all war-related things
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

//Funktion, um die Endstatistik updaten
//alle punkte aus der tmp-Tabelle werden in die endgültige statistiktabelle übertragen.
//außerdem wird berechnet, wer den krieg gewonnen hat
function calcStats ($aid1, $aid2)
{
	$query = "select * from war_stats_tmp where aid1=".$aid1." and aid2=".$aid2;
	if (!$res = mysql_query($query))
		log_err ("war_functions.php: Could not select temporary war stats. Query: ".$query );
	else
	{
		if ($row = mysql_fetch_array($res))
		{
			if ($row['score1'] > $row['score2'])
			{
				$won1 = 1;
				$lost1 = 0;
				$won2 = 0;
				$lost2  = 1;
			}
			else
			{
				$won1 = 0;
				$lost1 = 1;
				$won2 = 1;
				$lost2  = 0;
			}
			//echo "Kriegsstatistik eintrag gefunden!";
			//print_r($row);
		}
		else
			echo "Kein Kriegseintrag! ";
		$query = "update war_stats set score=score+".$row['score1'].", lost=lost+".$lost1.", won=won+".$won1." where aid=".$aid1;
		if (!mysql_query($query))
			log_err("war_functions.php: Could not calculate new score! ".$query);
		else
			log_debug ("updating war stats for : ".$aid1.": adding ".$row['score1']);
		$query = "update war_stats set score=score+".$row['score2'].", lost=lost+".$lost2.", won=won+".$won2." where aid=".$aid2;
		if (!mysql_query($query))
			log_err("war_functions.php: Could not calculate new score! ".$query);
		else
			log_debug ("updating war stats for : ".$aid2.": adding ".$row['score2']);

		//clearTmpStats($aid1, $aid2);

	}

}

function calctimeDiff ($aid1, $aid2, $causer_aid)
{
	//berechnet, wieviele tage der krieg "zu früh" beendet wurde
	$query = "SELECT time_to_sec(TIMEDIFF(adddate(A.begin, interval " . WAR_DURATION . " hour),now()))/3600/24 as diff FROM wars A WHERE aid1=".$aid1." AND aid2=".$aid2;
	if (!$res = mysql_query($query))
		log_err  ("war_functions.php: could not check time from now to end of war: ".$query);
	else
	{
		if ($row = mysql_fetch_array($res) )
		{
			$days = (int)$row['diff'];
		if ($aid1 == $causer_aid)
			$score_string = " score1 ";
		else
			$score_string = " score2 ";
		//(int)((0.5 /100) * $aid_score_own)
		$query = "UPDATE war_stats_tmp set ".$score_string."=(".$score_string."+(".$score_string."*".$days."/7) * 0.5) WHERE aid1=".$aid1." AND aid2=".$aid2;
		if (!mysql_query($query))
		{
			log_err("War_functions.php: Could not update war stats.".$query);
			log_debug("W00t: ".$query);
		}
		else
			log_err("war_functions: could not checkt time diff. ".$query);
		}
		else
		{
			echo "Auauauauauauau".$query;
		}
	}
}

//nicht vergessen -. die tmp-Tabelle muss gelöscht werden!
function clearTmpStats ($aid1, $aid2)
{
	$query = "delete from war_stats_tmp where aid1=".$aid1." and aid2=".$aid2;
	if (!mysql_query($query))
		log_err("war_functions: Could not delete war tmp stats: ".$query);
}

?>