<?php
//ob_start("ob_gzhandler");
require_once("include/bittorrent.php");
dbconn(false);
loggedinorreturn();




$r = @mysql_query("SELECT konto FROM users WHERE id=$CURUSER[id]") or sqlerr();
$user = mysql_fetch_array($r) or bark("No user with ID $id.");
//$CURUSER["uploaded"] = $user["uploaded"];
#$CURUSER["downloaded"] = $user["downloaded"];




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>";
}

//////////////////////////////////config
//////who is able to play just
//$player = UC_PEASANT;
$player = UC_USER;
//$player = UC_POWER_USER;
//$player = UC_VIP;
//$player = UC_UPLOADER;
//$player = UC_MODERATOR;
//$player = UC_ADMINISTRATOR;
//$player = UC_SYSOP;
$mb_basic=1024*1024;
$max_download_user = $mb_basic*1024*25; //// 25 GB
$max_download_global = $mb_basic*$mb_basic*2.5; //// 2.5 TB :-)
$required_ratio = 0.4; ///i think you know this
$max_trys = 50; ///50 games and no more


$user_everytimewin_mb = $mb_basic * 70;
$cheat_value = 1;
$cheat_breakpoint = 2;
$cheat_value_max = 5;
$cheat_ratio_user = 0.4;
$cheat_ratio_global = 0.4;
$win_amount = 3;
$win_amount_on_number = 6;
$show_real_chance = false;
$bet_value1 = $mb_basic * 20;
$bet_value2 = $mb_basic * 50;
$bet_value3 = $mb_basic * 100;
$bet_value4 = $mb_basic * 250;
$bet_value5 = $mb_basic * 500;
$bet_value6 = $mb_basic * 1024;
$bet_value7 = $mb_basic * 2048;

////////////config spiel 3

$minclass = $player;
$maxusrbet = '6';
$maxtotbet = 30;
$alwdebt = 'n';
$writelog='y';
$delold='n';
$sendfrom='0';
$casino=$HTTP_SERVER_VARS['PHP_SELF'];
//Config ende



 if($user[konto] == 0)
  stderr("Sorry ".$CURUSER["username"],"aber womit willst du spielen wenn du kein Guthaben auf dem Konto hast?");


if (get_user_class() < $player)
 stderr("Sorry ".$CURUSER["username"], "aber der Chef erlaubt der Userklasse (".$player.") nicht das spielen im Casino!");

$query ="select * from casino where userid = '".$CURUSER["id"]."'";
$result = mysql_query($query) or die (mysql_error());
if(mysql_affected_rows()!=1)
{
 mysql_query("INSERT INTO casino (userid, win, lost, trys, date) VALUES(" . $CURUSER["id"] . ",0,0,0, '" . get_date_time() . "')") or mysql_error();

 $result = mysql_query($query);
}

$row = mysql_fetch_array($result);
 $user_win = $row["win"];
 $user_lost = $row["lost"];
 $user_trys = $row["trys"];
 $user_date = $row["date"];
 $user_deposit = $row["deposit"];
 $user_enableplay = $row["enableplay"];

if($user_enableplay=="no")
 stderr("Sorry ".$CURUSER["username"],"Du wurdest vom Casino gebannt");

if($user_trys > $max_trys)
 stderr("Sorry ".$CURUSER["username"],"Du hast alle Spiele verbraucht. In 5 Stunden gehts wieder weiter!");


 //get users that bet the first time or win very less :-)
 if($user_win < $user_everytimewin_mb)
  $cheat_value = 0;
 else
 {
  //i think this is a god idea GLOBAL
  if($global_down > ($max_download_global / $cheat_breakpoint))
   $cheat_value = $cheat_value_max;
  if($casino_ratio_global < $cheat_ratio_global)
   $cheat_value = rand($cheat_value,$cheat_value_max);

  //i think this is a god idea for EACH USER
  if(($user_win - $user_lost) > ($max_download_user / $cheat_breakpoint))
   $cheat_value = $cheat_value_max;
  if($casino_ratio_user < $cheat_ratio_user)
   $cheat_value = rand($cheat_value,$cheat_value_max);
 }

if($global_down > $max_download_global)
 stderr("Sorry ".$CURUSER["username"],"aber der maximale Gewinn ist über ".mksize($max_download_global));


////////////////////////////////////////////////////////////////////////////////////////////////

