<?php
include('../inc/conf.php');
ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');
session_set_cookie_params(604800000, "/", SITE, false, false);
session_start();
if (!isset($_SESSION["uid"])){
echo "5";
exit;
}
function esend($e_mail, $mail_t, $mail_to, $mail_m) {
$message = '
<html>
<head>
<title>'.SITENAME.'</title>
</head>
<body>
<br /><br />
<center><b>'.$mail_m.'</b></center><br /><br />
</body>
</html>'; 
$headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
$headers .= "From: ".SITENAME." <noreply@".SITE.">\r\n";
mail($e_mail, $mail_t, $message, $headers); }
if(isset($_POST['ok'])){
if(!empty($_POST['ok'])){
$uid = intval($_SESSION["uid"]);
$end = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT * FROM t_data LIMIT 1"));
if ($end['endgame']-time() < 0) {
$winner = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT id,usr FROM t_play ORDER BY RAND() LIMIT 1"));
$winsum = $end['nowg'];
if($end['ngame'] % 10 == 0) {
$winsum = $winsum + $end['jack'];
mysqli_query($connect_db, "UPDATE `t_data` SET `jack` = '0' LIMIT 1");
}
$dt = time();
mysqli_query($connect_db, "INSERT INTO `t_games` (n,usr,win,dt) VALUES ('$end[ngame]','$winner[usr]','$winsum','$dt')");
mysqli_query($connect_db, "UPDATE `t_users` SET `bal` = `bal`+'$winsum' WHERE uid = '$winner[usr]'");
mysqli_query($connect_db, "UPDATE `t_data` SET `endgame` = '0', `nowg` = '0' LIMIT 1");
mysqli_query($connect_db, "DELETE FROM `t_play`");
$uwinner = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT uid,log,ref,em,tm FROM t_users WHERE uid='$winner[usr]'"));
//Рефералу
if ($uwinner['ref'] > 0) {
$rplus = '0.05';//Процент
$toref = $winsum*$rplus;
mysqli_query($connect_db, "UPDATE t_users SET toref=toref+'$toref' WHERE uid='$uwinner[uid]' LIMIT 1");
mysqli_query($connect_db, "UPDATE t_users SET bal=bal+'$toref' WHERE uid='$uwinner[ref]' LIMIT 1");
}
//Отправка мыла
if ($uwinner['tm'] == 1) {
$emb = $uwinner['em'];
$mail_themeb = "Выигрыш";
$mail_topb = "Выигрыш";
esend($emb, $mail_themeb, $mail_topb, "Вы выиграли $winsum руб.");
}
}
echo '1';
} else { echo '0'; }
} else { echo '6'; }
?>