<?php
//const WALLET_API_URL = 'http://20.196.209.15:3001';
const WALLET_API_URL = 'https://api.quantumchain.ai';
define('MIN_EXCHANGE_CRYSTAL_AMOUNT', 10000);
define('MAX_EXCHANGE_CRYSTAL_AMOUNT', 100000);
define('MAX_EXCHANGE_CRYSTAL_DAILY_LIMIT', 100000);
define('EXCHANGE_RATE_CRYSTAL_TO_TOKEN',0.95);
define('EXCHANGE_RATE_TOKEN_TO_CRYSTAL',1);
define('EXCHANGE_CHECKSUM','codbsdlcofladl#$1216!');

// GET 방식 함수
function get($url, $params=array()) 
{ 
    $url = $url.'?'.http_build_query($params, '', '&');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
 
// POST 방식 함수
function post($url, $fields)
{
    $post_field_string = http_build_query($fields, '', '&');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
    curl_setopt($ch, CURLOPT_POST, true);
    $response = curl_exec($ch);
    curl_close ($ch);
    return $response;
}

// post함수 호출
//post('url', array('field1'=>'value1', 'field2'=>'value2'));


function GetWalletInfo($dbhandle, $account_id, $login_key)
{
	$stmt = $dbhandle->prepare("select wallet_id , wallet_address from account where user_id=? and login_key=?");
	$stmt->bind_param("ss", $account_id, $login_key);
	$stmt->execute();
	$result = $stmt->get_result();
	$row = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	if( !$result )
	{
		return false;
	}
	
	return array($row["wallet_id"], $row["wallet_address"]);
}

function GetWP($dbhandle, $account_id, $character_uid)
{
	$stmt = $dbhandle->prepare("select wp from character_list where user_id=? and character_uid=?");
	$stmt->bind_param("ss", $account_id, $character_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	$row = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	if( !$result )
	{
		return -1; //error
	}
	
	return $row["wp"];
}

function GetCrystal($dbhandle, $account_id)
{
	$stmt = $dbhandle->prepare("select cash from account where user_id=?");
	$stmt->bind_param("s", $account_id);
	$stmt->execute();
	$result = $stmt->get_result();
	$row = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	if( !$result )
	{
		return -1; //error
	}
	
	return $row["cash"];
	
	
}


function EchoResponse($response, $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));
	
}

?>