<?php
include 'common.php';

/* function UpdateBox($dbhandle, $char_uid, $opened_box_inven_uid, $box_count_update)
{
	//Box로 아이템을 획득할경우 Box를 소모시켜야 한다.
	if($opened_box_inven_uid != -1)
	{
		if($box_count_update <=0)
		{
			$query = "delete from owned_item where owned_item_uid=".$opened_box_inven_uid." and character_uid='".$char_uid."'";
		}
		else
		{
			$query = "update owned_item set count=".$box_count_update." where owned_item_uid=".$opened_box_inven_uid." and character_uid='".$char_uid."'";
		}
		
		//Query Result
		$result = @mysqli_query($dbhandle,$query);
		if(!$result)
		{
			return false;
		}
	}
	
	return true;
} */

function SaveOpenSupplyBox($account_id, $char_uid, $login_key, $new_item_uid,$new_item_type, $gold_price, $crystal_price, $optType1, $optValue1, $optType2, $optValue2, $optType3, $optValue3, $optType4, $optValue4, $opened_box_inven_uid, $box_count_update)
{
	$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;
	}
	
	if($gold_price > 0) //case of Purchase
	{
		_ConsumeGold($dbhandle, $account_id, $char_uid, $gold_price);
	}
	
	if($crystal_price > 0) //case of Purchase
	{
		_ConsumeCrystal($dbhandle, $account_id, $crystal_price);
	}
	
	
	if(_UpdateBox($dbhandle, $char_uid, $opened_box_inven_uid, $box_count_update))
	{
		$response = "OPEN_RANDOM_BOX_FAILED";
		$error = "";
		
		$insert_query = "INSERT INTO owned_item (character_uid, item_uid, count, is_equiped, parts_type, optType1, optValue1, optType2, optValue2, optType3, optValue3, optType4, optValue4) VALUES (".$char_uid.",".$new_item_uid.",1,0,".$new_item_type.",'".$optType1."',".$optValue1.",'".$optType2."',".$optValue2.",'".$optType3."',".$optValue3.",'".$optType4."',".$optValue4." )";

		//Query Result
		$result = @mysqli_query($dbhandle,$insert_query);
		
		
		if($result)
		{
			$response = "OPEN_RANDOM_BOX_OK";
			// 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
	{
		$response = "UPDATE_BOX_FAILED&OPEN_RANDOM_BOX_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));
}




if(CheckValidatedServerIP())
{
	SaveOpenSupplyBox(
	$_GET['account_id'], 
	$_GET['char_uid'], 
	$_GET['login_key'], 
	$_GET['new_item_uid'],
	$_GET['new_item_type'], 
	$_GET['gold_price'], 
	$_GET['crystal_price'], 
	$_GET['optType1'],
	$_GET['optValue1'],
	$_GET['optType2'],
	$_GET['optValue2'],
	$_GET['optType3'],
	$_GET['optValue3'], 
	$_GET['optType4'],
	$_GET['optValue4'],
	$_GET['opened_box_inven_uid'],
	$_GET['box_count_update']);
}
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	));
}

?>