include("ajax_header.php");
switch($_GET['function']) {
case "buyStock":
global $CharacterInfo;
$post['amount'] = (int)$post['amount'];
if(is_numeric($post['amount']) && $post['amount'] > 0){
$SQ = mysql_query("SELECT price, name FROM stock_market WHERE id = '{$post_safe['id']}'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
if(mysql_num_rows($SQ) > 0){
if(mysql_result($SQ, 0, "price") >= 15) {
$cost = mysql_result($SQ, 0, "price") * $post['amount'];
if($CharacterInfo['money'] >= $cost){
updateCharacter("money", "(money - {$cost})", $CharacterInfo['id']);
$SC = mysql_query("SELECT units, stock_cost FROM stock_info where stock_id = '{$post_safe['id']}' AND owner = '{$CharacterInfo['id']}'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
if(mysql_num_rows($SC) > 0){
$number = mysql_result($SC, 0, "units") + $post['amount'];
$newcost = (mysql_result($SC, 0, "stock_cost") * $amount) + (mysql_result($SQ, 0, "price") * mysql_result($SC, 0, "units"));
$newcost = $newcost / ($amount + mysql_result($SC, 0, "units"));
mysql_query("UPDATE stock_info set units = '$number', stock_cost = '$newcost' WHERE owner = '{$CharacterInfo['id']}' AND stock_id = '{$post_safe['id']}'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
$result["status"] = true;
$result["message"] = "You have purchased {$post['amount']} of stock, and now have " . format($number) . " of " . mysql_result($SQ, 0, "name");
if(mt_rand(1, 200) == 1) {
addExp($CharacterInfo['id'], 1);
}
} else {
mysql_query("INSERT INTO stock_info (owner, stock_id, units, stock_cost) VALUES ('{$CharacterInfo['id']}', '{$post_safe['id']}', '{$post_safe['amount']}', '" . mysql_result($SQ, 0, "price") . "')") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
$result["status"] = true;
$result["message"] = "You have purchased " . format($post['amount']) . " of " . mysql_result($SQ, 0, "name");
if(mt_rand(1, 200) == 1) {
addExp($CharacterInfo['id'], 1);
}
}
} else {
$result["status"] = false;
$result["message"] = "You do not have enough money to do that!";
}
} else {
$result["status"] = false;
$result["message"] = "You cannot trade stock that is less than 16 KD";
}
} else {
$result["status"] = false;
$result["message"] = "Item does not exist in our records";
}
} else {
$result["status"] = false;
$result["message"] = "Please enter a number";
}
print json_encode($result);
break;
case "sellStock":
global $CharacterInfo;
$stock = explode("&", urldecode($post_safe['stock']));
foreach($stock as $id) {
$tmp = explode("=", $id);
$sell = $tmp[1];
$stock_id = $tmp[0];
if($sell > 0){
$ChQ = mysql_query("SELECT units FROM stock_info WHERE owner = '{$CharacterInfo['id']}' AND stock_id = '$stock_id'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
if(mysql_num_rows($ChQ) == 1){
//yep! now, get units
$units = mysql_result($ChQ, 0);
if($units >= $sell){ //check we have enough
//we do, continue!
//find selling price
$MktQ = mysql_query("SELECT name, price FROM stock_market WHERE id = '$stock_id'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
$moneytogive = $sell * mysql_result($MktQ, 0, "price");
updateCharacter("money", "(money + {$moneytogive})", $CharacterInfo['id']);
//remove shares from list
if($sell == $units){
mysql_query("DELETE FROM stock_info WHERE owner = '{$CharacterInfo['id']}' AND stock_id = '$stock_id'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
} else {
$newstock = $units - $sell;
mysql_query("UPDATE stock_info SET units = '$newstock' WHERE owner = '{$CharacterInfo['id']}' AND stock_id = '$stock_id'") or print(mysql_error() . " " . __FILE__ . " #" . __LINE__);
}
$moneytogive = format($moneytogive);
print "Sold {$sell} of " . mysql_result($MktQ, 0, "name") . " for $moneytogive KD
";
if(mt_rand(1, 200) == 1) {
addExp($CharacterInfo['id'], 1);
}
} else {
print "You cannot sell more shares than you own!
";
}
} elseif($Num ==0) {
print "I'm sorry, but you can't do that!
";
}
}
}
break;
}
?>