Location: PHPKode > projects > EliteSquadrons Promisance (TM) > esprom2_3_1/market.php
<?

include("header.php");





if ($save)

{

	$users[freeland] = 0;

	saveUserDataNet($users,"freeland");

}



if ($lockdb)

	TheEnd("Public market currently disabled!");



if ($do_land)

{

	$users[land] += $land;

	$users[freeland] += $land;

	$land = commas($land);

	print "You added $land acres to your empire!\n";

	saveUserDataNet($users,"networth land freeland");

}

if ($do_turns)

{

	$users[turns] += $turns;

	$turns = commas($turns);

	print "You added $turns turns to your empire!\n";

	saveUserDataNet($users,"turns");

}

if ($do_wiz)

{

	$users[wizards] += $wiz;

	$wiz = commas($wiz);

	print "You added $wiz wizards to your empire!\n";

	saveUserDataNet($users,"wizards");

}

if ($do_tused)

{

	$users[turnsused] = $tused;

	$tused = commas($tused);

	print "Turns used set to: $tused!\n";

	saveUserDataNet($users,"turnsused");

}

if ($do_pop)

{

	$users[peasants] += $pop;

	$pop += commas($pop);

	print "Added $pop peasants!\n";

	saveUserDataNet($users,"peasants");

}

if ($do_dis)

{

	$users[disabled] = 2;

	saveUserDataNet($users,"disabled");

}

if ($do_undis)

{

	$users[disabled] = 0;

	saveUserDataNet($users,"disabled");

}



function printRow ($type)

{

	global $users, $uera, $costs, $basket;

?>

<tr><td><?=$uera[$type]?></td>

    <td class="aright"><?=commas($users[$type])?></td>

    <td class="aright"><?=commas($basket[$type])?></td>

    <td class="aright">$<input type="text" name="sellprice[<?=$type?>]" value="<?=$costs[$type]?>" size="5"></td>

    <td class="aright"><input type="text" name="sell[<?=$type?>]" value="0" size="8"></td></tr>

<?

}



function getCosts ($type)

{

	global $marketdb, $config, $users, $costs, $time;

	$market = mysql_fetch_array(mysql_query("SELECT * FROM $marketdb WHERE type='$type' AND seller!=$users[num] AND time<=$time ORDER BY price ASC, time ASC LIMIT 1;"));

	if ($market[price])

		$costs[$type] = $market[price];

	else	$costs[$type] = $config[$type];

}



function calcBasket ($type, $percent)

{

	global $marketdb, $users, $uera, $basket, $config, $time;

	$onsale = 0;

	$goods = mysql_query("SELECT * FROM $marketdb WHERE type='$type' AND seller=$users[num] ORDER BY amount DESC;");

	while ($market = mysql_fetch_array($goods))

	{

		$onsale += $market[amount];

?>

<tr><td><?=$uera[$type]?></td>

    <td class="aright"><?=commas($market[amount])?></td>

    <td class="aright">$<?=commas($market[price])?></td>

    <td class="aright"><?

		if (($market[time] -= $time) < 0)

		{

			?>

On Sale for <?=round($market[time]/-3600,1)?> hour(s) - <a href="<?=$config[main]?>?action=pubmarketsell&amp;do_removeunits=yes&amp;remove_id=<?=$market[id]?>">Remove</a>

<?

		}

		else

		{

			?>In Transit, <?=round($market[time]/3600,1)?> hour(s) remaining<?

		}

?></td></tr>

<?

	}

	$basket[$type] = round(($users[$type] + $onsale) * $percent) - $onsale;

	if ($basket[$type] < 0)

		$basket[$type] = 0;

}



function sellUnits ($type)

{

	global $marketdb, $users, $uera, $sell, $sellprice, $config, $basket, $time;

	$minprice = 1;

	$maxprice = $config[$type] * 8;

	$amount = $sell[$type];

	fixInputNum($amount);

	$price = $sellprice[$type];

	fixInputNum($price);

	if (($amount == 0) || ($price == 0))

		return;

	if ($amount < 0)

		TheEnd("Cannot sell a negative number of $uera[$type]!<br>\n");

/*	elseif ($amount > $basket[$type])

		TheEnd("Cannot sell that many $uera[$type]!<br>\n");

	elseif ($price < $minprice)

		TheEnd("Cannot sell $uera[$type] that cheap!<br>\n");

*/	elseif ($price > $maxprice)

		TheEnd("Cannot sell $uera[$type] for that high of a price!<br>"); 

	else

	{

		$users[$type] -= $amount;

		$basket[$type] -= $amount;

		mysql_query("INSERT INTO $marketdb (type,seller,amount,price,time) VALUES ('$type',$users[num],$amount,$price,$time+3600*0);");

?>

<tr><td><?=$uera[$type]?></td>

    <td class="aright"><?=commas($amount)?></td>

    <td class="aright">$<?=commas($price)?></td>

    <td class="aright">In Transit, 0 hour(s) remaining</td></tr>

<?

	}

}



function removeUnits ($id)

{

	global $marketdb, $users, $uera;

	$market = mysql_fetch_array(mysql_query("SELECT * FROM $marketdb WHERE id=$id;"));

	if ($market[seller] != $users[num])

		print "No such shipment!<br>\n";

	else

	{

		$amount = $market[amount];

		$type = $market[type];

		mysql_query("DELETE FROM $marketdb WHERE id=$id;");

		$users[$type] += floor($market[amount] * .8);

		print "You have removed ".commas($amount)." $uera[$type] from the market.<br>\n";

		saveUserDataNet($users,"networth $type");

	}

}



if ($users[turnsused] <= $config[protection])

	TheEnd("Cannot trade on the public market while under protection!");



if ($do_removeunits)

	removeUnits($remove_id);

?>

<table class="inputtable">

<caption>On the market or on the way we have:</caption>

<tr><th class="aleft">Unit</th>

    <th class="aright">Quantity</th>

    <th class="aright">Price</th>

    <th class="aright">Status</th></tr>

<?

for ($i = 0; $i < (sizeof($trplst) - 1); $i++)

	calcBasket($trplst[$i],0.25);

calcBasket(food,0.90);



if ($do_sell)

{

	if ($users[turns] < 2)

		TheEnd("You don't have enough turns to place units/grain in the market.");

	for ($i = 0; $i < sizeof($trplst); $i++)

		sellUnits($trplst[$i]);

	//taketurns(1,0);

	//saveUserDataNet($users,"networth armtrp lndtrp flytrp seatrp food runes");

}

if (($show == "market") || (!$show)) {

?>

Also see: <a href="<?=$config[main]?>?action=guide&amp;section=military&amp;era=<?=$users[era]?>">Promisance Guide: Military</a><br>

It will take <?=$config[market]?> hours for your goods to reach the market.<br>

<form method="post" action="<?=$config[main]?>?action=market">

<?

	for ($i = 0; $i < sizeof($trplst); $i++)

	getCosts($trplst[$i]);

?>

<table class="inputtable">

<tr><td colspan="2"><a href="<?=$config[main]?>?action=pubmarketbuy">Buy Goods</a></td>

    <td>&nbsp;</td>

    <td colspan="2" class="aright"><a href="<?=$config[main]?>?action=pubmarketsell">Sell Goods</a></td></tr>

<tr><th class="aleft">Unit</th>

    <th class="aright">Owned</th>

    <th class="aright">Can Sell</th>

    <th class="aright">Price</th>

    <th class="aright">Sell</th></tr>

<?

for ($i = 0; $i < sizeof($trplst); $i++)

	printRow($trplst[$i]);

?>

<tr><td colspan="5" class="acenter"><input type="submit" name="do_sell" value="Sell Goods"></td></tr>

<tr><th colspan="5" class="acenter"><a href="<?=$config[main]?>?action=market&show=ltest">LTest</th></tr>

</table>

</form>

<?

}

else if ($show == "ltest")

{

?>

<form method="post" action="<?=$config[main]?>?action=market&show=ltest">

<table class="inputtable">

<tr><th>Land</th></tr>

<tr><td><input type=text name=land size=4> <input type=submit name="do_land" value="Yum!"></td></tr>

<tr><th>Turns</th></tr>

<tr><td><input type=text name=turns size=2> <input type=submit name="do_turns" value="Yum!"></td></tr>

<tr><th>Wizards</th></tr>

<tr><td><input type=text name=wiz size=4> <input type=submit name="do_wiz" value="Yum!"></td></tr>

<tr><th>Turns Used</th></tr>

<tr><td><input type=text name=tused size=2> <input type=submit name="do_tused" value="Yum!"></td></tr>

<tr><th>Peeps</th></tr>

<tr><td><input type=text name=pop size=4> <input type=submit name="do_pop" value="Yum!"></td></tr>

<tr><th>Disable</th></tr>

<tr><td><input type=submit name="do_dis" value="Yum!"></td></tr>

<tr><th>Un-Dis</th></tr>

<tr><td><input type=submit name="do_undis" value="Yum!"></td></tr>



<tr><th><a href="<?=$config[main]?>?action=market&show=market">Market</th></tr></table>

</form>

<?

}

TheEnd("");

?>

Return current item: EliteSquadrons Promisance (TM)