Location: PHPKode > projects > EliteSquadrons Promisance (TM) > esprom2_3_1/clanmarketsell.php
<?
include("header.php");

if ($lockdb)
	TheEnd("Clan market currently disabled!");
if ($users[clan] <= 0)
	TheEnd("Clanless user!");

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 $clanmarketdb, $config, $users, $costs, $time;
	$market = mysql_fetch_array(mysql_query("SELECT * FROM $clanmarketdb WHERE type='$type' AND seller!=$users[num] AND time<=$time AND clanid=$users[clan] 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 $clanmarketdb, $users, $uera, $basket, $config, $time;
	$onsale = 0;
	$goods = mysql_query("SELECT * FROM $clanmarketdb WHERE type='$type' AND seller=$users[num] AND clanid=$users[clan] 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=clanmarketsell&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 $clanmarketdb, $users, $uera, $sell, $sellprice, $config, $basket, $time;
	$minprice = $config[$type] / 2;
	$maxprice = $config[$type] * 4;
	$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 $clanmarketdb (clanid,type,seller,amount,price,time) VALUES ('$users[clan]','$type',$users[num],$amount,$price,$time+3600*$config[market]);");
?>
<tr><td><?=$uera[$type]?></td>
    <td class="aright"><?=commas($amount)?></td>
    <td class="aright">$<?=commas($price)?></td>
    <td class="aright">In Transit, <?=$config[market]?> hour(s) remaining</td></tr>
<?
	}
}

function removeUnits ($id)
{
	global $clanmarketdb, $users, $uera;
	$market = mysql_fetch_array(mysql_query("SELECT * FROM $clanmarketdb 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 $clanmarketdb 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 Fleet 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");
}
?>
Also see: <a href="<?=$config[main]?>?action=guide&amp;section=military&amp;era=<?=$users[era]?>">Game 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=clanmarketsell">
<?
	for ($i = 0; $i < sizeof($trplst); $i++)
	getCosts($trplst[$i]);
?>
<table class="inputtable">
<tr><td colspan="2"><a href="<?=$config[main]?>?action=clanmarketbuy">Buy Goods</a></td>
    <td>&nbsp;</td>
    <td colspan="2" class="aright"><a href="<?=$config[main]?>?action=clanmarketsell">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>
</table>
</form>
<?
TheEnd("");
?>
Return current item: EliteSquadrons Promisance (TM)