<?php
include 'common.php';


function SaveSkillSlotSettings($account_id, $char_uid, $login_key, $skill_slot_0, $skill_slot_1, $skill_slot_2, $skill_slot_3)
{
	$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;
	}
	
	
	$update_query = "update character_list set skill_slot_0=".$skill_slot_0.", skill_slot_1=".$skill_slot_1.",skill_slot_2=".$skill_slot_2.", skill_slot_3=".$skill_slot_3." where user_id='".$account_id."' and character_uid=".$char_uid;
	

	//Query Result
	$result = @mysqli_query($dbhandle,$update_query);
	$response = "SAVE_SKILL_SLOT_SETTINGS_FAILED";
	$error = "";
	if($result)
	{
		$response = "SAVE_SKILL_SLOT_SETTINGS_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);
 
    $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');
 
 //test
 //$error = $update_query;
    echo json_encode(array('result'=>$response,'error'=>$error));
}



if(CheckValidatedServerIP())
{
	SaveSkillSlotSettings($_GET['account_id'], $_GET['char_uid'], $_GET['login_key'], $_GET['skill_slot_0'], $_GET['skill_slot_1'],  $_GET['skill_slot_2'],  $_GET['skill_slot_3']);
}
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	));
}

?>