if((($_POST["color"]=="red"||$_POST["color"]=="black")||($_POST["number"]=="1"||$_POST["number"]=="2"||$_POST["number"]=="3"||$_POST["number"]=="4"||$_POST["number"]=="5"||$_POST["number"]=="6"))&&($_POST["betmb"]==$bet_value1||$_POST["betmb"]==$bet_value2||$_POST["betmb"]==$bet_value3||$_POST["betmb"]==$bet_value4||$_POST["betmb"]==$bet_value5||$_POST["betmb"]==$bet_value6||$_POST["betmb"]==$bet_value7))
{
if($_POST["number"])
{
 $win_amount = $win_amount_on_number;
 $cheat_value = $cheat_value+5;
 $winner_was = $_POST["number"];
}
else
 $winner_was = $_POST["color"];

$win = $win_amount*$betmb;

if($user[konto] < $betmb)
 stderr("Sorry ".$CURUSER["username"],"aber du hast kein Guthaben von ".mksize($betmb));


stdhead();


if(rand(0,$cheat_value)==$cheat_value)
{
 stdmsg("Jepp ".$CURUSER["username"], "Du hast auf's Richtige Pferd gesetzt, Du gewinnst ".mksize($win));
 ?>
<style type="text/css">
<!--
.Stil1 {
    font-size: medium;
    font-weight: bold;
}
-->
</style>

<a href="casino.php" class="Stil1">Zurück zum Casino</a>
<?
 $nachricht = "Gewinn Farbspiel";

 mysql_query("UPDATE users SET konto = konto + ".$win." WHERE id=".$CURUSER["id"]) or sqlerr();
 mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$win."','" . $CURUSER[id] . "')") ;
 mysql_query("UPDATE casino SET date = '".get_date_time()."', trys = trys + 1, win = win + ".$win." WHERE userid=".$CURUSER["id"]) or sqlerr();
}
else
{
 if($_POST["number"])
 {
  do
  {
   $fake_winner = rand(1,6);
  }
  while($_POST["number"]==$fake_winner);
 }
 else
 {
  if($_POST["color"]=="black")
   $fake_winner= "Rot";
  else
   $fake_winner= "Schwarz";
 }


 stdmsg("Sorry ".$CURUSER["username"], "Aber ".$fake_winner." gewinnt, du hast leider auf's falsche Pferd gesetzt. Du verlierst ".mksize($betmb));
 ?>
<a href="casino.php"><span class="Stil1">Zurück zum Casino</span></a><span class="Stil1"></span>
<?
$nachricht = "Verloren Farbspiel";
 mysql_query("UPDATE users SET konto = konto - ".$betmb." WHERE id=".$CURUSER["id"]) or sqlerr();
 mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$betmb."','" . $CURUSER[id] . "')") ;
 mysql_query("UPDATE casino SET date = '".get_date_time()."', trys = trys + 1 ,lost = lost + ".$betmb." WHERE userid=".$CURUSER["id"]) or sqlerr();
}

}
else
{




//////////////////////////////////////////////////////////////////////////////////////// game 3

///////////////////notice

//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'){
$amnt = $amnt < 0 ? 0 : $amnt;

 $nobits = $amnt*$mb_basic;}
else
 $nobits = $amnt*$mb_basic*1024;
}

if ($nobits > "858993459200")
stderr("Sorry ".$CURUSER["username"],"Du hast eine Wette gesetzt die höher ist als 800 GB");



//$ratio2 = number_format(($CURUSER['uploaded'])/$CURUSER['downloaded'],2);

if($CURUSER[uploaded] ==0|| $CURUSER[downloaded]==0)
$ratio='0';
else
$ratio = number_format(($CURUSER['uploaded']-$nobits)/$CURUSER['downloaded'],2);

$time = strtotime("now");
$time = date('Y-n-j G:i:s',$time);
$goback = "<a href=$casino>Zurück zum 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("Sorry","Was hast du denn jetzt vor?&nbsp;&nbsp;$goback");
elseif($tbet['challenged']!="empty")
 stderr("Sorry","aber die Wette wurde schon angenommen!&nbsp;&nbsp;&nbsp;$goback");

if($user[konto] < $tbet['amount'])
{
 $debt = $tbet['amount']-$user['konto'];
 $newup = $CURUSER['uploaded']-$debt;
}

if(isset($debt) && $alwdebt !='y')
 stderr("Sorry","<h2>Du hat ".mksize(($nobits-$user[konto]))." zu wenig um auf die Wette einzugehen!</h2>&nbsp;&nbsp;&nbsp;$goback");

