Advertisement
Guest User

assqasda

a guest
Jun 13th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.78 KB | None | 0 0
  1. <?php
  2.     ini_set("session_use_cookies",1);
  3.     ini_set("session_use_only_cookies",1);
  4.  
  5.     ini_set('xdebug.dump.SESSION', '*');
  6.     ini_set('xdebug.dump.POST', '*');
  7.     ini_set('xdebug.dump_undefined','on');
  8.     ini_set('xdebug.var_display_max_data', 'on');
  9.  
  10. session_start();
  11.     header("Content-Type: text/html; charset=utf-8");
  12.  
  13.     if (!isset($_SESSION["nickName"])) {
  14.         header("Location: /pokerDice/auth.php");
  15.         exit();
  16.     }
  17.  
  18.     function logOut() {
  19.         unset($_SESSION);
  20.         session_destroy();
  21.         header("Location: /pokerDice/auth.php");
  22.         exit();
  23.     }
  24.  
  25.     if (isset($_POST["gameOver"])) {
  26.         gameOver();
  27.     }
  28.  
  29.     var_dump($_SESSION);
  30.     var_dump($_POST);
  31.  
  32.     function gameOver() {
  33.         unset($_SESSION["rolls"] );
  34.         unset($_SESSION["bet"]);
  35.         unset($_SESSION["nextTurn"]);
  36.         unset($_SESSION["cash"]);
  37.         unset($_SESSION["state"]);
  38.         $_SESSION["turnNumber"]=1;
  39.         unset($_POST);
  40.         //session_destroy();
  41.         header("Location: /pokerDice/pokerDice.php");
  42.         exit();
  43.     }
  44.  
  45.     if (isset($_POST["post_logOut"])) {
  46.         logOut();
  47.     }
  48.  
  49.     if (isset($_POST["turnOver"])) {
  50.         //print "inside turnOver";
  51.         $_SESSION["state"] = 0;
  52.         unset($_SESSION["rolls"] );
  53.         unset($_SESSION["bet"]);
  54.         unset($_SESSION["nextTurn"]);
  55.         $_SESSION["turnNumber"]++;
  56.         unset($_POST["turnOver"]);
  57.         //$_SESSION["rolls"] = 0;
  58.         //$_SESSION["bet"] = 0;
  59.         //print "session: ";
  60.         //print_r($_SESSION);      
  61.     }
  62.    
  63.     if (!isset($_SESSION["cash"])) {
  64.         //1st time run
  65.         $_SESSION["cash"] = 100;       
  66.         $_SESSION["state"] = 0;
  67.         $_SESSION["turnNumber"]=1;
  68.         //print "NEW GAME";
  69.     }
  70.    
  71.     if (isset($_POST["NewGame"])) {
  72.         gameOver();
  73.     }
  74.    
  75.     if (isset($_SESSION["state"])) {
  76.         $money = "Money: $_SESSION[cash]$";
  77.         $debugMsg = "STATE: ".$_SESSION["state"];
  78.         $controlPanel = "<p>Hello $_SESSION[nickName]. Current turn: $_SESSION[turnNumber].</p><form method='post'>\n<input type='submit' name ='post_logOut' value='Log out'>\n</form>";
  79.         if ($_SESSION["state"] == 0) {
  80.             //its bid time - game begin or after turn
  81.             if ($_SESSION["cash"]<=1)
  82.                 gameOver();
  83.             if (isset($_POST["bet"])) {
  84.                 //user pressed bet button - checking it for correct value
  85.                 if (!preg_match('/^[0-9]+$/',$_POST["bet"],$regexpResult)) {
  86.                     $_POST["bet"] = 0;
  87.                     $error = "Only digits allowed!";
  88.                 } else {
  89.                     if ($_POST["bet"]>$_SESSION["cash"]) {
  90.                         $_POST["bet"] = 0;
  91.                         $error = "Not enough money!";
  92.                     }
  93.                     if ($_POST["bet"]>0) {
  94.                         //bid is correct
  95.                         $_SESSION["state"] = 1;
  96.                         $_SESSION["cash"]-=$_POST["bet"];
  97.                         $_SESSION["bet"] = $_POST["bet"];
  98.                         /*print "session: ";
  99.                         print_r($_SESSION);*/
  100.                     }
  101.                 }                                  
  102.             }
  103.             //checking state again
  104.             if ($_SESSION["state"] == 0) {
  105.                 //printing bet form inputs
  106.                 $form =  <<<HERE
  107.                 <form method="post">
  108.                     <p>Your bet:</p><input type="text" name="bet" class="textBet">                 
  109.                     <input type="submit" name="setBet" value="Set"><br />
  110.                     <input type="submit" name="NewGame" value="New game">
  111.                 </form>
  112. HERE;
  113.             }
  114.         }
  115.         if ($_SESSION["state"]==1) {
  116.             //bet is set - rolling dices
  117.             if (!isset($_SESSION["bet"])) {
  118.                 gameOver();
  119.                 die("bet run away");
  120.             }
  121.             $money = "Money: $_SESSION[cash]$, bet: $_SESSION[bet]$";
  122.             if (!isset($_POST["pressed"])) {
  123.                 //1st roll
  124.                 $form = "<div class='images'>\n";                          
  125.                 for ($i=1;$i<6;$i++) { 
  126.                     if (!isset($_SESSION["rolls"])) {  
  127.                         $val = rand(1,6);
  128.                     } else {
  129.                         $val = $_SESSION["rolls"][$i-1];
  130.                     }                  
  131.                     $vals[$i-1] = $val;
  132.                     $form = $form . "<img src='$val.png' class='diceimg$i'>\n";
  133.                 }              
  134.                 $form = $form . "</div>\n";
  135.                 $form2 = <<<HERE
  136.                 <form method="post">                   
  137.                     <input type="checkbox" name="cb1" class="checkbox" value="$vals[0]">
  138.                     <input type="checkbox" name="cb2" class="checkbox" value="$vals[1]">
  139.                     <input type="checkbox" name="cb3" class="checkbox" value="$vals[2]">
  140.                     <input type="checkbox" name="cb4" class="checkbox" value="$vals[3]">
  141.                     <input type="checkbox" name="cb5" class="checkbox" value="$vals[4]">
  142.                     <br />                 
  143.                     <br />                 
  144.                     <input type="submit" name="pressed" value="Reroll selected dices">                 
  145.                 </form>
  146. HERE;
  147.                 $form =  $form . $form2;
  148.                 $_SESSION["rolls"] = $vals;
  149.                 //print_r($_SESSION["rolls"]);
  150.             } else {
  151.                 if (!isset($_SESSION["nextTurn"])) {
  152.                     //re-rolling and final
  153.                     $form = "<div class='images'>\n";
  154.                     for ($i=1;$i<6;$i++) { 
  155.                         $val = isset($_POST["cb$i"]) ? rand(1,6) : $_SESSION["rolls"][$i-1];   
  156.                         $_SESSION["rolls"][$i-1] = $val;                       
  157.                         $form = $form . "<img src='$val.png' class='diceimg$i'>\n";                    
  158.                     }              
  159.                     $form = $form . "</div>\n";                    
  160.                     $parse = array(0,0,0,0,0,0);
  161.                     foreach ($_SESSION["rolls"] as $value) {
  162.                         $parse[$value-1]++;
  163.                     }
  164.                     $combinations = array(0,0,0,0,0,0);
  165.                     foreach ($parse as $key=>$value) {
  166.                         $combinations[$value]++;                               
  167.                     }
  168.                     $result="";
  169.                     $modifier = 0;
  170.                     if ($combinations[2]==2) {
  171.                         $result = "You got two pairs! You win ";
  172.                         $modifier = 1.2;
  173.                     } elseif ($combinations[3]==1&&$combinations[2]==1) {
  174.                         $result = "You got full house! You win ";
  175.                         $modifier = 2;
  176.                     } elseif ($combinations[3]==1&&$combinations[2]==0) {
  177.                         $result = "You got three of a kind! You win ";
  178.                         $modifier = 1.5;
  179.                     } elseif ($combinations[4]==1) {
  180.                         $result = "You got four of a kind! You win ";
  181.                         $modifier = 3;
  182.                     } elseif ($combinations[5]==1) {
  183.                         $result = "You got FIVE of a kind! You win ";
  184.                         $modifier = 3+$_SESSION["rolls"][2];
  185.                     } elseif ($combinations[1]==5&&($parse[0]==0||$parse[5]==0)) {
  186.                         $result = "You got STREET! You win ";
  187.                         $modifier = 4;
  188.                     } else {
  189.                         $result = "You got nothing. You lost ";
  190.                     }
  191.                     $won = $_SESSION["bet"]*$modifier;
  192.                     if ($won==0) {
  193.                         $form = $form . $result.$_SESSION["bet"]."$";
  194.                     }   else {
  195.                         $form = $form . $result.$won."$";
  196.                     }
  197.                     $_SESSION["cash"] += $won;
  198.                     if ($_SESSION["cash"] < 1) {
  199.                         $error = "You lost... <br /><img src='676406.jpg'>";
  200.                         $form = $form . "<form method='post'>\n<input type='submit' name='gameOver' value='New game'><br />";  
  201.                     } else {
  202.                         $form = $form . "<form method='post'>\n<input type='submit' name='turnOver' value='Next turn'><br />"; 
  203.                     }
  204.                     $_SESSION["nextTurn"] = 1;         
  205.                     /*print_r($_POST);   
  206.                     print "<br />";
  207.                     print_r($_SESSION);
  208.                     print "<br />";
  209.                     print_r($parse);
  210.                     print "<br />";
  211.                     print_r($combinations);*/
  212.                 } else {
  213.                     $_SESSION["state"] = 0;
  214.                     unset($_SESSION["rolls"] );
  215.                     unset($_SESSION["bet"]);
  216.                     unset($_SESSION["nextTurn"]);
  217.                     header("Location: /pokerDice/pokerDice.php");
  218.                     exit();
  219.                 }
  220.             }
  221.         }  
  222.     } else {
  223.         //we're not supposed to be here
  224.         print "You broke it!";
  225.         gameOver();
  226.         die("unknown_error");      
  227.     }
  228. ?>
  229. <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  230. <html>
  231.     <head>
  232.         <link rel="stylesheet" type="text/css" href="pokerMain.css">
  233.         <script src="jquery201.js" type="text/javascript"></script>
  234.         <title>PokerDice</title>
  235.     </head>
  236.     <body>
  237.         <header>
  238.             <div id="header_text"><h1>PokerDICE</h1></div>     
  239.         </header>
  240.         <content>
  241.             <div id="controlPanel">
  242.                 <?php
  243.                     if (isset($debugMsg)) print $debugMsg;
  244.                     if (isset($controlPanel)) print $controlPanel;
  245.                 ?>
  246.             </div>
  247.             <div id="main">
  248.                 <p class="money"><?php  print $money; ?></p>
  249.                 <?php
  250.                     print $form;
  251.                 ?> 
  252.                 <p class="error"><?php  if (isset($error)) print $error; ?></p>
  253.             </div>
  254.         </content>
  255.         <footer>
  256.         </footer>
  257.     </body>
  258. </html>
  259. <script>
  260.     $("#ayolaid").hide();
  261.    
  262.     $("img[class^=diceimg]").click(function () {       
  263.         $id = $(this).attr("class").charAt($(this).attr("class").length-1);
  264.         $state = $("input[name='cb" + $id + "']").prop("checked");
  265.         $("input[name='cb" + $id + "']").prop("checked", !$state);
  266.     });
  267. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement