Code:
if ( $exchange_submit )
{
$sql = "SELECT *
FROM " . VAULT_EXCHANGE_TABLE ."
ORDER BY stock_id
DESC LIMIT 1";
$result = $db->sql_query($sql);
if( !$result )
{
message_die(GENERAL_ERROR, 'Could not obtain stock exchange information', "", __LINE__, __FILE__, $sql);
}
$stock_data = $db->sql_fetchrow($result);
$max = $stock_data['stock_id'];
for ($i=0; $i <= $max; $i++)
{
$input = 'buy_item' . $i;
$$input = doubleval($HTTP_POST_VARS[$input]);
$input2 = 'sell_item' . $i;
$$input2 = doubleval($HTTP_POST_VARS[$input2]);
}
$sql = "SELECT stock_price , stock_id FROM " . VAULT_EXCHANGE_TABLE ."
ORDER BY stock_id";
$result = $db->sql_query($sql);
if( !$result )
{
message_die(GENERAL_ERROR, 'Could not obtain items pets information', "", __LINE__, __FILE__, $sql);
}
$items = $db->sql_fetchrowset($result);
for ( $i = 0 ; $i < count($items) ; $i ++ )
{
$price = 0;
$a = $items[$i]['stock_id'] ;
$buys = 'buy_item'.$items[$i]['stock_id'].'';
$buy = $$buys;
$sells = 'sell_item'.$items[$i]['stock_id'].'';
$sell = $$sells;
$price = ( ( $buy - $sell ) * $items[$i]['stock_price'] );
$ssql = "SELECT stock_amount FROM " . VAULT_EXCHANGE_USERS_TABLE ."
WHERE stock_id = ".$items[$i]['stock_id']."
AND user_id = ".$user_id;
$sresult = $db->sql_query($ssql);
if( !$sresult )
{
message_die(GENERAL_ERROR, 'Could not obtain shares information', "", __LINE__, __FILE__, $ssql);
}
$user_items = $db->sql_fetchrow($sresult);
if ( (( $sell - $buy ) > $user_items['stock_amount'] && is_numeric($user_items['stock_amount'])) || ( !(is_numeric($user_items['stock_amount'])) && (( $buy - $sell ) < 0) ) )
{
vault_previous( Vault_stock_lack , vault , '' , '');
}
if ( $price > $userdata['user_points'])
{
vault_previous( Vault_points_lack , vault , '' , '');
}
else
{
$sql = "UPDATE " . USERS_TABLE ."
SET user_points = user_points - $price
WHERE user_id = $user_id";
if( !$db->sql_query($sql))
{
message_die(GENERAL_ERROR, 'Could not obtain update user points', "", __LINE__, __FILE__, $sql);
}
}
$userdata['user_points'] = $userdata['user_points'] - $price;
$prize = $buy - $sell;
if ( is_numeric($user_items['stock_amount']) && $prize != 0 )
{
$rsql = "UPDATE " . VAULT_EXCHANGE_USERS_TABLE ."
SET stock_amount = stock_amount + $prize
WHERE user_id = $user_id
AND stock_id = ".$items[$i]['stock_id'];
if( !$db->sql_query($rsql))
{
message_die(GENERAL_ERROR, 'Could not update user stock', "", __LINE__, __FILE__, $rsql);
}
}
else if ( !(is_numeric($user_items['stock_amount'])) && $prize != 0 )
{
$rsql = "INSERT INTO " . VAULT_EXCHANGE_USERS_TABLE ."
( stock_id , user_id , stock_amount )
VALUES ( ".$items[$i]['stock_id']." , $user_id , $prize )";
if( !$db->sql_query($rsql))
{
message_die(GENERAL_ERROR, 'Could not update user stock', "", __LINE__, __FILE__, $rsql);
}
}
}
$stock_exchange = TRUE;
} |