if($random==1)
{
$nachricht = "Wette gewonnen";
 mysql_query("UPDATE users SET konto = konto+".$tbet['amount']." WHERE id = '".$CURUSER['id']."'") or sqlerr();
 mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$tbet[amount]."','".$CURUSER[id]."')") ;
 mysql_query("UPDATE casino SET deposit = deposit-".$tbet['amount']." WHERE userid = '".$tbet['userid']."'") or sqlerr();
 if(mysql_affected_rows()==0)
  mysql_query("INSERT INTO casino (userid, date, deposit) VALUES (".$tbet['userid'].", '$time', '-".$tbet['amount']."')") or sqlerr();

 mysql_query("UPDATE casino_bets SET challenged = '".$CURUSER['username']."'  WHERE id = $betid") or sqlerr();
 $msg = "Du hast die Wette verloren! ".$CURUSER[username]." hat die $nogb Wette gewonnen!";
sendPersonalMessage(0, $tbet['userid'], Casino, $msg, PM_FOLDERID_CASINO, 0);

 if($delold=='y')
  mysql_query("DELETE * FROM casino_bets WHERE id = $tbet[id]") or sqlerr();

 stderr("Gewonnen","<h2>Du hast gewonnen, $nogb wurden dir Gutgeschrieben bei <a href=userdetails.php?id=$tbet[userid]>$tbet[proposed]'s </a>Wette!</h2>&nbsp;&nbsp;&nbsp;$goback");
}
else
{
 if(empty($newup))
  $newup = $user['konto'] - $tbet['amount'];
 $newup2 = $tbet['amount']*2;
 $newup3 = $tbet['amount'];
$nachricht = "Wette verloren";
 mysql_query("UPDATE users SET konto = $newup WHERE id = '".$CURUSER['id']."'") or sqlerr();
 mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht."','".$newup3."','".$CURUSER[id]."')") ;
$nachricht2 = "Wette gewonnen";
 mysql_query("UPDATE users SET konto = konto + $newup2 WHERE id = '".$tbet['userid']."'") or sqlerr();
 mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('" . get_date_time() . "','".$nachricht2."','".$newup2."','".$tbet[userid]."')") ;
 mysql_query("UPDATE casino SET deposit = deposit-".$tbet['amount']." WHERE userid = '".$tbet['userid']."'");

 if(mysql_affected_rows()==0)
  mysql_query("INSERT INTO casino (userid, date, deposit) VALUES (".$tbet['userid'].", '$time', '-".$tbet['amount']."')") or sqlerr();
 mysql_query("UPDATE casino_bets SET challenged = '".$CURUSER['username']."' WHERE id = $betid") or sqlerr();

$msg = "Du hast $nogb von ".$CURUSER[username]." gewonnen!";
sendPersonalMessage(0, $tbet['userid'], Casino, $msg, PM_FOLDERID_CASINO, 0);

 if($delold=='y')
  mysql_query("DELETE * FROM casino_bets WHERE id = $tbet[id]") or sqlerr();

 stderr("Pech","<h2>Du hast die Wette verloren, <a href=userdetails.php?id=$tbet[userid]>$tbet[proposed]</a> hat $nogb von dir gewonnen!</h2> &nbsp;&nbsp;&nbsp;$goback");
}

exit();
}

//
$loca = mysql_query("SELECT * FROM casino_bets WHERE challenged ='empty'") or sqlerr();
$totbets = mysql_num_rows($loca);

