Location: PHPKode > projects > Trunk.net > casino.php
<?php
//ob_start("ob_gzhandler");
require_once("include/functions.php");
dbconn(false);
loggedinorreturn();

if ($CURUSER[casino] == 1 || $CURUSER[id] == 1) {
stderr("Ïîêà!","Äîñòóï â êàçèíî çàêðûò!");
die();
}

/*
if (substr($_SERVER[HTTP_REFERER], 0, 26) != $DEFAULTBASEURL && substr($_SERVER[HTTP_REFERER], 7, 10) != $HTTP_SERVER_VARS["SERVER_ADDR"])
    die("<html><head></head><body><table border=0 width=100% height=100%><tr><td><h3 align=center>Èãðàòü âîçìîæíî òîëüêî ñ òðåêåðà!</h3></td></tr></table></body></html>");
*/


//if ($CURUSER['class'] < 7) stderr("Âðåìåííî çàêðûòî","!");

function tr2()
{
$a = func_get_args(tr2);
for($i=0;$i< (func_num_args(tr2)/2)+1;$i++)
 $row .= "<td ".$a[$i].">".$a[++$i]."</td>";
echo "<tr>".$row."</tr>";
}

function validbet($bet) {
    return !preg_match('/^[0-9]+$/', $bet);
}

////////////////////////////////////////////////CONFIG
$bank = 10;	//ñëó÷àéíîå ÷èñëî, ïðè âûïàäå ðàíäîìîì ýòîãî ÷èñëà ñòàâêè èäóò Âèêå.
$mb_basic=1024*1024;
$maxusrbet = '1';    //Amount of bets to allow per person
$maxtotbet = 5;    //Amount of total open bets allowed
$writelog='n';     //Write a record to the log
$delold='n';     //Clear bets once finished? (cleanup.php will if value isn't 'y')
$sendfrom='0';     //The id of the user which notification PM's are noted as sent from
$casino=$HTTP_SERVER_VARS['PHP_SELF'];    //Name of file
$oneticket = 1073741824; //Minimum Cost Tickets 1Gb
$numwinners = 15;	//×èñëî ïîáåäèòåëåé
$lotinterval = 3;   // 3 days between drawings
$now = time();
////////////////////////////////////////////////END OF CONFIG


////////////////////////////////////////////////CASINO


//get user stats
$betsp = mysql_query("SELECT challenged FROM casino_bets WHERE proposed = '".$CURUSER['username']."'");
$openbet = 0;
while($tbet2 = mysql_fetch_assoc($betsp))
{
if($tbet2[challenged]=='empty')
 $openbet++;
}

//Convert bet amount into bits
if(isset($_POST['unit']))
 {
  if ($_POST["unit"] =='1')
    $nobits = $amnt*$mb_basic;
   else
    $nobits = $amnt*$mb_basic*1024;
 }
$time = strtotime("now");
$time = date('Y-n-j G:i:s',$time);
$goback = "<a href=$casino>Íàçàä</a>";


//Take Bet
if (isset($_GET["takebet"]))
{
$betid=$_GET["takebet"];
$random = rand(0,1);
$loc = mysql_query("SELECT * FROM casino_bets WHERE id = '$betid'");
$tbet= mysql_fetch_assoc($loc);
$nogb = mksize($tbet[amount]);

if($CURUSER['id']==$tbet['userid'])
    stderr("Îøèáêà", "Âû õîòèòå èãðàòü ñ ñîáîé?");
else if($tbet['challenged'] != "empty")
    stderr("Îøèáêà", "Êòî-òî âçÿë âàøó ñòàâêó!");

if(isset($CURUSER['casino']) && $CURUSER['casino'] < $tbet['amount'])
    stderr("Îøèáêà", "Ó âàñ íå õâàòàåò ãèãàáàéò ÷òîáû âçÿòü ýòó ñòàâêó!");


/*
if (rand(0,20) == $bank)
{
 mysql_query("UPDATE users SET casino = casino + ".($tbet['amount']*2)." WHERE id = 1") or sqlerr();
 mysql_query("UPDATE users SET casino = casino - ".$tbet['amount']." WHERE id = '".$CURUSER['id']."'") or sqlerr();
 mysql_query("UPDATE users SET casino = casino - ".$tbet['amount']." WHERE id = '".$tbet['userid']."'") or sqlerr();
 mysql_query("INSERT INTO messages (id, sender, receiver, added, msg, unread, poster, location) VALUES ('', '$sendfrom', '".$tbet['userid']."', '$time','Íèêòî íå âûèãðàë! Êðåäèò áûë äîáàâëåí â áàíê!' , 'yes', '$sendfrom','in')") or sqlerr();
 stderr("Áàíê","<h2>Íèêòî íå âûèãðàë. Êðåäèò áûë äîáàâëåí â áàíê!</h2>");
}
*/
if($random == 1)
{
 mysql_query("UPDATE users SET casino = casino+".$tbet['amount']." WHERE id = '".$CURUSER['id']."'") or sqlerr();
 mysql_query("UPDATE casino_bets SET result = '1', challenged = '".$CURUSER['username']."'  WHERE id = $betid") or sqlerr();
 mysql_query("INSERT INTO messages (id, sender, receiver, added, msg, unread, poster, location) VALUES ('', '$sendfrom', '".$tbet['userid']."', '$time','Âû ïðîèãðàëè ñòàâêó! ".$CURUSER['username']." âûèãðàë $nogb âàøåãî êðåäèòà!' , 'yes', '$sendfrom','in')") or sqlerr();
 mysql_query("DELETE FROM casino_bets WHERE id = $betid") or sqlerr();
 if($writelog=='y')
 write_log($CURUSER['username']." âûèãðàë $nogb êðåäèòà ó $tbet[proposed]","","casino");
 stderr("Âû âûèãðàëè","<h2>Âû âûèãðàëè ñòàâêó, $nogb áûëè ïîëîæåíû íà âàø ñ÷¸ò.</h2>&nbsp;&nbsp;&nbsp;$goback");
}
else
{
 if(empty($newup)) $newup = $CURUSER['casino'] - $tbet['amount'];
 $newup2 = $tbet['amount']*2;
 mysql_query("UPDATE users SET casino = $newup WHERE id = '".$CURUSER['id']."'") or sqlerr();
 mysql_query("UPDATE users SET casino = casino + $newup2 WHERE id = '".$tbet['userid']."'") or sqlerr();
 mysql_query("UPDATE casino_bets SET result = '2', challenged = '".$CURUSER['username']."' WHERE id = $betid") or sqlerr();
 mysql_query("INSERT INTO messages (id, sender, receiver, added, msg, unread, poster, location) VALUES ('', '$sendfrom', '".$tbet['userid']."', '$time','Âû âûèãðàëè $nogb êðåäèòà îò ïîëüçîâàòåëÿ ".$CURUSER['username']."!', 'yes', '$sendfrom','in')") or sqlerr();
 mysql_query("DELETE FROM casino_bets WHERE id = $betid") or sqlerr();
 if($writelog=='y')
  write_log("$tbet[proposed] âûèãðàë $nogb êðåäèòà ó ".$CURUSER['username'],"","casino");
 stderr("Ñîæàëåþ","<h2>Âû ïðîèãðàëè ñòàâêó, <a href=userdetails.php?id=$tbet[userid]>$tbet[proposed]</a> âûèãðàë $nogb âàøåãî êðåäèòà!</h2> &nbsp;&nbsp;&nbsp;$goback");
}

exit(); //// the user should not reach this code but for security :-)
}

