<?php 

ini_set('session.use_cookies','0'); 
ini_set('session.use_trans_sid','0'); 
ini_set('url_rewriter.tags',''); 
session_name("stw"); 
session_start();  
header("Cache-Control: no-cache");
if ($ver=="wml")header ("Content-type:text/vnd.wap.wml; charset=utf-8");
else header("Content-Type:text/html; charset=UTF-8");
require("../inc.php"); 
$link = connect_db();
list($row, $id, $ps, $fsize1, $fsize2) = check_login($link);
require("../version.php");
$posts=$row["posts"];
$guposts=$row["gposts"];

if(!session_is_registered("session")){ 
$session['round']=0; 
$session['money']=$posts; 
$session['con']=0; 
$session['histuser']=""; 
session_register("session"); 
}  
$ses2=SID;                  
$ses="$ses2&amp;$ses&amp;ref=$ref"; 

function cards_score($str){                 
if($str==1||$str==2||$str==3||$str==4){$num = 6;}                 
if($str==5||$str==6||$str==7||$str==8){$num = 7;}                 
if($str==9||$str==10||$str==11||$str==12){$num = 8;}                                 
if($str==13||$str==14||$str==15||$str==16){$num = 9;}                                 
if($str==17||$str==18||$str==19||$str==20){$num = 10;}                                 
if($str==21||$str==22||$str==23||$str==24){$num = 2;}                                 
if($str==25||$str==26||$str==27||$str==28){$num = 3;}                                       
if($str==29||$str==30||$str==31||$str==32){$num = 4;}                                       
if($str==33||$str==34||$str==35||$str==36){$num = 11;}                                       
return $num;                 
}   

