Example #1
0
function lotto_createDrawing()
{
    // The usual susglobals. ;)
    global $DB;
    global $MySelf;
    global $TIMEMARK;
    $count = $_POST[count];
    // is Lotto enabled at all?
    if (!getConfig("lotto")) {
        makeNotice("Your CEO disabled the Lotto module, request denied.", "warning", "Lotto Module Offline");
    }
    // Deny access to non-lotto-officials.
    if (!$MySelf->isLottoOfficial()) {
        makeNotice("You are not allowed to do this!", "error", "Permission denied");
    }
    // We only allow boards greater 1 ticket.
    if (!is_numeric($count) && $count < 1) {
        makeNotice("Invalid count for the new drawing!", "error", "Invaid Count", "index.php?action=editLotto", "[Cancel]");
    }
    // Is there already a drawing opened?
    if (lotto_getOpenDrawing()) {
        makeNotice("You can only have one drawing open at the same time!", "error", "Close other drawing", "index.php?action=editLotto", "[Cancel]");
    }
    $DB->query("INSERT INTO lotto (opened,isOpen) VALUES (?,?)", array($TIMEMARK, "1"));
    if ($DB->affectedRows() != 1) {
        makeNotice("Error creating new drawing in database! Inform admin!", "error", "Internal Error", "index.php?action=editLotto", "[Cancel]");
    }
    // Which ID are we now?
    $drawing = lotto_getOpenDrawing();
    // insert tickets!
    for ($i = 1; $i <= $_POST[count]; $i++) {
        $DB->query("INSERT INTO lotteryTickets (ticket, drawing) VALUES ('{$i}', '{$drawing}')");
    }
    makeNotice("Drawing created, have fun!", "notice", "Here you go.", "index.php?action=lotto", "lotto! LOTTO!");
}
Example #2
0
function lotto_editLottery()
{
    // We need some globals
    global $MySelf;
    global $DB;
    $formDisable = "";
    if (lotto_getOpenDrawing()) {
        $formDisable = "disabled";
    }
    // is Lotto enabled at all?
    if (!getConfig("lotto")) {
        makeNotice("Your CEO disabled the Lotto module, request denied.", "warning", "Lotto Module Offline");
    }
    // Deny access to non-lotto-officials.
    if (!$MySelf->isLottoOfficial()) {
        makeNotice("You are not allowed to do this!", "error", "Permission denied");
    }
    $table = new table(2, true);
    $table->addHeader(">> Open new drawing");
    $table->addRow();
    $table->addCol("Number of tickets in draw:");
    $table->addCol("<input type=\"text\" name=\"count\" " . $formDisable . " value=\"30\">");
    //	$newLotto = new table (2);
    $table->addHeaderCentered("<input type=\"submit\" name=\"submit\" " . $formDisable . " value=\"open new drawing\">", array("bold" => true, "colspan" => 2));
    $html = "<h2>Lotto Administration</h2>";
    $html .= "<form action=\"index.php\" method=\"POST\">";
    $html .= "<input type=\"hidden\" name=\"check\" value=\"true\">";
    $html .= "<input type=\"hidden\" name=\"action\" value=\"createDrawing\">";
    $html .= $table->flush();
    $html .= "</form>";
    if (lotto_getOpenDrawing()) {
        $html .= "[<a href=\"index.php?action=drawLotto\">Draw Winner</a>]";
    }
    return $html;
}
Example #3
0
function lotto_claimTicket()
{
    global $DB;
    global $MySelf;
    $LOTTO_MAX_PERCENT = getConfig("lottoPercent");
    if (!getConfig("lotto")) {
        makeNotice("Your CEO disabled the Lotto module, request denied.", "warning", "Lotto Module Offline");
    }
    // Only people with parents consent may play!
    if (!$MySelf->canPlayLotto()) {
        makeNotice("Im sorry, but you are not allowed to play Lotto. " . "Ask your CEO or a friendly Director to enable this for you.", "warning", "Unable to play :(");
    }
    // Ticket ID sane?
    numericCheck($_GET[ticket], 0);
    $ticket = $_GET[ticket];
    // Get the drawing ID.
    $drawing = lotto_getOpenDrawing();
    // Get my credits
    $MyStuff = $DB->getRow("SELECT lottoCredit, lottoCreditsSpent FROM users WHERE id='" . $MySelf->getID() . "'");
    $Credits = $MyStuff[lottoCredit];
    $CreditsSpent = $MyStuff[lottoCreditsSpent];
    // Are we broke?
    if ($Credits < 1) {
        makeNotice("You can not afford the ticket, go get more credits!", "warning", "You're broke!'", "index.php?action=lotto", "[ashamed]");
    }
    // Now check if we bust it.
    $myTickets = lotto_checkRatio($drawing);
    if ($myTickets <= 0) {
        makeNotice("You are already owning the maximum allowed tickets!", "warning", "Exceeded ticket ratio!", "index.php?action=lotto", "[Cancel]");
    }
    // Deduct credit from account.
    $newcount = $Credits - 1;
    $DB->query("UPDATE users SET lottoCredit='{$newcount}' WHERE id='" . $MySelf->getID() . "' LIMIT 1");
    if ($DB->affectedRows() != 1) {
        makeNotice("Internal Error: Problem with your bank account... :(", "error", "Internal Error", "index.php?action=lotto", "[Cancel]");
    }
    // Add to "Spent".
    $spent = $CreditsSpent + 1;
    $DB->query("UPDATE users SET lottoCreditsSpent='{$spent}' WHERE id='" . $MySelf->getID() . "' LIMIT 1");
    if ($DB->affectedRows() != 1) {
        makeNotice("Internal Error: Problem with your bank account... :(", "error", "Internal Error", "index.php?action=lotto", "[Cancel]");
    }
    // Lets check that the ticket is still unclaimed.
    $Ticket = $DB->getCol("SELECT owner FROM lotteryTickets WHERE ticket='{$ticket}' AND drawing='{$drawing}'");
    if ($Ticket[0] >= 0) {
        makeNotice("Im sorry, but someone else was faster that you and already claimed that ticket.", "warning", "Its gone, Jim!", "index.php?action=lotto", "[Damn!]");
    }
    // Give him the ticket.
    $DB->query("UPDATE lotteryTickets SET owner='" . $MySelf->getID() . "' WHERE ticket='{$ticket}' AND drawing='{$drawing}' LIMIT 1");
    if ($DB->affectedRows() == 1) {
        Header("Location: index.php?action=lotto");
    } else {
        makeNotice("Internal Error: Could not grant you the ticket :(", "error", "Internal Error", "index.php?action=lotto", "[Cancel]");
    }
}
Example #4
0
function lotto_draw()
{
    // We need some globals
    global $MySelf;
    global $DB;
    global $TIMEMARK;
    // is Lotto enabled at all?
    if (!getConfig("lotto")) {
        makeNotice("Your CEO disabled the Lotto module, request denied.", "warning", "Lotto Module Offline");
    }
    // Deny access to non-lotto-officials.
    if (!$MySelf->isLottoOfficial()) {
        makeNotice("You are not allowed to do this!", "error", "Permission denied");
    }
    // Database
    $max = lotto_getOpenDrawing();
    // confirm!
    confirm("Do you want to draw the winner for Drawing #{$max} now?");
    // No drawing open!
    if (!$max) {
        makeNotice("There is no open lottery. Open a new one, and try again.", "warning", "No open drawing", "index.php?action=editLotto", "[cancel]");
    }
    // Lock remaining tickets.
    $DB->query("UPDATE lotteryTickets SET owner='-2' WHERE drawing='{$max}' AND owner<'0'");
    // Pick the winner.
    $NrTickets = $DB->getCol("SELECT MAX(ticket) AS max FROM lotteryTickets WHERE drawing='{$max}'");
    $Winner = rand(1, $NrTickets[0]);
    // Set the ticket as "winner":
    $DB->query("UPDATE lotteryTickets SET isWinner='1' WHERE ticket='{$Winner}' AND drawing='{$max}'");
    // Get ID of possible winner:
    $luckyOne = $DB->getCol("SELECT owner FROM lotteryTickets WHERE isWinner='1' AND drawing='{$max}' LIMIT 1");
    $luckyOne = $luckyOne[0];
    // Calculate the potsize.
    $potSize = $DB->getCol("SELECT COUNT(id) AS count FROM lotteryTickets WHERE owner>='0' AND drawing='{$max}'");
    $potSize = $potSize[0] * 1000000;
    $potSizeOld = $potSize;
    // Get the JackPot.
    $jackPot = $DB->getCol("SELECT value FROM config WHERE name='jackpot' LIMIT 1");
    $jackPot = $jackPot[0];
    $potSize = $potSize + $jackPot;
    if ($luckyOne >= 0) {
        // We have a winner!
        $DB->query("UPDATE lotto SET winner='{$luckyOne}' WHERE drawing='{$max}' LIMIT 1");
        // Give him the money.
        $transaction = new transaction($luckyOne, 0, $potSize);
        $transaction->setReason("won the lottery");
        $transaction->commit();
        // Clean up the jackpot.
        $DB->query("DELETE FROM config WHERE name='jackpot' LIMIT 1");
    } else {
        // No winner, unclaimed ticket won :(
        $DB->query("UPDATE lotto SET winner='-1' WHERE drawing='{$max}' LIMIT 1");
        // Add to jackpot.
        $DB->query("DELETE FROM config WHERE name='jackpot' LIMIT 1");
        $DB->query("INSERT INTO config (name, value) VALUES ('jackpot','{$potSize}')");
    }
    $DB->query("UPDATE lotto SET closed='{$TIMEMARK}' WHERE drawing='{$max}' LIMIT 1");
    $DB->query("UPDATE lotto SET isOpen='0' WHERE drawing='{$max}' LIMIT 1");
    $DB->query("UPDATE lotto SET winningTicket='{$Winner}' WHERE drawing='{$max}' LIMIT 1");
    $DB->query("UPDATE lotto SET potSize='{$potSizeOld}' WHERE drawing='{$max}' LIMIT 1");
    header("Location: index.php?action=lotto");
}
Example #5
0
function lotto_playLotto()
{
    // Globals, as usual.
    global $DB;
    global $MySelf;
    $LOTTO_MAX_PERCENT = getConfig("lottoPercent");
    $ID = $MySelf->getID();
    // is Lotto enabled at all?
    if (!getConfig("lotto")) {
        makeNotice("Your CEO disabled the Lotto module, request denied.", "warning", "Lotto Module Offline");
    }
    // Only people with parents consent may play!
    if (!$MySelf->canPlayLotto()) {
        makeNotice("Im sorry, but you are not allowed to play Lotto. " . "Ask your CEO or a friendly Director to enable this for you.", "warning", "Unable to play :(");
    }
    // Get my credits
    $MyStuff = $DB->getRow("SELECT lottoCredit, lottoCreditsSpent FROM users WHERE id='" . $MySelf->getID() . "'");
    $Credits = $MyStuff[lottoCredit];
    $CreditsSpent = $MyStuff[lottoCreditsSpent];
    // Handle empty accounts.
    if ($Credits < 1) {
        $Credits = "None";
    }
    if ($CreditsSpent < 1) {
        $CreditsSpent = "None";
    }
    // My Info Table.
    $MyInfo = new table(2, true);
    $MyInfo->addHeader(">> My lotto assets");
    $MyInfo->addRow();
    $MyInfo->addCol("My balance (tickets):");
    $MyInfo->addCol($Credits);
    $MyInfo->addRow();
    $MyInfo->addCol("Total spent (tickets):");
    $MyInfo->addCol($CreditsSpent);
    $MyInfo->addHeader("Need more credits? <a href=\"index.php?action=buycredits\">Buy them here!</a>");
    // Print resent pots.
    $MyWins = new table(4, true);
    $MyWins->addHeader(">> Recent jackpots");
    $MyWins->addRow("#060622");
    $MyWins->addCol("Drawing");
    $MyWins->addCol("Winner");
    $MyWins->addCol("Winning Ticket");
    $MyWins->addCol("Jackpot");
    $Jackpots = $DB->query("SELECT * FROM lotto WHERE isOpen='0'");
    if ($Jackpots->numRows() >= 1) {
        while ($jp = $Jackpots->fetchRow()) {
            //			$TotalTickets_DS = $DB->Query("SELECT ticket FROM lotteryTickets WHERE drawing='" . $woot[drawing] . "' AND owner >= '0'");
            //			$TotalTickets = $TotalTickets_DS->numRows();
            $MyWins->addRow();
            $MyWins->addCol("<a href=\"index.php?action=lotto&showdrawing=" . $jp[drawing] . "\">#" . str_pad($jp[drawing], 3, "0", STR_PAD_LEFT) . "</a>");
            if ($jp[winner] == "-1") {
                $MyWins->addCol("<i>No one</i>");
            } else {
                $MyWins->addCol(ucfirst(idToUsername($jp[winner])));
            }
            $MyWins->addCol("#" . str_pad($jp[winningTicket], 3, "0", STR_PAD_LEFT));
            $MyWins->addCol(number_format($jp[potSize]) . " ISK");
            $GotWinners = true;
        }
    }
    $drawingID = lotto_getOpenDrawing();
    $drawingID = $drawingID[0];
    // Load the current drawing.
    if (!$_GET[showdrawing]) {
        $drawingID = lotto_getOpenDrawing();
        //$drawingID = $drawingID[0];
    } else {
        numericCheck($_GET[showdrawing], 0);
        $drawingID = $_GET[showdrawing];
    }
    // jackpot! WOOT!
    $Jackpot = $DB->getCol("SELECT value FROM config WHERE name='jackpot' LIMIT 1");
    if ($drawingID != is_null()) {
        $currentBuyin = $DB->getCol("SELECT COUNT(*) FROM lotteryTickets WHERE drawing=" . $drawingID . " AND owner >=0");
        $totalJackpot = $Jackpot[0] + $currentBuyin[0] * 1000000;
    }
    $MyWins->addHeader("The current jackpot is at " . number_format($totalJackpot, 2) . " ISK.");
    //	$MyWins->addHeader("Please contact your lotto officer to claim your prize.");
    // Only do this if we have an open  drawing, doh!
    if ($drawingID != is_null()) {
        $TICKETS = $DB->query("SELECT *  FROM lotteryTickets WHERE drawing = '{$drawingID}' ORDER BY ticket");
        $allowedTickets = lotto_checkRatio($drawingID);
        // Table header
        $drawing = new table(2, true);
        $drawing->addHeader(">> Drawing #{$drawingID}");
        // 1=left side, 0=right side.
        $side = 1;
        while ($ticket = $TICKETS->fetchRow()) {
            $ticketCount++;
            // If we are on the left side, open up a new table row.
            if ($side == 1) {
                $drawing->addRow();
            }
            // Ticket owned already?
            if ($ticket[owner] == -1) {
                if ($Credits >= 1 && $allowedTickets > 0) {
                    $drawing->addCol("<a href=\"index.php?action=claimTicket&drawing={$max}&ticket=" . $ticket[ticket] . "\">#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . "</a> (unclaimed)");
                } else {
                    $drawing->addCol("#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . " (unclaimed)");
                }
            } elseif ($ticket[owner] >= 0) {
                // Increase the chances counter.
                if ($ticket[owner] == $ID) {
                    $chances++;
                }
                if ($ticket[isWinner]) {
                    $drawing->addCol("#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . " (" . idToUsername($ticket[owner]) . ") <font color=\"#00ff00\"><b>WINNER!</b></font>");
                } else {
                    $drawing->addCol("#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . " (" . idToUsername($ticket[owner]) . ")");
                }
            } else {
                if ($ticket[isWinner]) {
                    $drawing->addCol("#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . " (locked) <font color=\"#00ff00\"><b>WINNER!</b></font>");
                } else {
                    $drawing->addCol("#" . str_pad($ticket[ticket], 4, "0", STR_PAD_LEFT) . " (locked)");
                }
            }
            // Toggle sides.
            $side = 1 - $side;
            $AreTickets = true;
        }
        // My Chances
        $winningChance = number_format(100 / ($ticketCount / $chances), 3) . "%";
        // Even the odds ;)
        if ($side == 0) {
            $drawing->addCol("---");
        }
        if ($allowedTickets > 0) {
            $drawing->addHeader("Click on a ticket to buy  it, up to {$allowedTickets} more ({$LOTTO_MAX_PERCENT}%). Your chances of winning are: {$winningChance}");
        } else {
            $drawing->addHeader("You exceeded the maximum allowed tickets ({$LOTTO_MAX_PERCENT}%). Your chances of winning are: {$winningChance}");
        }
    }
    // HTML goodness.
    $html = "<h2>Play Lotto</h2>";
    $html .= $MyInfo->flush() . "<br>";
    if ($GotWinners) {
        $html .= $MyWins->flush() . "<br>";
    }
    // only include ticket table if we have tickets.
    if ($AreTickets) {
        $html .= $drawing->flush();
    }
    // return the page.
    return $html;
}