//Add a new bet
if (isset($_POST['random']) == 'random' )
{
$rand = rand(0,100);
$nobits = $rand*$mb_basic*1024;
}
else
{
if ($_POST["unit"] =='1')
 $nobits = $_POST["amnt"]*$mb_basic;
else
 $nobits = $_POST["amnt"]*$mb_basic*1024;
}

if (isset($_POST["unit"]))
{
if($openbet >= $maxusrbet)
 stderr ("Èçâèíèòå","Ó Âàñ îòêðûòî ìàêñèìàëüíîå ÷èñëî ñòàâîê ($openbet)!");

if($nobits < 1)
 stderr ("Îøèáêà","Íåâåðíàÿ ñòàâêà");

if($CURUSER['casino'] < $nobits)
  stderr("Èçâèíèòå","<h2>".mksize($nobits)." ýòî áîëüøå ÷åì ó âàñ åñòü!</h2>$goback");

$newup = $CURUSER['casino'] - $nobits;
$betsp = mysql_query("SELECT id, amount FROM casino_bets WHERE userid = ".$CURUSER['id']." ORDER BY time ASC") or sqlerr();
$tbet2 = mysql_fetch_row($betsp);

$dummy = "<H2>Ñòàâêà äîáàâëåíà, âû ïîëó÷èòå ËÑ î ðåçóëüòàòå, åñëè êòî òî ïðèìåò âàøó ñòàâêó.</H2>";
if ($_POST['random'] == 'random' ) {
mysql_query("INSERT INTO casino_bets ( userid, proposed, challenged, amount, time, rand) VALUES ('".$CURUSER['id']."','".$CURUSER['username']."', 'empty', $nobits, '$time', 'yes')") or sqlerr(); }
else {
mysql_query("INSERT INTO casino_bets ( userid, proposed, challenged, amount, time) VALUES ('".$CURUSER['id']."','".$CURUSER['username']."', 'empty', $nobits, '$time')") or sqlerr(); 
}
mysql_query("UPDATE users SET casino = $newup  WHERE id = ".$CURUSER['id']) or sqlerr();
if(mysql_affected_rows()==0)
mysql_query("INSERT INTO casino (userid, date, deposit) VALUES (".$CURUSER['id'].", '$time', '".$nobits."')") or sqlerr(); 
}
$loca = mysql_query("SELECT * FROM casino_bets WHERE challenged ='empty'") or sqlerr();
$totbets = mysql_num_rows($loca);
////////////////////////////////////////////////END CASINO


////////////////////////////////////////////////LOTTERY
if ($_POST['tickets']) 
{
	$purchaseable = floor($CURUSER["casino"]/$oneticket);
	$buy = floor($_POST['tickets']);
		if ($buy > $purchaseable || $buy < 1)
		stderr("Îøèáêà","Âû ìîæåòå êóïèòü íå áîëåå $purchaseable áèëåòîâ");
	$casino = $CURUSER["casino"] - ($oneticket * $buy);
	mysql_query("UPDATE users SET casino=$casino WHERE id=". $CURUSER["id"]) or sqlerr();
	for ($i = 0; $i < $buy; $i++)
	mysql_QUERY("INSERT INTO tickets(user) VALUES($CURUSER[id])");
	$message = "<b>Ïîçäðàâëÿþ, êóïëåíî ".$buy." áèëåò(îâ)</b>";
}
////////////////////////////////////////////////END LOTTERY


///////////////////////////////////////////////BLACK JACK
function get_user_id($username)
{
    $r = mysql_query("select id from users where username = '$username'") or sqlerr(__FILE__,__LINE__);
    $a = mysql_fetch_row($r);
    return "$a[0]";
}

