Lottery mod

Started by Mindless, February 21, 2013, 08:43:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mindless

Credit: iMT Developers

CREATE TABLE `tickets` (
`id` int(4) NOT NULL auto_increment,
`user` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

CREATE TABLE `lottery_config` (
  `name` varchar(255) NOT NULL default '',
  `value` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `lottery_config` VALUES ('ticket_amount', '100');
INSERT INTO `lottery_config` VALUES ('ticket_amount_type', 'MB');
INSERT INTO `lottery_config` VALUES ('user_tickets', '1');
INSERT INTO `lottery_config` VALUES ('class_allowed', '80|96');
INSERT INTO `lottery_config` VALUES ('total_winners', '1');
INSERT INTO `lottery_config` VALUES ('prize_fund', '25000');
INSERT INTO `lottery_config` VALUES ('start_date', '0000-00-00 00:00:00');
INSERT INTO `lottery_config` VALUES ('end_date', '0000-00-00 00:00:00');
INSERT INTO `lottery_config` VALUES ('use_prize_fund', '0');
INSERT INTO `lottery_config` VALUES ('enable', '0');
INSERT INTO `lottery_config` VALUES ('lottery_winners', '');
INSERT INTO `lottery_config` VALUES ('lottery_winners_amount', '');
INSERT INTO `lottery_config` VALUES ('lottery_winners_time', '');


open cleanup.php

find this

} while (0);

after add



// START LOTTERY - THADON
$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

if ($arr_config['enable'] == 1)
{
if (get_date_time() > $arr_config['end_date'])
{
if ($arr_config["ticket_amount_type"] == GB)
$arr_config['ticket_amount'] = 1024 * 1024 * 1024 * $arr_config['ticket_amount'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['ticket_amount'] = 1024 * 1024 * $arr_config['ticket_amount'];
$size = $arr_config['ticket_amount'];

if ($arr_config["ticket_amount_type"] == GB)
$arr_config['prize_fund'] = 1024 * 1024 * 1024 * $arr_config['prize_fund'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['prize_fund'] = 1024 * 1024 * $arr_config['prize_fund'];
$prize_fund = $arr_config['prize_fund'];

$total = mysql_num_rows(mysql_query("SELECT * FROM tickets"));
if ($arr_config["use_prize_fund"])
{
$pot = $prize_fund / $arr_config['total_winners'];
$res = mysql_query("SELECT user FROM tickets ORDER BY RAND() LIMIT $arr_config[total_winners]") or sqlerr();
$who_won = array();
$msg = sqlesc("Congratulations, You have won: <b>".mksize($pot)."</b>.<br /><br />This has been added to your upload amount<br /><br />Thanks for playing Lottery.");
while ($arr = mysql_fetch_assoc($res))
{
$res2 = mysql_query("SELECT modcomment FROM users WHERE id = $arr[user]") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);
$modcomment = $arr2['modcomment'];
$modcom = sqlesc("User won the lottery: " . mksize($pot) . " at " . get_date_time() . "\n" . $modcomment);
mysql_query("UPDATE users SET uploaded = uploaded + $pot, modcomment = $modcom WHERE id = $arr[user]") or sqlerr();
mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, $arr[user], NOW(), 'You have won the Lottery', $msg, 0)") or sqlerr(__FILE__, __LINE__);
$who_won[] = $arr['user'];
}
}
else
{
$pot = $total * $size / $arr_config['total_winners'];
$res = mysql_query("SELECT user FROM tickets ORDER BY RAND() LIMIT $arr_config[total_winners]") or sqlerr();
$who_won = array();
$msg = sqlesc("Congratulations, You have won: <b>".mksize($pot)."</b>.<br /><br />This has been added to your upload amount<br /><br />Thanks for playing Lottery.");
while ($arr = mysql_fetch_assoc($res))
{
$res2 = mysql_query("SELECT modcomment FROM users WHERE id = $arr[user]") or sqlerr(__FILE__, __LINE__);
$arr2 = mysql_fetch_assoc($res2);
$modcomment = $arr2['modcomment'];
$modcom = sqlesc("User won the lottery: " . mksize($pot) . " at " . get_date_time() . "\n" . $modcomment);
mysql_query("UPDATE users SET uploaded = uploaded + $pot WHERE id = $arr[user]") or sqlerr();
mysql_query("INSERT INTO messages (sender, receiver, added, subject, msg, poster) VALUES(0, $arr[user], NOW(), 'You have won the Lottery', $msg, 0)") or sqlerr(__FILE__, __LINE__);
$who_won[] = $arr['user'];
}
}
$who_won = implode("|", $who_won);
$who_won_date = get_date_time();
$who_won_prize = $pot;
mysql_query("TRUNCATE TABLE tickets") or sqlerr(__FILE__, __LINE__);
if ($who_won != '')
{
mysql_query("UPDATE lottery_config SET value = '$who_won' WHERE name = 'lottery_winners'") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE lottery_config SET value = '$who_won_prize' WHERE name = 'lottery_winners_amount'") or sqlerr(__FILE__, __LINE__);
mysql_query("UPDATE lottery_config SET value = '$who_won_date' WHERE name = 'lottery_winners_time'") or sqlerr(__FILE__, __LINE__);
}
mysql_query("UPDATE lottery_config SET value = '0' WHERE name = 'enable'") or sqlerr(__FILE__, __LINE__);
}
}
// END LOTTERY  - THADON


Save & close cleanup.php...

This is an extra part, if you want to display the last winner/winners on the index, or anywhere of your choice.

open index.php

and add this anywhere you want



<?
 
$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

$who_won = explode("|", $arr_config['lottery_winners']);
$who_won = array_unique($who_won);
$lottery_winners = '';
for ($x = 0; $x < count($who_won); $x++)
{
$username = '';
$res2 = mysql_query("SELECT id, username FROM users") or sqlerr(__FILE__, __LINE__);
while ($arr2 = mysql_fetch_assoc($res2))
{
if ($arr2['id'] == $who_won[$x])
{
$username = '<a href="userdetails.php?id='. $arr2['id'] .'">'. $arr2['username'] .'</a>';
$lottery_winners .= (!$lottery_winners) ? $username : ', '. $username;
break;
}
}
}

if (count($who_won) > 1)
$winners = 'Winners';
else
$winners = 'Winner';

if (count($who_won) > 1)
$each = ' (Each)';
else
$each = '';
?>
<tr><td class="rowhead">Last Lottery</td><td class="rowhead" align="left"><?=count($who_won)?> <?=$winners?></td></tr>
<tr><td class="rowhead2">Last Lottery <?=$winners?></td><td class="rowhead2" align="left"><?=$lottery_winners?></td></tr>
<tr><td class="rowhead">Last Amount Won<?=$each?></td><td class="rowhead" align="left"><?=mksize($arr_config['lottery_winners_amount'])?></td></tr>
<tr><td class="rowhead2">Date Of Last Lottery</td><td class="rowhead2" align="left"><?=$arr_config['lottery_winners_time']?></td></tr>



Now create these files:

create lottery_config.php and put this



<?
require "include/bittorrent.php";

dbconn();

loggedinorreturn();

if (get_user_class() < UC_MODERATOR)
stderr("Sorry", "Access denied.");

stdhead("Lottery");

begin_main_frame();

if ($_POST['lottery'] == 'config')
{

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
{
$name = $arr['name'];

if ($name != 'class_allowed')
$new_data = $_POST[$name];
else
$new_data = implode("|", $_POST['class_allowed']);

if (($name != 'lottery_winners') && ($name != 'lottery_winners_amount') && ($name != 'lottery_winners_time'))
mysql_query("UPDATE lottery_config SET value = '$new_data' WHERE name = '$name'") or sqlerr(__FILE__, __LINE__);
}
stderr("Settings Changed", "Settings were successfully change.<br /><br /><a href=lottery_config.php>Go Back</a>");
}

?>
<form action="lottery_config.php" method=post>

<?

print("<table width=\"100%\">\n");

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

if (!$arr_config["enable"])
begin_frame("Lottery Configuration:", true);

if ($arr_config["enable"])
{
begin_frame("Lottery Enabled", true);
print("Lottery is currently enabled, so this configuration page is closed.<br /><br />Classes playing in this lottery, are: ");
$class = explode("|", $arr_config['class_allowed']);
$classes_playing = '';
for ($x = 0; $x < count($class); $x++)
{
$classes_playing = (!$classes_playing) ? get_user_class_name($class[$x]) : ', ' . get_user_class_name($class[$x]);
print($classes_playing);
}
die;
}

if ($arr_config["ticket_amount_type"] != 'seedbonus')
$selected = ' selected';


$use_prize_fund_yes = ($arr_config['use_prize_fund']) ? 'checked="checked"' : '';
$use_prize_fund_no = (!$arr_config['use_prize_fund']) ? 'checked="checked"' : '';

$enable_yes = ($arr_config['enable'] == 1) ? 'checked="checked"' : '';
$enable_no = ($arr_config['enable'] == 0) ? 'checked="checked"' : '';

print("<tr><td width=50% class=\"tableb\" align=left>Enable The Lottery</td><td class=\"tableb\" align=left>Yes <input class=\"tableb\" type=radio name=enable value=\"1\" $enable_yes /> No <input class=\"tableb\" type=radio name=enable value=\"0\" $enable_no /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Use Prize Fund (No, uses default pot of all users)</td><td class=\"tableb\" align=left>Yes <input class=\"tableb\" type=radio name=use_prize_fund value=\"1\" $use_prize_fund_yes /> No <input class=\"tableb\" type=radio name=use_prize_fund value=\"0\" $use_prize_fund_no /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Prize Fund</td><td class=\"tableb\" align=left><input type=text name=prize_fund value=\"$arr_config[prize_fund]\" /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Ticket Amount</td><td class=\"tableb\" align=left><input type=text name=ticket_amount value=\"$arr_config[ticket_amount]\" /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Ticket Amount Type</td><td class=\"tableb\" align=left><select name=ticket_amount_type><option value=\"GB\"$selected>GB</option><option value=\"MB\"$selected2>MB</option></select></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Amount Of Tickets Allowed</td><td class=\"tableb\" align=left><input type=text name=user_tickets value=\"$arr_config[user_tickets]\" /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Classes Allowed</td><td class=\"tableb\" align=left><ul class=\"checklist\">");

$maxclass = UC_SYSOP + 1;
for ($i = 0; $i < $maxclass; $i++)
{
$class_allowed = array_map('trim', @explode('|', $arr_config["class_allowed"]));
if (in_array($i, $class_allowed))
{
if ($c = get_user_class_name($i))
print("<li><label for=\"$i\"><input id=\"$i\" name=\"class_allowed[]\" type=\"checkbox\" checked=\"checked\" value=\"$i\" />$c</label></li>\n");
}
else
{
if ($c = get_user_class_name($i))
print("<li><label for=\"$i\"><input id=\"$i\" name=\"class_allowed[]\" type=\"checkbox\" value=\"$i\" />$c</label></li>\n");
}
}

print("</ul></td><tr><td width=50% class=\"tableb\" align=left>Total Winners</td><td class=\"tableb\" align=left><input type=text name=total_winners value=\"$arr_config[total_winners]\" /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Current Date/Time</td><td class=\"tableb\" align=left>" . get_date_time()  . "</td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>Start Date</td><td class=\"tableb\" align=left><input type=text name=start_date value=\"$arr_config[start_date]\" /></td></tr>");
print("<tr><td width=50% class=\"tableb\" align=left>End Date</td><td class=\"tableb\" align=left><input type=text name=end_date value=\"$arr_config[end_date]\" /></td></tr>");
if (get_user_class() >= UC_MODERATOR)
{
?>
<tr>
  <td class="tableb" colspan="4" align="center">
<input type="hidden" name="lottery" value="config"><input type="submit" name="submit" value="Apply Changes">
  </td>
</tr>
</table></form>
<?
}

end_frame();
end_main_frame();
stdfoot();
die;

?>



create purchasetickets.php and put this



<?

require "include/bittorrent.php";

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

$endday = $arr_config['end_date'];

if (!$arr_config["enable"])
stderr("Sorry", "Lottery is disabled.");

dbconn();

loggedinorreturn();

$ticket_amount_display = $arr_config['ticket_amount'];

if ($arr_config["ticket_amount_type"] == seedbonus)
$arr_config['ticket_amount'] = 1000 * 1000 * 1000 * $arr_config['ticket_amount'];


$minupload = $size; //Minimum Upload Required to Buy Ticket!

stdhead("Tickets Page");

if (get_user_class() < 0)
{
print("<h1>Sorry</h2><p>You must be Registered to request, see the <a href=faq.php><b>FAQ</b></a> for information on different user classes</p>");
die();
}

if (get_date_time() > $arr_config['end_date'])
{
print ("Sorry I cannot sell you any tickets!");
die();
}

$res = mysql_query("SELECT downloaded, uploaded FROM users WHERE id = $CURUSER[id]") or die(mysql_error());
$result = mysql_fetch_assoc($res);

$res2 = mysql_query("SELECT COUNT(id) AS tickets FROM tickets WHERE user = $CURUSER[id]") or die(mysql_error());
$result2 = mysql_fetch_assoc($res2);

$purchaseable = $arr_config['user_tickets'];
if (($result2['tickets'] + $_REQUEST['number']) > $purchaseable || $_REQUEST['number'] < 1 )
{
print("<table class=frame width=737 cellspacing=0 cellpadding=5><tr><td><table class=main width=100% cellspacing=0 cellpadding=5><tr><td class=colhead align=left>ERROR</td></tr><tr><td>The max number of tickets you can purchase is $purchaseable<br></td></tr></table></td></tr></table>");
stdfoot();
die;
}

if (($minupload * $_REQUEST['number']) > $result["uploaded"] )
{
print("<table class=frame width=737 cellspacing=0 cellpadding=5><tr><td><table class=main width=100% cellspacing=0 cellpadding=5><tr><td class=colhead align=left>ERROR</td></tr><tr><td>You do not have enough upload amount to buy a ticket<br></td></tr></table></td></tr></table>");
stdfoot();
die;
}
$upload = $result["uploaded"] - ($minupload * $_REQUEST['number']);
mysql_query("UPDATE users SET uploaded=$upload WHERE id=". $CURUSER["id"]) or die(mysql_error());
$tickets = $_REQUEST['number'];
for ($i = 0; $i < $tickets; $i++)
mysql_QUERY("INSERT INTO tickets(user) VALUES($CURUSER[id])");
$me = mysql_num_rows(mysql_query("SELECT * FROM tickets WHERE user=" . $CURUSER["id"]));
print("<br>\n");

?>
<table border=1 width=600 cellspacing=0 cellpadding=5>
<tr><td class=tabletitle width=600 align=center>iMT Lottery</td></tr>
<tr><td class=tableb align=left>
You just purchased <?= $_REQUEST["number"]; ?> ticket<? if ($_REQUEST["number"] > 1) echo "s"; ?>!<br>
Your new total is <?= $me; ?>!<br>
Your new upload total is <?= mksize($upload); ?>!<br><br>
<a href=tickets.php>Go Back</a>
</td></tr></table>

<?
print("<br>\n");

stdfoot();
die;

?>


create tickets.php put this



<?

require "include/bittorrent.php";

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

if (!$arr_config["enable"])
stderr("Sorry", "Lottery is disabled.");

$user_class = get_user_class();
$class_allowed = array_map('trim', @explode('|', $arr_config["class_allowed"]));
if (!in_array($user_class, $class_allowed))
{
stderr("Sorry", "This class level isn't allowed in this lottery.");
}

dbconn();

loggedinorreturn();

$ticket_amount_display = $arr_config['ticket_amount'];

if ($arr_config["ticket_amount_type"] == GB)
$arr_config['ticket_amount'] = 1024 * 1024 * 1024 * $arr_config['ticket_amount'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['ticket_amount'] = 1024 * 1024 * $arr_config['ticket_amount'];
$size = $arr_config['ticket_amount'];

if ($arr_config["ticket_amount_type"] == GB)
$arr_config['prize_fund'] = 1024 * 1024 * 1024 * $arr_config['prize_fund'];
else if ($arr_config["ticket_amount_type"] == MB)
$arr_config['prize_fund'] = 1024 * 1024 * $arr_config['prize_fund'];
$prize_fund = $arr_config['prize_fund'];

$ratioerr = "<font color=\"red\"><b>You must have uploaded atleast $arr_config[ticket_amount] $arr_config[ticket_amount_type] in order to buy a ticket!</b></font>";

stdhead("Tickets Page");

$total = mysql_num_rows(mysql_query("SELECT * FROM tickets"));
if ($arr_config["use_prize_fund"])
$pot = $prize_fund;
else
$pot = $total * $size;
$me = mysql_num_rows(mysql_query("SELECT * FROM tickets WHERE user=" . $CURUSER["id"]));
$me2 = mysql_query("SELECT * FROM tickets WHERE user=". $CURUSER['id'] ." ORDER BY id ASC");
while ($myrow = mysql_fetch_assoc($me2))
$ticketnumbers .= "$myrow[id] ";

$purchaseable = $arr_config['user_tickets'] - $me;

if ($me >= $arr_config["user_tickets"])
$purchaseable = 0;

if (get_date_time() > $arr_config['end_date'])
$purchaseable = 0;

print("<br>\n");

?>


<table border=1 width=600 cellspacing=0 cellpadding=5>
<tr><td class=tabletitle width=600 align=center>iMT Lottery</td></tr>
<tr><td align=left class=tableb>
<ul>
<li>Tickets are non-refundable</li>
<li>Each ticket costs <?= $ticket_amount_display . ' ' . $arr_config['ticket_amount_type']; ?> which is taken from your upload amount</li>
<li>Purchaseable shows how many tickets you can afford</li>
<li>You can only buy upto your purchaseable amount.</li>
<li>The competiton will end: <?= $arr_config["end_date"]; ?></li>
<li>There will be <?= $arr_config['total_winners']; ?> winners who will be picked at random</li>
<li>Each winner will get <?= mksize($pot/$arr_config['total_winners']); ?> added to their upload amount</li>
<li>The Winners will be announced once the lottery has closed and posted on the home page.</li>
<?
if (!$arr_config["use_prize_fund"])
{
?>
<li>The more tickets that are sold the bigger the pot will be!</li>
<?
}
?>
<li>You own ticket numbers: <?= $ticketnumbers; ?></li>
</ul>
Good Luck!
<hr>
<table align=center width=40% class=frame border=1 cellspacing=0 cellpadding=10><tr><td align=center>
<table width=100% class=tableb class=main border=1 cellspacing=0 cellpadding=5>
<tr>
<td class=tableb>Total Pot</td>
<td class=tableb><?= mksize($pot); ?></td>
</tr>
<tr>
<td class=tableb>Total Tickets Purchased</td>
<td class=tableb align=right><?= $total; ?> Tickets</td>
</tr>
<tr>
<td class=tableb>Tickets Purchased by You</td>
<td class=tableb align=right><?= $me; ?> Tickets</td>

</tr>
<tr>
<td class=tableb>Purchaseable</td>
<td class=tableb align=right><?= $purchaseable; ?> Tickets</td>
</tr>
</table>
</table>
<hr>
<?
if ($purchaseable > 0)
{
?>
<center>
<form method="post" action="purchasetickets.php">
Purchase <input type="text" name="number"> Tickets <input type="submit" value="Purchase">
</form>
</center>
<?
}
else if (get_date_time() > $arr_config['end_date'])
{
?>
<center><h1><font color = "red">Lottery is closed!</font></h1>
<?
}
?>
</td></tr></table>

<? print("<br>\n");

stdfoot();
die;

?>


create viewtickets.php and put this



<?

require "include/bittorrent.php";


dbconn();

loggedinorreturn();

stdhead("Tickets Sold");

$res = mysql_query("SELECT * FROM lottery_config") or sqlerr(__FILE__, __LINE__);
while ($arr = mysql_fetch_assoc($res))
$arr_config[$arr['name']] = $arr['value'];

$endday = $arr_config['end_date'];

if (!$arr_config["enable"])
stderr("Sorry", "Lottery is disabled.");

print("iMT Lottery Ends: <b>" . $endday . "</b><br /><br />");

?>
<table border="1" width="600" cellpadding="5">
<tr>
<td class="tabletitle">#</td>
<td class="tabletitle">Username</td>
<td class="tabletitle">Number of tickets</td>
<td class="tabletitle">Uploaded</td>
<td class="tabletitle">Downloaded</td>
<td class="tabletitle">Ratio</td>
</tr>
<?
$sql = mysql_query("SELECT user FROM tickets") or die (mysql_error());
while ($myrow = mysql_fetch_assoc($sql))
$user[] = $myrow["user"];
$user = array_values(array_unique($user));
for ($i = 0; $i < sizeof($user); $i++)
{
$tickets[] = mysql_num_rows(mysql_query("SELECT * FROM tickets WHERE user=$user[$i]"));
$username[] = end(mysql_fetch_row(mysql_query("SELECT username FROM users WHERE id=$user[$i]")));
$id[] = end(mysql_fetch_row(mysql_query("SELECT id FROM users WHERE id=$user[$i]")));
$uploaded[] = mksize(end(mysql_fetch_row(mysql_query("SELECT uploaded FROM users WHERE id=$user[$i]"))));
$downloaded[] = mksize(end(mysql_fetch_row(mysql_query("SELECT downloaded FROM users WHERE id=$user[$i]"))));
$ratio[] = end(mysql_fetch_row(mysql_query("SELECT downloaded FROM users WHERE id=$user[$i]")));
$ratio2[] = end(mysql_fetch_row(mysql_query("SELECT uploaded FROM users WHERE id=$user[$i]")));
echo "<tr><td class=tableb>" . ($i + 1) . "</td><td class=tableb><a href=userdetails.php?id=$id[$i]>$username[$i]</a></td><td class=tableb>$tickets[$i]</td><td class=tableb>$uploaded[$i]</td><td class=tableb>$downloaded[$i]</td><td class=tableb>" . number_format($ratio2[$i] / $ratio[$i], 3) . "</td></tr>";
}
?>
</table>
<?

stdfoot();
die;
?>



That code requires a lot of work, html is crap, php and mysql is horrible lol, anyhoo that's your problem now lol.