MyBB Tutorials
Newpoints Dice Rolling Game!
Submitted by Harley, 07-08-2017, 12:23 PM, Thread ID: 43150
Thread Closed
PHP Code:
<?php
$db->write_query("CREATE TABLE IF NOT EXISTS `".TABLE_PREFIX."dice` (
`id` int(10) unsigned NOT NULL auto_increment,
`user` varchar(240) NOT NULL,
`bet` int(10) NOT NULL,
`win` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
");
$mybbUser = $mybb->user['uid'];
$mybbUserName = $mybb->user['username'];
$query = $db->query("SELECT * FROM mybb_dice ORDER BY id DESC LIMIT 5");
while($rolls = $db->fetch_array($query))
{
$diceArray[] = $rolls;
}
$credits = floor($mybb->user['newpoints']);
$result = "welcome";
$win = 0;
if (is_numeric($_POST['betamount']) && is_numeric($_POST['guess']))
{
if ($mybb->user['newpoints'] < $_POST['betamount'])
{
$result = "not enough credits :(";
}
else
{
$win = rand(1, 6);
if ($_POST['betamount'] >= 10 && $_POST['betamount'] <= 500 && $_POST['guess'] >= 1 && $_POST['guess'] <= 6)
{
if ($_POST['guess'] == $win)
{
$winInfo = array(
"user" => $mybbUser,
"bet" => $_POST['betamount'],
"win" => $_POST['betamount'] * 2,
);
newpoints_addpoints($mybb->user['uid'], $_POST['betamount'] * 2);
$result = "won";
$credits = $mybb->user['newpoints'] + ($_POST['betamount'] * 2);
$db->insert_query("dice", $winInfo);
}
else
{
$winInfo = array(
"user" => $mybbUser,
"bet" => $_POST['betamount'],
"win" => $_POST['betamount'] * -1,
);
newpoints_addpoints($mybb->user['uid'], $_POST['betamount'] * -1);
$result = "lost";
$credits = $mybb->user['newpoints'] - $_POST['betamount'];
$db->insert_query("dice", $winInfo);
}
}
else
{
$result = "error";
$win = "?";
}
}
}
else
{
$result = "enter numbers";
$win = "#";
}
?>
<html>
<head>
<title>MyBB| Dice
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script>
<script>
var requestSent = false;
$(document).ready(function() {
$(function() {
$('#diceform').unbind().bind('submit', function(e) {
var me = $(this);
e.preventDefault();
if ( me.data('requestRunning') ) {
return;
}
me.data('requestRunning', true);
e.preventDefault();
var form = $(this);
var post_url = form.attr('action');
var post_data = form.serialize();
$.ajax({
type: 'POST',
url: post_url,
data: post_data,
success: function(data) {
var result = $('<div />').append(data).find('#row').html();
$('#row').html(result);
var recentrolls = $('<div />').append(data).find('.recentrolls').html();
$('.recentrolls').html(recentrolls);
}
,
error: function(xhr, status) {
alert("Sorry, there was a problem!");
}
,
complete: function(xhr, status) {
me.data('requestRunning', false);
}
}
);
}
);
}
);
}
);
</script>
<style>
@import url(https://fonts.googleapis.com/css?family=Lato);
body {
padding-top: 120px;
}
h1 {
font-family: 'Lato', sans-serif;
text-align: center;
}
.dice {
border: 3px solid #333;
border-radius: 3px;
cursor: pointer;
height: 100px;
margin: 0 auto;
overflow: hidden;
width: 100px;
}
input[type="number"] {
width: 150px;
text-align: center;
margin-bottom:4px;
border: 2px solid #cacaca;
}
.roll.lost {
background: #940000;
}
.recentrolls {
position: relative;
margin-left: 44.5%;
margin: 0 auto;
overflow: hidden;
width: 150px;
}
.roll {
color: #fff;
padding: 2px;
background:#3498DB;
}
input[type='number'] {
-moz-appearance:textfield;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
}
input#sendbutton {
width: 150px;
background: #fff;
border: 2px solid #cacaca;
}
form {
text-align: center;
margin-top: 20px;
}
.side:hover {
color: #fff;
background: #000;
}
.dice:active {
background: #333;
}
.dice:active .dot {
border-color: white;
}
.side {
height: 100px;
position: relative;
width: 100%;
text-align: center;
line-height: 100px;
font-size: 100px;
}
.rollid {
display: inline-block;
color: #c3c3c3;
text-align: left;
width: 25%;
}
.rolluser {
display: inline-block;
/* margin-left: 10px; */
width: 20%;
text-align: center;
}
.rollbet {
display: inline-block;
}
.rollwin {
display: inline-block;
float: right;
}
.roll.lost.rollwin {
color: #f00 !important;
}
.rollbet {
/* margin-left: 10px; */
width: 20%;
text-align: center;
}
.side:first-child {
margin-top: -00px;
transition: 2s;
}
.side.lost {
background: #940000;
color: #fff;
}
.roll.won {
background: #1d9400;
}
.side.won {
background: #1d9400;
color: #fff;
}
.side.error {
background: #000a92;
color: #fff;
}
</style>
</head>
<div id="row">
<h1>Credits:
<?php echo $credits; ?>
</h1>
<h1>
<?php echo $result; ?>
</h1>
<div class="dice">
<div class="side <?php echo $result; ?>">
<?php echo $win; ?>
</div>
</div>
</div>
<form method="post" id="diceform">
<input type="number" id="guess" name="guess" placeholder="Guess (1-6)" min="1" max="6">
<br>
<input type="number" id="bet" name="betamount" placeholder="Bet (10-500)" min="10" max="500">
<br>
<input type="submit" id="sendbutton" value="Roll">
</form>
<div class="recentrolls">
<div class="roll">
<div class="rollid">ID
</div>
<div class="rolluser">User
</div>
<div class="rollbet">Bet
</div>
<div class="rollwin">Win
</div>
</div>
<?php
for( $i= 0 ; $i < count($diceArray) ; $i++ )
{
if ($diceArray[$i]['win'] > 0)
{
echo "<div class=\"roll won\">";
}
else
{
echo "<div class=\"roll lost\">";
}
echo "<div class=\"rollid\">";
echo $diceArray[$i]['id'];
echo "</div>";
echo "<div class=\"rolluser\">";
echo $diceArray[$i]['user'];
echo "</div>";
echo "<div class=\"rollbet\">";
echo $diceArray[$i]['bet'];
echo "</div>";
echo "<div class=\"rollwin\">";
echo $diceArray[$i]['win'];
echo "</div>";
echo "</div>";
}
?>
</div>
</html>
Users browsing this thread: 4 Guest(s)