Selador / stadt2.php

Full commit

/* stadt2.php - Front-end code for the old, table-based city view
 * 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
 *   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 <>.

// This is probably horribly out-dated and here for historic purposes.

// require_once("php/classes.php");
require_once ("php/base/");

$precalcpath = $city_path.$me->activevillageid;
// Acquire lock for village here!

$glob_build = array();
$buildingdur = 0;
// $glob_build = 0;  // FIXME bad
// Handle past events
// $me->event_handler (true, $me->activevillageid, $gui);

// Then calculate ressources through checking income - this takes into account
// past events, because event_handler might have updated $secspassed
// $me->activevillage->do_income ();

// Test No 1: Construction and level set, and building queue not full
if (isset ($_REQUEST['r']) && isset ($_REQUEST['l']) && (count($gui->building_queue) < 2) && (!$me->activeoutpostid))
  $enhance = $_REQUEST['r'];
  $tolevel = $_REQUEST['l'];
  $levelfac = $tolevel * pow (NEXTLEVEL_COSTS, $tolevel - 1);
  require_once ("php/citycont.php");
  $build = city_fetch_db ($activevill);
  $research = research_fetch_db ($me->uid);

  if (isset ($build[$enhance]))
		$build_id = $build[$enhance][7];

		// If we are already upgrading this building, mark it as being one level higher in the data
		if (isset ($gui->buildid_queue))
			foreach ($gui->buildid_queue as $this_build)
				if ($this_build == $build_id)

		if (!( $errstr = Calc_Building_Dep ($enhance, $build[$enhance][1], $build, $research, 1)))
			// Test No. 2: Is the level that we want to enhance the thing to one more than the actual level?
			if ($build[$enhance][1]+1 == $tolevel)
				// Test No. 3: Do we have enough ressources?
				if (($build[$enhance][2]*$levelfac <= $me->activevillage->ress[1]) && ($build[$enhance][3]*$levelfac <= $me->activevillage->ress[2]) && ($build[$enhance][4]*$levelfac <= $me->activevillage->ress[3]))
					// Test No. 4: Counter reload build queue auto-submit
					if (!check_event_village2 ($me->uid, $me->activevillageid, 1, $tolevel, $build[$enhance][7]))
						$costs = (int)(($build[$enhance][2] + $build[$enhance][3] + $build[$enhance][4]) * $levelfac);
						$newbuiltdur =  (int)($costs * $me->activevillage->speed_build / WORLD_SPEED) + (int)$gui->building_duration;
						// Insert into event queue FIXME: Type depends on tile type, timestamp depends on finish time of last building in queue
						$query = "insert into event (uid1, village1, uid2, village2, type, param, param2, timestamp, score) values (".$me->uid.", ".$me->activevillageid.", 0, ".$build[$enhance][7].", 1, ".$tolevel.", ".$enhance.", addtime(now(), sec_to_time(".$newbuiltdur.")), ".$costs.")";
						if (!mysql_query ($query))
							log_err ("Cannot insert building event: ".$query.": ".mysql_error());
							$eid = mysql_insert_id ();
							$query = "insert into eventlog (eid, uid1, village1, uid2, village2, type, param, param2, fired, happened, score, state) values (".$eid.", ".$me->uid.", ".$me->activevillageid.", 0, ".$build[$enhance][7].", 1, ".$tolevel.", ".$enhance.", NULL, addtime(now(), sec_to_time(".$newbuiltdur.")), ".$costs.", 0)";

							if (!@mysql_query ($query))
								log_err ("Cannot log building event: ".$query.", ".mysql_error());

							log_info ($build[$enhance][0]." (".$bid.") angefangen, wird ".$newbuiltdur." Sekunden dauern. Query: ".$query);

							if (count ($gui->building_queue) == 0)
								$gui->building_queue[$newbuiltdur] = $build[$enhance][0]." ".$tolevel." <span title=\"Fertig am ".date ("d.m. H:i:s", time () + $newbuiltdur)."\" id=\"timer".$gui->displayedtimers."\">";
								$gui->building_queue[$newbuiltdur] .= external_calcdur ($newbuiltdur)."</span>";
								$gui->building_queue[$newbuiltdur] = $build[$enhance][0]." ".$tolevel." <span title=\"Fertig am ".date ("d.m. H:i:s", time () + $newbuiltdur)."\">".external_calcdur ($newbuiltdur - (int)$gui->building_duration)."</span>";

							$me->activevillage->ress[1] -= (int)($build[$enhance][2]*$levelfac);
							$me->activevillage->ress[2] -= (int)($build[$enhance][3]*$levelfac);
							$me->activevillage->ress[3] -= (int)($build[$enhance][4]*$levelfac);
							$me->activevillage->ress[4] -= (int)($build[$enhance][15]*$levelfac);

						// Add to global variable of buildings being built, so that we won't give the user the option
						// of enhancing the same level again
						$gui->buildid_queue[] = $build[$enhance][7];

						// Delete the cached files
						@unlink ($precalcpath."-0.php");
						@unlink ($precalcpath."-1.php");
						@unlink ($precalcpath."-2.php");
			$build_err = "Du erfüllst die Vorraussetzungen nicht: ".$errstr;
			log_info ("Cheater! Wanted to construct ".$enhance.", but missing ".$errstr);
		$build_err = "Dieses Gebäude existiert nicht";
		log_info ("Cheater! Wanted to construct ".$enhance);

$me->activevillage->update_stamp ();

// We include basic design parts here
require_once ("styles/php/base_design-".$_SESSION['theme'].".php");

if ($me->activeoutpostid)
	echo "<div id=\"stadt\"><b><center>Ein Aussenposten hat erst Gebäude, wenn er zur Stadt wird</center></b>\n";
	if (isset ($_REQUEST['p']))
		$page = $_REQUEST['p'];
		$page = 0;

	// print_r ($me);

	if (!file_exists ($precalcpath."-".$page.".php"))
		require_once ("php/citycont.php");
	//  $buildspeed = $_SESSION['villbuildsp'];
		city_calc_contents ($me->activevillageid, $me->activevillage->name, $precalcpath, $me->uid, $me->activevillage->speed_build, 1);

	require_once ($precalcpath."-".$page.".php");

	echo "</table>";

	if (isset ($build_err))
		echo "<b>".$build_err."</b>";

echo "</div></div>\n";

/*echo "<div id=\"bauprojekt\">";
// print_r ($gui);
  if (isset($gui->building_queue))
    foreach ($gui->building_queue as $thisone)
      echo "<img src=\"gfx/bau.gif\" width=\"16\" height=\"16\" border=\"0\">".$thisone."<br>";
  if (isset ($gui->research_string))
    echo "<img src=\"gfx/forschung.gif\" width=\"16\" height=\"16\" border=\"0\">".$gui->research_string."<br>";
  if (isset ($gui->unit_first))
    echo "<img src=\"gfx/forschung.gif\" width=\"16\" height=\"16\" border=\"0\">".$gui->unit_first;

echo "&nbsp;</div>\n";*/