<?php
include 'common.php';

function ServerMonsterCrystalDrop($account_id, $login_key, $crystal)
{
	$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 = "MONSTER_DROP_CRYSTAL_FAILED";
	$error = "";
	$addType = "MONSTER_DROP";
	$character_uid = 0;
	
	if(AddCash($dbhandle,$account_id, $crystal))
	{
		LogAddCrystal($dbhandle, $account_id, $character_uid, $addType, $crystal);
		$response = "MONSTER_DROP_CRYSTAL_OK";
	}
	else
	{
		$error = "Add Crystal Failed";
	}
	
	


	$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);
}

	




//if(CheckValidatedServerIP())
//{
	ServerMonsterCrystalDrop($_GET["account_id"], $_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	));
//}

	

?>