Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set("session_use_cookies",1);
- ini_set("session_use_only_cookies",1);
- ini_set('xdebug.dump.SESSION', '*');
- ini_set('xdebug.dump.POST', '*');
- ini_set('xdebug.dump_undefined','on');
- ini_set('xdebug.var_display_max_data', 'on');
- session_start();
- header("Content-Type: text/html; charset=utf-8");
- if (!isset($_SESSION["nickName"])) {
- header("Location: /pokerDice/auth.php");
- exit();
- }
- function logOut() {
- unset($_SESSION);
- session_destroy();
- header("Location: /pokerDice/auth.php");
- exit();
- }
- if (isset($_POST["gameOver"])) {
- gameOver();
- }
- var_dump($_SESSION);
- var_dump($_POST);
- function gameOver() {
- unset($_SESSION["rolls"] );
- unset($_SESSION["bet"]);
- unset($_SESSION["nextTurn"]);
- unset($_SESSION["cash"]);
- unset($_SESSION["state"]);
- $_SESSION["turnNumber"]=1;
- unset($_POST);
- //session_destroy();
- header("Location: /pokerDice/pokerDice.php");
- exit();
- }
- if (isset($_POST["post_logOut"])) {
- logOut();
- }
- if (isset($_POST["turnOver"])) {
- //print "inside turnOver";
- $_SESSION["state"] = 0;
- unset($_SESSION["rolls"] );
- unset($_SESSION["bet"]);
- unset($_SESSION["nextTurn"]);
- $_SESSION["turnNumber"]++;
- unset($_POST["turnOver"]);
- //$_SESSION["rolls"] = 0;
- //$_SESSION["bet"] = 0;
- //print "session: ";
- //print_r($_SESSION);
- }
- if (!isset($_SESSION["cash"])) {
- //1st time run
- $_SESSION["cash"] = 100;
- $_SESSION["state"] = 0;
- $_SESSION["turnNumber"]=1;
- //print "NEW GAME";
- }
- if (isset($_POST["NewGame"])) {
- gameOver();
- }
- if (isset($_SESSION["state"])) {
- $money = "Money: $_SESSION[cash]$";
- $debugMsg = "STATE: ".$_SESSION["state"];
- $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>";
- if ($_SESSION["state"] == 0) {
- //its bid time - game begin or after turn
- if ($_SESSION["cash"]<=1)
- gameOver();
- if (isset($_POST["bet"])) {
- //user pressed bet button - checking it for correct value
- if (!preg_match('/^[0-9]+$/',$_POST["bet"],$regexpResult)) {
- $_POST["bet"] = 0;
- $error = "Only digits allowed!";
- } else {
- if ($_POST["bet"]>$_SESSION["cash"]) {
- $_POST["bet"] = 0;
- $error = "Not enough money!";
- }
- if ($_POST["bet"]>0) {
- //bid is correct
- $_SESSION["state"] = 1;
- $_SESSION["cash"]-=$_POST["bet"];
- $_SESSION["bet"] = $_POST["bet"];
- /*print "session: ";
- print_r($_SESSION);*/
- }
- }
- }
- //checking state again
- if ($_SESSION["state"] == 0) {
- //printing bet form inputs
- $form = <<<HERE
- <form method="post">
- <p>Your bet:</p><input type="text" name="bet" class="textBet">
- <input type="submit" name="setBet" value="Set"><br />
- <input type="submit" name="NewGame" value="New game">
- </form>
- HERE;
- }
- }
- if ($_SESSION["state"]==1) {
- //bet is set - rolling dices
- if (!isset($_SESSION["bet"])) {
- gameOver();
- die("bet run away");
- }
- $money = "Money: $_SESSION[cash]$, bet: $_SESSION[bet]$";
- if (!isset($_POST["pressed"])) {
- //1st roll
- $form = "<div class='images'>\n";
- for ($i=1;$i<6;$i++) {
- if (!isset($_SESSION["rolls"])) {
- $val = rand(1,6);
- } else {
- $val = $_SESSION["rolls"][$i-1];
- }
- $vals[$i-1] = $val;
- $form = $form . "<img src='$val.png' class='diceimg$i'>\n";
- }
- $form = $form . "</div>\n";
- $form2 = <<<HERE
- <form method="post">
- <input type="checkbox" name="cb1" class="checkbox" value="$vals[0]">
- <input type="checkbox" name="cb2" class="checkbox" value="$vals[1]">
- <input type="checkbox" name="cb3" class="checkbox" value="$vals[2]">
- <input type="checkbox" name="cb4" class="checkbox" value="$vals[3]">
- <input type="checkbox" name="cb5" class="checkbox" value="$vals[4]">
- <br />
- <br />
- <input type="submit" name="pressed" value="Reroll selected dices">
- </form>
- HERE;
- $form = $form . $form2;
- $_SESSION["rolls"] = $vals;
- //print_r($_SESSION["rolls"]);
- } else {
- if (!isset($_SESSION["nextTurn"])) {
- //re-rolling and final
- $form = "<div class='images'>\n";
- for ($i=1;$i<6;$i++) {
- $val = isset($_POST["cb$i"]) ? rand(1,6) : $_SESSION["rolls"][$i-1];
- $_SESSION["rolls"][$i-1] = $val;
- $form = $form . "<img src='$val.png' class='diceimg$i'>\n";
- }
- $form = $form . "</div>\n";
- $parse = array(0,0,0,0,0,0);
- foreach ($_SESSION["rolls"] as $value) {
- $parse[$value-1]++;
- }
- $combinations = array(0,0,0,0,0,0);
- foreach ($parse as $key=>$value) {
- $combinations[$value]++;
- }
- $result="";
- $modifier = 0;
- if ($combinations[2]==2) {
- $result = "You got two pairs! You win ";
- $modifier = 1.2;
- } elseif ($combinations[3]==1&&$combinations[2]==1) {
- $result = "You got full house! You win ";
- $modifier = 2;
- } elseif ($combinations[3]==1&&$combinations[2]==0) {
- $result = "You got three of a kind! You win ";
- $modifier = 1.5;
- } elseif ($combinations[4]==1) {
- $result = "You got four of a kind! You win ";
- $modifier = 3;
- } elseif ($combinations[5]==1) {
- $result = "You got FIVE of a kind! You win ";
- $modifier = 3+$_SESSION["rolls"][2];
- } elseif ($combinations[1]==5&&($parse[0]==0||$parse[5]==0)) {
- $result = "You got STREET! You win ";
- $modifier = 4;
- } else {
- $result = "You got nothing. You lost ";
- }
- $won = $_SESSION["bet"]*$modifier;
- if ($won==0) {
- $form = $form . $result.$_SESSION["bet"]."$";
- } else {
- $form = $form . $result.$won."$";
- }
- $_SESSION["cash"] += $won;
- if ($_SESSION["cash"] < 1) {
- $error = "You lost... <br /><img src='676406.jpg'>";
- $form = $form . "<form method='post'>\n<input type='submit' name='gameOver' value='New game'><br />";
- } else {
- $form = $form . "<form method='post'>\n<input type='submit' name='turnOver' value='Next turn'><br />";
- }
- $_SESSION["nextTurn"] = 1;
- /*print_r($_POST);
- print "<br />";
- print_r($_SESSION);
- print "<br />";
- print_r($parse);
- print "<br />";
- print_r($combinations);*/
- } else {
- $_SESSION["state"] = 0;
- unset($_SESSION["rolls"] );
- unset($_SESSION["bet"]);
- unset($_SESSION["nextTurn"]);
- header("Location: /pokerDice/pokerDice.php");
- exit();
- }
- }
- }
- } else {
- //we're not supposed to be here
- print "You broke it!";
- gameOver();
- die("unknown_error");
- }
- ?>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <html>
- <head>
- <link rel="stylesheet" type="text/css" href="pokerMain.css">
- <script src="jquery201.js" type="text/javascript"></script>
- <title>PokerDice</title>
- </head>
- <body>
- <header>
- <div id="header_text"><h1>PokerDICE</h1></div>
- </header>
- <content>
- <div id="controlPanel">
- <?php
- if (isset($debugMsg)) print $debugMsg;
- if (isset($controlPanel)) print $controlPanel;
- ?>
- </div>
- <div id="main">
- <p class="money"><?php print $money; ?></p>
- <?php
- print $form;
- ?>
- <p class="error"><?php if (isset($error)) print $error; ?></p>
- </div>
- </content>
- <footer>
- </footer>
- </body>
- </html>
- <script>
- $("#ayolaid").hide();
- $("img[class^=diceimg]").click(function () {
- $id = $(this).attr("class").charAt($(this).attr("class").length-1);
- $state = $("input[name='cb" + $id + "']").prop("checked");
- $("input[name='cb" + $id + "']").prop("checked", !$state);
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement