<?php
include 'common.php';



function SaveOwnedTransformingIsused($account_id, $char_uid, $transforming_uid, $release_uid, $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 = "SAVE_OWNED_TRANSFORMING_FAILED";
	$error = "";
	// 소환
	if($transforming_uid != 0)
	{
		$insert_query = "UPDATE owned_transforming SET is_used = 1 WHERE owned_transforming_uid = ".$transforming_uid." And character_uid = ".$char_uid;
	
		//Query Result
		$result = @mysqli_query($dbhandle,$insert_query);
		$response = "SAVE_OWNED_TRANSFORMING_FAILED";
		
		if($result)
		{
			$response = "TRANSFORMING_OK";
		}
		
		else
		{
			$response = "TRANSFORMING_FAIL";
			$transforming_uid = -1;
			$error = mysqli_error($dbhandle);
		}
	}
	
	else
	{
		$response = "TRANSFORMING_OK";
	}
	
	// 해제
	if($release_uid != 0)
	{
		$insert_query = "UPDATE owned_transforming SET is_used = 0 WHERE owned_transforming_uid = ".$release_uid." And character_uid = ".$char_uid;

		//Query Result
		$result2 = @mysqli_query($dbhandle,$insert_query);
		
		if($result2)
		{
			$response = $response."_RELEASE_OK";
		}
		
		else
		{
			$response = $response."_RELEASE_FAIL";
			$release_uid = -1;
			$error = mysqli_error($dbhandle);
		}
	}
	
	else
	{
		$response = $response."_RELEASE_OK";
	}
	
	
	 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,'transformingUid'=>$transforming_uid,'releaseUid'=>$release_uid));
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$char_uid = $json->char_uid;
$login_key = $json->login_key;
$transforming_uid = $json->transforming_uid;
$release_uid = $json->release_uid;

//$gold_update = $json->gold_update;
//$is_passive = $json->is_passive;




if(CheckValidatedServerIP())
{
	SaveOwnedTransformingIsused($account_id, $char_uid, $transforming_uid, $release_uid, $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	));
}



?>