Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //getRaffle.php
- //Polls database for finalized Q4 game results that have not processed as raffle
- //Figures our winners against rnd score card matrix and players
- //Generates Raffle inserts
- //CALCS Payout Winners
- //* * * * * php -f /admin/crons/getRaffles.php
- //Author: Brett Kokinadis
- //Dec 6, 2017. - v.1
- //Global Settings
- include 'config/config.config';
- //Check for closing game
- $getScores = "SELECT GameID, TeamAQ1Score, TeamBQ1Score, TeamAQ2Score, TeamBQ2Score, TeamAQ3Score, TeamBQ3Score, TeamAQ4Score, TeamBQ4Score, Q1ScoreFinal, Q2ScoreFinal, Q3ScoreFinal, Q4ScoreFinal, Q1Processed, Q2Processed, Q3Processed, Q4Processed FROM Games WHERE (Q1ScoreFinal=1 or Q2ScoreFinal=1 or Q3ScoreFinal=1 or Q4ScoreFinal=1) AND (Q1Processed=0 OR Q2Processed=0 OR Q3Processed=0 OR Q4Processed=0) AND BettingClosed=1 AND WinnersProcessed = 0 LIMIT 1";
- //Just for saftey clear qtr calc
- $qtr = 0;
- $scores = $dbc->query($getScores);
- $gid = $scores->fetch_assoc();
- $gameID = $gid["GameID"];
- $q1isFinal = $gid["Q1ScoreFinal"];
- $q2isFinal = $gid["Q2ScoreFinal"];
- $q3isFinal = $gid["Q3ScoreFinal"];
- $q4isFinal = $gid["Q4ScoreFinal"];
- $q1isProcessed = $gid["Q1Processed"];
- $q2isProcessed = $gid["Q2Processed"];
- $q3isProcessed = $gid["Q3Processed"];
- $q4isProcessed = $gid["Q4Processed"];
- // Check if any games returned that need processing and are eligible.
- if ($scores->num_rows === 0) {
- notReady();
- }
- //What QTR are we processing?
- if ($q1isFinal == 1 && $q1isProcessed == 0) {
- $qtr = 1;
- } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 0) {
- $qtr = 2;
- } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q3isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 1 && $q3isProcessed == 0) {
- $qtr = 3;
- } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q3isFinal == 1 && $q4isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 1 && $q3isProcessed == 1 && $q3isProcessed == 1 && $q4isProcessed == 0) {
- $qtr = 4;
- } else {
- $message = "No Quarters to process, or something went wrong with Game ID: " . $gameID ;
- echo $message;
- die();
- }
- //Get Array of last digit of scores for each team at the end of each qtr
- $scoreArray = findScores($gid);
- //Get Game RND Values for Square Card matrix values.
- $getGameCard = "SELECT A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9 FROM GameCards WHERE GameID=" . $gameID . " LIMIT 1";
- //Get the play card x y values that were generated by RND
- $sqrCard = $dbc->query($getGameCard);
- $sqrCard = $sqrCard->fetch_assoc();
- //Get Team A and Team B Qtr score match to SquareMatrix for raffle deterimation
- $winSquare = squareCard($sqrCard, $scoreArray, $qtr, $dbc);
- findRaffleWinner($gameID, $qtr, $winSquare, $dbc, $syslogFile);
- die();
- function findScores($gid)
- {
- // $scores = $scores->fetch_assoc();
- $teamAQ1Score = substr($gid["TeamAQ1Score"], -1);
- $teamBQ1Score = substr($gid["TeamBQ1Score"], -1);
- $teamAQ2Score = substr($gid["TeamAQ2Score"], -1);
- $teamBQ2Score = substr($gid["TeamBQ2Score"], -1);
- $teamAQ3Score = substr($gid["TeamAQ3Score"], -1);
- $teamBQ3Score = substr($gid["TeamBQ3Score"], -1);
- $teamAQ4Score = substr($gid["TeamAQ4Score"], -1);
- $teamBQ4Score = substr($gid["TeamBQ4Score"], -1);
- $scoreArray = array($teamAQ1Score, $teamBQ1Score, $teamAQ2Score, $teamBQ2Score, $teamAQ3Score, $teamBQ3Score, $teamAQ4Score, $teamBQ4Score);
- return $scoreArray;
- }
- //Get Game Card Number Order and compare to team sccore for quarter to compare for winning squares
- function squareCard($sqrCard, $scoreArray, $qtr, $dbc)
- {
- if ($qtr == '1') {
- $teamAScore = $scoreArray[0];
- $teamBScore = $scoreArray[1];
- } elseif ($qtr == '2') {
- $teamAScore = $scoreArray[2];
- $teamBScore = $scoreArray[3];
- } elseif ($qtr == '3') {
- $teamAScore = $scoreArray[4];
- $teamBScore = $scoreArray[5];
- } elseif ($qtr == '4') {
- $teamAScore = $scoreArray[6];
- $teamBScore = $scoreArray[7];
- } else {
- echo 'Error: Qtr score array at function squareCard.';
- die();
- }
- $a0Card = $sqrCard["A0"];
- $a1Card = $sqrCard["A1"];
- $a2Card = $sqrCard["A2"];
- $a3Card = $sqrCard["A3"];
- $a4Card = $sqrCard["A4"];
- $a5Card = $sqrCard["A5"];
- $a6Card = $sqrCard["A6"];
- $a7Card = $sqrCard["A7"];
- $a8Card = $sqrCard["A8"];
- $a9Card = $sqrCard["A9"];
- $b0Card = $sqrCard["B0"];
- $b1Card = $sqrCard["B1"];
- $b2Card = $sqrCard["B2"];
- $b3Card = $sqrCard["B3"];
- $b4Card = $sqrCard["B4"];
- $b5Card = $sqrCard["B5"];
- $b6Card = $sqrCard["B6"];
- $b7Card = $sqrCard["B7"];
- $b8Card = $sqrCard["B8"];
- $b9Card = $sqrCard["B9"];
- //Find RND Game Card Match to SquarePair Matrix for Team A
- if ($teamAScore == $a0Card) {
- $teamAMatch = '0';
- } elseif ($teamAScore == $a1Card) {
- $teamAMatch = '1';
- } elseif ($teamAScore == $a2Card) {
- $teamAMatch = '2';
- } elseif ($teamAScore == $a3Card) {
- $teamAMatch = '3';
- } elseif ($teamAScore == $a4Card) {
- $teamAMatch = '4';
- } elseif ($teamAScore == $a5Card) {
- $teamAMatch = '5';
- } elseif ($teamAScore == $a6Card) {
- $teamAMatch = '6';
- } elseif ($teamAScore == $a7Card) {
- $teamAMatch = '7';
- } elseif ($teamAScore == $a8Card) {
- $teamAMatch = '8';
- } elseif ($teamAScore == $a9Card) {
- $teamAMatch = '9';
- } else {
- echo "Error: No Team A Pair Match";
- die();
- }
- //Find RND Game Card Match to SquarePair Matrix for Team B
- if ($teamBScore == $b0Card) {
- $teamBMatch = '0';
- } elseif ($teamBScore == $b1Card) {
- $teamBMatch = '1';
- } elseif ($teamBScore == $b2Card) {
- $teamBMatch = '2';
- } elseif ($teamBScore == $b3Card) {
- $teamBMatch = '3';
- } elseif ($teamBScore == $b4Card) {
- $teamBMatch = '4';
- } elseif ($teamBScore == $b5Card) {
- $teamBMatch = '5';
- } elseif ($teamBScore == $b6Card) {
- $teamBMatch = '6';
- } elseif ($teamBScore == $b7Card) {
- $teamBMatch = '7';
- } elseif ($teamBScore == $b8Card) {
- $teamBMatch = '8';
- } elseif ($teamBScore == $b9Card) {
- $teamBMatch = '9';
- } else {
- echo "Error: No Team B Pair Match";
- die();
- }
- //Find WinSquare
- $winSquareSql = "SELECT SquareID FROM SquarePair WHERE Awin=$teamAMatch and Bwin=$teamBMatch";
- $wSquare = $dbc->query($winSquareSql);
- $wSquarev = $wSquare->fetch_assoc();
- $winSquare = $wSquarev["SquareID"];
- return ($winSquare);
- }
- //Find purchased square that are winners from RND to SQR Matrix
- function findWinningSquare($teamMatch)
- {
- $getSquarePair = "SELECT SquareID FROM SquarePair where AWin=$teamMatch[0] and BWin=$teamBmatch[1]";
- $getWinSquar = $dbc->query($getSquarePair);
- $winSquare = $row["SquareID"];
- return ($winSquare);
- }
- //Compares finwinningsquare pair to boxbuys table to determine raffel entries
- function findRaffleWinner($gameID, $qtr, $winSquare, $dbc, $syslogFile)
- {
- // need to get team match and squair pair - (((((((((((((((((((((((((())))))))))))))))))))))))))
- $getRaffleWinners = "SELECT BoxBuyID, UserID, UserSession, Qty FROM BoxBuys WHERE GameID = $gameID AND QTR = $qtr AND Square=$winSquare";
- if ($raffleWinners = $dbc->query($getRaffleWinners)) {
- if ($raffleWinners->num_rows === 0) {
- //no raffle winners for the game qtr
- $logmsg = date("Y-m-d H:i:s", time()) . ": Processed GameID: " . $gameID . ' for QTR: ' . $qtr . ' with no raffle winners.' . "\n";
- GameLog($logmsg, $syslogFile);
- markQTRProcessed($dbc, $gameID, $qtr, $syslogFile);
- die();
- }
- /* fetch associative array for Raffle Winners */
- while ($row = $raffleWinners->fetch_assoc()) {
- $BoxBuyID = $row["BoxBuyID"];
- $UserID = $row["UserID"];
- $squareQty = $row["Qty"];
- //Update BoxGuys with Winner
- $updateBoxBuy = "UPDATE BoxBuys Set isRaffelWinner=1 WHERE BoxBuyID=$BoxBuyID AND UserID=$UserID";
- $dbc->query($updateBoxBuy);
- //insert into Winning Raffle drawing for each winner for each qty of winning squares
- $insertqty = 0;
- while ($insertqty < $squareQty):
- $insertqty++;
- $insertRaffleWinners = "INSERT INTO Raffles (RaffleID, GameID, BoxBuyID, isPayoutWinner, notifiedWinner) VALUES ('',$gameID, $BoxBuyID, '', '')";
- $dbc->query($insertRaffleWinners);
- endwhile;
- }
- markQTRProcessed($dbc, $gameID, $qtr);
- //*****NEED NOTIFY ***
- }
- }
- function markQTRProcessed($dbc, $gameID, $qtr, $syslogFile)
- {
- // Set QTR as processed
- $updateQTRProcessed = "UPDATE Games SET Q" . $qtr . "Processed=1 WHERE GameID=$gameID";
- echo $updateQTRProcessed;
- $dbc->query($updateQTRProcessed);
- }
- //No games available, or game is not eligible for processing.
- function notReady()
- {
- echo "Sorry, no games eligible for raffle process.\n\n";
- die();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement