Source

Selador / merchant / merchant_functions.php

<?php
function getPageLink ($name, $current_f, $linked_f)
{
	if ($current_f == $linked_f)
		$class = 'aktiv';
	else
		$class = 'inactive';
	echo '<a href="handel.php?f='.$linked_f.'" class="'.$class.'">'.$name.'</a>';
}

function getPage ($request, $me)
{
	if (isset ($request['f']))
	{
		$f = $request['f'];
		if (($f == 4) && ($me->class != 4)) // supr market is just for traders
			$f = 1;
	}
	else
		$f = 1;
	return $f;
}


function calc_maximum ($resource)
{
	global $me;
	global $merchants_gone;
	$capacity = (($me->activevillage->merchants)-$merchants_gone)*($me->merchant_cap);
	return (min($capacity, $me->activevillage->now_ress($resource) ));

}


function draw_offer ($kind, $amount, $check)
{
	global $me, $draw_err, $merchants_here;
	$drawing_offer = "";
	if (1 == $kind)
	{
		$searchkind = "holz";
		$tooltip = "Holz";
	}
	if (2 == $kind)
	{
		$searchkind = "stein";
		$tooltip = "Stein";
	}
	if (3 == $kind)
	{
		$searchkind = "eisen";
		$tooltip = "Eisen";
	}
	if (4 == $kind)
	{
		$searchkind = "gold";
		$tooltip = "Gold";
	}

	$drawing_offer .= "<img src=\"styles/".$_SESSION['theme']."/".$searchkind.".gif \" title=\"".$tooltip."\" alt=\"".$searchkind."\" width=\"20px\">";

	if ($check)
	{
		if (!$me->activevillage->can_spend_type ($kind, $amount))
		{
			//echo "<span id=\"bug\">".$amount."</span>";
			$draw_err = "Nicht gen&uuml;gend Rohstoffe";
			//return false;
		}

		if ($amount > $merchants_here * $me->merchant_cap)
			$draw_err = "<span id=\"bug\">Nicht genug H&auml;ndler hier!</span>";
	}

	$drawing_offer .= prettynumber($amount);
	return $drawing_offer;
}

// Formulate the query wrt to offer and search
function offer_query ($kind, $amount)
{
	switch ($kind)
	{
		case 1:
			$query = $amount.", 0, 0, 0";
			break;
		case 2:
			$query = "0, ".$amount.", 0, 0";
			break;
		case 3:
			$query = "0, 0, ".$amount.", 0";
			break;
		case 4:
			$query = "0, 0, 0, ".$amount;
			break;
		default:
			return false;
	}

	return $query;
}

function calcDuration ($dist, $me)
{
	$time = $dist /($me->merchant_speed * WORLD_MERCH_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;
	return $hours.":".$minutes.":".$seconds;
}


function calcMerchants ()
{
	global $merchants_gone;
	global $gui;
	global $othermerchants;
	global $mymerchants;
	global $me;
	global $merchants_here;

	$merchants_gone = 0;
	if (count ($gui->all_transports))
	{
		$query_transport = "select A.*, B.name as from_vill, B.x as from_x, B.y as from_y, C.name as to_vill, C.x as to_x, C.y as to_y from transport A join village B on (A.village1=B.villageid) join village C on (A.village2=C.villageid) where A.merchantid in (".implode (",", array_keys ($gui->all_transports)).");";

		if ($result_transport = mysql_query($query_transport))
		{
			while (($transportrow = mysql_fetch_array ($result_transport)))
			{
				if ($transportrow['village1'] == $me->activevillageid)
				{
					$mymerchants[$transportrow['merchantid']] = $transportrow;
					$merchants_gone += $transportrow['merchants'];
				}
				else
					$othermerchants[$transportrow['merchantid']] = $transportrow;
			}
			mysql_free_result($result_transport);
		}
	}
	else
	{
		$othermerchants = array();
		$mymerchants = array ();
	}

	$query = "select sum(merchants*number) from offers where villageid=".$me->activevillageid;
	if (!($res = mysql_query ($query)))
	{
		log_err ("Cannot sum up merchants taken by offers in marketplace. Query: ".$query.", ".mysql_error());
	}
	else
	{
		if (!($row = mysql_fetch_row ($res)))
		{
			log_err ("No entries for merchant sum. Query: ".$query);
		}
		else
		{
			$merchants_gone += $row[0];
		}
	}

	$merchants_here = $me->activevillage->merchants - $merchants_gone;
}

?>