<?php
include 'common.php';


function AlreadyExistsEvent($dbhandle,$event_id, $character_uid)
{
	$return =false;
	
	
	$stmt = $dbhandle->prepare("SELECT count(*) AS cnt FROM event_levelup_data where event_id=? and character_uid=?");	
	$stmt->bind_param("is", $event_id, $character_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	$num = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	
	if($num["cnt"] > 0)
	{
		$return = true;
	}
	
	return $return;
}

function InsertCheckEventData($dbhandle, $event_id, $character_uid)
{
	$last_rewarded = "";
	$check_time = "";
	$step = GetLevelUpEventStep($dbhandle, $event_id);
	for ($i=0;$i<$step;$i++)
	{
		if ($i < $step - 1)
		{
			$last_rewarded = $last_rewarded."0/";
			$check_time = $check_time."0/";
		}
		else
		{
			$last_rewarded = $last_rewarded."0";
			$check_time = $check_time."0";
		}
	}
	$query = "INSERT INTO event_levelup_data (event_id, character_uid, last_rewarded, check_time) values (".$event_id.",".$character_uid.",'".$last_rewarded."','".$check_time."')"; 
	$result = @mysqli_query($dbhandle,$query);
	if($result == 1)
	{
		//성공
		$return = "LEVELUP_EVENT_DATA_OK";
		$error = "";
		$list = $event_id.",". $character_uid.",".$last_rewarded;

		$response = array('result'=>$return,'error'=>$error,'list'=>$list);
		EchoLevelUpData($response);
	}
	else
	{
		//실패
		$return = "LEVELUP_EVENT_DATA_FAILED";
		$error = "Insert DB Error";
		$list = "";

		$response = array('result'=>$return,'error'=>$error,'list'=>$list);
		EchoLevelUpData($response);
	}
	
}
function CheckLastCheckDate($dbhandle, $event_id, $character_uid)
{
	$query = "SELECT * from event_levelup_data where event_id=".$event_id." and character_uid=".$character_uid;

	$result = mysqli_query($dbhandle, $query);
	if ($result)
	{
		//성공
		if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$return = "LEVELUP_EVENT_DATA_OK";
			$error = "";
			$list = $event_id.",". $character_uid.",".$row["last_rewarded"];

			$response = array('result'=>$return,'error'=>$error,'list'=>$list);
			EchoLevelUpData($response);
		}
		mysqli_free_result($result);
	}
	else
	{
		//실패
		$return = "LEVELUP_EVENT_DATA_FAILED";
		$error = "Select DB Error";
		$list = "";

		$response = array('result'=>$return,'error'=>$error,'list'=>$list);
		EchoLevelUpData($response);
	}
	
}

function LevelUpEventData($event_id, $account_id, $character_uid, $login_key)
{
	//Connect DB
	$dbhandle = DB_Connect();
	
	
	$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;
	}

	$existEvent = AlreadyExistsEvent($dbhandle, $event_id, $character_uid);
	if ($existEvent)
	{
		CheckLastCheckDate($dbhandle, $event_id, $character_uid);
	}
	else
	{
		InsertCheckEventData($dbhandle, $event_id, $character_uid);
	}
	
	mysqli_close($dbhandle);
}

function EchoLevelUpData($response)
{
	$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($response);
}

function GetLevelUpEventStep($dbhandle, $event_id)
{
	$query = "SELECT step from event_levelup_info where event_id=".$event_id."";

	if ($result = mysqli_query($dbhandle, $query))
	{
		if ($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			return intval($row["step"]);
		}
	}
	return -1;
}

$json = json_decode(file_get_contents('php://input'));
$event_id = $json->event_id;
$account_id = $json->account_id;
$character_uid = $json->character_uid;
$login_key = $json->login_key;

if(CheckValidatedServerIP())
{
	LevelUpEventData($event_id, $account_id, $character_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	));
}


?>