if ($_POST["bet"] || $_POST["game"] || is_numeric($_GET["takegame"]))
{

    //Óçíàåì ñòàâêó íà êîòîðóþ èãðàåò þçåð
    $bet = $_POST["bet"];	//ñòàâêà, íà êîòîðóþ ïîëüçîâàòåëü èãðàåò
    if ($bet == '100 GB') $mb = 100*1024*1024*1024;
    elseif ($bet == '300 GB') $mb = 300*1024*1024*1024;
    elseif ($bet == '500 GB') $mb = 500*1024*1024*1024;
    elseif ($bet == '1 TB') $mb = 1024*1024*1024*1024;
    elseif ($bet == '3 TB') $mb = 3*1024*1024*1024*1024;
    elseif ($bet == '5 TB') $mb = 5*1024*1024*1024*1024;
    else $mb = 50*1024*1024*1024;	//50Gb
    //Óçíàåì ñòàâêó íà êîòîðóþ èãðàåò þçåð

    $cardcount = mysql_num_rows(mysql_query("select id from cards"));
    if ($_POST["game"] == 'start' || $_GET["takegame"]) //çàïóñêàåì èãðó
    {
	$cardid = rand(1,$cardcount);
	$cardres = mysql_query("select * from cards where id=$cardid") or sqlerr(__FILE__, __LINE__);
	$cardarr = mysql_fetch_array($cardres);
	if ($_POST["game"] == 'start')
	{
	    $numbets = mysql_query("SELECT id FROM bj WHERE placeholder = '".$CURUSER[username]."' AND plstat = 'waiting'") or sqlerr(__FILE__, __LINE__);
	    $check5 = mysql_num_rows($numbets);
	    if($CURUSER["casino"] < $mb) stderr("Èçâèíèòå", "Ó Âàñ íå õâàòàåò ñðåäñòâ äëÿ èãðû.") or sqlerr(__FILE__, __LINE__);
	    if($check5 >= 5) stderr("Îøèáêà", "Âû íå ìîæåòå íà÷èíàòü èãðó, ïîêà ó âàñ îòêðûòî áîëåå 5 èãð.") or sqlerr(__FILE__, __LINE__);
	    mysql_query("UPDATE users SET casino = casino - $mb WHERE id = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);	//ñíèìàåì Ãáàéòû çà èãðó ñðàçó
	    mysql_query("INSERT INTO bj (placeholder, points, plstat, bet, cards, date) values('$CURUSER[username]', $cardarr[points], 'playing', $mb, $cardid, $now)") or sqlerr(__FILE__, __LINE__);
	    $id = mysql_insert_id();
	}

        if (is_numeric($_GET["takegame"]))	//êòî òî âçÿë èãðó
	{
	    $gid = $_GET["takegame"];
	    $bet = mysql_fetch_row(mysql_query("SELECT bet, gamer, placeholder FROM bj WHERE id = $gid")) or die("<html><head></head><body><table border=0 width=100% height=100%><tr><td><h3 align=center>Îøèáêà. Èãðà íå íàéäåíà â áàçå.</h3></td></tr></table></body></html>");
	    if($CURUSER["casino"] < $bet[0]) stderr("Èçâèíèòå", "Ó Âàñ íå õâàòàåò ñðåäñòâ äëÿ èãðû.");
	    if($bet[1])	
	    {
		header("Location: /casino.php");
		exit();
	    }
	    if($bet[2] == $CURUSER[username]) stderr("Îøèáêà", "Èãðàòü ñàìîìó ñ ñîáîé òóïî!") or sqlerr(__FILE__, __LINE__);
	    mysql_query("UPDATE users SET casino = casino - $bet[0] WHERE id = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);	//ñíèìàåì Ãáàéòû çà èãðó ñðàçó
	    mysql_query("UPDATE bj SET gamer = '$CURUSER[username]' WHERE id = $gid") or sqlerr(__FILE__, __LINE__);		//äîáàâëÿåì èãðîêà ó òîé ñòðîêè ñ êîòîðîé èãðàþò (îáíîâëÿåì íîâûì id)
	    mysql_query("INSERT INTO bj (placeholder, points, plstat, bet, cards, date, gamewithid) VALUES('$CURUSER[username]', $cardarr[points], 'playing', $bet[0], $cardid,  $now, $gid)") or sqlerr(__FILE__, __LINE__);	//busy = òîé ñ êåì èãðàþò
	    $id = mysql_insert_id();	//èãðàåì äëÿ ñâîåé ñòðîêè, âñå äàííûå çàïèñûâàþòñÿ â íå¸, à íå â òó ñ êåì èãðàåì
	}
	stdhead();
	print("<h1>Äîáðî ïîæàëîâàòü, <a href=userdetails.php?id=$CURUSER[id]>$CURUSER[username]</a>!</h1>\n");
	print("<table  cellspacing=0 cellpadding=3 width=600>\n");
	print("<tr><td colspan=2 cellspacing=0 cellpadding=5 >");
	print("<form name=bj method=post action=casino.php>");
	print("<table class=message width=100% cellspacing=0 cellpadding=5 bgcolor=white>\n");
	print("<tr><td align=center><img src=pic/cards/".$cardarr["pic"]." border=0></td></tr>");
	print("<tr><td align=center><b>Points = $cardarr[points]</b></td></tr>");	//åñëè èãðó íå âçÿëè òî id=ïðîñòîìó íîìåðó ñòðîêè, èíà÷å = íîìåðó  èãðû
	print("<tr><td align=center><input type=hidden name=id value='$id'><input type=hidden name=game value=cont><input type=submit value='Åù¸'></td></tr>");
	print("</table><br>");
	print("</form>");
	print("</td></tr></table><br>");
    }
    elseif ($_POST["game"] == 'cont')	//ïîëüçîâàòåëü íàæàë ÅÙ¨!
    {
	$id = $_POST["id"];
        $playeres = mysql_query("select * from bj where id=$id") or sqlerr(__FILE__, __LINE__);
        $playerarr = mysql_fetch_array($playeres);
	if ($playerarr["plstat"] == 'waiting') 
	    stderr("Îøèáêà", "Âû çàêîí÷èëè ýòó èãðó");
        $showcards = "";
        $cards = $playerarr["cards"];
        $usedcards = explode(" ", $cards);
        $arr = array();
        foreach($usedcards as $array_list)
    	    $arr[] = $array_list;
	foreach($arr as $card_id)
	{
	    $used_card = mysql_query("SELECT * FROM cards WHERE id='$card_id'") or sqlerr(__FILE__, __LINE__);
	    $used_cards = mysql_fetch_array($used_card);
	    $showcards .= "<img src=pic/cards/".$used_cards["pic"]." border=0> "; 
	    $i++;
	}
	$cardid = rand(1,$cardcount);
	while (in_array($cardid, $arr))
	    $cardid = rand(1,$cardcount);
	$cardres = mysql_query("select * from cards where id=$cardid") or sqlerr(__FILE__, __LINE__);
	$cardarr = mysql_fetch_array($cardres);
	$showcards .= "<img src=pic/cards/".$cardarr["pic"]." border=0> ";
	$points = $playerarr["points"] + $cardarr["points"];
	$mysqlcards = "$playerarr[cards] $cardid";
	mysql_query("update bj set points=points+$cardarr[points], cards='$mysqlcards' where id=$id") or sqlerr(__FILE__, __LINE__);
	if ($points == 21)	//Î÷êî
	{
	    mysql_query("UPDATE bj SET plstat = 'waiting', date = $now WHERE id = $id") or sqlerr(__FILE__, __LINE__);	//äëÿ òåêóùåãî þçåðà îñòàíàâëèâàåì èãðó
	    $check101 = mysql_query("SELECT gamewithid FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
	    $check = mysql_fetch_row($check101);
	    if ($check[0])	//Î÷êî ó òîãî êòî ïðèíÿë èãðó
	    {
		$r = mysql_query("SELECT * FROM bj WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);	//÷üþ èãðó ïðèíÿë
	        $a = mysql_fetch_assoc($r);
	        if ($a["points"] != 21)
	        {
	    	    $winorlose = "âû âûèãðàëè ".mksize($a[bet]).". <a href=$casino>Íàçàä</a>";
		    mysql_query("update users set casino = casino + ".($a[bet]*2)." where id = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);
		    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âû ïðîèãðàëè ".mksize($a[bet])." ïîëüçîâàòåëþ $CURUSER[username] (Âû íàáðàëè $a[points] î÷êîâ, $CURUSER[username] íàáðàë 21).', 0)") or sqlerr(__FILE__, __LINE__); 
		    mysql_query("UPDATE bj SET winner = '$CURUSER[username]', gamewithid = $points, date = $now, plstat = 'finished' WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
		}
		else
		{
		    mysql_query("UPDATE bj SET winner = 'Íè÷üÿ', gamewithid = $points, date = $now, plstat = 'finished' WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
		    mysql_query("update users set casino = casino + $a[bet] where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);	//âîçâðàùàåì ìáàéòû âçÿòûå âíà÷àëå èãðû
		    mysql_query("update users set casino = casino + $a[bet] where id=".get_user_id($a[placeholder])."") or sqlerr(__FILE__, __LINE__);	//âîçâðàùàåì ìáàéòû âçÿòûå âíà÷àëå èãðû
		    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Íèêòî íå âûèãðàë. Âû íàáðàëè $a[points] î÷êîâ, $CURUSER[username] íàáðàë $points î÷êîâ).', 0)") or sqlerr(__FILE__, __LINE__); 
		    $winorlose = "Íèêòî íå âûèãðàë. <a href=$casino>Íàçàä</a>";
		}
		mysql_query("DELETE FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
		stderr("Êîíåö èãðû", "Âû íàáðàëè $points î÷êîâ, âàøèì îïïîíåíòîì áûë $a[placeholder], îí íàáðàë $a[points] î÷êîâ, $winorlose");
	    }
	    else		//Èíà÷å î÷êî ó òîãî êòî íà÷àë èãðó
		stderr("Êîíåö èãðû", "Âû íàáðàëè î÷êî (21). Âàì ïðèä¸ò ëè÷íîå ñîîáùåíèå î ðåçóëüòàòå èãðû. <a href=$casino>Íàçàä</a>");
	}
	elseif ($points > 21)	//êîíåö èãðû, ïåðåáîð
	{

	    mysql_query("UPDATE bj SET plstat = 'waiting', date = $now WHERE id = $id");
	    $check = mysql_fetch_row(mysql_query("SELECT gamewithid FROM bj WHERE id = $id"));
	    if ($check[0])	//ïåðåáîð ó òîãî êòî ïðèíÿë èãðó
	    {
		$r = mysql_query("SELECT * FROM bj WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
	        $a = mysql_fetch_assoc($r);
	        if (($a["points"] >= $points || $points >= $a["points"]) && $a["points"] > 21)	//ïåðåáîð ó îáîèõ
		{
		    $winorlose = "íèêòî íå âûèãðàë. âåñü âûèãðûø óõîäèò â áàíê. <a href=$casino>Íàçàä</a>";
		    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âàøèì îïïîíåíòîì áûë $CURUSER[username], íèêòî íå âûèãðàë. Âåñü âûèãðûø óõîäèò â áàíê.', 0)") or sqlerr(__FILE__, __LINE__);
		    mysql_query("UPDATE bj SET winner = 'Âèêà', gamewithid = $points, date = $now, plstat = 'finished' WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
		    mysql_query("update users set casino = casino + ".($a[bet]*2)." where id=1") or sqlerr(__FILE__, __LINE__);
		}
		elseif ($a["points"] < $points && $a["points"] <= 21)	//ïåðåáîð ó òîãî êòî ïðèíÿë èãðó
		{
		    $winorlose = "âû ïðîèãðàëè ".mksize($a[bet]).". <a href=$casino>Íàçàä</a>";
		    mysql_query("update users set casino = casino + ".($a[bet]*2)." where username = '$a[placeholder]'") or sqlerr(__FILE__, __LINE__);
		    if($a[placeholder])
			mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âû âûèãðàëè ".mksize($a[bet])." ó ïîëüçîâàòåëÿ $CURUSER[username] (Âû íàáðàëè $a[points] î÷êîâ, $CURUSER[username] íàáðàë $points î÷êîâ).', 0)") or sqlerr(__FILE__, __LINE__);
		    mysql_query("UPDATE bj SET winner = '$a[placeholder]', gamewithid = $points, date = $now, plstat = 'finished' WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
		}
		mysql_query("DELETE FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
		stderr("Êîíåö èãðû", "Âû íàáðàëè $points î÷êîâ, âàøèì îïïîíåíòîì áûë $a[placeholder], îí íàáðàë $a[points] î÷êîâ, $winorlose");
	    }
	    else		//Èíà÷å ïåðåáîð ó òîãî êòî íà÷àë èãðó
		stderr("Êîíåö èãðû", "Âû íàáðàëè $points î÷êîâ, à ýòî áîëüøå 21. Âàì ïðèä¸ò ëè÷íîå ñîîáùåíèå î ðåçóëüòàòå èãðû. <a href=$casino>Íàçàä</a>");
	}
	else		//åñëè î÷êîâ ìåíüøå 21, òî ïðîäîëæàåì èãðàòü
	{
	    stdhead();
	    print("<h1>Äîáðî ïîæàëîâàòü, <a href=userdetails.php?id=$CURUSER[id]>$CURUSER[username]</a>!</h1>\n");
	    print("<table  cellspacing=0 cellpadding=3 width=600>\n");
	    print("<tr><td colspan=2 cellspacing=0 cellpadding=5 >");
	    print("<table class=message width=100% cellspacing=0 cellpadding=5 bgcolor=white>\n");
	    print("<tr><td align=center>$showcards</td></tr>");
	    print("<tr><td align=center><b>Points = $points</b></td></tr>");
	    print("<form name=bj method=post action=casino.php>");
	    print("<tr><td align=center><input type=hidden name=id value='$id'><input type=hidden name=game value=cont><input type=submit value='Åù¸'></td></tr>");
	    print("</form>");
	    print("<form name=bj method=post action=casino.php onSubmit=this.submit.disabled=true>");
	    print("<tr><td align=center><input type=hidden name=id value='$id'><input type=hidden name=game value=stop><input type=submit name=submit value='Õâàòèò' onkeypress=\"return false\"></td></tr>");
	    print("</form>");
	    print("</table><br>");
	    print("</td></tr></table><br>");
	}
    }
    elseif ($_POST["game"] == 'stop')
    {
	$id = $_POST["id"];
	$playeres = mysql_query("select * from bj where id = $id") or sqlerr(__FILE__, __LINE__);
	$playerarr = mysql_fetch_array($playeres);
	if ($playerarr["plstat"] == 'waiting' || !$playerarr)
	    stderr("Îøèáêà", "Âû çàêîí÷èëè ýòó èãðó");
	mysql_query("UPDATE bj SET plstat = 'waiting', date = $now WHERE id = $id");
	if ($playerarr[gamewithid])	//îñòàíîâêà èãðû ó òîãî êòî ïðèíÿë èãðó, $playerarr - ýòî ÿ
	{
	    $r = mysql_query("SELECT * FROM bj WHERE id = $playerarr[gamewithid]") or sqlerr(__FILE__, __LINE__);	//âûáèðàåì âñ¸ ó òîãî êòî íà÷àë èãðó
	    $a = mysql_fetch_assoc($r);
	    if ($a["points"] == $playerarr[points] && $a["points"] < 22 && $playerarr[points] < 22)
	    {
		$winorlose = "íèêòî íå âûèãðàë. <a href=$casino>Íàçàä</a>";
		mysql_query("update users set casino = casino + $a[bet] where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
		mysql_query("update users set casino = casino + $a[bet] where username = '$a[placeholder]'") or sqlerr(__FILE__, __LINE__);
		mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âàøèì îïïîíåíòîì áûë $CURUSER[username], íèêòî íå âûèãðàë.', 0)") or sqlerr(__FILE__, __LINE__);
		mysql_query("UPDATE bj SET winner = 'Íè÷üÿ', gamewithid = $playerarr[points], date = $now, plstat = 'finished' WHERE id = $playerarr[gamewithid]") or sqlerr(__FILE__, __LINE__);
		mysql_query("DELETE FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
	    }
	    elseif ($playerarr[points] < $a["points"] && $a[points] > 21 || $playerarr[points] > $a["points"] && $playerarr[points] < 22)
	    {
		$winorlose = "âû âûèãðàëè ".mksize($a[bet]).". <a href=$casino>Íàçàä</a>";
		mysql_query("update users set casino = casino + ".($a[bet]*2)." where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
		if($a[placeholder])
		    mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âû ïðîèãðàëè ".mksize($a[bet])." ïîëüçîâàòåëþ $CURUSER[username] (Âû íàáðàëè $a[points] î÷êîâ, $CURUSER[username] íàáðàë $playerarr[points] î÷êîâ).', 0)") or sqlerr(__FILE__, __LINE__);
		mysql_query("UPDATE bj SET winner = '$CURUSER[username]', gamewithid = $playerarr[points], date = $now, plstat = 'finished' WHERE id = $playerarr[gamewithid]") or sqlerr(__FILE__, __LINE__);
		mysql_query("DELETE FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
	    }
	    elseif ($playerarr["points"] < $a["points"] && $a["points"] <= 21)
	    {
		$winorlose = "âû ïðîèãðàëè ".mksize($a[bet]).". <a href=$casino>Íàçàä</a>";
		mysql_query("update users set casino = casino + ".($a[bet]*2)." where username = '$a[placeholder]'") or sqlerr(__FILE__, __LINE__);
		mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âû âûèãðàëè ".mksize($a[bet])." ó ïîëüçîâàòåëÿ $CURUSER[username] (Âû íàáðàëè $a[points] î÷êîâ, $CURUSER[username] íàáðàë $playerarr[points] î÷êîâ).', 0)") or sqlerr(__FILE__, __LINE__);
		mysql_query("UPDATE bj SET winner = '$a[placeholder]', gamewithid = $playerarr[points], date = $now, plstat = 'finished' WHERE id = $playerarr[gamewithid]") or sqlerr(__FILE__, __LINE__);
		mysql_query("DELETE FROM bj WHERE id = $id") or sqlerr(__FILE__, __LINE__);
	    }
	    elseif ($a["points"] > 21 && $playerarr["points"] > 21)	//ïåðåáîð ó îáîèõ
	    {
	        $winorlose = "íèêòî íå âûèãðàë. âåñü âûèãðûø óõîäèò â áàíê. <a href=$casino>Íàçàä</a>";
		mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, ".get_user_id($a[placeholder]).", NOW(), 'Âàøèì îïïîíåíòîì áûë $CURUSER[username], íèêòî íå âûèãðàë. Âåñü âûèãðûø óõîäèò â áàíê.', 0)") or sqlerr(__FILE__, __LINE__);
		mysql_query("UPDATE bj SET winner = 'Âèêà', gamewithid = $points, date = $now, plstat = 'finished' WHERE id = $check[0]") or sqlerr(__FILE__, __LINE__);
		mysql_query("update users set casino = casino + ".($a[bet]*2)." where id=1") or sqlerr(__FILE__, __LINE__);
	    }
	    stderr("Êîíåö èãðû", "Âû íàáðàëè $playerarr[points] î÷êîâ, âàøèì îïïîíåíòîì áûë $a[placeholder], îí íàáðàë $a[points] î÷êîâ, $winorlose");
	}
	else	//Îñòàíîâêà ó òîãî êòî íà÷àë èãðó
	    header("Location: $DEFAULTBASEURL$casino");
//	    stderr("Êîíåö èãðû", "Âû íàáðàëè $playerarr[points] î÷êîâ, âàì ïðèä¸òñÿ ïîäîæäàòü ïîêà ñ âàìè ñûãðàþò. Âû ïîëó÷èòå óâåäîìëåíèå î ðåçóëüòàòå èãðû. <a href=$casino>Íàçàä</a>");
    }	//GAME == STOP
}	/////////////////////////BLACKJACK
else
{
    {
	stdhead("Èãðû");
	echo("<h2>Ñòàâêè ñ ïîëüçîâàòåëÿìè</h2>");
	print("<table class=message width=650 cellspacing=0 cellpadding=5>\n");
	print("<tr><td align=center>");
	//echo($dummy);

	//Place bet table
	if ($openbet < $maxusrbet)
	{
	if($totbets >= $maxtotbet)
	    echo "<br>Óæå îòêðûòî ìàêñèìàëüíîå êîëè÷åñòâî ñòàâîê $maxtotbet, âîçüìèòå îòêðûòóþ ñòàâêó!<br>";
	else
	{
	    ?><table width=90% cellspacing=0 cellpadding=3>
	    <tr><td align=center colspan=4 class=colhead>Ñòàâêè</td></tr>
	    <tr><td align=center><b>Ñòàâèì</b></td>
	    <td align=left width=50% style="border-right: none;"><form name=p2p method=post action=casino.php onSubmit=this.submit.disabled=true>
	    <input type=text maxlength=3 name=amnt size=40 value=1 onkeypress="if((event.keyCode||e.keyCode)==13)return false">
	    <select name=unit>
	    <option value=2>GB</option>
	    <option value=1>MB</option>
	    </select></td>
	    <td align=left style="border-left: none; border-right: none;"><input type=checkbox name=random value=random> Ñëó÷àéíî</td>
	    <td align=right style="border-left: none; padding-right: 10px"><input name=submit type=submit onkeypress="return false" value=Ïîåõàëè!></td></tr>
	    </table></form>
	    <?
	}
    }

    //Open Bets table
    echo ("<table width=90% cellspacing=0 cellpadding=3><br>");
    tr2('align=center class=colhead colspan=4','Îòêðûòûå ñòàâêè');
    echo("<tr>");
    echo("<td align=center width=15%><b>Ïîëüçîâàòåëü</b></td><td width=15% align=center><b>Ïîñòàâèë</b></td>");
    echo("<td width=45% align=center><b>Âðåìÿ</b></td><td align=center><b>Ñûãðàòü</b></td>");
    echo("</tr>");

while ($res = mysql_fetch_assoc($loca))
{
 echo (" <tr>");
 echo("<td align=center>$res[proposed]</td>");
 if ($res['rand'] == 'yes') {
 echo("<td align=center>Ñëó÷àéíî</td>"); }
 else {
 echo("<td align=center>".mksize($res['amount'])."</td>"); }
 echo("<td align=center>$res[time]</td>");
 echo("<td align=center><b><a href=".$DEFAULTBASEURL.$casino."?takebet=$res[id]>Âçÿòü ñòàâêó</a></b></td>");
 echo("</tr>");
 $abcdefgh=1;
}
if($abcdefgh==false)
 echo("<tr><td align=center colspan=4>Èçâèíèòå, ñòàâîê íåò</td></tr>");
echo "</table>";
echo('</table><br>');
}

?>
<h2>Èãðà ñ ïîëüçîâàòåëÿìè â BlackJack</h2>
<table width=650 cellspacing=0 cellpadding=5>
    <tr><td colspan=3 cellspacing=0 cellpadding=5 align=center style="border-bottom: none;">
    <form name=blackjack method=post action=casino.php>
    <input type=hidden name=game value=start>
    <input type=submit name=bet style="width: 70px; height: 18px; background: #74AE04; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='50 GB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #9E9E9E; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='100 GB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #0574C9; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='300 GB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #DB48A2; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='500 GB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #D8DB04; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='1 TB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #EFA900; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='3 TB'>&nbsp;&nbsp;&nbsp;
    <input type=submit name=bet style="width: 70px; height: 18px; background: #DC0000; color: #FFFFFF; font-weight: bold; border: 1px solid white" value='5 TB'>
    </form>
    </td></tr><tr><td colspan=4 style="border-top: none;">
	<form name=takegame method=post action=casino.php?>
	<table width=90% align=center cellpadding=2 cellspacing=0>
	    <tr><td align=center class=colhead colspan=4>Îòêðûòûå èãðû:</td></tr>
	    <tr><td align=center width=15%><b>Íà÷àë</b></td><td align=center width=15%><b>Ïðèíÿë</b></td><td align=center width=20%><b>Âðåìÿ</b></td><td align=center width=40%><b>Ñûãðàòü</b></td></tr>
	    <? //ñíà÷àëà âûâîäèì òå èãðû êîòîðûå ñâîáîäíûå...
	    $qr = mysql_query("SELECT placeholder, gamer, id, date, winner, bet FROM bj WHERE plstat = 'waiting' ORDER BY `gamer` ASC");
	    while ($arr = @mysql_fetch_assoc($qr))
	    {	//åñëè ñòàâêà âçÿòà èëè òîò êòî ñòàâèë îí è åñòü òî îòðóáàåì êíîïêó
		$self = ($arr[placeholder] == $CURUSER[username] || $arr[gamer] ? "disabled" : "");
		$color = ($arr[bet] == 50*1024*1024*1024 ? "74AE04" : "").($arr[bet] == 100*1024*1024*1024 ? "9E9E9E" : "").($arr[bet] == 300*1024*1024*1024 ? "0574C9" : "").($arr[bet] == 500*1024*1024*1024 ? "DB48A2" : "").($arr[bet] == '1099511627780' ? "D8DB04" : "").($arr[bet] == '3298534883330' ? "EFA900" : "").($arr[bet] == 5*1024*1024*1024*1024 ? "DC0000" : "");
		if ($arr[gamer] && !$arr[winner]) $winner = "&nbsp;---><b>&nbsp;???????</b>&nbsp;";
		echo "<tr><td width=15% align=center>$arr[placeholder]</td>\n<td width=15% align=center>$arr[gamer]</td>\n<td width=20% align=center>".date("Y-m-d H:i:s", $arr["date"])."</td>\n<td width=40% align=".($winner ? "left" : "center").">$pts<input type=button style=\"".($winner ? "margin-left: 70px;" : "")." width: 70px; height: 18px; background: #$color; color: #FFFFFF; font-weight: normal; border: 1px solid white\" $self value='".mksize($arr[bet])."' onClick=\"window.location.href='casino.php?takegame=$arr[id]'\">$winner</td></tr>\n";
		unset($self);
		unset($winner);
		unset($pts);
	    }
	    //òåïåðü âûâîäèì èãðû êîòîðûå ñûãðàíû
	    $qr2 = mysql_query("SELECT placeholder, gamer, points, id, date, winner, bet, gamewithid FROM bj WHERE plstat = 'finished' ORDER BY `date` DESC  LIMIT 50");
	    while ($arr = @mysql_fetch_assoc($qr2))
	    {	//åñëè ñòàâêà âçÿòà èëè òîò êòî ñòàâèë îí è åñòü òî îòðóáàåì êíîïêó
		unset($self);
		unset($winner);
		unset($bgcolor);
		unset($pts);
		$bgcolor = ($CURUSER[username] == $arr[gamer] || $CURUSER[username] == $arr[placeholder] ? "bgcolor=#F5F4EA" : "");
		$self = ($arr[placeholder] == $CURUSER[username] || $arr[gamer] ? "disabled" : "");
		$color = ($arr[bet] == 50*1024*1024*1024 ? "74AE04" : "").($arr[bet] == 100*1024*1024*1024 ? "9E9E9E" : "").($arr[bet] == 300*1024*1024*1024 ? "0574C9" : "").($arr[bet] == 500*1024*1024*1024 ? "DB48A2" : "").($arr[bet] == '1099511627780' ? "D8DB04" : "").($arr[bet] == '3298534883330' ? "EFA900" : "").($arr[bet] == 5*1024*1024*1024*1024 ? "DC0000" : "");
		if ($arr[gamer] && !$arr[winner]) $winner = "&nbsp;---><b>&nbsp;???????</b>&nbsp;";
		if ($arr[winner]) { $winner = "&nbsp;---><b>&nbsp;$arr[winner]</b>&nbsp;"; $pts = $arr[points]." | ".$arr[gamewithid]; }
		echo "<tr><td $bgcolor width=15% align=center>$arr[placeholder]</td>\n<td $bgcolor width=15% align=center>$arr[gamer]</td>\n<td $bgcolor width=20% align=center>".date("Y-m-d H:i:s", $arr["date"])."</td>\n<td $bgcolor width=40% align=".($winner ? "left" : "center")."><input type=button style=\"".($winner ? "margin-left: 30px;" : "")." width: 70px; height: 18px; background: #$color; color: #FFFFFF; font-weight: normal; border: 1px solid white\" $self value='".mksize($arr[bet])."' onClick=\"window.location.href='casino.php?takegame=$arr[id]'\">$winner$pts</td></tr>\n";
	    }
	    ?>
	</form></table>
    </td></tr>
</table>
<?
$arr = mysql_fetch_assoc(mysql_query("SELECT value_u, value_s, value_i FROM avps WHERE arg='lottery'")) or sqlerr();
$oldwin = $arr[value_i];
$oldwinners = $arr[value_s];
$ts = $arr[value_u];

if ($ts < $now) 
{
$q2 = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tickets")) or sqlerr();
$numtickets = $q2[0];
$win = ($numtickets / $numwinners) * 1024*1024*1024;
 $res = mysql_query("SELECT t.*, u.username FROM tickets as t LEFT JOIN users as u ON t.user = u.id GROUP BY t.user ORDER BY RAND() LIMIT $numwinners") or sqlerr();
   $winners = array();
   $msg = sqlesc("Ïîçäðàâëÿþ, êóïëåííûé áèëåò â ëîòåðåå îêàçàëñÿ âûèãðûøíûì.\nÂû ïîëó÷àåòå ".mksize($win)." íà ñ÷¸ò â êàçèíî.\nÑïàñèáî çà èãðó");

	while ($a = mysql_fetch_array($res))
	{
	  $user = $a[username];
	  mysql_query("UPDATE users SET casino = casino + $win WHERE id = $a[user]") or sqlerr();
	  mysql_query("INSERT INTO messages (sender, receiver, added, msg, poster) VALUES(0, $a[user], NOW(), $msg, 0)") or sqlerr(__FILE__, __LINE__);
	  $winners[] = $user;
	}
   $winners = implode(", ", $winners);
 $lotdate = $ts + $lotinterval*86400;
mysql_query("UPDATE avps SET value_u = $lotdate, value_s = ".sqlesc($winners).", value_i = $win WHERE arg='lottery'") or sqlerr();
mysql_query("TRUNCATE TABLE tickets") or sqlerr();
}

$purchaseable = ($CURUSER["casino"] > 1024*1024*1024 ? floor($CURUSER[casino]/$oneticket) : 0);
$q1 = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tickets"));
$total = $q1[0];
$pot = $total * $oneticket;
$me = mysql_query("SELECT * FROM tickets WHERE user=" . $CURUSER["id"]);
$my = mysql_num_rows($me);

while ($myrow = mysql_fetch_assoc($me))
$ticketnumbers .= "$myrow[id] ";
?>
<h2>Ëîòåðåÿ</h2>
<table border=1 width=650 cellspacing=0 cellpadding=5>
<tr><td align=left class=tableb>
<ul>
<li>Ïðîøëûå ïîáåäèòåëè: <br><font color=red><b><?= $oldwinners ?></b></font></li>
<li>Ïðîøëûé âûèãðûø ñîñòàâèë: <b><?=mksize($oldwin)?></b></li>
<li>Ïðîñìîòð <b><a href=viewtickets.php>ñòàòèñòèêè</a></b> ïðîäàæ áèëåòîâ.</li>
<li>Ñòîèìîñòü îäíîãî áèëåòà <b><?= mksize($oneticket) ?></b> âàøåãî ñ÷åòà â êàçèíî.</li>
<li>Ðîçûãðûø ëîòåðåè ïðîâîäèòñÿ êàæäûå <b><?= $lotinterval ?></b> äíÿ.</li>
<li>Ñëåäóþùèé ðîçûãðûø áèëåòîâ ñîñòîèòñÿ: <b><?=date("d-m-Y H:i",$ts) ?></b></li>
<li>Ñëó÷àéíûì îáðàçîì áóäóò âûáðàíû <b><?= $numwinners; ?></b> ïîáåäèòåëåé.</li>
<li>Êàæäûé èç ïîáåäèòåëåé ïîëó÷èò <b><?= mksize($pot/$numwinners); ?></b> íà ñ÷åò â êàçèíî.</li>
<li>×åì áîëüøå áèëåòîâ áóäåò ïðîäàíî, òåì áîëüøå áóäåò äæåêïîò!</li>
<li>Âû ïðèîáðåëè áèëåòû ñ íîìåðàìè: <a href="javascript: klappe_news('a1')"><img border="0" src="pic/plus.gif" id="pica1" alt=\"Show / Hide\"></a>
<div id="ka1" style="display: none;"><?= $ticketnumbers; ?></div></li>
</ul>
<hr>
<table align=center width=40% class=main border=1 cellspacing=0 cellpadding=10><tr><td align=center>
<table width=100% border=1 cellspacing=0 cellpadding=5>
    <tr>
      <td class=tableb>Äæåêïîò</td>
      <td class=tableb align=right><?= mksize($pot); ?></td>
    </tr>
    <tr>
      <td class=tableb>Ïðîäàíî</td>
      <td class=tableb align=right><?= $total; ?> áèëåò(îâ)</td>
    </tr>
    <tr>
       <td class=tableb>Âû êóïèëè</td>
      <td class=tableb align=right><?= $my; ?> áèëåò(îâ)</td>
    </tr>
    <tr>
      <td class=tableb>Âû ìîæåòå êóïèòü</td>
      <td class=tableb align=right><?= $purchaseable; ?> áèëåò(îâ)</td>
    </tr>
</table>
</table>
<hr>
<?
if ($purchaseable > 0)
{
?>
<center>
<form method="post" action="casino.php" onSubmit=this.submit.disabled=true>
Áèëåòû <input type="text" name="tickets" size=5 onkeypress="if((event.keyCode||e.keyCode)==13)return false"> <input name=submit type="submit" onkeypress="return false" value="Êóïèòü">
<center><?=$message?></center>
</form>
</center>
<?
}

print("</td></tr></table>");

}
stdfoot();
mysql_close();
?>
Return current item: Trunk.net