<?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/bonus.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'] == 'last')
	{
		$q = $mysql->query("SELECT * FROM db_bonus ORDER BY Uid DESC LIMIT 10");
		if($q->rowCount() > 0)
		{
			$tmp = '<tr align="left">
						<td><span><b>ID</b></span></td>
						<td><span><b>Логин</b></span></td>
						<td><span><b>Сумма</b></span></td>
						<td><span><b>Время</b></span></td>
					</tr>';
			while($r = $q->fetch())
			{
				$tmp .= '<tr bgcolor="#f0e8d5">
							<td>#'.$r['Uid'].'</td>
							<td>'.$r['Login'].'</td>
							<td class="middle"><img src="/images/icons/buster.jpg"> + '.$r['Summa'].'</td>
							<td>'.date("d.m.Y (H:i)", $r['DateAdd']).'</td>
						</tr>';
			}
			echo json_encode($result = array("lastResult" => $tmp));
		}
		else
		{
			echo json_encode($result = array("lastResult" => "<div class=\"table-level\">Бонус еще никто не получал</div>"));
		}
	}
	elseif($_GET['action'] == 'take')
	{
		$d = $mysql->prepare("SELECT * FROM db_bonus WHERE UserId = ? ORDER BY Uid DESC LIMIT 1");
		$d->execute(array($_SESSION['id']));
		if($d->rowCount() == 0)
		{
			$Level = GetUser('Level', $_SESSION['id']);
			$money = GetUser('MoneyIn', $_SESSION['id']);
			$money2 = GetUser('MoneyOut', $_SESSION['id']);
				if($Level == 1) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 2) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 3) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 4) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 5) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 6) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 7) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 8) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 9) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 10) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 11) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 12) $sum = rand(1, $BonusSum[$Level]);
				//$sum = 122;
				$f = $mysql->prepare("INSERT INTO db_bonus SET UserId = ?, Login = ?, Summa = ?, DateAdd = ?, LastDate = ?");
				$f->execute(array($_SESSION['id'], $_SESSION['login'], $sum, time(), time() + 86400));
				
				$r = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ? WHERE Uid = ?");
				$r->execute(array($sum, $_SESSION['id']));
				$tmp = 'Получение бустеров : 
				<img src="/images/icons/buster.jpg"/>
				<span class="title fs14">+ '.$sum.'</span>';
				$g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$g->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
				
				
				echo json_encode($result = array("moneyin" => ($money + $sum), "bonusResult" => "<div class=\"log green\" id=\"mess\" align=\"center\">Вы получили бустеров : <img src=\"/images/icons/buster.jpg\"/><span class=\"title fs14\">+ ".$sum."</span></div>  <script type=\"text/javascript\">process('','/process\/bonus.php?action=last','lastResult','lastResult','last'); 
 				setTimeout('$(\"#mess\").fadeOut(1500)',10000);</script> До следующего бонуса : <span id=\"timer\">--:--:--<\/span>
				<script type=\"text/javascript\"> 
					var hour = 23; 
					var minute = 59; 
					var second = 59; 
					function timer() { 
						$(\"#timer\").html((hour < 10 ? \"0\"+hour : hour)+\":\"+(minute < 10 ? \"0\"+minute : minute)+\":\"+(second < 10 ? \"0\"+second : second)); --second; if(second < 0) { --minute; if(minute < 0) { --hour; if(hour < 0) { clearInterval(timer_id); } tminute = 59; } second = 59; } } timer(); timer_id = setInterval('timer()',1000); </script>"));
		}
		else
		{
			$dd = $d->fetch();
			if($dd['LastDate'] <= time())
			{
				$Level = GetUser('Level', $_SESSION['id']);
				$money = GetUser('MoneyIn', $_SESSION['id']);
			$money2 = GetUser('MoneyOut', $_SESSION['id']);
				if($Level == 1) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 2) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 3) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 4) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 5) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 6) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 7) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 8) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 9) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 10) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 11) $sum = rand(1, $BonusSum[$Level]);
				elseif($Level == 12) $sum = rand(1, $BonusSum[$Level]);
				//$sum = 122;
				$f = $mysql->prepare("INSERT INTO db_bonus SET UserId = ?, Login = ?, Summa = ?, DateAdd = ?, LastDate = ?");
				$f->execute(array($_SESSION['id'], $_SESSION['login'], $sum, time(), time() + 86400));
				
				$r = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ? WHERE Uid = ?");
				$r->execute(array($sum, $_SESSION['id']));
				$tmp = 'Получение бустеров : 
				
				<img src="/images/icons/buster.jpg"/>
				<span>+ '.$sum.'</span>';
				$g = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$g->execute(array($_SESSION['id'], time(), $tmp, $money, $money2));
				
				
				echo json_encode($result = array("moneyin" => ($money + $sum), "bonusResult" => "<div class=\"log green\" id=\"mess\" align=\"center\">Вы получили бустеров : <img src=\"/images\/icons\/buster.jpg\"/><span class=\"title fs14\">+ ".$sum."</span></div>  <script type=\"text/javascript\">process('','/process\/bonus.php?action=last','lastResult','lastResult','last'); 
 setTimeout('$(\"#mess\").fadeOut(1500)',10000);</script> До следующей раздачи бустеров : <span id=\"timer\">--:--:--<\/span>
<script type=\"text/javascript\"> 
var hour = 23; 
var minute = 59; 
var second = 59; 
function timer() { 
$(\"#timer\").html((hour < 10 ? \"0\"+hour : hour)+\":\"+(minute < 10 ? \"0\"+minute : minute)+\":\"+(second < 10 ? \"0\"+second : second)); --second; if(second < 0) { --minute; if(minute < 0) { --hour; if(hour < 0) { clearInterval(timer_id); } tminute = 59; } second = 59; } } timer(); timer_id = setInterval('timer()',1000); </script>"));
			}
			else
			{
				echo json_encode($result = array("bonusResult" => "<div class=\"log green\" id=\"mess\" align=\"center\">Вы же получали сегодня бустеры!</div>"));
			}
		}
	}
}

?>