<?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/shop.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');
//include($_SERVER['DOCUMENT_ROOT'].'/lib/libmail.php');

sleep(3);
if(isset($_GET['ident']))
{
	$id = (int)$_GET['ident'];
	
	if($id >= 1 and $id <= 6)
	{
		$money = GetUser('MoneyIn', $_SESSION['id']);
		$money2 = GetUser('MoneyOut', $_SESSION['id']);
		$Reut = GetUser('Reuting', $_SESSION['id']);
		$LevelAcc = GetUser('Level', $_SESSION['id']);
		if($money >= $Price[$id])
		{
			if($Level[$id] <= $LevelAcc){
				$reuting = $Price[$id] * 0.5;
				$srokk = time() + (60*60*24*$Srok[$id]);
				$q = $mysql->prepare("INSERT INTO db_farm SET UserId = ?, `Type` = ?, DateAdd = ?, LastDate = ?, Status = ?");
				$q->execute(array($_SESSION['id'], $id, time(), $srokk, 0));
				$w = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn - ? WHERE Uid = ?");
				$w->execute(array($Price[$id], $_SESSION['id']));
				$FullOpyt = $Lev[$LevelAcc];
				ReutingUser($_SESSION['id'], $LevelAcc, $Reut, $FullOpyt, $reuting);
				$tmp = 'Покупка военной техники : 
				<img src="/images/icons/opit.jpg"/>
				<span class="title fs14">+ '.$reuting.'</span>
				<img src="/images/icons/buster.jpg"/>
				<span class="title fs14">- '.$Price[$id].'</span>';
				$g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$g->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
				
				
				$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($money - $Price[$id]), "moneyout" => intval($money2), "level" => "<a href=\"/levels\" title=\"Опыт\"><img src=\"/images/icons/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>", "shop$id .result" => "<span class=\"green\">Вы успешно купили военную технику!</span>"));
			}
			else
			{
				echo json_encode($result = array("shop$id .result" => "Недостаточный уровень!"));
			}
		}
		else
		{
			echo json_encode($result = array("shop$id .result" => "Не достаточно бустеров на балансе!"));
		}
		//echo json_encode($result = array("shop$id .result" => $Price[$id]));
		
	}
	else
	{
		exit();//echo json_encode($result = array("shop$id .result" => "Не выбрана шахта"));
	}
	
}

?>