Skip to content

Instantly share code, notes, and snippets.

@bls1999
Last active July 1, 2020 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bls1999/0f7ae0fd7500f93abbdc28d89a3a5480 to your computer and use it in GitHub Desktop.
Save bls1999/0f7ae0fd7500f93abbdc28d89a3a5480 to your computer and use it in GitHub Desktop.
Winners picker for the time-based raffle program (w/ mybb integration)
<?php
/*
MIT License
Copyright (c) 2020 Ben Schwartz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* * * * *
-- Purpose --
MAIN PROGRAM: https://gist.github.com/bls1999/df10c7021a8d93da187ce2b552825b30
The following script is part of a MyBB-integrated PHP/MySQL raffle program.
It chooses a winner from a list of entrants based on their total entries in the raffle.
The result is returned in JSON "pretty-printed" format.
* * * * *
-- Example MySQL Table Structure --
CREATE TABLE `raffle` (
`raffle_participant_id` int(4) NOT NULL AUTO_INCREMENT,
`user_id` int(9) NOT NULL,
`user_name` varchar(20) NOT NULL,
`last_time` bigint(11) NOT NULL,
`entries` int(6) NOT NULL,
PRIMARY KEY (`raffle_participant_id`),
UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
*/
// db connection function
require_once __DIR__ . '/link/to/db/fn.php';
// explicitly defines the contest host's mybb user ID
define('CONTEST_HOST', 1);
// mybb things
define('IN_MYBB', 1);
require_once __DIR__ . '/path/to/global.php';
// defines prize names
$prizes = ['25M', '10M', '10M', '1M', '1M'];
header('Content-type: text/plain');
$ret = new stdClass();
$ret->success = false;
try {
// check to make sure the user is able to do this
if (!isset($mybb) || $mybb->user['uid'] != CONTEST_HOST) {
throw new Exception('You\'re not the contest host.');
}
// generic function to instantiate a db connection via PDO
$pdo = pdo_connect();
// get all users and the amount of entries they have
$stmt = $pdo->query('SELECT user_id, user_name, entries FROM raffle');
$entries = $stmt->fetchAll(PDO::FETCH_OBJ);
// sanity: did anyone enter?
if (empty($entries)) {
throw new Exception('No one entered in your raffle. Too bad. V sad. :(');
}
// sanity: did not enough people enter?
if (count($entries) < count($prizes)) {
throw new Exception('Not enough people entered your raffle. Big oof. :/');
}
// add to array
$entrants = [];
foreach ($entries as $entry) {
$entrant = new stdClass();
$entrant->user_id = (int) $entry->user_id;
$entrant->user_name = utf8_encode($entry->user_name);
$num = (int) $entry->entries;
for ($i = 0; $i < $num; $i++) {
$entrants[] = $entrant;
}
}
// loop through prizes and assign a winner of each prize
$ret->winners = [];
foreach ($prizes as $prize) {
$winner = $entrants[rand(0, count($entrants) - 1)]; // randomly pick winner
while (in_array($winner, $entrants)) { // remove all instances of winner from array
$key = array_search($winner, $entrants);
array_splice($entrants, $key, 1);
}
$winner->prize = $prize; // assign prize to winner
$ret->winners[] = $winner; // push winner to return array
}
$ret->success = true;
} catch (Exception $e) {
$ret->error = $e->getMessage();
} finally {
die(json_encode($ret, JSON_PRETTY_PRINT));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment