<?php
include 'common.php';


function CheckLevelDownPanelty($account_id, $character_uid, $login_key, $level)
{
	$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 = "LEVEL_DOWN_GAP_LESS_3";
	$error = "";

	$stmt = $dbhandle->prepare("SELECT max(prev_level) as max_level FROM drop_exp_log WHERE character_uid=?");	
	$stmt->bind_param("i", $character_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	if ($result)
	{
		if($row = $result->fetch_assoc())
		{
			mysqli_stmt_free_result($stmt);
			$max_level = $row["max_level"];
			$gap = $max_level - $level;
			if ($gap >= 3)
			{
				$response = "LEVEL_DOWN_GAP_MORE_3";
			}
			
			
		}
	}
	

	$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));
		
	mysqli_close($dbhandle);
}

$account_id = $_GET['account_id'];
$character_uid = $_GET['character_uid'];
$login_key = $_GET['login_key'];
$level = $_GET['level'];

if(CheckValidatedServerIP())
{
	CheckLevelDownPanelty($account_id, $character_uid, $login_key, $level);
}
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	));
}

?>