<?php
include 'common.php';



function IsRecoveryChange($dbhandle, $drop_exp_log_id)
{
	$now = new DateTime('now', new DateTimeZone('UTC'));
	$strNow = $now->format('Y-m-d H:i:s');
	$stmt = $dbhandle->prepare("UPDATE drop_exp_log SET is_recovery=1, recovery_time=? WHERE id=?");
	$stmt->bind_param("si", $strNow, $drop_exp_log_id);
	$result = $stmt->execute();
	$stmt->close();
}

function ConsumInvenlItem($dbhandle, $character_uid, $inven_uid)
{	
	$delete_query = "DELETE FROM owned_item WHERE owned_item_uid=".$inven_uid." and character_uid=".$character_uid;
	//Query Result
	$result = @mysqli_query($dbhandle,$delete_query);	
	if($result)
	{
		return true;
	}
	
	return false;
}

function UseItem_RecoveryExp2($account_id, $character_uid, $login_key, $group_drop_log_id, $total_drop_exp, $pay_amount_crystal, $pay_amount_gold, $inven_uid, $item_uid, $final_level, $final_exp, $add_stat)
{
	$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($pay_amount_gold > 0) //case of Purchase
	{
		_ConsumeGold($dbhandle, $account_id, $character_uid, $pay_amount_gold);
	}
	if($pay_amount_crystal > 0) //case of Purchase
	{
		_ConsumeCrystal($dbhandle, $account_id, $pay_amount_crystal);
	}
	if($inven_uid != -1)
	{
		//아이템 소모
		ConsumInvenlItem($dbhandle, $character_uid, $inven_uid);
	}

	$currentGold = GetGold($dbhandle, $account_id, $character_uid);
	$currentCrystal = GetCash($dbhandle, $account_id);	
	
	$split = ",";
	$arrDropLogID = explode($split, $group_drop_log_id);
	for($i=0;$i< sizeof($arrDropLogID);$i++)
	{
		IsRecoveryChange($dbhandle, $arrDropLogID[$i]);
	}
	
	

	$response = "RECOVERY_EXP2_FAILED";
	$error = "";
	
	
	if ($add_stat < 0)
	{
		$add_stat = 0;
	}

	$update_query = "UPDATE character_list SET level=".$final_level.", character_exp=".$final_exp.", stat_point=stat_point+".$add_stat." WHERE user_id='".$account_id."' and character_uid=".$character_uid;
	$result = @mysqli_query($dbhandle,$update_query);
	if($result)
	{
		$response = "RECOVERY_EXP2_OK";
	}
	else
	{
		$response = "DB_ERROR";
		$error = mysqli_error($dbhandle);
	}
	


	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, 'item_uid'=>$item_uid, 'pay_amount_gold'=>$pay_amount_gold, 'pay_amount_crystal'=>$pay_amount_crystal, 'gold'=>$currentGold, 'crystal'=>$currentCrystal, 'recovery_exp'=>$total_drop_exp, 'final_level'=>$final_level, 'final_exp'=>$final_exp));
}


$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$character_uid = $json->character_uid;
$login_key = $json->login_key;
$group_drop_log_id = $json->group_drop_log_id;
$total_drop_exp = $json->total_drop_exp;
$pay_amount_crystal = $json->pay_amount_crystal;
$pay_amount_gold = $json->pay_amount_gold;
$inven_uid = $json->inven_uid;
$item_uid = $json->item_uid;
$final_level = $json->final_level;
$final_exp = $json->final_exp;
$add_stat = $json->add_stat;



if(CheckValidatedServerIP())
{
	UseItem_RecoveryExp2($account_id, $character_uid, $login_key, $group_drop_log_id, $total_drop_exp, $pay_amount_crystal, $pay_amount_gold, $inven_uid, $item_uid, $final_level, $final_exp, $add_stat);

}
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	));
}

?>