<?

require "include/bittorrent.php";
dbconn();
$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", "Die Userklasse in der du bist darf diesmal nicht an der Lotterie teilnehmen.");
}

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");

?>


<?
// ANFANG LOTTO
$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 = 'Gewinner';
else
$winners = 'Gewinner';

if (count($who_won) > 1)
$each = ' (Each)';
else
$each = '';
begin_frame("Army of Hell Lotterie",FALSE,"91%");
print'<table width=100% border=0 cellspacing=0 cellpadding=5>';
print'<tr>';
?>
<tr><td class="rowhead">Letzte Lotterie</td><td class="rowhead" align="left"><?=count($who_won)?> <?=$winners?></td></tr>
<tr><td class="rowhead2">Letzter Lotterie<?=$winners?></td><td class="rowhead2" align="left"><?=$lottery_winners?></td></tr>
<tr><td class="rowhead">Letzte gewonnene Menge<?=$each?></td><td class="rowhead" align="left"><?=mksize($arr_config['lottery_winners_amount'])?></td></tr>
<tr><td class="rowhead2">Datum der letzten Lotterie</td><td class="rowhead2" align="left"><?=$arr_config['lottery_winners_time']?></td></tr>
<?
print'</tr>';
print '</table>';
end_frame();
?>


<table border=1 width=600 cellspacing=0 cellpadding=5>
<tr><td class=tabletitle width=600 align=center>Army of Hell Lotterie</td></tr>
<tr><td align=left class=tableb>
<ul>
<li>Es werden und können keine Tickets erstattet werden</li>
<li>Ein Ticket kostet <?= $ticket_amount_display . ' ' . $arr_config['ticket_amount_type']; ?> Der Einsatz wird von deinem Upload abgezogen</li>
<li>Bei Noch zu kaufen steht wieviele Tickets Sie noch kaufen können </li>
<li>Sie können nur so viele Tickets kaufen wie Ihr Upload zulässt.</li>
<li>Die Lotterie endet am: <?= $arr_config["end_date"]; ?></li>
<li>Es wird <?= $arr_config['total_winners']; ?> Sieger geben, Der/Die nach dem zufallsprinzip ausgewählt werden</li>
<li>Jeder Sieger bekommt <?= mksize($pot/$arr_config['total_winners']); ?> Hinzugefügt zu seinem Upload</li>
<li>Der/Die Sieger werden bekannt gegeben, sobald die Lotterie geschlossen ist.</li>
<?
if (!$arr_config["use_prize_fund"])
{
?>
<li>Je mehr Tickets verkauftwerden desto grösser ist der Pot!</li>
<?
}
?>
<li>Deine Ticket Nummer(n): <?= $ticketnumbers; ?></li>
</ul>
Viel Spass und Viel Glück wünscht euch der Army of Hell STAFF!
<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>Pot</td>
<td class=tableb><?= mksize($pot); ?></td>
</tr>
<tr>
<td class=tableb>Insgesamt gekaufte Tickets</td>
<td class=tableb align=right><?= $total; ?> Tickets</td>
</tr>
<tr>
<td class=tableb>Von dir gekaufte Tickets</td>
<td class=tableb align=right><?= $me; ?> Tickets</td>

</tr>
<tr>
<td class=tableb>Noch zu Kaufen</td>
<td class=tableb align=right><?= $purchaseable; ?> Tickets</td>
</tr>
</table>
<tr><td class="tablea"><center><a style="display:block;" href="viewtickets.php"><b>Alle Mitspieler</b></a></td></tr></center>
</table>
<hr>
<?
if ($purchaseable > 0)
{
?>
<center>
<form method="post" action="purchasetickets.php">
Kauf <input type="text" name="number"> Tickets <input type="submit" value="Kaufen">
</form>
</center>
<?
}
else if (get_date_time() > $arr_config['end_date'])
{
?>
<center><h1><font color = "red">Lotterie ist geschlossen!</font></h1>
<?
}
?>
</td></tr></table>

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

stdfoot();
die;

?>