if($posts<"3000"){
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n"; 
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n"; 
echo "<card id=\"index\" title=\"Стоп!\" ontimer=\"../enter.php?$ses\"><timer value=\"15\"/>\n";
echo "<p align=\"center\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>Стоп!</title>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=../enter.php?$ses\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "У Вас недостаточно постов для игры!<br/>";
echo "Необходимо не менее 3000 постов<br/>";
echo $fsize2;
echo $divide;
echo "<a href=\"../enter.php?id=$id&amp;ps=$ps&amp;ref=$ref\">Прихожая</a><br/>\n"; 
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
exit; 
}else{
if($session['round']==0){ 
$max = $session['money']; 
if($max==0){ 
session_unregister("session"); 
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n"; 
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n"; 
echo "<card id=\"index\" title=\"21\">\n"; 
echo "<p align=\"center\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>21</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1; 
echo "У Вас не достаточно постов для продолжения текущей игры!<br/>\n"; 
echo "<a href=\"out.php?$ses\">Выйти</a><br/>";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
exit; 
}
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n";
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n"; 
echo "<card id=\"index\" title=\"21\">\n"; 
echo "<p align=\"center\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>21</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "У Вас $max постов<br/>\n"; 
echo $fsize2;
$ru = rand(1,36); 
if($session['histuser']!=="")$ru=trim(str_replace("|","", $session['histuser']));   
echo $fsize1;
echo "<b>Ваши карты:</b><br/>\n";
echo $fsize2;
echo "<img src=\"cards/$ru.gif\" alt=\"cards\"/><br/>\n"; 
$uscore=cards_score($ru); $pstr = "очков"; 
if($uscore==2||$uscore==3||$uscore==4)$pstr = "очка"; 
echo $fsize1;
echo "$uscore $pstr<br/>\n"; 
$maxlen=strlen($max); 
echo "Ваша ставка(1-$max):<br/>\n"; 
echo $fsize2;
if ($ver=="wml"){
echo "<input name=\"mn$ref\" maxlength=\"$maxlen\" title=\"pass\" format=\"*N\"/><br/>\n"; 
echo $fsize1;
echo "<anchor title=\"go\">Играть<go href=\"21.php?$ses\" method=\"post\">\n"; 
echo "<postfield name=\"mn\" value=\"$(mn$ref)\"/>\n"; 
echo "</go></anchor>\n";
echo $fsize2;
echo "<br/>\n";  
}else{
echo "<form method=\"POST\" action=\"21.php?$ses\" name=\"auth\">\n";
echo "<input name=\"mn\" maxlength=\"$maxlen\" title=\"pass\"/><br/>\n"; 
echo "<input type=\"submit\" value=\"Играть\" name=\"enter\"><br/>\n";
}
echo $fsize1;
echo $divide;
echo "<a href=\"faq.php?$ses\">Правила игры</a><br/>\n"; 
echo "<a href=\"out.php?$ses\">Выход</a><br/>\n";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
$session['histuser']="|$ru"; 
$session['round']=1; 
} else { 
if($session['con']==0){ 
if($mn>$session['money']||$mn===""||!isset($mn)||$mn<1){
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n"; 
echo "<card id=\"error\" title=\"Ошибка\" ontimer=\"21.php?$ses\"><timer value=\"15\"/>\n"; 
echo "<p align=\"center\">\n"; 
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>Ошибка</title>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=21.php?$ses\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
if($mn===""||$mn<1)echo "Вы не указали ставку!\n"; 
else echo "У Вас недостаточно постов для подобной ставки!\n"; 
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
$session['round']=0; 
mysql_close($link);
exit; 
}     
$session['con']=round($mn); 
$session['money']=$session['money']-$session['con']; 
} 
$rate=$session['con']*2; 
$stavka=$session['con']; 
$histuser=split("\|",$session['histuser']); 
if(!isset($end)){ 
do{$randgen=1; $ru = rand(1,36); for($i=1;$i < count($histuser);$i++){if($ru==$histuser[$i]){ $randgen=0; break; } } }
while($randgen==0);  
$session['histuser']=$session['histuser']."|$ru"; 
$countus = count($histuser); 
$histuser[$countus] = $ru; 
}   
for($i=1;$i < count($histuser);$i++){ 
$ustemp=cards_score($histuser[$i]); @$uscore=@$uscore+$ustemp; 
}   
$udoublet=0; 
if((count($histuser)==3)&&($uscore==22)){ 
$tone=0; 
$ttwo=0; 
if($histuser[1]==33||$histuser[1]==34||$histuser[1]==35||$histuser[1]==36)$tone=1; if($histuser[2]==33||$histuser[2]==34||$histuser[2]==35||$histuser[2]==36)$ttwo=1; 
if($tone==1&&$ttwo==1)$udoublet=1; 
} 
if($uscore>21&&$udoublet==0)$close=1; 
if($udoublet==1||$uscore==21||$uscore==20)$end=1;  
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n"; 
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n"; 
echo "<card id=\"index\" title=\"21\">\n"; 
print "<p align=\"center\">";
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>21</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
$max = $session['money']; 
echo "У Вас $max постов<br/>\n"; 
if(isset($close))
echo "<b>Перебор!</b><br/>\n"; 
$win=0; if(isset($end)){ 
$histbot[0]=""; 
$hist=$histuser; 
for($i=1;$i < 10;$i++){ 
do{$randgen=1; $rb = rand(1,36); for($i=1;$i < count($hist);$i++){ if($rb==$hist[$i]){ $randgen=0; break; } } } 
while($randgen==0);  
$histbot[]=$rb; 
$hist[]=$rb; 
$btemp=cards_score($rb); 
@$bcore=@$bcore+$btemp; 
if($bcore==20)break; 
if($bcore==21)break; 
if($bcore==$uscore)break; 
if($bcore>$uscore)break; 
if($bcore>21)break; }  
echo "<b>Банкир открылся:</b><br/>\n"; 
for($i=1;$i < count($histbot);$i++){ 
echo "<img src=\"cards/$histbot[$i].gif\" alt=\"cards\"/>"; 
} 
$pstr = "очков"; 
if($bcore==2||$uscore==3||$bcore==4||$bcore==22||$bcore==23||$bcore==24)$pstr = "очка"; 
if($bcore==21)$pstr = "<b>очко!!!</b>"; 
echo "<br/>$bcore $pstr<br/>\n"; 
$bdoublet=0; 
if((count($histbot)==3)&&($bcore==22)){ 
$tone=0; 
$ttwo=0; 
if($histbot[1]==33||$histbot[1]==34||$histbot[1]==35||$histbot[1]==36)$tone=1; if($histbot[2]==33||$histbot[2]==34||$histbot[2]==35||$histbot[2]==36)$ttwo=1; 
if($tone==1&&$ttwo==1)$bdoublet=1; 
} 
if($bcore>21&&$bdoublet==0)$win=1; 
if($bcore<$uscore)$win=1; 
if($udoublet==1)$win=1; 
if($bdoublet==1)$win=0; 
if($uscore==21)$win=1; 
if($bcore==21)$win=0; 
$close=1; 
} 
echo "<b>Ваши карты:</b><br/>\n"; 
for($i=1;$i < count($histuser);$i++){ 
echo "<img src=\"cards/$histuser[$i].gif\" alt=\"cards\"/>"; 
} 
$pstr = "очков"; 
if($uscore==2||$uscore==3||$uscore==4||$uscore==22||$uscore==23||$uscore==24)$pstr = "очка"; 
if($uscore==21)$pstr = "<b>очко!!!</b>"; 
echo "<br/>$uscore $pstr<br/>\n"; 
if(isset($close)){ 
$session['round']=0;
$session['histuser']=""; 
if($win==0){ 
echo "<b>Вы проиграли</b><br/>\n";
$f=fopen("lost.dat","a+"); 
flock($f,LOCK_EX); 
$data=file("lost.dat"); 
@$data[0]=trim($data[0])+1; 
$stavka=$session['con'];
@$data[1]=trim($data[1])+$session['con']; 
ftruncate($f,0); 
fwrite($f,"$data[0]\n"); 
fwrite($f,"$data[1]\n"); 
flock($f,LOCK_UN); 
fclose($f); 
ctype_digit($stavka);
mysql_query ("Update users set posts=posts-'".$stavka."', gposts=gposts-'".$stavka."' where id ='".$id."';");
$guposts=$row["gposts"];
$session['con']=0; 
echo "Ваш проигрыш <b>".$stavka."</b> постов<br/>\n";
echo "Игровой баланс <b>".$guposts."</b> постов<br/>\n";
} else { 
echo "<b>Вы выиграли!</b><br/>\n"; 
$session['money']=$session['money']+$rate; 
$f=fopen("win.dat","a+"); 
flock($f,LOCK_EX); 
$data=file("win.dat"); 
@$data[0]=trim($data[0])+1; 
@$data[1]=trim($data[1])+$session['con']; 
ftruncate($f,0); 
fwrite($f,"$data[0]\n"); 
fwrite($f,"$data[1]\n"); 
flock($f,LOCK_UN); 
fclose($f);
ctype_digit($rate);
mysql_query ("Update users set posts=posts+'".$rate."', gposts=gposts+'".$rate."' where id ='".$id."';");
$guposts=$row["gposts"];
$session['con']=0; 
echo "Ваш выигрыш <b>".$rate."</b> постов<br/>\n";
echo "Игровой баланс <b>".$guposts."</b> постов<br/>\n";
} 
echo "<a href=\"21.php?$ses\">Сыграть ещё</a><br/>\n"; 
} else { 
echo "На кону ".$rate." постов<br/>\n"; 
echo "<a href=\"21.php?$ses\">Ещё</a>\n"; 
echo "&lt;или&gt;\n"; 
echo "<a href=\"21.php?$ses&amp;end\">Открыть</a><br/>\n"; 
} 
echo $divide;
echo "<a href=\"out.php?$ses\">Выход</a><br/>\n"; 
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);
} 
}
?>   