Permalink
Browse files

Update how we store count data - make it "resistant" to db-truncations (

#349)

* get pokecountdata in crontabs.include

* add IDE files to .gitigonore

* updatet how counts are stored

* remove from data loader

* make scrutinizer happy

* add default values for all 801 pokemon

* use spawn instead of despawn time
  • Loading branch information...
123FLO321 authored and Obihoernchen committed Feb 27, 2018
1 parent 00d4aed commit b66ee5279d3eee737cb2fe2a8e242a0cc513cc79
View
@@ -9,6 +9,10 @@ ehthumbs.db
Thumbs.db
*~
# IDE files #
#############
.idea/*
# Project config files #
########################
@@ -52,6 +52,9 @@
if (is_file($pokemonstats_file)) {
$pokedatas = json_decode(file_get_contents($pokemonstats_file), true);
}
if (is_file($pokedex_counts_file)) {
$pokecountdatas = json_decode(file_get_contents($pokedex_counts_file), true);
}
if (is_file($pokedex_raids_file)) {
$raiddatas = json_decode(file_get_contents($pokedex_raids_file), true);
}
@@ -3,19 +3,64 @@
// get alltime pokemon counts for pokedex and pokemon pages
$maxpid = $config->system->max_pokemon;
if (is_null($pokecountdatas)) {
$newpokecountdatas = array();
} else {
$newpokecountdatas = $pokecountdatas;
}
$total_pokemon = 0;
$pokedex_counts = new stdClass();
for ($pid = 1; $pid <= $maxpid; $pid++) {
$req = "SELECT COUNT(*) AS pokemon_spawns FROM pokemon WHERE pokemon_id = '".$pid."'";
if (!isset($newpokecountdatas[$pid])) {
$emptyArray = array(
"count" => 0,
"last_update" => 0,
"disappear_time" => null,
"latitude" => null,
"longitude" => null
);
$newpokecountdatas[$pid] = $emptyArray;
}
$last_update = $newpokecountdatas[$pid]['last_update'];
$where = "WHERE p.pokemon_id = '".$pokemon_id."' AND (UNIX_TIMESTAMP(p.disappear_time) - (LENGTH(s.kind) - LENGTH( REPLACE ( kind, \"s\", \"\") )) * 900) > '".$last_update."'";
$req = "SELECT count, (UNIX_TIMESTAMP(p.disappear_time) - (LENGTH(s.kind) - LENGTH( REPLACE ( kind, \"s\", \"\") )) * 900) as last_timestamp, (CONVERT_TZ(p.disappear_time, '+00:00', '".self::$time_offset."')) AS disappear_time_real, p.latitude, p.longitude
FROM pokemon p
JOIN spawnpoint s ON p.spawnpoint_id = s.id
JOIN (
SELECT count(*) as count
FROM pokemon p
JOIN spawnpoint s ON p.spawnpoint_id = s.id
" . $where."
) x
" . $where . "
ORDER BY last_timestamp DESC
LIMIT 0,1";
$result = $mysqli->query($req);
$data = $result->fetch_object();
$pokedex_counts->$pid = (int) $data->pokemon_spawns;
$total_pokemon += $data->pokemon_spawns;
if (isset($data)) {
$count = $data->count;
} else {
$count = 0;
}
if ($count != 0) {
$newpokecountdatas[$pid]['count'] += $count;
$newpokecountdatas[$pid]['last_update'] = $data->last_timestamp;
$newpokecountdatas[$pid]['disappear_time'] = $data->disappear_time_real;
$newpokecountdatas[$pid]['latitude'] = $data->latitude;
$newpokecountdatas[$pid]['longitude'] = $data->longitude;
$total_pokemon += $count;
}
}
// add total count
$pokedex_counts->total = (int) $total_pokemon;
if (!isset($newpokecountdatas["total"])) {
$newpokecountdatas["total"] = 0;
}
$newpokecountdatas["total"] += $total_pokemon;
// Write to file
file_put_contents($pokedex_counts_file, json_encode($pokedex_counts));
file_put_contents($pokedex_counts_file, json_encode($newpokecountdatas));
@@ -3,14 +3,28 @@
// get raid counts since last update for pokemon page
$maxpid = $config->system->max_pokemon;
$newraiddatas = $raiddatas;
if (is_null($raiddatas)) {
$newraiddatas = array();
} else {
$newraiddatas = $raiddatas;
}
for ($pid = 1; $pid <= $maxpid; $pid++) {
// Get count since update
if (isset($raiddatas[$pid]['last_update'])) {
$last_update = $raiddatas[$pid]['last_update'];
} else {
$last_update = 0;
if (!isset($newraiddatas[$pid])) {
$emptyArray = array(
"count" => 0,
"last_update" => 0,
"end_time" => null,
"latitude" => null,
"longitude" => null
);
$newraiddatas[$pid] = $emptyArray;
}
$last_update = $newraiddatas[$pid]['last_update'];
if (!isset($newraiddatas[$pid])) {
$newraiddatas[$pid] = array();
}
$where = "WHERE pokemon_id = '".$pid."' && UNIX_TIMESTAMP(start) > '".$last_update."'";
@@ -40,12 +54,6 @@
$newraiddatas[$pid]['end_time'] = $data->end_time_real;
$newraiddatas[$pid]['latitude'] = $data->latitude;
$newraiddatas[$pid]['longitude'] = $data->longitude;
} elseif (is_null($newraiddatas[$pid]['count'])) {
$newraiddatas[$pid]['count'] = 0;
$newraiddatas[$pid]['last_update'] = 0;
$newraiddatas[$pid]['end_time'] = null;
$newraiddatas[$pid]['latitude'] = null;
$newraiddatas[$pid]['longitude'] = null;
}
}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -152,22 +152,6 @@
$pokemon->spawns_per_day = $pokemon->per_day;
}
// Last seen
$req = "SELECT disappear_time, (CONVERT_TZ(disappear_time, '+00:00', '".$time_offset."')) AS disappear_time_real, latitude, longitude
FROM pokemon
WHERE pokemon_id = '".$pokemon_id."'
ORDER BY disappear_time DESC
LIMIT 0,1";
$result = $mysqli->query($req);
$data = $result->fetch_object();
if (isset($data)) {
$pokemon->last_seen = strtotime($data->disappear_time_real);
$pokemon->last_position = new stdClass();
$pokemon->last_position->latitude = $data->latitude;
$pokemon->last_position->longitude = $data->longitude;
}
// Related Pokemons
// ----------------
@@ -210,8 +210,17 @@
}
}
// Add pokemon counts to array
$pokemon->spawn_count = $pokemon_counts->$pokeid;
// Add raid data to array
$count_data = $pokemon_counts->$pokeid;
if (isset($count_data->disappear_time)) {
$pokemon->last_seen = strtotime($count_data->disappear_time);
$pokemon->last_position = new stdClass();
$pokemon->last_position->latitude = $count_data->latitude;
$pokemon->last_position->longitude = $count_data->longitude;
$pokemon->spawn_count = $count_data->count;
} else {
$pokemon->spawn_count = $count_data->count;
}
// Add raid data to array
$raid_data = $raid_counts->$pokeid;
@@ -220,8 +229,10 @@
$pokemon->last_raid_position = new stdClass();
$pokemon->last_raid_position->latitude = $raid_data->latitude;
$pokemon->last_raid_position->longitude = $raid_data->longitude;
$pokemon->raid_count = $raid_data->count;
} else {
$pokemon->raid_count = 0;
}
$pokemon->raid_count = $raid_data->count;
// Calculate and add rarities to array
$spawn_rate = $pokemons_rarity->$pokeid->rate;
@@ -250,7 +261,11 @@
}
// Add total pokemon count
$pokemons->total = $pokemon_counts->total;
if (isset($pokemon_counts->total)) {
$pokemons->total = $pokemon_counts->total;
} else {
$pokemons->total = 0;
}
// Translate typecolors array keys as well
$types_temp = new stdClass();

0 comments on commit b66ee52

Please sign in to comment.