<?php
include 'common.php';

function GetCurrentStatPoint($dbhandle, $char_uid)
{
	$stat_point = 0;
	
	
	$stmt = $dbhandle->prepare("select stat_point from character_list where character_uid=?");	
	$stmt->bind_param("i", $char_uid);
	$stmt->execute();
	$result = $stmt->get_result();	
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			$stat_point = $row["stat_point"];
			mysqli_stmt_free_result($stmt);
		}
	}
	else{
		$error = mysqli_error($dbhandle);
	}
	
	
	return $stat_point;
}

function GetCurrentStatTotal($dbhandle, $char_uid)
{
	$stat_total = 0;
	
	
	$stmt = $dbhandle->prepare("select stat_strength, stat_intellect, stat_agility, stat_wisdom, stat_health from character_list where character_uid=?");	
	$stmt->bind_param("i", $char_uid);
	$stmt->execute();
	$result = $stmt->get_result();	
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			
			$stat_total += $row["stat_strength"];
			$stat_total += $row["stat_intellect"];
			$stat_total += $row["stat_agility"];
			$stat_total += $row["stat_wisdom"];
			$stat_total += $row["stat_health"];
			mysqli_stmt_free_result($stmt);
		}
	}
	else{
		$error = mysqli_error($dbhandle);
	}
	
	
	return $stat_total;
}


function GetLevel($dbhandle, $char_uid)
{
	$level = 1;
	
	
	$stmt = $dbhandle->prepare("select level from character_list where character_uid=?");	
	$stmt->bind_param("i", $char_uid);
	$stmt->execute();
	$result = $stmt->get_result();	
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			$level = $row["level"];
			mysqli_stmt_free_result($stmt);
		}
	}
	else{
		$error = mysqli_error($dbhandle);
	}
	
	
	return $level;
}

function CheckStatDistribution($dbhandle, $char_uid, $new_stat_sum)
{
	//$level = GetLevel($dbhandle, $char_uid);
	//$cur_stat_total = GetCurrentStatTotal($dbhandle, $char_uid);
	$cur_stat_point = GetCurrentStatPoint($dbhandle, $char_uid); //remain point
	
	//$final_stat_sum = $cur_stat_total + $new_stat_sum;
	
	//$limit = ($level * 3) -3;
	
	if($cur_stat_point < $new_stat_sum)
		return false;
	
	//if($final_stat_sum > $limit)
	//	return false;
	
	if($new_stat_sum <=0) 
		return false;
	
	
	return true;
}

function SaveSpDistribution($account_id, $char_uid, $added_str, $added_int, $added_dex, $added_wis, $added_con, $login_key)
{
	$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 = "SAVE_SP_DIST_FAILED";
	$error = "";
	
	//
	
	
	
	$sum = $added_str + $added_int + $added_dex + $added_wis + $added_con;
	
	
	
	if(CheckStatDistribution($dbhandle, $char_uid, $sum ))
	{
		$update_query = "update character_list set stat_point= stat_point-".$sum.", stat_strength=stat_strength+".$added_str.", stat_intellect=stat_intellect+".$added_int.", stat_agility=stat_agility+".$added_dex.",stat_wisdom=stat_wisdom+".$added_wis.",stat_health=stat_health+".$added_con." where user_id='".$account_id."' and character_uid='".$char_uid."'";

		//Query Result
		$result = @mysqli_query($dbhandle,$update_query);
		if($result)
		{
			$response = "SAVE_SP_DIST_OK";
			// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
			//mysqli_free_result($result); 
		}
		else
		{
			$error = mysqli_error($dbhandle);
		}
		
		 mysqli_close($dbhandle);
	}
	else
	{
		$response = "SAVE_SP_DIST_FAILED";
		$error = "abusing stat";
	}
	
	
	
 
    $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));
}



if(CheckValidatedServerIP())
{	
	if($_GET['added_str'] < 0 || $_GET['added_int'] < 0 || $_GET['added_dex'] <0 || $_GET['added_wis'] <0 || $_GET['added_con'] <0)
	{
		$response = "SAVE_SP_DIST_FAILED";
		$error = "CheckValidation 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	));
		exit();
	}
	
	SaveSpDistribution($_GET['account_id'], $_GET['char_uid'], $_GET['added_str'], $_GET['added_int'], $_GET['added_dex'], $_GET['added_wis'], $_GET['added_con'], $_GET['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	));
}

?>