Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. <?php
  2. //getRaffle.php
  3. //Polls database for finalized Q4 game results that have not processed as raffle
  4. //Figures our winners against rnd score card matrix and players
  5. //Generates Raffle inserts
  6. //CALCS Payout Winners
  7. //* * * * * php -f /admin/crons/getRaffles.php
  8.  
  9. //Author: Brett Kokinadis
  10. //Dec 6, 2017. - v.1
  11.  
  12. //Global Settings
  13. include 'config/config.config';
  14.  
  15. //Check for closing game
  16.  
  17. $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";
  18.  
  19. //Just for saftey clear qtr calc
  20. $qtr = 0;
  21.  
  22. $scores = $dbc->query($getScores);
  23.  
  24. $gid = $scores->fetch_assoc();
  25. $gameID = $gid["GameID"];
  26.  
  27. $q1isFinal = $gid["Q1ScoreFinal"];
  28. $q2isFinal = $gid["Q2ScoreFinal"];
  29. $q3isFinal = $gid["Q3ScoreFinal"];
  30. $q4isFinal = $gid["Q4ScoreFinal"];
  31. $q1isProcessed = $gid["Q1Processed"];
  32. $q2isProcessed = $gid["Q2Processed"];
  33. $q3isProcessed = $gid["Q3Processed"];
  34. $q4isProcessed = $gid["Q4Processed"];
  35.  
  36. // Check if any games returned that need processing and are eligible.
  37. if ($scores->num_rows === 0) {
  38. notReady();
  39. }
  40.  
  41. //What QTR are we processing?
  42.  
  43. if ($q1isFinal == 1 && $q1isProcessed == 0) {
  44. $qtr = 1;
  45.  
  46. } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 0) {
  47. $qtr = 2;
  48.  
  49. } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q3isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 1 && $q3isProcessed == 0) {
  50. $qtr = 3;
  51.  
  52. } elseif ($q1isFinal == 1 && $q2isFinal == 1 && $q3isFinal == 1 && $q4isFinal == 1 && $q1isProcessed == 1 && $q2isProcessed == 1 && $q3isProcessed == 1 && $q3isProcessed == 1 && $q4isProcessed == 0) {
  53. $qtr = 4;
  54.  
  55. } else {
  56. $message = "No Quarters to process, or something went wrong with Game ID: " . $gameID ;
  57. echo $message;
  58. die();
  59. }
  60.  
  61. //Get Array of last digit of scores for each team at the end of each qtr
  62. $scoreArray = findScores($gid);
  63.  
  64. //Get Game RND Values for Square Card matrix values.
  65. $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";
  66.  
  67. //Get the play card x y values that were generated by RND
  68. $sqrCard = $dbc->query($getGameCard);
  69.  
  70. $sqrCard = $sqrCard->fetch_assoc();
  71.  
  72. //Get Team A and Team B Qtr score match to SquareMatrix for raffle deterimation
  73.  
  74. $winSquare = squareCard($sqrCard, $scoreArray, $qtr, $dbc);
  75. findRaffleWinner($gameID, $qtr, $winSquare, $dbc, $syslogFile);
  76.  
  77. die();
  78.  
  79. function findScores($gid)
  80. {
  81. // $scores = $scores->fetch_assoc();
  82. $teamAQ1Score = substr($gid["TeamAQ1Score"], -1);
  83. $teamBQ1Score = substr($gid["TeamBQ1Score"], -1);
  84. $teamAQ2Score = substr($gid["TeamAQ2Score"], -1);
  85. $teamBQ2Score = substr($gid["TeamBQ2Score"], -1);
  86. $teamAQ3Score = substr($gid["TeamAQ3Score"], -1);
  87. $teamBQ3Score = substr($gid["TeamBQ3Score"], -1);
  88. $teamAQ4Score = substr($gid["TeamAQ4Score"], -1);
  89. $teamBQ4Score = substr($gid["TeamBQ4Score"], -1);
  90.  
  91. $scoreArray = array($teamAQ1Score, $teamBQ1Score, $teamAQ2Score, $teamBQ2Score, $teamAQ3Score, $teamBQ3Score, $teamAQ4Score, $teamBQ4Score);
  92.  
  93.  
  94. return $scoreArray;
  95. }
  96.  
  97. //Get Game Card Number Order and compare to team sccore for quarter to compare for winning squares
  98. function squareCard($sqrCard, $scoreArray, $qtr, $dbc)
  99. {
  100.  
  101. if ($qtr == '1') {
  102. $teamAScore = $scoreArray[0];
  103. $teamBScore = $scoreArray[1];
  104.  
  105. } elseif ($qtr == '2') {
  106. $teamAScore = $scoreArray[2];
  107. $teamBScore = $scoreArray[3];
  108. } elseif ($qtr == '3') {
  109. $teamAScore = $scoreArray[4];
  110. $teamBScore = $scoreArray[5];
  111. } elseif ($qtr == '4') {
  112. $teamAScore = $scoreArray[6];
  113. $teamBScore = $scoreArray[7];
  114. } else {
  115. echo 'Error: Qtr score array at function squareCard.';
  116. die();
  117. }
  118.  
  119. $a0Card = $sqrCard["A0"];
  120. $a1Card = $sqrCard["A1"];
  121. $a2Card = $sqrCard["A2"];
  122. $a3Card = $sqrCard["A3"];
  123. $a4Card = $sqrCard["A4"];
  124. $a5Card = $sqrCard["A5"];
  125. $a6Card = $sqrCard["A6"];
  126. $a7Card = $sqrCard["A7"];
  127. $a8Card = $sqrCard["A8"];
  128. $a9Card = $sqrCard["A9"];
  129. $b0Card = $sqrCard["B0"];
  130. $b1Card = $sqrCard["B1"];
  131. $b2Card = $sqrCard["B2"];
  132. $b3Card = $sqrCard["B3"];
  133. $b4Card = $sqrCard["B4"];
  134. $b5Card = $sqrCard["B5"];
  135. $b6Card = $sqrCard["B6"];
  136. $b7Card = $sqrCard["B7"];
  137. $b8Card = $sqrCard["B8"];
  138. $b9Card = $sqrCard["B9"];
  139.  
  140. //Find RND Game Card Match to SquarePair Matrix for Team A
  141. if ($teamAScore == $a0Card) {
  142. $teamAMatch = '0';
  143.  
  144. } elseif ($teamAScore == $a1Card) {
  145. $teamAMatch = '1';
  146.  
  147. } elseif ($teamAScore == $a2Card) {
  148. $teamAMatch = '2';
  149.  
  150. } elseif ($teamAScore == $a3Card) {
  151. $teamAMatch = '3';
  152.  
  153. } elseif ($teamAScore == $a4Card) {
  154.  
  155. $teamAMatch = '4';
  156.  
  157. } elseif ($teamAScore == $a5Card) {
  158.  
  159. $teamAMatch = '5';
  160.  
  161. } elseif ($teamAScore == $a6Card) {
  162.  
  163. $teamAMatch = '6';
  164.  
  165. } elseif ($teamAScore == $a7Card) {
  166.  
  167. $teamAMatch = '7';
  168.  
  169. } elseif ($teamAScore == $a8Card) {
  170.  
  171. $teamAMatch = '8';
  172.  
  173. } elseif ($teamAScore == $a9Card) {
  174.  
  175. $teamAMatch = '9';
  176.  
  177. } else {
  178. echo "Error: No Team A Pair Match";
  179. die();
  180. }
  181.  
  182. //Find RND Game Card Match to SquarePair Matrix for Team B
  183. if ($teamBScore == $b0Card) {
  184.  
  185. $teamBMatch = '0';
  186.  
  187. } elseif ($teamBScore == $b1Card) {
  188.  
  189. $teamBMatch = '1';
  190.  
  191. } elseif ($teamBScore == $b2Card) {
  192.  
  193. $teamBMatch = '2';
  194.  
  195. } elseif ($teamBScore == $b3Card) {
  196.  
  197. $teamBMatch = '3';
  198.  
  199. } elseif ($teamBScore == $b4Card) {
  200.  
  201. $teamBMatch = '4';
  202.  
  203. } elseif ($teamBScore == $b5Card) {
  204.  
  205. $teamBMatch = '5';
  206.  
  207. } elseif ($teamBScore == $b6Card) {
  208.  
  209. $teamBMatch = '6';
  210.  
  211. } elseif ($teamBScore == $b7Card) {
  212.  
  213. $teamBMatch = '7';
  214.  
  215. } elseif ($teamBScore == $b8Card) {
  216.  
  217. $teamBMatch = '8';
  218.  
  219. } elseif ($teamBScore == $b9Card) {
  220.  
  221. $teamBMatch = '9';
  222.  
  223. } else {
  224. echo "Error: No Team B Pair Match";
  225. die();
  226. }
  227.  
  228. //Find WinSquare
  229.  
  230. $winSquareSql = "SELECT SquareID FROM SquarePair WHERE Awin=$teamAMatch and Bwin=$teamBMatch";
  231. $wSquare = $dbc->query($winSquareSql);
  232. $wSquarev = $wSquare->fetch_assoc();
  233. $winSquare = $wSquarev["SquareID"];
  234. return ($winSquare);
  235. }
  236.  
  237. //Find purchased square that are winners from RND to SQR Matrix
  238. function findWinningSquare($teamMatch)
  239. {
  240. $getSquarePair = "SELECT SquareID FROM SquarePair where AWin=$teamMatch[0] and BWin=$teamBmatch[1]";
  241. $getWinSquar = $dbc->query($getSquarePair);
  242. $winSquare = $row["SquareID"];
  243.  
  244. return ($winSquare);
  245. }
  246.  
  247. //Compares finwinningsquare pair to boxbuys table to determine raffel entries
  248.  
  249. function findRaffleWinner($gameID, $qtr, $winSquare, $dbc, $syslogFile)
  250. {
  251.  
  252. // need to get team match and squair pair - (((((((((((((((((((((((((())))))))))))))))))))))))))
  253.  
  254. $getRaffleWinners = "SELECT BoxBuyID, UserID, UserSession, Qty FROM BoxBuys WHERE GameID = $gameID AND QTR = $qtr AND Square=$winSquare";
  255.  
  256. if ($raffleWinners = $dbc->query($getRaffleWinners)) {
  257. if ($raffleWinners->num_rows === 0) {
  258. //no raffle winners for the game qtr
  259. $logmsg = date("Y-m-d H:i:s", time()) . ": Processed GameID: " . $gameID . ' for QTR: ' . $qtr . ' with no raffle winners.' . "\n";
  260. GameLog($logmsg, $syslogFile);
  261. markQTRProcessed($dbc, $gameID, $qtr, $syslogFile);
  262. die();
  263. }
  264. /* fetch associative array for Raffle Winners */
  265. while ($row = $raffleWinners->fetch_assoc()) {
  266.  
  267. $BoxBuyID = $row["BoxBuyID"];
  268.  
  269. $UserID = $row["UserID"];
  270.  
  271. $squareQty = $row["Qty"];
  272.  
  273. //Update BoxGuys with Winner
  274. $updateBoxBuy = "UPDATE BoxBuys Set isRaffelWinner=1 WHERE BoxBuyID=$BoxBuyID AND UserID=$UserID";
  275. $dbc->query($updateBoxBuy);
  276.  
  277. //insert into Winning Raffle drawing for each winner for each qty of winning squares
  278.  
  279. $insertqty = 0;
  280. while ($insertqty < $squareQty):
  281. $insertqty++;
  282. $insertRaffleWinners = "INSERT INTO Raffles (RaffleID, GameID, BoxBuyID, isPayoutWinner, notifiedWinner) VALUES ('',$gameID, $BoxBuyID, '', '')";
  283. $dbc->query($insertRaffleWinners);
  284. endwhile;
  285.  
  286. }
  287. markQTRProcessed($dbc, $gameID, $qtr);
  288. //*****NEED NOTIFY ***
  289. }
  290.  
  291. }
  292.  
  293. function markQTRProcessed($dbc, $gameID, $qtr, $syslogFile)
  294. {
  295. // Set QTR as processed
  296. $updateQTRProcessed = "UPDATE Games SET Q" . $qtr . "Processed=1 WHERE GameID=$gameID";
  297. echo $updateQTRProcessed;
  298. $dbc->query($updateQTRProcessed);
  299. }
  300.  
  301. //No games available, or game is not eligible for processing.
  302. function notReady()
  303. {
  304. echo "Sorry, no games eligible for raffle process.\n\n";
  305. die();
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement