I was asked before to re-program a mod for you guys too see my programming. So i choose the lotto mod because i looked in the requests and someone had asked for one. So here goes. This hasnt been tested though.
Original mod: http://makewebgames.io/board791/21297-mccode-v2-lottery
This is the lotto file, call it what you want.
<?php
/*
SQLS:
ALTER TABLE `users` ADD `lottery` INT(11) NOT NULL DEFAULT '0';
DROP TABLE IF EXISTS `lottery`;
CREATE TABLE IF NOT EXISTS `lottery` (
`id` int(11) NOT NULL auto_increment,
`userid` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`jackpot` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
*/
include_once(dirname(__FILE__) .'/globals.php');
///Settings
$cost = 100; //Cost per ticket
$max = 100; //Max amount of tickets
//End settings
$_GET['lotto'] = (is_string($_GET['a']) && isset($_GET['a']) && !empty($_GET['a']) && $_GET['a'] == 'buy') ? $_GET['lotto'] : false;
$_GET['amnt'] = (isset($_GET['amnt']) && !empty($_GET['amnt']) && is_numeric($_GET['amnt']) && is_int($_GET['amnt'])) ? $_GET['amnt'] : false;
switch($_GET['lotto']) {
case'buy' : buy(); break;
default: lottory(); break;
}
function lottory() {
global $db, $ir, $h, $cost, $max;
$lotto = $db->fetch_single($db->query("SELECT `jackpot` FROM `lottery`"));
echo'<span style="color: red; size: 5; font-weight: bold;">Welcome to the National Jackpot.</span>
You currently have '. number_format($ir['lottery']) .' ticket\'s. You can purchase '. number_format($max-$ir['lottery']) .' more tickets this week. Each ticket costs '. money_formatter($cost) .'.
<span style="color: white; font-weight: bold; size: 5;">The jackpot is currently '. money_formatter($lotto) .'.</span>
<form action="?lotto=buy" method="get">
Amount of tickets:
<input type="text" name="amnt" value="'. $max-$ir['lottery'] .'" />
<input type="submit" value="Buy the tickets!" />
</form>';
$h->endpage();
exit();
}
function buy() {
global $db, $ir, $h, $cost, $max;
if(($ir['money']*$_GET['amnt']) < $cost) {
echo'You don\'t seem to have enought money.';
$h->endpage();
exit();
}
if($_GET['amnt'] == 0 || $_GET['amnt'] > $max) {
echo'The amount of tickets you want, cannot be purchased.';
$h->endpage();
exit();
}
if($ir['lottery'] > $max) {
$db->query("UPDATE `users` SET `lottery` = ". $max ." WHERE (`userid` = ". $ir['userid'] .");");
echo'Lotto tickets fixed.';
$h->endpage();
exit();
}
if($ir['lottery']+$_GET['amnt'] > $max) {
echo'If we let you purchase these tickets, you would have too many.';
$h->endpage();
exit();
}
echo'You have purchased '. number_format($_GET['amnt']) .' tickets.';
$db->query("UPDATE `users` SET `lottery` = `lottery` + ". $_GET['amnt'] .", `money` = `money` - ". ($cost*$_GET['amnt']) ." WHERE (`userid` = ". $ir['userid'] .");");
$db->query("INSERT INTO `lottery` (`id`, `userid`, `amount`) VALUES ('', ". $ir['userid'] .", ". $cost*$_GET['amnt'] .");");
$db->query("UPDATE `lottery` SET `jackpot` = `jackpot` + ". $cost*$_GET['amnt'] .";");
$h->endpage();
exit();
}
?>
Now this is the cron, run it every week:
<?php
include_once(dirname(__FILE__) .'/config.php');
global $_CONFIG;
if($_GET['code'] != $_CONFIG['code']) { echo'Wrong code.'; exit(); }
define('mono_on', 1, true);
require_once(dirname(__FILE__) .'/class/class_db_'. $config['driver'] .'.php');
$db = new database;
$db->configure($_CONFIG['hostname'], $_CONFIG['username'], $_CONFIG['password'], $_CONFIG['database'], $_CONFIG['persistent']);
$db->connect();
$c = $db->connection_id;
include_once(dirname(__FILE__) .'/global_func.php');
$thesql = $db->query("SELECT * FROM `lottery`");
$amnt = $db->num_rows($thesql);
$lotto = $db->fetch_row($thesql);
$winnerid = rand(1, $amnt);
$user = $db->fetch_single($db->query("SELECT `userid` FROM `lottery` WHERE (`id` = ". $winnerid .");"));
$db->query("UPDATE `users` SET `money` = `money` + ". $lotto['jackpot'] ." WHERE (`userid` = ". $user .");");
event_add($user, 'You\'ve won the weekly lottery and won '. money_formatter($lotto['jackpot']) .'.', $c);
$db->query("UPDATE `users` SET `lottery` = 0;");
$db->query("TRUNCATE TABLE `lottery`");
echo'ID: '. $winnderid .' won the lotto.';
exit();
?>