<?php
session_start();
header("Content-type: text/html; charset=windows-1251");
if(isset($_SESSION["login"])){
define("PATH", $_SERVER['DOCUMENT_ROOT']);
include(PATH."/core/func.php");
include(PATH."/core/db.php");
include(PATH."/core/set.php");
include(PATH."/core/define.php");
include(PATH."/core/defineset.php");

$hide = "<script type='text/javascript'>
$('#play-".intval($_POST["id"])."').hide();
</script>";


db_connect();
$res = db_query("SELECT `balance` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["login"])."'");
$user = db_assoc($res);
$res = db_query("SELECT * FROM `".DBpref."games` WHERE `id` = ".intval($_POST["id"]));

if(db_rows($res) == 0){
	echo "<script type='text/javascript'>
$('.play-".intval($_POST["id"])."').html('<font style=\"margin:10px 0px 0px 170px;\">Игра не найдена</font>');
</script>";
}else{
if($_POST['h_block_hidden'] == 'h_gold_0'){
   $_POST["item"] = 1;
} else if($_POST['h_block_hidden'] == 'h_gold_1'){
   $_POST["item"] = 2;
} else if($_POST['h_block_hidden'] == 'h_random'){
   $_POST["item"] = rand(1,2);
   //echo $_POST["item"];
}
$row = db_assoc($res);
	$err = NULL;
	if(round($user["balance"],2) < round($row["summa"],2))
		$err .= "На Вашем балансе недостаточно средств. ";
	if(empty($_POST["h_block_hidden"]))
		$err .= "Выберите предмет. ";
		
	if($err == NULL){
	
	//победа
		if($row["item"] == $_POST["item"]){
			db_query("INSERT INTO `".DBpref."games_end` (`summa`, `item`, `item2`, `login`, `login2`, `dat`, `dat2`, `md5`) VALUES (".round($row["summa"],2).", ".$row["item"].", ".intval($_POST["item"]).", '".db_string($row["login"])."', '".db_string($_SESSION["login"])."', '".$row["dat"]."', '".date("Y-m-d H:i:s")."', '".$row["md5"]."')");
			
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".(round($row["summa"]*(1-playCom),2)).", `game_win` = `game_win` + 1, `rating` = `rating` + ".($row["summa"] / 100)."  WHERE `login`  = '".db_string($_SESSION["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `game_loss` = `game_loss` + 1, `rating` = `rating` + ".($row["summa"] / 100 / 2)." WHERE `login`  = '".db_string($row["login"])."'");
			//начисление бонуса в кнб и ежемес
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");

			//Возврат комиссии
			$refback = db_assoc(db_query("SELECT `rating`, `bonus_lvl` FROM `".DBpref."users` WHERE `login`  = '".db_string($row["login"])."'"));
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($row["summa"]/1000*getRefProc($refback["rating"], $refback["bonus_lvl"]),2)."  WHERE `login`  = '".db_string($row["login"])."'");
			//
			$REFrow = db_assoc(db_query("SELECT `ref`, `refback` FROM `".DBpref."users` WHERE `login`  = '".db_string($_SESSION["login"])."'"));
			if($REFrow['ref'] != NULL){
				$p_rate = db_assoc(db_query("SELECT `partner_rating` FROM `".DBpref."users` WHERE `login`  = '".$REFrow['ref']."'"));

				//сумма которую получит партнер с сыгравшего рефа
				$partner_sum = getPartnerPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 
				//сумма которую получит реф с рефбека
				$ref_sum = getRefPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 

				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($ref_sum,2)."  WHERE `login`  = '".db_string($_SESSION["login"])."'");
				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				db_query("UPDATE `".DBpref."users` SET `partner_rating` = `partner_rating` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				$partner_bonus = $partner_sum*10000;
				db_query("UPDATE `".DBpref."users` SET `partner_bonus` = `partner_bonus` + ".($partner_bonus)."  WHERE `login`  = '".db_string($REFrow["ref"])."'");
			}
			//история
			db_query("INSERT INTO `knb_history` (`user`, `date`, `amount`, `method`, `status`, `tipo`) VALUES('".db_string($_SESSION["login"])."', '".$row["dat"]."', '".(round($row["summa"]*(1-playCom),2))."', 'Выигрыш', 'Зачислено', 'popoln')") or die(db_error());
			
			
			db_query("DELETE FROM `".DBpref."games` WHERE `id` = ".intval($_POST["id"]));
			$aa = round($row["summa"]*(1-playCom));
			echo "<script type='text/javascript'>$('.play-".intval($_POST["id"])."').html('<font color=\"#0F680B\" style=\"margin:10px 0px 0px 170px;\">Выигрыш</font>');
 $('.profile-balance-value').html(parseFloat($('.profile-balance-value').text(),2) + parseFloat('".$aa."', 2));

 </script>";

//поражение
		}elseif(($row["item"] == 1 AND $_POST["item"] == 2) OR ($row["item"] == 2 AND $_POST["item"] == 1)){
			db_query("INSERT INTO `".DBpref."games_end` (`summa`, `item`, `item2`, `login`, `login2`, `dat`, `dat2`, `md5`) VALUES (".round($row["summa"],2).", ".$row["item"].", ".intval($_POST["item"]).", '".db_string($row["login"])."', '".db_string($_SESSION["login"])."', '".$row["dat"]."', '".date("Y-m-d H:i:s")."', '".$row["md5"]."')");
			
			//начисление бонуса в кнб и ежемес
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			

			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` - ".round($row["summa"],2).", `game_loss` = `game_loss` + 1, `rating` = `rating` + ".($row["summa"] / 100 / 2)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			//история
			db_query("INSERT INTO `knb_history` (`user`, `date`, `amount`, `method`, `status`, `tipo`) VALUES('".db_string($_SESSION["login"])."', '".$row["dat"]."', '".round($row["summa"],2)."', 'Поражение', 'Списано', 'rashod')") or die(db_error());
			
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".(round($row["summa"],2) + round($row["summa"]*(1-playCom),2)).", `game_win` = `game_win` + 1, `rating` = `rating` + ".($row["summa"] / 100)." WHERE `login`  = '".db_string($row["login"])."'");
			//история
			db_query("INSERT INTO `knb_history` (`user`, `date`, `amount`, `method`, `status`, `tipo`) VALUES('".db_string($row["login"])."', '".$row["dat"]."', '".(round($row["summa"]*(1-playCom),2))."', 'Выигрыш', 'Зачислено', 'popoln')") or die(db_error());

			//Возврат комиссии
			$refback = db_assoc(db_query("SELECT `rating`, `bonus_lvl` FROM `".DBpref."users` WHERE `login`  = '".db_string($_SESSION["login"])."'"));
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($row["summa"]/1000*getRefProc($refback["rating"], $refback["bonus_lvl"]),2)."  WHERE `login`  = '".db_string($_SESSION["login"])."'");
			//


			$REFrow = db_assoc(db_query("SELECT `ref`, `refback` FROM `".DBpref."users` WHERE `login`  = '".db_string($row["login"])."'"));
			if($REFrow['ref'] != NULL){
				$p_rate = db_assoc(db_query("SELECT `partner_rating` FROM `".DBpref."users` WHERE `login`  = '".$REFrow['ref']."'"));

				//сумма которую получит партнер с сыгравшего рефа
				$partner_sum = getPartnerPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 

				//сумма которую получит реф с рефбека
				$ref_sum = getRefPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 

				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($ref_sum,2)."  WHERE `login`  = '".db_string($row["login"])."'");
				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				db_query("UPDATE `".DBpref."users` SET `partner_rating` = `partner_rating` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				$partner_bonus = $partner_sum*10001;
				db_query("UPDATE `".DBpref."users` SET `partner_bonus` = `partner_bonus` + ".($partner_bonus)."  WHERE `login`  = '".db_string($REFrow["ref"])."'");
			}

			
			db_query("DELETE FROM `".DBpref."games` WHERE `id` = ".intval($_POST["id"]));
			echo "<script type='text/javascript'>$('.play-".intval($_POST["id"])."').html('<font color=\"#f00\" style=\"margin:10px 0px 0px 170px;\">Поражение</font>');
$('.profile-balance-value').html(parseFloat($('.profile-balance-value').text(),1) - parseFloat('".round($row["summa"])."', 2));

</script>";
		}else{

			//начисление бонуса в кнб и ежемес
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `orel_bonus` = `orel_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5000)." WHERE `login`  = '".db_string($row["login"])."'");
			db_query("UPDATE `".DBpref."users` SET `month_bonus` = `month_bonus` + ".($row["summa"] * 5001)." WHERE `login`  = '".db_string($_SESSION["login"])."'");
			//

			//Возврат комиссии
			$refback = db_assoc(db_query("SELECT `rating`, `bonus_lvl` FROM `".DBpref."users` WHERE `login`  = '".db_string($row["login"])."'"));
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($row["summa"]/1000*getRefProc($refback["rating"], $refback["bonus_lvl"]),2)."  WHERE `login`  = '".db_string($row["login"])."'");
			//

			db_query("INSERT INTO `".DBpref."games_end` (`summa`, `item`, `item2`, `login`, `login2`, `dat`, `dat2`, `md5`) VALUES (".round($row["summa"],2).", ".$row["item"].", ".intval($_POST["item"]).", '".db_string($row["login"])."', '".db_string($_SESSION["login"])."', '".$row["dat"]."', '".date("Y-m-d H:i:s")."', '".$row["md5"]."')");
			
			db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".(round($row["summa"]*(1-playCom),2)).", `game_win` = `game_win` + 1, `rating` = `rating` + ".($row["summa"] / 100)."  WHERE `login`  = '".db_string($_SESSION["login"])."'");
			//история
			db_query("INSERT INTO `knb_history` (`user`, `date`, `amount`, `method`, `status`, `tipo`) VALUES('".db_string($_SESSION["login"])."', '".$row["dat"]."', '".(round($row["summa"]*(1-playCom),2))."', 'Выигрыш', 'Зачислено', 'popoln')") or die(db_error());
			
			db_query("UPDATE `".DBpref."users` SET `game_loss` = `game_loss` + 1, `rating` = `rating` + ".($row["summa"] / 100 / 2)." WHERE `login`  = '".db_string($row["login"])."'");
			//история
			db_query("INSERT INTO `knb_history` (`user`, `date`, `amount`, `method`, `status`, `tipo`) VALUES('".db_string($row["login"])."', '".$row["dat"]."', '".round($row["summa"],2)."', 'Поражение', 'Списано', 'rashod')") or die(db_error());
			
			
			$REFrow = db_assoc(db_query("SELECT `ref`, `refback` FROM `".DBpref."users` WHERE `login`  = '".db_string($_SESSION["login"])."'"));
			if($REFrow['ref'] != NULL){
				$p_rate = db_assoc(db_query("SELECT `partner_rating` FROM `".DBpref."users` WHERE `login`  = '".$REFrow['ref']."'"));

				//сумма которую получит партнер с сыгравшего рефа
				$partner_sum = getPartnerPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 
				//сумма которую получит реф с рефбека
				$ref_sum = getRefPay($row["summa"], getPartnerProc($p_rate['partner_rating']), $REFrow['refback']); 

				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($ref_sum,2)."  WHERE `login`  = '".db_string($_SESSION["login"])."'");
				db_query("UPDATE `".DBpref."users` SET `balance` = `balance` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				db_query("UPDATE `".DBpref."users` SET `partner_rating` = `partner_rating` + ".round($partner_sum,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				$partner_bonus = $partner_sum*10000;
				db_query("UPDATE `".DBpref."users` SET `partner_bonus` = `partner_bonus` + ".($partner_bonus)."  WHERE `login`  = '".db_string($REFrow["ref"])."'");
			}
			
			db_query("DELETE FROM `".DBpref."games` WHERE `id` = ".intval($_POST["id"]));
			/*echo "<script type='text/javascript'>$('.play-".intval($_POST["id"])."').html('<font color=\"#0F680B\">Победа</font>');
</script>"."<script type='text/javascript'>$(function(){
$('#play-balance').text(parseFloat($('#play-balance').text()) + ".(round($row["summa"]*(1-playCom),2)).");
});</script>";*/
echo "<script type='text/javascript'>$('.play-".intval($_POST["id"])."').html('<font color=\"#0F680B\" style=\"margin:10px 0px 0px 170px;\">Выигрыш</font>'); 
$('.profile-balance-value').html(parseFloat($('.profile-balance-value').text(), 1) + ".(round($row["summa"]*(1-playCom),2)).");


</script>";
		}
	}else{
		echo "<script type='text/javascript'>$('.play-".intval($_POST["id"])."').html('".$err."');
</script>";
	}
}

db_close();
?>


<?php 
}
?>

