<?php
require_once('globals.php');
echo '<h3>High/Low</h3>';
$bet = array_key_exists('bet', $_POST) && ctype_digit($_POST['bet']) ? abs(intval($_POST['bet'])) : 0;
$oldnum = array_key_exists('oldnum', $_POST) && ctype_digit($_POST['oldnum']) ? abs(intval($_POST['oldnum'])) : 0;
$choice = array_key_exists('choice', $_POST) && is_string($_POST['choice']) ? stripslashes(strip_tags($_POST['choice'])) : '';
if($bet && !$choice) {
if(!in_array($bet, array('100', '1000', '5000', '10000'))) {
echo 'The bet you selected is not available.';
$h->endpage();
exit;
}
if($bet > $ir['money']) {
echo 'Not enough money!';
$h->endpage();
exit;
}
$num = mt_rand(1,10);
$check = $db->query("SELECT `userid` FROM `highlow` WHERE `userid` = ".$userid);
if($db->num_rows($check))
$db->query("UPDATE `highlow` SET `oldnum` = ".$num." WHERE `userid` = ".$userid);
else if(!$db->num_rows($check))
$db->query("INSERT INTO `highlow` (`userid`,`oldnum`) VALUES ($userid,$num)");
$db->free_result($check);
echo 'The number is '.$num.'.
Will the next number be higher or lower than '.$num.'?
<table>
<tr>
<td>
<form method="post">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="high" name="choice">
<input type="submit" value="Higher">
</form>
</td>
<td>
<form method="post">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="low" name="choice">
<input type="submit" value="Lower">
</form>
</td>
</tr>
</table>';
}
else if($choice && $bet) {
if(!in_array($bet, array('100', '1000', '5000', '10000'))) {
echo 'The bet you selected is not available.';
$h->endpage();
exit;
}
if($bet > $ir['money']) {
echo 'Not enough money!';
$h->endpage();
exit;
}
if(!in_array($choice, array('high','low'))) {
echo 'Invalid guess, must be high or low.';
$h->endpage();
exit;
}
$newnum = mt_rand(1,10);
$oldn = $db->query("SELECT `oldnum` FROM `highlow` WHERE `userid` = ".$userid);
$oldnum = $db->fetch_row($oldn);
$db->free_result($oldn);
if($oldnum['oldnum'] <= 0 || $oldnum['oldnum'] > 10) {
echo 'Invalid number generated, please message staff';
$h->endpage();
exit;
}
if($newnum < $oldnum['oldnum'] && $choice == 'high')
$result = 2;
else if($newnum < $oldnum['oldnum'] && $choice == 'low')
$result = 1;
else if($newnum > $oldnum['oldnum'] && $choice == 'high')
$result = 1;
else if($newnum > $oldnum['oldnum'] && $choice == 'low')
$result = 2;
else if($newnum == $oldnum['oldnum'])
$result = 1;
if(!$result)
die("Error, contact Staff!");
$db->query("UPDATE `users` SET `money` = `money` - ".$bet." WHERE `userid` = ".$userid);
if($result === 1) {
$prize = $bet * 2;
if(!in_array($prize, array('200','2000','10000','20000'))) {
echo 'An invalid amount of prize money has been determined.';
$h->endpage();
exit;
}
$_SESSION['wins'] = $_SESSION['wins'] + 1;
echo 'Congratulations! You won your bet of '.money_formatter($bet);
echo '<br /> You are on a '.money_formatter($_SESSION['wins'], false).' streak';
if($_SESSION['wins'] > $ir['winstreak'])
$db->query("UPDATE `users` SET `winstreak` = ".abs(intval($_SESSION['wins']))." WHERE `userid` = ".$userid);
$db->query("UPDATE `users` SET `money` = `money` + ".$prize." WHERE `userid` = ".$userid);
$db->query("UPDATE `highlow` SET `oldnum` = ".$newnum." WHERE `userid` = ".$userid);
echo 'You guessed '.$choice.', the previous number was '.$oldnum['oldnum'].' and the new number was '.$newnum.'.
Is the next number going to be higher or lower than '.$newnum.'?
<table>
<tr>
<td>
<form method="post">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="high" name="choice">
<input type="submit" value="Higher">
</form>
</td>
<td>
<form method="post">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="low" name="choice">
<input type="submit" value="Lower">
</form>
</td>
</tr>
</table>';
}
else if($result === 2) {
if($_SESSION['wins'])
$_SESSION['wins'] = 0;
$db->query("UPDATE `highlow` SET `oldnum` = ".$newnum." WHERE `userid` = ".$userid);
echo 'Sorry you lost your bet of '.money_formatter($bet).'
You guessed '.$choice.', the previous number was '.$oldnum['oldnum'].' and the new number was '.$newnum.'.
Is the next number going to be higher or lower than '.$newnum.'?
<table>
<tr>
<td>
<form method="post">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="high" name="choice">
<input type="submit" value="Higher">
</form>
</td>
<td>
<form method="post">
<input type="hidden" value="'.$newnum.'" name="oldnum">
<input type="hidden" value="'.$bet.'" name="bet">
<input type="hidden" value="low" name="choice">
<input type="submit" value="Lower">
</form>
</td>
</tr>
</table>';
}
}
else {
$_SESSION['wins'] = 0;
echo 'Welcome to the High/Low casino!
<form action="highlow.php" method="post">
Select your bet:
<select type="dropdown" name="bet">
<option value="100">$100</option>
<option value="1000">$1000</option>
<option value="5000">$5000</option>
<option value="10000">$10000</option>
</select>
<input type="submit" value="Start Game!">
</form>';
}
$h->endpage();