<commit_before><?php
require_once("module.php");
require_once("players.php");
require_once("qrcodes.php");


class Locations extends Module
{

	/**
     * Fetch all location in a game
     *
     * @param integer $intGameID The game identifier
     * @return returnData
     * @returns a returnData object containing an array of locations
     * @see returnData
     */
	public function getLocations($intGameID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		
		$query = "SELECT * FROM {$prefix}_locations";
		$rsResult = @mysql_query($query);
		
		if (mysql_error()) return new returnData(3, NULL, "SQL Error");
		return new returnData(0, $rsResult);	
	}
	
	
	/**
     * Fetch all locations in a game with matching QR Code information
     *
     * @param integer $intGameID The game identifier
     * @return returnData
     * @returns a returnData object containing an array of locations with the QR code record id and code
     * @see returnData
     */
	public function getLocationsWithQrCode($intGameID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		
		$query = "SELECT {$prefix}_locations.*,{$prefix}_qrcodes.qrcode_id,{$prefix}_qrcodes.code
					FROM {$prefix}_locations JOIN {$prefix}_qrcodes
					ON {$prefix}_qrcodes.link_id = {$prefix}_locations.location_id
					WHERE {$prefix}_qrcodes.link_type = 'Location'";
		NetDebug::trace($query);	

		$rsResult = @mysql_query($query);
		NetDebug::trace(mysql_error());	
		
		if (mysql_error()) return new returnData(3, NULL, "SQL Error");
		return new returnData(0, $rsResult);	
	}	
	
