So i got bored and wanted to have a go at doing a lottery module i think it turned out all right in the end
Lets start with the SQL files
CREATE TABLE IF NOT EXISTS `lottery` (
`lotofund` int(30) NOT NULL DEFAULT '0',
`tickets` int(30) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO `lottery` (`lotofund`, `tickets`) VALUES (0, 0);
CREATE TABLE IF NOT EXISTS `lottery_players` (
`user` int(30) NOT NULL DEFAULT '0',
`tickets` int(10) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE IF NOT EXISTS `lotowinners` (
`user` int(20) NOT NULL DEFAULT '0',
`amountwon` int(50) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE IF NOT EXISTS `lottery_config` (
`ticketprice` int(30) NOT NULL DEFAULT '0',
`maxtickets` int(30) NOT NULL DEFAULT '0',
`pagedisable` enum('Yes','No') NOT NULL DEFAULT 'No'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `lottery_config` (`ticketprice`, `maxtickets`, `pagedisable`) VALUES (1000, 10, 'No');
ALTER TABLE `grpgusers` ADD `tickets` INT(30) NOT NULL DEFAULT 0;
open up classes.php and add this to the user class
$this->tickets = $worked['tickets'];
You can add this to your style sheet if you have one if not add it into the header.php
.infobox {
background: #101010;
width: 200px;
height: auto;
border: 1px solid gray;
border-radius: 5px;
padding: 10px;
float: left;
text-align: center;
}
.otherbox {
background: #101010;
width: 400px;
height: auto;
border: 1px solid gray;
border-radius: 5px;
padding: 4px;
float: right;
text-align: center;
}
.buysection,.infobox {
margin-top: 5px;
}
.btn {
background: #585858;
width: auto;
height: auto;
border: 1px solid black;
border-radius: 3px;
}
.input {
background: #202020;
color: #FFFFFF;
border: 1px solid black;
padding: 1px;
}
Ok now time for the files.
lottery.php
<?php
include(__DIR__.'/header.php');
$res = mysql_query("SELECT * FROM `lottery`");
$row = mysql_fetch_array($res);
$checkp = mysql_query("SELECT * FROM `lottery_players` WHERE `user` = ".$user_class->id);
$num = mysql_num_rows($checkp);
$checkt = mysql_fetch_array($checkp);
$config = mysql_query("SELECT * FROM `lottery_config`");
$worked = mysql_fetch_array($config);
if($worked['pagedisable'] == 'Yes') {
echo Message('Admin has disabled the Lottery Page.');
include_once(__DIR__.'/footer.php');
exit;
}
$_POST['amount'] = isset($_POST['amount']) && ctype_digit($_POST['amount']) ? abs(intval($_POST['amount'])) : null;
if(array_key_exists('buy', $_POST)) {
if(empty($_POST['amount'])) {
echo Message("You didn't enter a valid amount");
include(__DIR__ . '/footer.php');
exit;
}
$cost = $worked['ticketprice'] * $_POST['amount'];
$usercash = $user_class->money - $cost;
$tickets = $user_class->tickets + $_POST['amount'];
$loto = $row['lotofund'] + $cost;
$ltick = $row['tickets'] + $_POST['amount'];
$deficit = $worked['maxtickets'] - $user_class->tickets;
$havetick = $checkt['tickets'] + $_POST['amount'];
if($tickets > $worked['maxtickets']) {
echo Message('The max tickets is '.number_format($worked['maxtickets']).'. This means you can only buy '.number_format($worked['maxtickets'] - $user_class->tickets).' more ticket'.($deficit == 1 ? '' : 's'));
include_once(__DIR__.'/footer.php');
exit;
}
if($_POST['amount'] > $worked['maxtickets']) {
echo Message('You can only purchase '.number_format($worked['maxtickets']));
include_once(__DIR__.'/footer.php');
exit;
}
if($user_class->money < $worked['ticketprice']) {
echo Message('You cant afford '.number_format($_POST['amount']).' tickets. It costs $'.number_format($worked['ticketprice'] * $_POST['amount']));
include_once(__DIR__.'/footer.php');
exit;
}
if($user_class->tickets > $worked['maxtickets']) {
echo Message('You have already purchased max tickets which is '.number_format($worked['maxtickets']));
include_once(__DIR__.'/footer.php');
exit;
}
if($user_class->money >= $worked['ticketprice'] && $_POST['amount'] <= $worked['maxtickets']) {
if($num) {
echo Message('You have purchase '.$_POST['amount'].' lottery tickets for $'.number_format($cost));
mysql_query("UPDATE `grpgusers` SET `money` = ".$usercash.", `tickets` = ".$tickets." WHERE `id` = ".$user_class->id);
mysql_query("UPDATE `lottery` SET `lotofund` = ".$loto.", `tickets` = ".$ltick);
mysql_query("UPDATE `lottery_players` SET `tickets` = ".$havetick." WHERE `user` = ".$user_class->id);
}
else {
echo Message('You have purchase '.$_POST['amount'].' lottery tickets for $'.number_format($cost));
mysql_query("UPDATE `grpgusers` SET `money` = ".$usercash.", `tickets` = ".$tickets." WHERE `id` = ".$user_class->id);
mysql_query("UPDATE `lottery` SET `lotofund` = ".$loto.", `tickets` = ".$ltick);
mysql_query("INSERT INTO `lottery_players` VALUES('$user_class->id',".$_POST['amount'].")");
}
}
}
?>
<tr><td class="contenthead">Lottery</td></tr>
<tr><td class="contentcontent">
<div class="infobox">
<span>Welcome to the lottery</span>
<span>Each ticket costs $<?php echo number_format($worked['ticketprice']); ?></span>
<span>Maximum of <?php echo number_format($worked['maxtickets']); ?> tickets per person</span>
</div>
<div class="otherbox">
<span>Lottery Fund: $<?php echo number_format($row['lotofund']); ?></span>
<span>Tickets Purchased: <?php echo number_format($row['tickets']); ?></span>
</div>
</td></tr>
<tr><td class="contentcontent">
<div class="buysection">
<form method="post">
[buy Tickets] <input type="text" name="amount" value="1" maxlength="2" />
<input type="submit" name="buy" value="Buy Ticket" />
</form>
</div>
</td></tr>
<tr><td class="contenthead">Previous Winners</td></tr>
<tr><td class="contentcontent">
<?php
$win = mysql_query("SELECT * FROM `lotowinners`");
$check = mysql_num_rows($win);
while($row = mysql_fetch_array($win)) {
$user = new User($row['user']);
if($check == 0) {
echo "<div class='infobox'><span>There has been no previous Winners</span></div>";
}
else {
echo "
<table class='infobox'>
<tr>
<td>".$user->formattedname." - $".number_format($row['amountwon'])."</td>
</tr>";
}
echo "</table>";
}
?>
</td></tr>
lottery_config.php
<?php
include(__DIR__.'/header.php');
$allowed = array(1,2);
if($user_class->id != in_array(1,$allowed)) {
echo Message('You are not allowed here.');
include_once(__DIR__.'/footer.php');
exit;
}
$res = mysql_query("SELECT * FROM `lottery_config");
$row = mysql_fetch_array($res);
if(isset($_POST['disable'])) {
if($row['pagedisable'] == 'No') {
echo Message('You have disabled the lottery page');
mysql_query("UPDATE `lottery_config` SET `pagedisable` = 'Yes' WHERE `pagedisable` = 'No'");
} else if($row['pagedisable'] == 'Yes') {
echo Message('You have enabled the lottery page.');
mysql_query("UPDATE `lottery_config` SET `pagedisable` = 'No' WHERE `pagedisable` = 'Yes'");
}
}
$_POST['price'] = isset($_POST['price']) && ctype_digit($_POST['price']) ? abs(intval($_POST['price'])) : null;
if(isset($_POST['setprice'])) {
if(empty($_POST['price'])) {
echo Message('Invalid Price');
include_once(__DIR__.'/footer.php');
exit;
}
if(!is_numeric($_POST['price'])) {
echo Message('Price must be a number.');
include_once(__DIR__.'/footer.php');
exit;
}
if($_POST['price'] == $row['ticketprice']) {
echo Message('It is already set to that nothing has changed.');
include_once(__DIR__.'/footer.php');
}
if($_POST['price'] != $row['ticketprice']) {
echo Message('You have updated the price to $'.number_format($_POST['price']));
mysql_query("UPDATE `lottery_config` SET `ticketprice` = ".$_POST['price']);
}
}
$_POST['tickets'] = isset($_POST['tickets']) && ctype_digit($_POST['tickets']) ? abs(intval($_POST['tickets'])) : null;
if(isset($_POST['settickets'])) {
if(empty($_POST['tickets'])) {
echo Message('Invalid Price');
include_once(__DIR__.'/footer.php');
exit;
}
if(!is_numeric($_POST['tickets'])) {
echo Message('Tickets must be a number.');
include_once(__DIR__.'/footer.php');
exit;
}
if($_POST['ticketprice'] == $_POST['tickets']) {
echo Message('It is already set to this so nothing has changed.');
include_once(__DIR__.'/footer.php');
}
if($_POST['tickets'] != $row['maxtickets']) {
echo Message('You have updated the max tickets to '.number_format($_POST['tickets']));
mysql_query("UPDATE `lottery_config` SET `maxtickets` = ".$_POST['tickets']);
}
}
?>
<tr><td class="contenthead">Lottery Panel</td></tr>
<tr><td class="contentcontent">
<table width="100%" style="background: #101010" cellpadding="5">
<tr>
<td>
<span>Disable/Enable Lottery Page<span>
<form method="post">
<?php
if($row['pagedisable'] == 'No') {
echo '
<input type="submit" name="disable" class="btn" value="Disable Page" />';
}
if($row['pagedisable'] == 'Yes') {
echo '
<input type="submit" name="disable" class="btn" value="Enable Page" />';
}
?>
</form>
</td>
</tr>
<tr>
<td>
<hr>
<i>Set Ticket Price</i>
<form method="post">
Price: <input type="text" name="price" value="0" class="input" />
<input type="submit" name="setprice" class="btn" value="Set Ticket Price" />
</form>
</td>
</tr>
<tr>
<td>
<hr>
<i>Set Max Tickets per person</i>
<form method="post">
Max Tickets: <input type="text" name="tickets" value="0" class="input" />
<input type="submit" name="settickets" class="btn" value="Set Max Tickets" />
</form>
</td>
</tr>
</table>
</td></tr>
lot_cron.php
<?php
include('dbcon.php');
include('classes.php');
if(isset($_GET['code']) != 'Your code here') {
echo "<span style='color:red'><h2>Error</h2></span>";
exit;
}
else {
$res = mysql_query("SELECT * FROM `lottery_players` ORDER BY RAND() LIMIT 1");
$checkwinner = mysql_num_rows($res);
while($row = mysql_fetch_array($res)) {
if($checkwinner == 0) {
}
else {
$winner = new User($row['user']);
$check = mysql_query("SELECT * FROM `lottery`");
$worked = mysql_fetch_array($check);
Send_Event($winner->id, "Congrats you have won the lottery your prize is $".number_format($worked['lotofund']));
mysql_query("UPDATE `grpgusers` SET `money` = `money` + {$worked['lotofund']} WHERE `id` = ".$winner->id);
mysql_query("UPDATE `lottery` SET `lotofund` = '0',`tickets` = '0'");
mysql_query("UPDATE `grpgusers` SET `tickets` = '0' WHERE `tickets` > '0'");
mysql_query("INSERT INTO `lotowinners` VALUES('$winner->id',".$worked['lotofund'].")");
mysql_query("TRUNCATE TABLE `lottery_players`");
}
}
}
?>
ok some screenshots
[ATTACH=CONFIG]1372[/ATTACH][ATTACH=CONFIG]1373[/ATTACH][ATTACH=CONFIG]1374[/ATTACH]