Skip to content

Commit

Permalink
Generate random IDs, rather than ordering the whole table by rand(), …
Browse files Browse the repository at this point in the history
…on full table queries.
  • Loading branch information
nerdydrew committed Jul 24, 2016
1 parent 22006b3 commit 2661ac7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
12 changes: 6 additions & 6 deletions random.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ function htmlifyListOfPokemon(generatedPokemonJson) {
output += htmlifyPokemon(generatedPokemon[i]);
}
output += '</ol>';

return output;
}

// Converts JSON for a single Pokémon into an HTML list item.
function htmlifyPokemon(pokemon) {
var title = (pokemon.shiny) ? 'Shiny ' + pokemon.name : pokemon.name;

if (pokemon.sprite) {
var out = '<li title="' + title + '">';
} else {
var out = '<li class="imageless">';
}

if (pokemon.nature) {
out += '<span class="nature">' + pokemon.nature + "</span> ";
}
Expand All @@ -64,8 +64,8 @@ function htmlifyPokemon(pokemon) {
if (pokemon.sprite) {
out += '<div class="wrapper"><img src="' + pokemon.sprite + '" alt="' + title + '" title="' + title + '" /></div>';
}

out += '</li>';

return out;
}
}
33 changes: 16 additions & 17 deletions random.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,28 @@
$paramArray[] = $tier_column . ' = "FE"';
} else {
// We want to query for 2 of the 3 tiers, leaving out either Ubers or NFEs.
if (count($paramArray) == 0) {
// If there are no parameters so far, it's more efficient to take the
// union of two equalities rather than one inequality.
if ($ubers) {
$paramArray[] = '(' . $tier_column . ' = "FE" OR ' . $tier_column . ' = "Uber")';
} else if ($nfes) {
$paramArray[] = '(' . $tier_column . ' = "FE" OR ' . $tier_column . ' = "NFE")';
}
} else {
// If there already are some other parameters, it's more efficient to use
// the index for them remove the unwanted tier by inequality.
if ($ubers) {
$paramArray[] = '(' . $tier_column . ' != "NFE")';
} else if ($nfes) {
$paramArray[] = '(' . $tier_column . ' != "Uber")';
}
if ($ubers) {
$paramArray[] = '(' . $tier_column . ' != "NFE")';
} else if ($nfes) {
$paramArray[] = '(' . $tier_column . ' != "Uber")';
}
}
$parameters = (count($paramArray) > 0) ? "WHERE " . implode(" AND ", $paramArray) : "";

// Connect to the database and execute the query.
$connection = new mysqli($sql_host, $sql_username, $sql_password, $sql_database);
$dbOutput = $connection->query("SELECT id, name FROM dex " . $parameters . " ORDER BY rand() LIMIT $n ");
if ($parameters == "") {
// If we're generating from all Pokemon, it's much more efficient to generate
// IDs and then query them directly, rather than randomizing the whole database.
$max = $connection->query("SELECT COUNT(*) AS count FROM dex")->fetch_object()->count;
$ids_array = generate_distinct_random_numbers(1, $max, $n);
$ids_string = implode(", ", $ids_array);
$sql = "SELECT id, name FROM dex WHERE id IN (" . $ids_string . ")";
} else {
$sql = "SELECT id, name FROM dex " . $parameters . " ORDER BY rand() LIMIT $n";
}

$dbOutput = $connection->query($sql);

// Convert the results to an array.
while($row = $dbOutput->fetch_assoc()) {
Expand Down
18 changes: 15 additions & 3 deletions utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function get_or_set($get_key, $default, $possible_values = null) {
$using_boolean = ($default === true || $default === false);
$validate_from_list = ($possible_values != null);

if (isset($_GET[$get_key]) && (!$validate_from_list || in_array($_GET[$get_key], $possible_values))) {
if ($using_boolean) {
return filter_var($_GET[$get_key], FILTER_VALIDATE_BOOLEAN);
Expand All @@ -31,11 +31,23 @@ function redirect($path) {
$protocol = (isset($_SERVER['HTTPS'])) ? 'https' : 'http';
$path = $protocol . '://' . $_SERVER['HTTP_HOST'] . $path;
}

header('Location: ' . $path);
die();
}

// Most efficient for large ranges ($max-$min) and small $n values.
function generate_distinct_random_numbers($min, $max, $n) {
$numbers = array();
while (count($numbers) < $n) {
$number = mt_rand($min, $max);
if (strrpos($numbers, $number) === false) {
$numbers[] = $number;
}
}
return $numbers;
}


//////// LISTS for data validation, default values, and generation of natures ////////

Expand All @@ -55,4 +67,4 @@ function redirect($path) {
'nfes' => true,
'sprites' => true,
'natures' => false
);
);

0 comments on commit 2661ac7

Please sign in to comment.