<?php
include 'common.php';


function CheckDailyLimit($dbhandle,$account_id)
{
	//함수 밖으로 빼지 마라
	$DAILY_LIMIT_AMOUNT = 10000;

	$stmt = $dbhandle->prepare("SELECT sum(amount) as sum_amount_today FROM crystal_add_log where user_id = ? and  str_to_date(log_time, '%Y-%m-%d')  = str_to_date(now(), '%Y-%m-%d')");	
	$stmt->bind_param("s", $account_id);
	$stmt->execute();
	$result = $stmt->get_result();	
	$stmt->close();
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			mysqli_stmt_free_result($stmt);

			$sum_amount_today = $row["sum_amount_today"];
			if($sum_amount_today == null)//no data today
			{
				return true;
			}


	// 		$response = "TEST";
	// 		$error = "sum_amount_today:".$sum_amount_today;
	// 		$currentCash = $DAILY_LIMIT_AMOUNT;

	// 		$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, 'crystal'=>$currentCash));
	// mysqli_close($dbhandle);
	// exit;

			if($sum_amount_today < $DAILY_LIMIT_AMOUNT)
			{
				return true;
			}
			else
			{
				return false;
			}

			
		}
	}
	else
	{
		return false;
	}


}

function ServerMonsterCrystalDrop($account_id, $character_uid, $login_key, $crystal, $monster_class_id, $monster_kill_count, $remote_addr, $device_unique_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;
	}
	
    
	//@todo : daily limit



	$response = "MONSTER_DROP_CRYSTAL_FAILED";
	$error = "";
	$addType = "MONSTER_DROP";
	$payload = $device_unique_id . "&" . $monster_kill_count;

	if(CheckDailyLimit($dbhandle,$account_id))
	{
		if(AddCash($dbhandle,$account_id, $crystal))
		{
			LogAddCrystalByMonsterDrop($dbhandle, $account_id, $character_uid, $addType, $crystal, $payload, $remote_addr, $device_unique_id);
			$response = "MONSTER_DROP_CRYSTAL_OK";
		}
		else
		{
			$error = "Add Crystal Failed";
		}
	}
	else
	{
		$response = "MONSTER_DROP_CRYSTAL_DAILY_LIMIT_ERROR";
		$error = "You can't earn crystal from monsters anymore Today (Daily Limit)";
	}
	
	
	


	$currentCash = GetCash($dbhandle, $account_id);

	$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, 'crystal'=>$currentCash));
	

	mysqli_close($dbhandle);
}

	
$account_id = $_GET["account_id"];
$character_uid = $_GET["character_uid"];
$login_key = $_GET["login_key"];
$monster_class_id =  $_GET["monster_class_id"];
$monster_kill_count = $_GET["monster_kill_count"];
$device_unique_id = $_GET["device_unique_id"];
$checksum = $_GET["checksum"];

$head = $device_unique_id . $account_id;
$tail = $monster_class_id . $character_uid . $monster_kill_count;
if(CheckSum($head,$tail,$checksum))
{
	ServerMonsterCrystalDrop($account_id, $character_uid, $login_key, 1, $monster_class_id, $monster_kill_count, get_ip(), $device_unique_id);
}


//if(CheckValidatedServerIP())
//{
//	ServerMonsterCrystalDrop($_GET["account_id"],$_GET["character_uid"], $_GET["login_key"], 1);
//}
//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	));
//}

	

?>