<?
require_once("include/bittorrent.php");
dbconn(false);
loggedinorreturn();
function get_user_name($userid){
$r = mysql_query("select username from users where id=$userid");
$a = mysql_fetch_array($r);
return "$a[username]";
}

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 playerexists($userid){
$r = mysql_query("select count(*) from casino where userid=$userid");
$a = mysql_fetch_array($r);
if ($a[0] > 0) return true;
else return false;
}*/
$mb = 1024*1024*100;//bet size
if ($_POST["game"]){
$cardcountres = mysql_query("select count(id) from cards") or sqlerr(__FILE__, __LINE__);
$cardcountarr = mysql_fetch_array($cardcountres);
$cardcount = $cardcountarr[0];
if ($_POST["game"] == 'start'){

if($CURUSER["konto"] < $mb)
stderr("Sorry ".$CURUSER["username"],"<center><h2>Du hast noch keine ".mksize($mb)." auf deinem Spielekonto.</h2></center>");
$required_ratio = 0.3;
if ($CURUSER["konto"] > 0)
$ratio = number_format($CURUSER["konto"] / $CURUSER["konto"], 2);
else
if ($CURUSER["konto"] > 0)
$ratio = 999;
else
$ratio = 0;
if($ratio < $required_ratio)
stderr("Sorry ".$CURUSER["username"],"<center><h2>Deine Ratio ist unter ".$required_ratio."</h2></center>");
$res = mysql_query("select count(*) from blackjack where userid=$CURUSER[id] and status='waiting'");
$arr = mysql_fetch_array($res);
if ($arr[0] > 0) {
stderr("Sorry","<center><h2>Dein letztes Spiel ist noch offen, du musst erst warten bis Jemand gegen dich gespielt hat.</h2></center>");
}else{
$res = mysql_query("select count(*) from blackjack where userid=$CURUSER[id] and status='playing'");
$arr = mysql_fetch_array($res);
if ($arr[0] > 0)
stderr("Sorry","<center><h2>Du musst zuerst dein altes Spiel beenden. <form method=post name=form action=$phpself><input type=hidden name=game value=cont><input type=submit value='Altes Spiel fortsetzen'></form></h2></center>");}
$cardid = rand(1,$cardcount);
$cardres = mysql_query("select * from cards where id=$cardid") or sqlerr(__FILE__, __LINE__);
$cardarr = mysql_fetch_array($cardres);
mysql_query("insert into blackjack (userid, points, cards, started) values($CURUSER[id], $cardarr[points], $cardid, 1)") or sqlerr(__FILE__, __LINE__);

stdhead();
begin_frame("<class='normalfont'><img src='/pic/AsPik.gif' width='22' height='22' style='vertical-align: middle'><b> BlackJack</b>","true","100%");
print("<table align=center cellpadding=3 cellspacing=0 border=0 width=600 class=tableb>
<tr class=tabletitle width=100%>
<td colspan=10 width=100%><span class=normalfont><center><b>Das Spiel hat begonnen</b></center></span></td></tr>\n");
print("<tr><td colspan=2 cellspacing=0 cellpadding=5 >");
print("<form name=blackjack method=post action=$phpself>");
print("<table class=message width=100% cellspacing=0 cellpadding=5>\n");
print("<tr><td border=1 align=center><img src=pic/cards/".$cardarr["pic"]." border=0></td></tr>");
print("<tr><td align=center><b>Punkte = $cardarr[points]</b></td></tr>");
print("<tr><td align=center><input type=hidden name=game value=cont><input type=submit value='Noch eine Karte!'></td></tr>");
print("</table><br>");
print("</form>");
print("</td></tr></table><br>");

end_frame();
stdfoot();
}
elseif ($_POST["game"] == 'cont'){

$playeres = mysql_query("select * from blackjack where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
$playerarr = mysql_fetch_array($playeres);
$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 blackjack set points=points+$cardarr[points], cards='$mysqlcards' where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
if ($points == 21){
$waitres = mysql_query("select count(*) from blackjack where status='waiting'");
$waitarr = mysql_fetch_array($waitres);
if ($waitarr[0] > 0){
$r = mysql_query("select * from blackjack where status='waiting' order by date asc LIMIT 1");
$a = mysql_fetch_assoc($r);
if ($a["points"] != 21){
$winorlose = "du hast 100MB gewonnen";
mysql_query("update users set konto = konto + $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto - $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." an $CURUSER[username] verloren (Du hattest $a[points] Punkte, $CURUSER[username] hatte 21 Punkte). [url=blackjack.php]Nochmal[/url].");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}else{
$winorlose = "keiner hat gewonnen";
}
stderr("Game Over", "<left><h2>Du hast 21 Punkte, dein Gegner ".get_user_name($a["userid"])." hatte $a[points] Punkte, $winorlose.</left></h2><br><center><a href=blackjack.php>Nochmal?</a></center>");
}
else{
mysql_query("update blackjack set status = 'waiting', date='".get_date_time()."' where userid = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);
stderr("Game Over", "<center><h2>Du hast 21 Punkte, zur Zeit gibt es keinen Gegner, du musst also warten bis Jemand gegen dich spielt. Du wirst per PN über das Ergebnis informiert.</h2></center>");
}
}
elseif ($points > 21) {
$waitres = mysql_query("select count(*) from blackjack where status='waiting'");
$waitarr = mysql_fetch_array($waitres);
if ($waitarr[0] > 0){
$r = mysql_query("select * from blackjack where status='waiting' order by date asc LIMIT 1");
$a = mysql_fetch_assoc($r);
if ($a["points"] == $points){
$winorlose = "keiner hat gewonnen";
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Dein Gegner war $CURUSER[username], ihr hattet die gleiche Punktzahl, keiner hat gewonnen . [url=blackjack.php]Nochmal[/url].");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);}
elseif ($a["points"] > $points){
$winorlose = "du hast 100MB gewonnen";
mysql_query("update users set konto = konto + $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto - $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." an $CURUSER[username] verloren (Du hattest $a[points] Punkte, $CURUSER[username] hatte $points Punkte). [url=blackjack.php]Nochmal[/url].");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
elseif ($a["points"] < $points){
$winorlose = "du hast 100MB verloren";
mysql_query("update users set konto = konto - $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto + $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." von $CURUSER[username] gewonnen (Du hattest $a[points] Punkte, $CURUSER[username] hatte $points Punkte).[url=blackjack.php]Nochmal[/url].");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
stderr("Game Over", "<left><h2>Du hast $points Punkte, dein Gegner ".get_user_name($a["userid"])." hatte $a[points] Punkte, $winorlose.</left></h2><br><center><a href=blackjack.php>Nochmal?</a></center>");
}
else
{
mysql_query("update blackjack set status = 'waiting', date='".get_date_time()."' where userid = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);
stderr("Game Over", "<center><h2>Du hast $points Punkte, zur Zeit ist kein Gegner vorhanden, du musst also warten bis Jemand gegen dich spielt. Du wirst per PN über das Ergebnis informiert</h2></center>");
}
}

else{
stdhead();
begin_frame("<class='normalfont'><img src='/pic/AsPik.gif' width='22' height='22' style='vertical-align: middle'><b> BlackJack</b>","true","100%");
 print("<table align=center cellpadding=3 cellspacing=0 border=0 width=600 class=tableb>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Deine Karten</b></center></span></td></tr>\n");
print("<tr><td align=center colspan=2 cellspacing=0 cellpadding=5>");
print("<table class=message width=100% cellspacing=0 cellpadding=5 >\n");
print("<tr><td align=center>$showcards</td></tr>");
print("<tr><td align=center><b>Punkte = $points</b></td></tr>");
print("<form name=blackjack method=post action=$phpself>");
print("<tr><td align=center><input type=hidden name=game value=cont><input type=submit value='Noch eine Karte!'></td></tr>");
print("</form>");
print("<form name=blackjack method=post action=$phpself>");
print("<tr><td align=center><input type=hidden name=game value=stop><input type=submit value='Genug!'></td></tr>");
print("</form>");
print("</table><br>");
print("</td></tr></table><br>");

end_frame();
stdfoot();
}
}elseif ($_POST["game"] == 'stop')
{

$playeres = mysql_query("select * from blackjack where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
$playerarr = mysql_fetch_array($playeres);

if ($playerarr["started"] == 0)
stdmsg("Fehler","<center><h2>Spiel noch nicht neu gestartet.</h2></center>");

$waitres = mysql_query("select count(*) from blackjack where status='waiting'");
$waitarr = mysql_fetch_array($waitres);
if ($waitarr[0] > 0){
$r = mysql_query("select * from blackjack where status='waiting' order by date asc LIMIT 1");
$a = mysql_fetch_assoc($r);
if ($a["points"] == $playerarr[points]){
$winorlose = "keiner hat gewonnen";
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Dein Gegner war $CURUSER[username], ihr hattet beide die gleiche Punktzahl, keiner gewinnt . [url=blackjack.php]Nochmal ?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);}
elseif ($a["points"] < $playerarr[points] && $a[points] < 21){
$winorlose = "du hast 100MB gewonnen";
mysql_query("update users set konto = konto + $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto - $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." an $CURUSER[username] verloren (Du hattest $a[points] Punkte, $CURUSER[username] hatte $playerarr[points] Punkte). [url=blackjack.php]Nochmal ?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
elseif ($a["points"] > $playerarr[points] && $a[points] < 21){
$winorlose = "du hast 100MB verloren";
mysql_query("update users set konto = konto - $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto + $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." von $CURUSER[username] gewonnen, (Du hattest $a[points] Punkte, $CURUSER[username] hatte $playerarr[points] Punkte). [url=blackjack.php]Nochmal ?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
elseif ($a["points"] == 21){
$winorlose = "du hast 100MB verloren";
mysql_query("update users set konto = konto - $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto + $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." von $CURUSER[username] gewonnen, (Du hattest $a[points] Punkte, $CURUSER[username] hatte $playerarr[points] Punkte). [url=blackjack.php]Nochmal ?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
elseif ($a["points"] < $playerarr[points] && $a[points] > 21){
$winorlose = "du hast 100MB verloren";
mysql_query("update users set konto = konto - $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto + $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." von $CURUSER[username] gewonnen, (Du hattest $a[points] Punkte, $CURUSER[username] hatte $playerarr[points] Punkte). [url=blackjack.php]Nochmal?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
elseif ($a["points"] > $playerarr[points] && $a[points] > 21){
$winorlose = "du hast 100MB gewonnen";
mysql_query("update users set konto = konto + $mb where id=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
mysql_query("update users set konto = konto - $mb where id=$a[userid]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set win = win + $mb where userid=$CURUSER[id]") or sqlerr(__FILE__, __LINE__);
//mysql_query("update casino set lost = lost + $mb where userid=$a[userid]") or sqlerr(__FILE__, __LINE__);
mysql_query("delete from blackjack where userid=$CURUSER[id]");
mysql_query("delete from blackjack where userid=$a[userid]");
$dt = sqlesc(get_date_time());
$msg = sqlesc("Du hast ".mksize($mb)." an $CURUSER[username] verloren, (Du hattest $a[points] Punkte, $CURUSER[username] hatte $playerarr[points] Punkte). [url=blackjack.php]Nochmal ?[/url]");
sendPersonalMessage(0, $a["userid"], Blackjack, $msg, PM_FOLDERID_BLACKJACK, 0);
}
stderr("Game Over", "<left><h2>Du hattest $playerarr[points] Punkte, dein Gegner ".get_user_name($a["userid"])." hatte $a[points] Punkte, $winorlose.</left></h2><br><center><a href=blackjack.php>Nochmal?</a></center> ");
}
else
{
mysql_query("update blackjack set status = 'waiting', date='".get_date_time()."' where userid = $CURUSER[id]") or sqlerr(__FILE__, __LINE__);
stderr("Game Over", "<center><h2>Du hast $playerarr[points] Punkte, zur Zeit ist kein Gegner vorhanden, also wirst du warten müssen bis Jemand gegen dich spielt. Du wirst per PN über das Ergebnis informiert.</h2></center>");
}
}
}
else
{
//if (playerexists($CURUSER[id]) == false)
//mysql_query("insert into casino (userid, win, lost) values($CURUSER[id], 0, 0)") or sqlerr(__FILE__, __LINE__);
stdhead();
begin_frame("<class='normalfont'><img src='/pic/AsPik.gif' width='22' height='22' style='vertical-align: middle'><b> BlackJack</b>","true","100%");
print("<table align=center cellspacing=0 cellpadding=3 width=420>\n");
print("<tr><td colspan=2 cellspacing=0 cellpadding=5 align=center>");
//print("<h3><a href=players.php>Stats</a></h3>");
print("<table class=tableoutborder width=100% cellspacing=1 cellpadding=4>\n");
tr2('align=center colspan=3 class=tabletitle','<font size=2><b>Regeln</b></font>');
print("<td class=tableb align=center><b><br><br>Du musst 21 Punkte sammeln. Der Einsatz beträgt ".mksize($mb).".");
print("<br><br><br></b></td></tr>");
print("</table><br>");
print("<form name=form method=post action=$phpself><input type=hidden name=game value=start><input type=submit class=btn value='Start!'>");
print("</td></tr></table></form>");
end_frame();
stdfoot();
}
?>