	/**
     * Fetch locations with fulfilled requirements and other player positions
     *
     * @param integer $intGameID The game identifier
     * @param integer $intPlayerID The player identifier
     * @return returnData
     * @returns a returnData object containing an array of locations
     * @see returnData
     */
	public function getLocationsForPlayer($intGameID, $intPlayerID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		$query = "SELECT * FROM {$prefix}_locations 
				WHERE latitude != '' AND longitude != ''
				AND (type != 'Item' OR (item_qty IS NULL OR item_qty != 0))
				";
		$rsLocations = @mysql_query($query);
		if (mysql_error()) return new returnData(3, NULL, "SQL Error" . mysql_error());
		
		$arrayLocations = array();
		
		while ($location = mysql_fetch_object($rsLocations)) {			
			//If location and object it links to meet requirments, add it to the array
			NetDebug::trace('Location ' . $location->location_id . ' Found. Checking Reqs');	

			if ($this->objectMeetsRequirements ($prefix, $intPlayerID, 'Location', $location->location_id)
				AND
				$this->objectMeetsRequirements ($prefix, $intPlayerID, $location->type, $location->type_id)) {
				
					NetDebug::trace('Reqs Met. Adding to Result');	

					//If location's icon is not defined, use the object's icon
					if (!$location->icon_media_id) {
						switch ($location->type) {
							case 'Item':
								$query = "SELECT icon_media_id FROM {$prefix}_items WHERE item_id = {$location->type_id}";
								break;
							case 'Node':
								$query = "SELECT icon_media_id FROM {$prefix}_nodes WHERE node_id = {$location->type_id}";
								break;
							case 'Npc':
								$query = "SELECT icon_media_id FROM {$prefix}_npcs WHERE npc_id = {$location->type_id}";
								break;
						}
						$rsObject = @mysql_query($query);
						NetDebug::trace(mysql_error());	
						$object = @mysql_fetch_object($rsObject);
						NetDebug::trace(mysql_error());	
						$objectsIconMediaId = $object->icon_media_id;
						$location->icon_media_id = $objectsIconMediaId;
						NetDebug::trace("Icon media changed to {$location->icon_media_id}");	

					}
					
					//Add it
					$arrayLocations[] = $location;

			}
			else NetDebug::trace('Reqs Failed. Moving On');	
		}
		
		//Add the others players from this game, making them look like reqular locations
		$playersJSON = Players::getOtherPlayersForGame($intGameID, $intPlayerID);
		$playersArray = $playersJSON->data;
		
		foreach ($playersArray as $player) {
			NetDebug::trace("adding player: " . $player->user_name );	
			
			$tmpPlayerObject = new stdClass();

      		$tmpPlayerObject->name = $player->user_name;
			$tmpPlayerObject->latitude = $player->latitude;
      		$tmpPlayerObject->longitude = $player->longitude;
      		$tmpPlayerObject->type_id = $player->player_id;
      		
      		$tmpPlayerObject->error = "5";
      		$tmpPlayerObject->type = "Player";
      		
			$tmpPlayerObject->description = '';<commit_msg>locations are only returned when they point to a valid object<commit_after><?php
require_once("module.php");
require_once("players.php");
require_once("qrcodes.php");


class Locations extends Module
{

	/**
     * Fetch all location in a game
     *
     * @param integer $intGameID The game identifier
     * @return returnData
     * @returns a returnData object containing an array of locations
     * @see returnData
     */
	public function getLocations($intGameID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		
		$query = "SELECT * FROM {$prefix}_locations";
		$rsResult = @mysql_query($query);
		
		if (mysql_error()) return new returnData(3, NULL, "SQL Error");
		return new returnData(0, $rsResult);	
	}
	
	
	/**
     * Fetch all locations in a game with matching QR Code information
     *
     * @param integer $intGameID The game identifier
     * @return returnData
     * @returns a returnData object containing an array of locations with the QR code record id and code
     * @see returnData
     */
	public function getLocationsWithQrCode($intGameID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		
		$query = "SELECT {$prefix}_locations.*,{$prefix}_qrcodes.qrcode_id,{$prefix}_qrcodes.code
					FROM {$prefix}_locations JOIN {$prefix}_qrcodes
					ON {$prefix}_qrcodes.link_id = {$prefix}_locations.location_id
					WHERE {$prefix}_qrcodes.link_type = 'Location'";
		NetDebug::trace($query);	

		$rsResult = @mysql_query($query);
		NetDebug::trace(mysql_error());	
		
		if (mysql_error()) return new returnData(3, NULL, "SQL Error");
		return new returnData(0, $rsResult);	
	}	
	
	/**
     * Fetch locations with fulfilled requirements and other player positions
     *
     * @param integer $intGameID The game identifier
     * @param integer $intPlayerID The player identifier
     * @return returnData
     * @returns a returnData object containing an array of locations
     * @see returnData
     */
	public function getLocationsForPlayer($intGameID, $intPlayerID)
	{
		$prefix = Module::getPrefix($intGameID);
		if (!$prefix) return new returnData(1, NULL, "invalid game id");
		
		$query = "SELECT * FROM {$prefix}_locations 
				WHERE latitude != '' AND longitude != ''
				AND (type != 'Item' OR (item_qty IS NULL OR item_qty != 0))
				";
		$rsLocations = @mysql_query($query);
		if (mysql_error()) return new returnData(3, NULL, "SQL Error" . mysql_error());
		
		$arrayLocations = array();
		
		while ($location = mysql_fetch_object($rsLocations)) {			
			//If location and object it links to meet requirments, add it to the array
			NetDebug::trace('Location ' . $location->location_id . ' Found. Checking Reqs');	

			//Does it Exist?
			switch ($location->type) {
				case 'Item':
					$query = "SELECT icon_media_id FROM {$prefix}_items WHERE item_id = {$location->type_id}";
					break;
				case 'Node':
					$query = "SELECT icon_media_id FROM {$prefix}_nodes WHERE node_id = {$location->type_id}";
					break;
				case 'Npc':
					$query = "SELECT icon_media_id FROM {$prefix}_npcs WHERE npc_id = {$location->type_id}";
					break;
			}
			
			$rsObject = @mysql_query($query);
			$object = @mysql_fetch_object($rsObject);
			
			if (!$object) {
				NetDebug::trace("Skipping Location:{$location->location_id} becasue it points to something bogus");	
				continue;
			}

			//Does it meet it's requirements?
			if (!$this->objectMeetsRequirements ($prefix, $intPlayerID, 'Location', $location->location_id)
				OR
				!$this->objectMeetsRequirements ($prefix, $intPlayerID, $location->type, $location->type_id)) {
			
				NetDebug::trace("Skipping Location:{$location->location_id} becasue it doesn't meet it's requirements");
				continue;
			}
				
			
			NetDebug::trace('Location:{$location->location_id} is ok');	

			//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;
			}
			
			//Add it
			$arrayLocations[] = $location;

		}
		
		//Add the others players from this game, making them look like reqular locations
		$playersJSON = Players::getOtherPlayersForGame($intGameID, $intPlayerID);
		$playersArray = $playersJSON->data;
		
		foreach ($playersArray as $player) {
			NetDebug::trace("adding player: " . $player->user_name );	
			
			$tmpPlayerObject = new stdClass();

      		$tmpPlayerObject->name = $player->user_name;
			$tmpPlayerObject->latitude = $player->latitude;
      		$tmpPlayerObject->longitude = $player->longitude;
      		$tmpPlayerObject->type_id = $player->player_id;
      		
      		$tmpPlayerObject->error = "5";
      		$tmpPlayerObject->type = "Player";
      		
			$tmpPlayerObject->description = '';
{'fraction_non_alphanumeric': 0.1302897644361059, 'fraction_numerical': 0.0020846362309776944, 'mean_word_length': 3.577767175572519, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '489603', 'n_tokens_mistral': 3413, 'n_tokens_neox': 2903, 'n_words': 778}