示例#1
0
 public function getLocationsForPlayer($gameId, $intPlayerID, $lat = 0, $lon = 0)
 {
     $arrayLocations = array();
     //Gets all non-spawned locations
     $query = "SELECT game_locations.*, gamefountains.fountain_id, gamefountains.spawn_probability, gamefountains.spawn_rate, gamefountains.max_amount, gamefountains.last_spawned, gamefountains.active FROM (SELECT * FROM locations WHERE game_id = {$gameId}) AS game_locations LEFT JOIN (SELECT * FROM spawnables WHERE game_id = {$gameId}) AS gamespawns ON game_locations.type = gamespawns.type AND game_locations.type_id = gamespawns.type_id LEFT JOIN (SELECT * FROM fountains WHERE game_id = {$gameId}) AS gamefountains ON game_locations.location_id = gamefountains.location_id WHERE game_locations.latitude != '' AND game_locations.longitude != '' AND (spawnable_id IS NULL OR gamespawns.active = 0)";
     $rsLocations = Module::query($query);
     if (mysql_error()) {
         return new returnData(3, NULL, "SQL Error" . mysql_error());
     }
     $query = "SELECT full_quick_travel FROM games WHERE game_id = '{$gameId}'";
     $fqtresult = Module::query($query);
     $fullQuickTravel = mysql_fetch_object($fqtresult)->full_quick_travel == 1 ? true : false;
     while ($location = mysql_fetch_object($rsLocations)) {
         //If location and object it links to meet requirments, add it to the array
         //Does it Exist?
         switch ($location->type) {
             case 'Item':
                 $query = "SELECT icon_media_id FROM items WHERE game_id = {$gameId} AND item_id = {$location->type_id} LIMIT 1";
                 break;
             case 'Node':
                 $query = "SELECT icon_media_id FROM nodes WHERE game_id = {$gameId} AND node_id = {$location->type_id} LIMIT 1";
                 break;
             case 'Npc':
                 $query = "SELECT icon_media_id FROM npcs WHERE game_id = {$gameId} AND npc_id = {$location->type_id} LIMIT 1";
                 break;
             case 'WebPage':
                 $query = "SELECT icon_media_id FROM web_pages WHERE web_page_id = {$location->type_id} LIMIT 1";
                 break;
             case 'AugBubble':
                 $query = "SELECT icon_media_id FROM aug_bubbles WHERE aug_bubble_id = {$location->type_id} LIMIT 1";
                 break;
             case 'PlayerNote':
                 $query = "SELECT public_to_map FROM notes WHERE note_id = {$location->type_id} LIMIT 1";
                 break;
         }
         if ($location->type == 'PlayerNote') {
             $rsObject = Module::query($query);
             $object = @mysql_fetch_object($rsObject);
             if (!$object || $object->public_to_map == 0) {
                 continue;
             }
         } else {
             $rsObject = Module::query($query);
             $object = @mysql_fetch_object($rsObject);
             if (!$object) {
                 continue;
             }
         }
         //Deal with qty (whether empty, or fountain)
         if ($location->fountain_id && $location->active) {
             $secondsOfSpawning = strtotime("now") - strtotime($location->last_spawned);
             while ($secondsOfSpawning > $location->spawn_rate && $location->item_qty < $location->max_amount) {
                 if (rand(0, 100) < $location->spawn_probability) {
                     $location->item_qty++;
                 }
                 $secondsOfSpawning -= $location->spawn_rate;
                 $query = "UPDATE fountains SET last_spawned = now() WHERE fountain_id = " . $location->fountain_id;
                 Module::query($query);
             }
             if ($location->item_qty >= $location->max_amount) {
                 $query = "UPDATE fountains SET last_spawned = now() WHERE fountain_id = " . $location->fountain_id;
                 Module::query($query);
             }
             $query = "UPDATE locations SET item_qty = " . $location->item_qty . " WHERE game_id = {$gameId} AND location_id = " . $location->location_id;
             Module::query($query);
         }
         if ($location->type == 'Item' && $location->item_qty < 1 && $location->item_qty != -1) {
             continue;
         }
         //Does it meet it's requirements?
         if (!$this->objectMeetsRequirements($gameId, $intPlayerID, 'Location', $location->location_id)) {
             continue;
         }
         //Special Case for Notes
         if ($location->type == 'PlayerNote') {
             $query = "SELECT public_to_map, public_to_notebook, owner_id FROM notes WHERE note_id='{$location->type_id}' LIMIT 1";
             $result = Module::query($query);
             $note = mysql_fetch_object($result);
             //If note doesn't exist, or if it is neither public nor owned by the owner, skip it.
             if (!$note || !($note->public_to_map || $note->owner_id == $intPlayerID)) {
                 continue;
             }
             if ($note->public_to_notebook || $note->owner_id == $intPlayerId) {
                 $location->allow_quick_travel = 1;
             }
         }
         //If location's icon is not defined, use the object's icon
         if (!$location->icon_media_id) {
             $objectsIconMediaId = $object->icon_media_id;
             $location->icon_media_id = $objectsIconMediaId;
         }
         $location->delete_when_viewed = 0;
         if ($fullQuickTravel) {
             $location->allow_quick_travel = true;
         }
         //Add it
         $arrayLocations[] = $location;
     }
     //Get all spawned locations (needs separate calculations, as requirements are not associated with each location)
     $query = "SELECT * FROM spawnables WHERE game_id = " . $gameId . " AND active = 1";
     $results = Module::query($query);
     while ($spawnable = mysql_fetch_object($results)) {
         //If spawnable and object it links to meet requirments, add it to the array
         //Does it Exist?
         switch ($spawnable->type) {
             case 'Item':
                 $query = "SELECT name as title, icon_media_id FROM items WHERE game_id = {$gameId} AND item_id = {$spawnable->type_id} LIMIT 1";
                 break;
             case 'Node':
                 $query = "SELECT title, icon_media_id FROM nodes WHERE game_id = {$gameId} AND node_id = {$spawnable->type_id} LIMIT 1";
                 break;
             case 'Npc':
                 $query = "SELECT name as title, icon_media_id FROM npcs WHERE game_id = {$gameId} AND npc_id = {$spawnable->type_id} LIMIT 1";
                 break;
             case 'WebPage':
                 $query = "SELECT name as title, icon_media_id FROM web_pages WHERE web_page_id = {$spawnable->type_id} LIMIT 1";
                 break;
             case 'AugBubble':
                 $query = "SELECT name as title, icon_media_id FROM aug_bubbles WHERE aug_bubble_id = {$spawnable->type_id} LIMIT 1";
                 break;
             case 'PlayerNote':
                 $query = "SELECT public_to_map FROM notes WHERE note_id = {$spawnable->type_id} LIMIT 1";
                 break;
         }
         $rsObject = Module::query($query);
         $object = @mysql_fetch_object($rsObject);
         if (!$object) {
             continue;
         }
         $spawnable->icon_media_id = $object->icon_media_id;
         $spawnable->title = $object->title;
         //Does it meet it's requirements?
         if (!$this->objectMeetsRequirements($gameId, $intPlayerID, 'Spawnable', $spawnable->spawnable_id)) {
             continue;
         } else {
         }
         //Create spawnables
         if ($spawnable->location_bound_type == 'PLAYER') {
             if ($lat == 0 && $lon == 0) {
                 //Find player location from log and set lat and lon accordingly
                 $query = "SELECT event_detail_1, event_detail_2 FROM player_log WHERE player_id = {$intPlayerID} AND (game_id = {$gameId} OR game_id = 0) AND event_type = 'MOVE' AND deleted = 0 ORDER BY timestamp DESC LIMIT 1";
                 $result = Module::query($query);
                 if ($obj = mysql_fetch_object($result)) {
                     $lat = $obj->event_detail_1;
                     $lon = $obj->event_detail_2;
                 }
             }
         } else {
             if ($spawnable->location_bound_type == 'LOCATION') {
                 $lat = $spawnable->latitude;
                 $lon = $spawnable->longitude;
             }
         }
         if ($spawnable->amount_restriction == 'PER_PLAYER') {
             //Special case for calculating max on a per_player basis with a set spawn location
             if ($spawnable->location_bound_type == 'LOCATION') {
                 $query = "SELECT DISTINCT player_id FROM player_log WHERE game_id = {$gameId}  AND deleted = 0 AND timestamp >= NOW() - INTERVAL 20 MINUTE";
                 $result = Module::query($query);
                 $spawnable->amount *= mysql_num_rows($result);
             }
             $radius = Module::mToDeg($spawnable->max_area);
             $query = "SELECT * FROM locations WHERE game_id = {$gameId} AND type = '" . $spawnable->type . "' AND type_id = " . $spawnable->type_id . " AND latitude < " . ($lat + $radius) . " AND latitude > " . ($lat - $radius) . " AND longitude < " . ($lon + $radius) . " AND longitude > " . ($lon - $radius);
             $result = Module::query($query);
             $numLocs = mysql_num_rows($result);
         } else {
             if ($spawnable->amount_restriction == 'TOTAL') {
                 $query = "SELECT * FROM locations WHERE game_id = {$gameId} AND type = '" . $spawnable->type . "' AND type_id = " . $spawnable->type_id;
                 $result = Module::query($query);
                 $numLocs = mysql_num_rows($result);
             }
         }
         $secondsOfSpawning = strtotime("now") - strtotime($spawnable->last_spawned);
         while ($secondsOfSpawning > $spawnable->spawn_rate && $numLocs < $spawnable->amount) {
             if (rand(0, 100) < $spawnable->spawn_probability) {
                 $numLocs++;
                 $spawnLoc = Module::randomLatLnWithinRadius($lat, $lon, $spawnable->min_area, $spawnable->max_area);
                 $newLat = $spawnLoc->lat;
                 //$lat+Module::mToDeg(((rand(0,100)/50)*$spawnable->max_area)-$spawnable->max_area);
                 $newLon = $spawnLoc->lon;
                 //$lon+Module::mToDeg(((rand(0,100)/50)*$spawnable->max_area)-$spawnable->max_area);
                 Locations::createLocationWithQrCode($gameId, $spawnable->location_name, $spawnable->icon_media_id, $newLat, $newLon, $spawnable->error_range, $spawnable->type, $spawnable->type_id, 1, $spawnable->hidden, $spawnable->force_view, $spawnable->allow_quick_travel, $spawnable->wiggle, $spawnable->show_title, '', 0, "You've incorrectly encountered a spawnable! Weird...");
             }
             $query = "UPDATE spawnables SET last_spawned = now() WHERE spawnable_id = " . $spawnable->spawnable_id;
             Module::query($query);
             $secondsOfSpawning -= $spawnable->spawn_rate;
             if (location_bound_type != 'LOCATION') {
                 $secondsOfSpawning = 0;
             }
             //Only simulate once unless location bound is a location
         }
         if ($numLocs >= $spawnable->amount) {
             $query = "UPDATE spawnables SET last_spawned = now() WHERE spawnable_id = " . $spawnable->spawnable_id;
             Module::query($query);
         }
         //Destroy spawnables
         if ($spawnable->time_to_live != -1) {
             /*$query = "DELETE game_locations, game_qrcodes 
                FROM (SELECT * FROM locations WHERE game_id = {$gameId}) AS game_locations 
                LEFT_JOIN (SELECT * FROM qrcodes WHERE game_id = {$gameId}) AS game_qrcodes ON game_locations.location_id = game_qrcodes.link_id 
                WHERE type = '".$spawnable->type."' AND type_id = ".$spawnable->type_id." AND ((spawnstamp < NOW() - INTERVAL ".$spawnable->time_to_live." SECOND) OR (type = 'Item' AND item_qty = 0))";
               */
             $query = "DELETE locations, qrcodes FROM locations, qrcodes WHERE locations.game_id = {$gameId} AND qrcodes.game_id = {$gameId} AND locations.location_id = qrcodes.link_id AND locations.type = '" . $spawnable->type . "' AND locations.type_id = " . $spawnable->type_id . " AND ((locations.spawnstamp < NOW() - INTERVAL " . $spawnable->time_to_live . " SECOND) OR (locations.type = 'Item' AND locations.item_qty = 0))";
             Module::query($query);
         }
         $query = "SELECT * FROM locations WHERE game_id = {$gameId} AND type = '" . $spawnable->type . "' AND type_id = " . $spawnable->type_id;
         $locresults = Module::query($query);
         while ($locobj = mysql_fetch_object($locresults)) {
             //If location's icon is not defined, use the object's icon
             if (!$locobj->icon_media_id) {
                 $locobj->icon_media_id = $object->icon_media_id;
             }
             $locobj->delete_when_viewed = $spawnable->delete_when_viewed && $spawnable->active;
             //Add it
             if ($locobj->type != 'Item' || ($locobj->item_qty == -1 || $locobj->item_qty > 0)) {
                 $arrayLocations[] = $locobj;
             }
         }
     }
     //Add the others players from this game, making them look like reqular locations
     $playersJSON = Players::getOtherPlayersForGame($gameId, $intPlayerID);
     $playersArray = $playersJSON->data;
     foreach ($playersArray as $player) {
         $tmpPlayerObject = new stdClass();
         $tmpPlayerObject->name = $player->user_name;
         if ($player->display_name) {
             $tmpPlayerObject->name = $player->display_name;
         }
         $tmpPlayerObject->latitude = $player->latitude;
         $tmpPlayerObject->longitude = $player->longitude;
         $tmpPlayerObject->type_id = $player->player_id;
         $tmpPlayerObject->error = "5";
         $tmpPlayerObject->type = "Player";
         $tmpPlayerObject->description = '';
         $tmpPlayerObject->force_view = "0";
         $tmpPlayerObject->hidden = "0";
         $tmpPlayerObject->icon_media_id = "0";
         $tmpPlayerObject->item_qty = "0";
         $tmpPlayerObject->location_id = "0";
         $arrayLocations[] = $tmpPlayerObject;
     }
     return new returnData(0, $arrayLocations);
 }