<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: /process/exchanger.php
*
*/

session_start();
header('Content-type: application/json');
Header("Content-Type: text/html;charset=UTF-8");
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit();}
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/CofigFarm.php');

if(isset($_POST['moneyout']))
{
	$money = intval($_POST['moneyout']);
	$UserMoney = GetUser('MoneyOut', $_SESSION['id']);
	$Reut = GetUser('Reuting', $_SESSION['id']);
	$LevelAcc = GetUser('Level', $_SESSION['id']);
	$moneyIn = GetUser('MoneyIn', $_SESSION['id']);
	$moneyOut = GetUser('MoneyOut', $_SESSION['id']);


	if($UserMoney >= $money)
	{

		$Exch = $money + ($money * 0.05);
		$Reuting = $money;
		$q = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ?, MoneyOut = MoneyOut - ? WHERE Uid = ?");
		$q->execute(array($Exch, $money, $_SESSION['id']));
		$FullOpyt = $Lev[$LevelAcc];
		
		ReutingUser($_SESSION['id'], $LevelAcc, $Reut, $FullOpyt, $Reuting);
			
		$Reut = GetUser('Reuting', $_SESSION['id']);
		$LevelAcc = GetUser('Level', $_SESSION['id']);
		
		if($LevelAcc < 12)
		{
			$FullOpyt = $Lev[$LevelAcc];
		}
		else
		{
			$FullOpyt = '&infin; max';
		}
		if($LevelAcc != 12)
		$width = 100 / $FullOpyt * $Reut;
		else $width = 100;
						
						
		echo json_encode($result = array("moneyin" => intval($moneyIn + $Exch), "moneyout" => intval($moneyOut - $money), "level" => "<a href=\"/levels\" title=\"Опыт\"><img src=\"/images/opit.jpg\"/> <span class=\"title\">".$LevelAcc."</span> <span id=\"led\"><span id=\"strip\" style=\"width:".$width."%;\"><span id=\"exp-one\">".$Reut."</span>/".$FullOpyt."</span></span></a>", "exchangerForm .fail" => 'Обмен прошел успешно!'));
	}
	else
	{
		echo json_encode($result = array("exchangerForm .fail" => 'Недостаточно средств на балансе'));
	}
}

?>