if (isset($_POST["unit"]))
{
if($openbet >= $maxusrbet)
 stderr ("Sorry","Du hast bereit $openbet Wetten offen, nimm eine offene Wette an!&nbsp;&nbsp;&nbsp;<a href=$casino>Zurück zum Casino</a>");
if($nobits==0)
 stderr ("Sorry","aber wenn Du eine Wette mit 0, zero, nada, niente oder nichts gewinnst bist du auch nicht glücklich. Also bitte mach einen Wetteinsatz!");
 if($nobits<0)
 stderr ("Sorry","aber wenn Du bei einer Wette NullKommaNichts setzt, dann bringt dich das auch nicht weiter!");
 
$newup = $user['konto'] - $nobits;
$debt  = $nobits-$user['konto'];
if($user['konto'] < $nobits)
{
 if($alwdebt!='y')
  stderr("Sorry","<h2>".mksize($debt)." ist mehr als du hast!</h2>$goback");
  stderr("Hallooooo?","<h2>So geht's aber nicht, wenn du noch ".mksize($debt)." auf deinem Konto hättest dann könntest du jetzt auch spielen!</h2>$goback");
}
$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 = "<H1>Wette gespeichert, Du bekommst eine PM wenn die Wette von jemanden angenommen wird.</H1>";
$nachricht = "Wetteeinsatz";
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 konto = $newup  WHERE id = ".$CURUSER['id']) or sqlerr();
mysql_query("INSERT INTO konto (date, text, amount, userid) VALUES('".get_date_time()."','".$nachricht."','".$nobits."','".$CURUSER[id]."')") ;

mysql_query("UPDATE casino SET deposit = deposit + $nobits WHERE userid = ".$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'");
$totbets = mysql_num_rows($loca);

////////////////////////////////////////////////standard html begin
stdhead();





///////////////////////////////Willkommen :-)

begin_frame("<class='normalfont'><img src='/pic/chips.png' width='22' height='22' style='vertical-align: middle'><b> Herzlich Willkommen im WDB-Casino $CURUSER[username]</b> !","true","100%");
/////////////////bilD anfang

?>

<p align="center"><img src="<?=$GLOBALS["PIC_BASE_URL"]?>/casino.png" alt="casino" width="500" height="75" /></p>

<?
/////////////////BILD ende

print("<table align=center 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>Das Limit von Max. $maxtotbet offenen Wetten wurde erreicht, bitte nimm erst eine offene Wette an!<br>";
else
{
begin_frame("<class='normalfont'><img src='/pic/moneybag.gif' width='22' height='22' style='vertical-align: middle'><b>Setze hier deine Wette!<b>");
?>
<center>
<?
 echo "<br><table width=45% cellspacing=0 cellpadding=3>";

 tr2('align=right','<b>Wetteinsatz</b>',
  'align=center','<form name=p2p method=post action='.$phpself.'><input type=text name=amnt size=20 value=1>
  <select name=unit>
  <option value=1>MB</option>
  <option value=2>GB</option>
     </select>');
 tr2('align=center colspan=2','<input type=submit value=Wech!>');
 echo"</table></form>";
}
}

else
echo "<br>Du hast bereits $maxusrbet offene Wetten, warte bis diese beendet sind.<br>";
end_frame();
?>
</center>

<?


//offene wett tabelle

 begin_frame("Offene Wetten!");?>
 <center>
     <?
     begin_table();

        echo "<tr><td class=tabletitle align=left>Name</td><td class=tabletitle align=left>Einsatz</td><td class=tabletitle>Datum</td><td class=tabletitle>Wette annehmen</td></td></tr>";
        while ($res = mysql_fetch_assoc($loca))
{
                echo "<tr><td class=tablea><b>" . $res[proposed]. "</b></td>\n";
                print "<td class=tablea>" .mksize($res['amount']) . "</td>\n";
                echo "<td class=tablea style=\"text-align:center\">" . $res[time] . "</td>\n";
                echo "<td class=tablea><a href=".$casino."?takebet=$res[id]>Hier Klicken!</a></td>\n";
 $abcdefgh=1;
}
if($abcdefgh==false)
 echo("<tr><td class=tablea colspan=4>Zur Zeit spielt kein User!</td></tr>");
echo "</table>";
echo('</table><br>');
?>
</center>
<?
 //end_table();
?>


<?
//////////////////////////////////////////////////////////////////////////////////////// spiel 3

begin_frame("<class='normalfont'><img src='/pic/wuerfel.gif' width='22' height='22' style='vertical-align: middle'><b> Spieltische</b>","true","100%");

print("<form name=casino method=post action=$phpself>");
 print("<table align=center cellpadding=5 cellspacing=2 class=message width=400 class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Tisch 1</b></center></span></td>\n");
  tr("Schwarz",'<input name="color" type="radio" checked value="black">',1);
  tr("Rot",'<input name="color" type="radio" value="red">',1);
  tr("Wieviel",'
  <select name="betmb">
  <option value="'.$bet_value1.'">'.mksize($bet_value1).'</option>
  <option value="'.$bet_value2.'">'.mksize($bet_value2).'</option>
  <option value="'.$bet_value3.'">'.mksize($bet_value3).'</option>
  <option value="'.$bet_value4.'">'.mksize($bet_value4).'</option>
  <option value="'.$bet_value5.'">'.mksize($bet_value5).'</option>
  <option value="'.$bet_value6.'">'.mksize($bet_value6).'</option>
  <option value="'.$bet_value7.'">'.mksize($bet_value7).'</option>
  </select>',1);
 if($show_real_chance)
  $real_chance=$cheat_value+1;
 else
  $real_chance=2;
 tr("Deine chance","1 : ".$real_chance,1);
 tr("Du kannst Gewinnen",$win_amount." * soviel",1);
 tr("Farbenwette:","<input type=submit value='Wetten!' >",1);
 echo('</table><br>');
 print("</form>");

 ?>

 <p>
   <?

 print("<form name=casino2 method=post action=$phpself>");
 print("<table align=center cellpadding=5 cellspacing=2 class=message width=400 class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Tisch 2</b></center></span></td>\n");
  tr("Zahl",'<input name="number" type="radio" checked value="1">1&nbsp;&nbsp;<input name="number" type="radio" value="2">2&nbsp;&nbsp;<input name="number" type="radio" value="3">3',1);
  tr("Zahl",'<input name="number" type="radio" value="4">4&nbsp;&nbsp;<input name="number" type="radio" value="5">5&nbsp;&nbsp;<input name="number" type="radio" value="6">6',1);
  tr("Wieviel",'
  <select name="betmb">
  <option value="'.$bet_value1.'">'.mksize($bet_value1).'</option>
  <option value="'.$bet_value2.'">'.mksize($bet_value2).'</option>
  <option value="'.$bet_value3.'">'.mksize($bet_value3).'</option>
  <option value="'.$bet_value4.'">'.mksize($bet_value4).'</option>
  <option value="'.$bet_value5.'">'.mksize($bet_value5).'</option>
  <option value="'.$bet_value6.'">'.mksize($bet_value6).'</option>
  <option value="'.$bet_value7.'">'.mksize($bet_value7).'</option>
  </select>',1);

 if($show_real_chance)
  $real_chance=$cheat_value+5;
 else
  $real_chance=6;
 tr("Deine Chancen","1 : ".$real_chance,1);
 tr("Du kannst gewinnen",$win_amount_on_number." * soviel",1);
 tr("Wette auf eine Zahl:","<input type=submit value='Wetten!' >",1);
 echo('</table><br>');
 print("</form>");

end_frame();



 ///////////////////STATS

 print("<table cellpadding=4 cellspacing=1 border=0 width=100% class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><img src='pic/stats.jpg' width=22 height=22 alt='' style='vertical-align: middle;'><b> Casino-Stats</b></center></span></td></tr>\n");

 print("<td align=center width=250 class=tablea valign=top>\n");
 print("<table align=center cellpadding=5 cellspacing=2 class=message width=250 class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Deine Casino stats:</b></center></span></td>\n");
 print("<table align=center class=tablecat class=tableb class=message  cellspacing=2 width=250 cellpadding=5>\n");
  tr("Du kannst Gewinnen",mksize($max_download_user),1);
  tr("Gewonnen",mksize($user_win),1);
  tr("Verloren",mksize($user_lost),1);
  tr("Ratio",$casino_ratio_user,1);
  tr('Deposit on P2P', mksize($user_deposit+$nobits));
 echo('</table>');

 print("<td align=center width=250 class=tablea valign=top>\n");
 print("<table align=center cellpadding=5 cellspacing=2 class=message width=250 class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Allgemeine Casino stats:</b></center></span></td>\n");
 print("<table align=center class=tablecat class=tableb class=message  cellspacing=2 width=250 cellpadding=5>\n");
  tr("Max",mksize($max_download_global),1);
  tr("Gewonnen",mksize($global_win),1);
  tr("Verloren",mksize($global_lost),1);
  tr("Ratio",$casino_ratio_global,1);
  tr("Deposit",mksize($global_deposit));
 echo('</table>');

 print("<td align=center width=250 class=tablea valign=top>\n");
 print("<table align=center cellpadding=5 cellspacing=2 class=message width=250 class=tableinborder>
 <tr class=tabletitle width=100%>
 <td colspan=10 width=100%><span class=normalfont><center><b>Deine Torrent stats:</b></center></span></td>\n");
 print("<table align=center class=tablecat class=tableb class=message  cellspacing=2 width=250 cellpadding=5>\n");
 tr('Uploaded',mksize($CURUSER['uploaded'] - $nobits));
 tr('Downloaded',mksize($CURUSER['downloaded']));
 tr('Ratio',$ratio);
 tr("&nbsp;","");
 tr("&nbsp;","");
echo('</table>');
print("</td></tr><br>");
print("</table><br>");
 ///////////////////STATS

}

end_frame();
end_frame();
end_frame();
stdfoot();
?>