Selador / laufzeitenrechner.php


/* laufzeitenrechner.php - Front-end code for "Laufzeitenrechner" page
 * Copyright (C) 2006, 2007, 2008 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
 *   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 <>.

// kread: Don't know if this works or if it is finished.

<head><title>Selador Laufzeiten-Berechner</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <th><h3>Selador Laufzeiten-Berechner</h3></th></tr></table>

$db = mysql_pconnect ( "localhost", DB_USER, DB_PASS);
mysql_select_db(DB_NAME) or die("Auswahl der Datenbank fehlgeschlagen");
echo "<table><tr><td colspan=\"3\">";
echo "<h5>Bitte Koordinaten eingeben!</h5><br></td></tr>\n";
echo "<tr><td>Angreifer:</td>";
echo "<form action=\"laufzeitenrechner.php\" method=\"POST\">";
if (isset ($_REQUEST['xattacker']))
  echo "<td>x <input name=\"xattacker\" value=\"".$_REQUEST['xattacker']."\" size=\"3\"></td>\n";
  echo "<td>x <input name=\"xattacker\" size=\"3\"></td>\n";
if (isset ($_REQUEST['yattacker']))
	echo "<td>y <input name=\"yattacker\" value=\"".$_REQUEST['yattacker']."\" size=\"3\"></td>\n";
	echo "<td>y <input name=\"yattacker\" size=\"3\"></td>\n";

echo "<tr><td>Opfer:</td>\n";
if (isset ($_REQUEST['xvictim']))
	echo "<td>x <input name=\"xvictim\" value=\"".$_REQUEST['xvictim']."\" size=\"3\"></td>\n";
	echo "<td>x <input name=\"xvictim\" size=\"3\"></td>\n<td>";
if (isset ($_REQUEST['yvictim']))
	echo "<td>y <input name=\"yvictim\" value=\"".$_REQUEST['yvictim']."\" size=\"3\"></td>\n";
	echo "y <input name=\"yvictim\" size=\"3\"></td></tr>\n";

echo "<table><tr><th style=\"text-align:left\"><h5><br>Bitte angeben, was berechnet werden soll</h5></th></tr><tr>";
echo "<td><input type=\"radio\" name=\"typ\" value=\"start\">Wann kommt welche Einheit an, wenn ich um xxx Uhr loslaufe?</td></tr>\n";
echo "<tr><td><input type=\"radio\" name=\"typ\" value=\"ankunft\">Wann muss ich loslaufen, um um xxx Uhr dazusein?</td></tr>\n";
echo "<tr><td><input type=\"radio\" name=\"typ\" value=\"nichts\">Wie lange braucht welche Einheit?</td></tr>\n";
echo "</tr></table>";
if (isset($_POST['Berechne']))
	$day = $_POST['tag'];
	$month = $_POST['monat'];
	$year = $_POST['jahr'];
	$hour = $_POST['hh'];
	$minute = $_POST['mm'];
	$second = $_POST['ss'];
	$day = date("d");
	$month = date("m");
	$year = date("Y");
	$hour = date("H");
	$minute = date("i");
	$second = date("s");
echo "<table>
			<tr><th><h5><br>Bitte eingeben, wann die Einheiten loslaufen bzw ankommen sollen (keine Eingabe n&ouml;tig f&uuml;r \"Wie lange braucht welche Einheit?\")</h5></th></tr>
			<input type=\"text\" size=2 name=\"tag\" value=\"".$day."\">.\n
			<input type=\"text\" size=2 name=\"monat\" value=\"".$month."\">.\n
			<input type=\"text\" size=4 name=\"jahr\" value=\"".$year."\"> &nbsp;\n

			<input type=\"text\" size=2 name=\"hh\" value=\"".$hour."\">:\n
			<input type=\"text\" size=2 name=\"mm\" value=\"".$minute."\">:\n
			<input type=\"text\" size=2 name=\"ss\" value=\"".$second."\">\n

echo "<tr><td><input type=\"submit\" name=\"Berechne\" value=\"Berechnen\"></td></tr></table>";

if (isset($_POST['Berechne']))

	if (isset ($_REQUEST['xattacker']) && isset ($_REQUEST['yattacker']) &&   isset ($_REQUEST['xvictim']) && isset ($_REQUEST['yattacker']) && ($_REQUEST['xattacker'] != "") && ($_REQUEST['yattacker'] != "") && ($_REQUEST['xvictim'] != "") && ($_REQUEST['yvictim'] != "") && isset($_POST['typ']))

		/*evaluate the form data*/
		$xattacker = (int)$_REQUEST['xattacker'];
		$yattacker = (int)$_REQUEST['yattacker'];
		$xvictim = (int)$_REQUEST['xvictim'];
		$yvictim = (int)$_REQUEST['yvictim'];

		/*calculate the distance*/
		$distance_a = ($xvictim - $xattacker) * ($xvictim - $xattacker);
		$distance_b = ($yvictim - $yattacker) * ($yvictim - $yattacker);
		$distance = sqrt ($distance_a + $distance_b);

		echo "<table><tr><td colspan=\"5\"><h5><br>Entfernung. ".$distance." Felder</h5></td></tr>";
		echo "<tr><th>Speed</th>\n";
		echo "<th colspan=\"2\">Einheit</th>\n";
		echo "<th>Dauer</td>\n";
		echo "<th>Abschicken um/Ankunft um...</th></tr>";

	/*fuer Startzeit und Ankunftsberechnung */
		$hh = (int)$_REQUEST['hh'];
		$mm = (int)$_REQUEST['mm'];
		$ss = (int)$_REQUEST['ss'];
		$tag = (int)$_REQUEST['tag'];
		$monat = (int)$_REQUEST['monat'];
		$jahr = (int)$_REQUEST['jahr'];

		if ($_REQUEST['typ'] == "start")
			 $query = "select unitid, raceid, speed, date_format(addtime(\"".$jahr."-".$monat."-".$tag." ";
			 $query .= $hh.":".$mm.":".$ss."\",(sec_to_time(".$distance."/speed*3600))), \"Ankunft am %d.%m.%Y um %H:%i:%S\") as duration, name from units where speed>0 order by speed";
		if ($_REQUEST['typ'] == "ankunft")
			$query = "select unitid, raceid, speed, date_format(subtime(\"".$jahr."-".$monat."-".$tag." ";
			$query .= $hh.":".$mm.":".$ss."\",(sec_to_time(".$distance."/speed*3600))), \"Abschicken am %d.%m.%Y um %H:%i:%S\") as duration, name from units where speed>0 order by speed";
		if ($_REQUEST['typ'] == "nichts")
			$query = "select unitid, raceid, speed,";
			$query .= "name from units where speed>0 order by speed";
		$result = mysql_query($query) or die("Anfrage fehlgeschlagen: Fehler: " . mysql_error());

	/*Berechnung der Dauer*/

		while (($line = mysql_fetch_array($result, MYSQL_ASSOC)))
			echo "<tr><td width=\"80px\">".$line['speed']."</td>";
			echo "<td><img src=\"gfx/u/".$line['unitid'].".gif\"></td>\n";
			echo "<td width=\"200px\">".$line['name']."</td>";

			$time = $distance / $line['speed'];

			$hours = (int) $time;
			$rest = $time - $hours;
			$minutes = (int) ($rest * 60);
			$rest2 = ($rest*60) - $minutes;
			$seconds = round ($rest2 * 60);

			$minutes += (int)($seconds/60);
			$seconds = ($seconds % 60);

			if ($hours < 10)
			$hours = "0".$hours;
			if ($minutes < 10)
			$minutes = "0".$minutes;
			if ($seconds < 10)
			$seconds = "0".$seconds;

				echo "<td width=\"120px\">".$hours.":".$minutes.":".$seconds."</td>";
	/*Berechnung der Ankunft, falls "Startzeit" ausgewählt wurde*/
			if ($_REQUEST['typ'] == "start")
				/*$timestamp = gmmktime ( $hh, $mm, $ss, $monat, $tag, $jahr);
				$duration_in_seconds = (int) ($time * 3600);
				$arrival_in_seconds = $timestamp + $duration_in_seconds;
				$arrival = strftime ("Ankunft am %d.%m.%Y um %H:%M:%S ", $arrival_in_seconds);*/
				echo "<td>".$line['duration']."</td></tr>";
			if ($_REQUEST['typ'] == "ankunft")
				echo "<td>".$line['duration']."</td></tr>";
			if ($_REQUEST['typ'] == "nichts")
				echo "<td>---</td></tr>";
			echo "<tr><td colspan=\"5\"><img src=\"styles/sun/line_thin.gif\" style=\"width:100%;height:1px\"></td></tr>";
		mysql_free_result ($result);
		echo "<br><br><b>Bitte f&uuml;lle alle Felder aus!</b><br>";