Skip to content

Commit

Permalink
MAD rocketmap search
Browse files Browse the repository at this point in the history
  • Loading branch information
jepke committed Feb 12, 2019
1 parent 9d3db90 commit fafa203
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
$libs[] = "search/Search.rdm.php";
$libs[] = "search/Search.monocle_pmsf.php";
$libs[] = "search/Search.monocle_mad.php";
$libs[] = "search/Search.rocketmap_mad.php";
$libs[] = "submit/Submit.php";
$libs[] = "submit/Submit.rdm.php";
$libs[] = "submit/Submit.monocle_pmsf.php";
Expand Down
170 changes: 170 additions & 0 deletions lib/search/Search.rocketmap_mad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace Search;

class RocketMap_MAD extends Search
{
public function search_reward($lat, $lon, $term)
{
global $db, $defaultUnit, $maxSearchResults;

$conds = array();
$params = array();

$params[':lat'] = $lat;
$params[':lon'] = $lon;

$pjson = file_get_contents( 'static/dist/data/pokemon.min.json' );
$prewardsjson = json_decode( $pjson, true );
$presids = array();
foreach($prewardsjson as $p => $preward){
if( $p > 493){
break;
}
if(strpos(strtolower($preward['name']), strtolower($term)) !== false){
$presids[] = $p;
}
}
$ijson = file_get_contents( 'static/dist/data/items.min.json' );
$irewardsjson = json_decode( $ijson, true );
$iresids = [];
foreach($irewardsjson as $i => $ireward){
if(strpos(strtolower($ireward['name']), strtolower($term)) !== false){
$iresids[] = $i;
}
}
if (!empty($presids)) {
$conds[] = "tq.quest_pokemon_id IN (" . implode(',',$presids) . ")";
}
if (!empty($iresids)) {
$conds[] = "tq.quest_item_id IN (" . implode(',',$iresids) . ")";
}
$query = "SELECT p.pokestop_id AS id,
p.name,
p.latitude AS lat,
p.longitude AS lon,
p.image AS url,
tq.quest_type,
tq.quest_pokemon_id,
tq.quest_item_id,
ROUND(( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ),2) AS distance
FROM pokestop p
LEFT JOIN trs_quest tq ON tq.GUID = p.pokestop_id
WHERE :conditions
ORDER BY distance LIMIT " . $maxSearchResults . "";

$query = str_replace(":conditions", join(" OR ", $conds), $query);

$rewards = $db->query($query, $params)->fetchAll(\PDO::FETCH_ASSOC);

$data = array();

foreach($rewards as $reward){
$reward['pokemon_name'] = !empty($reward['pokemon_name']) ? $prewardsjson[$reward['quest_pokemon_id']]['name'] : null;
$reward['quest_pokemon_id'] = intval($reward['quest_pokemon_id']);
$reward['item_name'] = !empty($reward['item_name']) ? $irewardsjson[$reward['quest_item_id']]['name'] : null;
$reward['quest_item_id'] = intval($reward['quest_item_id']);
if($defaultUnit === "km"){
$reward['distance'] = round($reward['distance'] * 1.60934,2);
}
$data[] = $reward;
}
return $data;
}

public function search_nests($lat, $lon, $term)
{
global $manualdb, $defaultUnit, $maxSearchResults;

$json = file_get_contents( 'static/dist/data/pokemon.min.json' );
$mons = json_decode( $json, true );
$resids = [];
foreach($mons as $k => $mon){
if( $k > 386){
break;
}
if(strpos(strtolower($mon['name']), strtolower($term)) !== false){
$resids[] = $k;
} else{
foreach($mon['types'] as $t){
if(strpos(strtolower($t['type']), strtolower($term)) !== false){
$resids[] = $k;
break;
}
}
}
}
if ( $manualdb->info()['driver'] === 'pgsql' ) {
$query = "SELECT nest_id,pokemon_id,lat,lon, ROUND(cast( 3959 * acos( cos( radians(:lat) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( lat ) ) ) as numeric),2) AS distance FROM nests WHERE pokemon_id IN (" . implode(',',$resids) . ") ORDER BY distance LIMIT " . $maxSearchResults . "";
} else{
$query = "SELECT nest_id,pokemon_id,lat,lon, ROUND(( 3959 * acos( cos( radians(:lat) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( lat ) ) ) ),2) AS distance FROM nests WHERE pokemon_id IN (" . implode(',',$resids) . ") ORDER BY distance LIMIT " . $maxSearchResults . "";
}
$data = $manualdb->query($query,[ ':lat' => $lat, ':lon' => $lon])->fetchAll();
foreach($data as $k => $p){
$data[$k]['name'] = $mons[$p['pokemon_id']]['name'];
if($defaultUnit === "km"){
$data[$k]['distance'] = round($data[$k]['distance'] * 1.60934,2);
}
}
return $data;
}

public function search_portals($lat, $lon, $term)
{
global $manualdb, $defaultUnit, $maxSearchResults;

if ( $manualdb->info()['driver'] === 'pgsql' ) {
$query = "SELECT id,external_id,name,lat,lon,url, ROUND(cast( 3959 * acos( cos( radians(:lat) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( lat ) ) ) as numeric),2) AS distance FROM ingress_portals WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
} else {
$query = "SELECT id,name,lat,lon,url, ROUND(( 3959 * acos( cos( radians(:lat) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( lat ) ) ) ),2) AS distance FROM ingress_portals WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
}
$searches = $manualdb->query( $query, [ ':name' => "%" . strtolower( $term ) . "%", ':lat' => $lat, ':lon' => $lon ] )->fetchAll();

$data = array();
$i = 0;

foreach($searches as $search){
$search['url'] = str_replace("http://", "https://images.weserv.nl/?url=", $search['url']);
if($defaultUnit === "km"){
$search['distance'] = round($search['distance'] * 1.60934,2);
}
$data[] = $search;
unset($searches[$i]);
$i++;
}
return $data;
}

public function search($dbname, $lat, $lon, $term)
{
global $db, $defaultUnit, $maxSearchResults;
if ( $dbname === 'pokestop' ) {
if ( $db->info()['driver'] === 'pgsql' ) {
$query = "SELECT pokestop_id,name,latitude AS lat,longitude AS lon,image AS url, ROUND(cast( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) as numeric),2) AS distance FROM " . $dbname . " WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
} else {
$query = "SELECT pokestop_id,name,latitude AS lat,longitude AS lon,image AS url, ROUND(( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ),2) AS distance FROM " . $dbname . " WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
}
} else if ( $dbname === 'gym' ) {
if ( $db->info()['driver'] === 'pgsql' ) {
$query = "SELECT g.gym_id,gd.name,g.latitude AS lat,g.longitude AS lon,gd.url, ROUND(cast( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) as numeric),2) AS distance FROM " . $dbname . " g LEFT JOIN gymdetails gd ON gd.gym_id = g.gym_id WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
} else {
$query = "SELECT g.gym_id,gd.name,g.latitude AS lat,g.longitude AS lon,gd.url, ROUND(( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lon) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ),2) AS distance FROM " . $dbname . " g LEFT JOIN gymdetails gd ON gd.gym_id = g.gym_id WHERE LOWER(name) LIKE :name ORDER BY distance LIMIT " . $maxSearchResults . "";
}
}
$searches = $db->query( $query, [ ':name' => "%" . strtolower( $term ) . "%", ':lat' => $lat, ':lon' => $lon ] )->fetchAll();

$data = array();
$i = 0;

foreach($searches as $search){
$search['url'] = str_replace("http://", "https://images.weserv.nl/?url=", $search['url']);
if($defaultUnit === "km"){
$search['distance'] = round($search['distance'] * 1.60934,2);
}
$data[] = $search;
unset($searches[$i]);
$i++;
}
return $data;
}
}
8 changes: 8 additions & 0 deletions search.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@
}
$search = new \Search\Monocle_MAD();
}
} elseif (strtolower($map) === "rocketmap") {
if ( $action === "pokestops" ) {
$dbname = "pokestop";
} elseif ( $action === "forts" ) {
$dbname = "gym";
}
$search = new \Search\RocketMap_MAD();
}

if ($action === "reward") {
$data["reward"] = $search->search_reward($lat, $lon, $term);
}
Expand Down

0 comments on commit fafa203

Please sign in to comment.