<?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/deposits.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($_GET['action']))
{
	if($_GET['action'] == 'sell')
	{
		$id = intval($_GET['ident']);
		$q = $mysql->prepare("SELECT * FROM db_farm WHERE Uid = ? AND UserId = ?");
		$q->execute(array($id, $_SESSION['id']));
		if($q->rowCount() == 1)
		{
			$r = $q->fetch();
			$type = $r['Type'];
			$money = GetUser('MoneyIn', $_SESSION['id']);
			$money2 = GetUser('MoneyOut', $_SESSION['id']);
			$Reut = GetUser('Reuting', $_SESSION['id']);
			$LevelAcc = GetUser('Level', $_SESSION['id']);
			$rr = (time() - $r['DateAdd']) / 60;
			
			$prod = intval($rr * $Dohod[$type]);
			$Golden = intval($prod / $CountGold[$type]);
			$Goldenn = intval($Golden / 100 * $PercentOut[$LevelAcc]);
			$PercSerebro = 100 - $PercentOut[$LevelAcc];
			$Serebro = intval($Golden / 100 * $PercSerebro);
			$Reuting = intval($prod * 0.1);
			
			if($Golden >= 1)
			{
				$e = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ?, MoneyOut = MoneyOut + ? WHERE Uid = ?");
				$e->execute(array($Serebro, $Goldenn, $_SESSION['id']));
				$f = $mysql->prepare("UPDATE db_farm SET DateAdd = ? WHERE Uid = ? AND `Type` = ? AND UserId = ?");
				$f->execute(array(time(), $id, $type, $_SESSION['id']));
				
				$tmp = 'Продажа ресурсов : 
				<img src="/images/level.png"/>
				<span class="title fs14">+ '.$Reuting.'</span>
				<img src="/images/moneyin.png"/>
				<span class="title fs14">+ '.$Serebro.'</span>
				<img src="/images/moneyout.png"/>
				<span class="title fs14">+ '.$Goldenn.'</span>';
				$g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$g->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
				
				$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($money + $Serebro), "moneyout" => intval($money2 + $Goldenn), "level" => "<a href=\"/levels\" title=\"Опыт\"><img src=\"/images/level.png\"/> <span class=\"title\">".$LevelAcc."</span> <span id=\"led\"><span id=\"strip\" style=\"width:".$width."%;\"><span id=\"exp-one\">".$Reut."</span>/".$FullOpyt."</span></span></a>", "depo$id .result" => "<span class=\"green\">Вы успешно продали ресурсы!</span>", "depomonin$id" => '0', "depomonout$id" => '0', "depoexp$id" => '0', "depores$id" => '0'));
			}
			else
			{
				echo json_encode($result = array("depo$id .result" => "Не достаточно ресурсов"));
			}
			
			//echo json_encode($result = array("depo$id .result" => " $prod"));
		}
		else
		{
			echo json_encode($result = array("depo$id .result" => "Ошибочка, это не ваша шахта!"));
		}
	}
	elseif($_GET['action'] == 'update')
	{
		
		$id = intval($_GET['ident']);
		$q = $mysql->prepare("SELECT * FROM db_farm WHERE Uid = ? AND UserId = ?");
		$q->execute(array($id, $_SESSION['id']));
		if($q->rowCount() == 1)
		{
			$r = $q->fetch();
			$money = GetUser('MoneyIn', $_SESSION['id']);
			$money2 = GetUser('MoneyOut', $_SESSION['id']);
			$Reut = GetUser('Reuting', $_SESSION['id']);
			$LevelAcc = GetUser('Level', $_SESSION['id']);
			$Reuting = intval($PriceModer[$r['Type']] * 0.5);
			if($r['Type'] < 6){
				if($money >= $PriceModer[$r['Type']]){
					$tt = $r['Type'] + 1;
					$w = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn - ? WHERE Uid = ?");
					$w->execute(array($PriceModer[$r['Type']], $_SESSION['id']));
					$a = $mysql->prepare("UPDATE db_farm SET `Type` = `Type` + ?, DateAdd = ?, LastDate = ? WHERE Uid = ? AND UserId = ?");
					$a->execute(array(1, time(), time() + (60*60*24*$Srok[$tt]), $id, $_SESSION['id']));
					
					
					$tmp = 'Улучшение военной технике: '.$Name[$r['Type']].' <img src="/images/icons/opit.jpg"/><span class="title fs14">+ '. $PriceModer[$r['Type']] * 0.1 .'</span>
					<img src="/images/icons/buy.jpg"/><span class="title fs14">- '.$PriceModer[$r['Type']].'</span>';
					$ggq = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
					$ggq->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
					
					
						$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($money - $PriceModer[$r['Type']]), "moneyout" => intval($money2), "level" => "<a href=\"/levels\" title=\"Опыт\"><img src=\"/images/level.png\"/> <span class=\"title\">".$LevelAcc."</span> <span id=\"led\"><span id=\"strip\" style=\"width:".$width."%;\"><span id=\"exp-one\">".$Reut."</span>/".$FullOpyt."</span></span></a>", "depo$id .result" => "Техника улучшена! <script> setTimeout(function() { location.reload(); }, 2000); </script>"));
				}
				else
				{
					echo json_encode($result = array("depo$id .result" => "Недостаточно средств!"));
				}
			}
			else
			{
				echo json_encode($result = array("depo$id .result" => "Данную технику нельзя улучшить!"));
			}
		}
		else
		{
			echo json_encode($result = array("depo$id .result" => "Ошибочка, это не ваше здание!"));
		}
	}
	elseif($_GET['action'] == 'rem')
	{
		$id = intval($_GET['ident']);
		$q = $mysql->prepare("SELECT * FROM db_farm WHERE Uid = ? AND UserId = ?");
		$q->execute(array($id, $_SESSION['id']));
		if($q->rowCount() == 1)
		{
			$r = $q->fetch();
			$money = GetUser('MoneyIn', $_SESSION['id']);
			$money2 = GetUser('MoneyOut', $_SESSION['id']);
			$Reut = GetUser('Reuting', $_SESSION['id']);
			$LevelAcc = GetUser('Level', $_SESSION['id']);
			$Reuting = intval($Remont[$r['Type']] * 1);
			
				if($money >= $Remont[$r['Type']]){
					$tt = $r['Type'];
					$w = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn - ? WHERE Uid = ?");
					$w->execute(array($Remont[$r['Type']], $_SESSION['id']));
					$a = $mysql->prepare("UPDATE db_farm SET LastDate = ? WHERE Uid = ? AND UserId = ?");
					$a->execute(array(time() + (60*60*24*$Srok[$tt]), $id, $_SESSION['id']));
					
					
					$tmp = 'Ремонт здания: '.$Name[$r['Type']].' <img src="/images/level.png"/><span class="title fs14">+ '. $Remont[$r['Type']] * 0.1 .'</span>
					<img src="/images/moneyin.png"/><span class="title fs14">- '.$Remont[$r['Type']].'</span>';
					$ggq = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
					$ggq->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
					
					
						$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($money - $Remont[$r['Type']]), "moneyout" => intval($money2), "level" => "<a href=\"/levels\" title=\"Опыт\"><img src=\"/images/level.png\"/> <span class=\"title\">".$LevelAcc."</span> <span id=\"led\"><span id=\"strip\" style=\"width:".$width."%;\"><span id=\"exp-one\">".$Reut."</span>/".$FullOpyt."</span></span></a>", "depo$id .result" => "Здание Отремонтировано! <script> setTimeout(function() { location.reload(); }, 2000); </script>"));
				}
				else
				{
					echo json_encode($result = array("depo$id .result" => "Недостаточно средств!"));
				}
			
		}
		else
		{
			echo json_encode($result = array("depo$id .result" => "Ошибочка, это не ваша техника!"));
		}
	}
	elseif($_GET['action'] == 'dell')
	{
		
		$id = intval($_GET['ident']);
		$q = $mysql->prepare("SELECT * FROM db_farm WHERE Uid = ? AND UserId = ?");
		$q->execute(array($id, $_SESSION['id']));
		if($q->rowCount() == 1)
		{
			$q = $mysql->prepare("DELETE FROM db_farm WHERE Uid = ? AND UserId = ?");
			$q->execute(array($id, $_SESSION['id']));
			echo json_encode($result = array("depo$id" => "<script> $(\"div.load$id\").remove(); setTimeout(function() { location.reload(); }, 1000); </script>"));
		}
		else
		{
			echo json_encode($result = array("depo$id .result" => "Ошибочка, это не ваша техника!"));
		}
	}
}
?>