<?php
include 'common.php';

function CharacterPetQuery($character_uid){
	//Connect DB
	$dbhandle = DB_Connect();
	
	
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	//@todo
	//if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	//{
	//	PrintLoginKeyError();
	//	return;
	//}
	
	
	
	$strQuery = "select * from owned_pet where character_uid=".$character_uid." order by pet_uid asc";
	
	//echo $strQuery;
	//return;
	
	//Query Result
	if($result = mysqli_query($dbhandle, $strQuery))
	{
		$count = 0;
		$list = "";
		while($row=mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$count++;
			$dataArray[] = $row["owned_pet_uid"].",". $row["character_uid"].",".$row["pet_uid"] .",".$row["is_spawned"].",".$row["level"];
		}
		
	}
	else
	{
		
	}
	mysqli_free_result($result);

	
	if($count <= 0  )
	{
		$return = "NOT_EXIST_OWNED_PET";
		
		$response = array('result'=>$return,'error'=>"",'list'=>null);
	}
	else
	{
		$return = "OWNED_PET_LIST_OK";
		$response = array('result'=>$return,'error'=>"",'list'=>$dataArray);
	}
	
	$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);
		
	mysqli_close($dbhandle);
}

$json = json_decode(file_get_contents('php://input'));
$character_uid = $json->character_uid;

CharacterPetQuery($character_uid);

?>