<?php
include 'common.php';


function DropExp($account_id, $character_uid, $login_key, $attacker_uid, $prev_level, $prev_exp, $reduced_exp, $final_level, $final_exp, $zone_id)
{
	$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;
	}

	$update_query = "UPDATE character_list SET level=".$final_level.", character_exp=".$final_exp." WHERE user_id='".$account_id."' and character_uid=".$character_uid;
		
	//Query Result
	$result = @mysqli_query($dbhandle,$update_query);
	$response = "DROP_EXP_FAILED";
	$error = "";
	
	if($result)
	{
		$response = "DROP_EXP_OK";
		LogDropExp($dbhandle, $character_uid, $attacker_uid, $prev_level, $prev_exp, $reduced_exp, $final_level, $final_exp, $zone_id);
	}
	else
	{
		$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,'updated_level'=>$final_level, 'updated_exp'=>$final_exp));
}

function LogDropExp($dbhandle, $character_uid, $attacker_uid, $prev_level, $prev_exp, $reduced_exp, $final_level, $final_exp, $zone_id)
{
	$now = new DateTime();
	//$now->setTimezone(new DateTimeZone('UTC'));
	$strTime =  date_format($now, "Y-m-d H:i:s");

	$stmt = $dbhandle->prepare("INSERT INTO drop_exp_log (character_uid, attacker_uid, prev_level, prev_exp, reduced_exp, final_level, final_exp, zone_id, log_time) values(?,?,?,?,?,?,?,?,?)");
	$stmt->bind_param("iiiiiiiis", $character_uid, $attacker_uid, $prev_level, $prev_exp, $reduced_exp, $final_level, $final_exp, $zone_id, $strTime);
	$result = $stmt->execute();
	$stmt->close();
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$character_uid = $json->character_uid;
$login_key = $json->login_key;
$attacker_uid = $json->attacker_uid;
$prev_level = $json->prev_level;
$prev_exp = $json->prev_exp;
$reduced_exp = $json->reduced_exp;
$final_level = $json->final_level;
$final_exp = $json->final_exp;
$zone_id = $json->zone_id;

if(CheckValidatedServerIP())
{
	DropExp($account_id, $character_uid, $login_key, $attacker_uid, $prev_level, $prev_exp, $reduced_exp, $final_level, $final_exp, $zone_id);
}
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	));
}

?>