Location: PHPKode > projects > Crown of Evanion > final/trader.php
<?php
/*
*   This file is part of 'Crown of Evanion'.
*
*    'Crown of Evanion' is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    'Crown of Evanion' is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with 'Crown of Evanion'; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

$title = "Trader";
include("include.php");

# Removes entries with no items left in them
mysql_query("DELETE FROM trader WHERE quan = '0'");

echo "<div align=\"center\"><p class=\"drag\"><a href=$PHP_SELF?page=shop>Buy</a> | <a href=$PHP_SELF?page=back>Sell</a></p></div>";

$hour = (date("G"));

if($hour == 12) {
	echo "Sorry, I'm out of my shop. I'll be back soon!";
	include("footer.php");
	die;
}

if(!$page) {
	echo "<div align=\"center\"><p>Welcome to my humble home! I sell used and exotic items here. Or, would you like to step into the back room and sell me something special?</p></div>";
}

if($page == "shop") {
	echo "<div align=\"center\">Take your time. I know I have a strange selection.</div>";
	$select = mysql_query("SELECT * FROM trader");
	$num = mysql_num_rows($select);
	if($num == 0) {
		echo "<div align=\"center\"><h5>Sorry, all sold out!</h5></div>";
	} else {
		while ($stocks = mysql_fetch_array($select)) {
			$sel = mysql_query("SELECT * FROM items WHERE id = '$stocks[itemid]'");
			$item = mysql_fetch_array($sel);
			
			if($item[rare] == 1) {
				$price = round($item[price] / 2.2);
			}			
			if($item[rare] == 2) {
				$price = round($item[price] / 1.3);
			}
			if($item[rare] == 3) {
				$price = round($item[price] * 3);
			}
			
						
			echo "<div class=\"item\">
			<a href=\"$PHP_SELF?page=buy&id=$stocks[id]\">
			<img src=\"$item[image]\" alt=\"$item[name]\" width=\"90\" height=\"90\" border=\"0\">
			</a>
			<p class=\"name\">
			<a href=\"$PHP_SELF?page=buy&id=$stocks[id]\">
			$item[name]
			Quantity: $stocks[quan]
			Price: $price
			</a>
			</p>
			</div>";			
		}
		echo "</div>";
	}			
}

if($page == "buy") {
	$id = $_REQUEST['id'];
	$select = mysql_query("SELECT * FROM trader WHERE id = '$id'");
	while ($stock = mysql_fetch_array($select)) {
		if($stock[quan] <= 0) {
			header("location: $PHP_SELF?error=1");
			include("footer.php");
			die;
		}
		
		$select = mysql_query("SELECT * FROM items WHERE id = '$stock[itemid]'");
		$item = mysql_fetch_array($select);
		if($item[rare] == 1) {
			$price = round($item[price] / 2.2);
		}			
		if($item[rare] == 2) {
			$price = round($item[price] / 1.3);
		}
		if($item[rare] == 3) {
			$price = round($item[price] * 3);
		}

		if($price > $Money) {
			header("location: $PHP_SELF?error=2");
			include("footer.php");
			die;
		}	
		
		mysql_query("UPDATE users SET money = money-$price WHERE id = '$UserID'");
		mysql_query("UPDATE funds SET amount = amount+$price WHERE fund = 'shops'");
		mysql_query("INSERT INTO useritems (itemid,owner) VALUES ('$item[id]','$UserID')");
		mysql_query("UPDATE trader SET quan = quan-1 WHERE id = '$stock[id]'");
		echo "<div align=\"center\"><strong>You've purchased a $item[name]!</div>";
		
	}
}

if($page == "back") {
	$select = mysql_query("SELECT * FROM useritems WHERE owner = '$UserID'");
	echo "<div align=\"center\"><p>Ah, you have an item you woudl like to discuss selling? Take it out, and we shall step into the back of my humble store...</p>
	<form action=\"$PHP_SELF\" method=\"POST\">
	<input type=\"hidden\" name=\"page\" value=\"sell\">
	<select name=\"id\">";
	while ($item = mysql_fetch_array($select)) {
		$sel = mysql_query("SELECT name FROM items WHERE id = '$item[itemid]'");
		$arr = mysql_fetch_array($sel);
		echo "<option value=\"$item[id]\">$arr[name]</option>";
	}
	echo "</select>
	<input type=\"submit\" value=\"Proceed\">
	</form>
	</div>";
}

if($page == "sell") {
	$id = $_POST['id'];
	$select = mysql_query("SELECT * FROM useritems WHERE id = '$id'");
	$item = mysql_fetch_array($select);
	
	if($item[owner] != $UserID) {
		header("location: $PHP_SELF?error=3");
		include("footer.php");
		die;
	}
	
	$select = mysql_query("SELECT * FROM items WHERE id = '$item[itemid]'");
	$iteminfo = mysql_fetch_array($select);
	
		if($iteminfo[rare] == 1) {
			$price = round($iteminfo[price] / 2.2);
		}			
		if($iteminfo[rare] == 2) {
			$price = round($iteminfo[price] / 1.3);
		}
		if($iteminfo[rare] == 3) {
			$price = round($iteminfo[price] * 3);
		}
		
		if($price < 0) {
			echo "Sorry, not interested.";
			include("footer.php");
			die;
		}
	
	echo "<div align=\"center\"><p>Ahh, you wish to sell me a $iteminfo[name]? I'll give you $price for it.</p>
	<p class=\"drag\"><a href=$PHP_SELF?page=sold&id=$item[id]>Yes</a> | <a href=\"$PHP_SELF>No</a></p></div>";
}

if($page == "sold") {
	$id = $_REQUEST['id'];
	$select = mysql_query("SELECT * FROM useritems WHERE id = '$id'");
	$item = mysql_fetch_array($select);
	
	if($item[owner] != $UserID) {
		header("location: $PHP_SELF?error=3");
		include("footer.php");
		die;
	}
	
	$select = mysql_query("SELECT * FROM items WHERE id = '$item[itemid]'");
	$iteminfo = mysql_fetch_array($select);
	
		if($iteminfo[rare] == 1) {
			$price = round($iteminfo[price] / 2.2);
		}			
		if($iteminfo[rare] == 2) {
			$price = round($iteminfo[price] / 1.3);
		}
		if($iteminfo[rare] == 3) {
			$price = round($iteminfo[price] * 3);
		}
		
		if($price < 0) {
			echo "Sorry, not interested.";
			include("footer.php");
			die;
		}
	mysql_query("UPDATE users SET money = money+$price WHERE id = '$UserID'");
	mysql_query("DELETE FROM useritems WHERE id = '$id'");
	
	$select = mysql_query("SELECT * FROM trader WHERE itemid = '$iteminfo[id]'");
	$trader = mysql_num_rows($select);
	
	if($trader > 0) {
		mysql_query("UPDATE trader SET quan = quan+1 WHERE itemid = '$iteminfo[id]'");
	} else {
		mysql_query("INSERT INTO trader (itemid,quan) VALUES ('$iteminfo[id]','1')");
	}
	echo "<div align=\"center\">You sold your $iteminfo[name]!</div>";
}
include("footer.php");
?>	
Return current item: Crown of Evanion