<?php
include 'common.php';


function GetInvenExtraCount($dbhandle, $account_id, $char_uid)
{
	$inven_extra_count = 0;
	$strQuery = "select inven_extra_count from character_list where user_id='".$account_id."' and character_uid='".$char_uid."'";
	if($query_result = mysqli_query($dbhandle, $strQuery))
	{
		$data = mysqli_fetch_row($query_result);
		if(count($data) > 0)
		{			
			$inven_extra_count = $data[0];
			
			mysqli_free_result($query_result); 
		}
	}
	else{
		$error = mysqli_error($dbhandle);
	}
	
	return $inven_extra_count;
	
}



function InvenExpand($account_id, $char_uid, $add_slot, $price, $login_key)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	
	if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	{
		PrintLoginKeyError();
		mysqli_close($dbhandle);
		return;
	}
	
	
	$response = "INVEN_EXPAND_FAILED";
	$error = "";
	$inven_extra_count = 0;
	$cash = 0;
		
	//$cash = GetCash($dbhandle, $account_id);
	//if($cash < $price)
	//{
	//	$response = "INVEN_EXPAND_FAILED";
	//	$error = "Not Enough Cash";
	//}
	//else
	//{		
		
		 
		//if(ConsumeCash($dbhandle, $account_id, $price) == true)
		if(_ConsumeCrystal($dbhandle, $account_id, $price))
		{
			$update_query = "update character_list set inven_extra_count=inven_extra_count+".$add_slot." where user_id='".$account_id."' and character_uid='".$char_uid."'";

			//Query Result
			$result = @mysqli_query($dbhandle,$update_query);
			
			if($result)
			{
				$response = "INVEN_EXPAND_OK";
				
				$inven_extra_count = GetInvenExtraCount($dbhandle,$account_id, $char_uid );
				
				$current_cash = GetCash($dbhandle, $account_id);
				if($current_cash <0)
				{
					$current_cash = 0;
				}
				// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
				//mysqli_free_result($result); 
			}
			else
			{
				$error = mysqli_error($dbhandle);
			}
			
		}	
		else
		{
			$error = "ConsumeCash Failed";
		}		
		
	//}
	
	
	 mysqli_close($dbhandle);
 
    $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
    (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
    ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
 
    echo json_encode(array('result'=>$response,'error'=>$error,'inven_extra_count'=>$inven_extra_count,'current_cash'=>$current_cash));
}



if(CheckValidatedServerIP())
{
	InvenExpand($_GET['account_id'], $_GET['char_uid'],$_GET['add_slot'],$_GET['price'], $_GET['login_key']);
}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
	(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
	($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain'); 
 
	echo json_encode(array(	'result'=>$response,'error'=>$error	));
}

?>