public function ParseAndSaveWorldMap($world_response, $descriptor)
 {
     $log_seq = 0;
     $func_args = func_get_args();
     $func_args[0] = 'Removed Hex Array. See WS Request Log.';
     $func_log_id = DataLoadLogDAO::startFunction($this->db, $this->data_load_id, __CLASS__, __FUNCTION__, $func_args);
     DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'INFO', $descriptor);
     // Get the Hexes section of the world response
     $success = $world_response['responses'][0]['return_value']['success'];
     if ($success != 1) {
         $reason = $world_response['responses'][0]['return_value']['reason'];
         DataLoadLogDAO::completeFunction($this->db, $func_log_id, "Error Getting Map Data: {$reason}", 1);
         // Even though this is technically a failure, we return true because we don't want to retry this request
         return true;
     }
     $world = $world_response['responses'][0]['return_value']['hexes'];
     $hex_count = 0;
     // Sometimes this happens but I'm not sure why... so I created a return code to try again...
     if (empty($world)) {
         DataLoadLogDAO::completeFunction($this->db, $func_log_id, 'No Hexes Found', 1);
         echo "No hexes found.\r\n";
         return false;
     }
     // Parse and store each Hex
     foreach ($world as $key => $hex_arr) {
         $hex_count++;
         // Take the array and create the Hex object
         $hex = Hex::FromJson($hex_arr);
         $hex->data_load_id = $this->data_load_id;
         // Set the world ID
         $hex->world_id = $this->auth->world_id;
         // Recalculate the x coordinate.  Not sure why they store the data this way.
         $hex->hex_x = (int) self::convertToMapCoordinate($hex->hex_x, $hex->hex_y);
         // Check if this hex already exists so we know whether we're inserting or updating
         $hex_exists = WorldMapDAO::checkHexExists($this->db, $hex->world_id, $hex->hex_x, $hex->hex_y);
         if ($hex->building_id) {
             $hex->building_id = BuildingDAO::getLocalIdFromGameId($this->db, $hex->building_id);
         }
         // Determine whether we should use the player ID or NPC ID
         if (isset($hex->player_id)) {
             $game_player_id = $hex->player_id;
         } else {
             if (isset($hex->npc_player_id)) {
                 $game_player_id = $hex->npc_player_id;
             } else {
                 $game_player_id = null;
             }
         }
         // Get the local Player ID from our database
         $player_id = PlayerDAO::getLocalIdFromGameId($this->db, $game_player_id);
         // Start building a new player record
         $player = new Player();
         // Set the local Player ID if we found one
         if ($player_id) {
             $player->id = $player_id;
         }
         // Set the world ID and game player ID
         $player->world_id = $hex->world_id;
         $player->game_player_id = $game_player_id;
         // Initialize flags to NULL
         $hex->is_sb = null;
         $hex->is_npc = null;
         // Handle Alliance Base as a special case
         if ($hex->is_guild_town_center === 1) {
             $hex->town_name = $hex->guild_name;
             $hex->player_name = $hex->guild_name;
             $hex->player_level = $hex->guild_town_phase;
             $hex->building_id = 15;
         }
         // If this is a town tile then we have additional information, so let's process it
         if (isset($hex->town_name) || in_array($hex->building_id, array(14))) {
             $hex->command_center = true;
             // Set base properties
             $hex->is_sb = ($hex->town_radius == 2 and $hex->building_id == 1) ? 1 : 0;
             $hex->is_npc = $hex->town_name === 'Renegade Outpost' ? 1 : 0;
             // Set the player's name and level
             $player->player_name = $hex->player_name;
             $player->level = $hex->player_level;
             $player->data_load_id = $this->data_load_id;
             // Calculate the end of the player's bubble
             if ($hex->immune_until_ts > 0) {
                 $player->immune_until = date('Y-m-d H:i:s', $hex->immune_until_ts);
             } else {
                 $player->immune_until = null;
             }
             // If this player is in a guild, process that information
             if (isset($hex->guild_id)) {
                 // Get the local Guild ID from our database
                 $guild_id = GuildDAO::getLocalIdFromGameId($this->db, $hex->guild_id);
                 // If we didn't find this guild, then start building the record.
                 // Otherwise, don't bother because this information won't change frequently.
                 if (!$guild_id) {
                     $guild = new Guild();
                     $guild->world_id = $hex->world_id;
                     $guild->game_guild_id = $hex->guild_id;
                     $guild->guild_name = $hex->guild_name;
                     $guild->data_load_id = $this->data_load_id;
                     // Insert the guild record into our database and keep the new local ID for later
                     $guild_id = GuildDAO::insertGuild($this->db, $guild);
                     if ($this->db->hasError()) {
                         echo 'Error inserting Guild: ';
                         print_r($this->db->getError());
                         $log_msg = print_r($guild, true) . "\r\n\r\n" . print_r($this->db->getError(), true);
                         DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error inserting Guild [{$guild->guild_name}] into World [{$guild->world_id}]", $log_msg, 1);
                         echo "\r\n";
                     }
                 }
                 // Set the player's guild
                 $player->guild_id = $guild_id;
             }
         }
         // If this player didn't already exist in our database, create it.  Otherwise, update it.
         if (!$player_id) {
             if ($game_player_id) {
                 //DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'DEBUG', "Inserting Player [{$player->player_name}] into World [{$player->world_id}]", var_export($player, true));
                 $player_id = PlayerDAO::insertPlayer($this->db, $player);
                 if ($this->db->hasError()) {
                     echo 'Error inserting Player: ';
                     print_r($this->db->getError());
                     echo "\r\n";
                     $log_msg = print_r($player, true) . "\r\n\r\n" . print_r($this->db->getError(), true);
                     DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error inserting Player [{$player->player_name}] into World [{$player->world_id}]", $log_msg, 1);
                 }
             }
         } else {
             if ($player->player_name) {
                 //DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'DEBUG', "Updating Player [{$player->player_name}] in World [{$player->world_id}]", var_export($player, true));
                 // Update the player, but exclude battle points, power, and the number of bases because these aren't available in this case
                 $updateCount = PlayerDAO::updatePlayer($this->db, $player, array('battle_points', 'glory_points', 'bases'));
                 if ($this->db->hasError()) {
                     echo 'Error updating Player: ';
                     print_r($this->db->getError());
                     echo "\r\n";
                     $log_msg = var_export($player, true) . "\r\n\r\n" . print_r($this->db->getError(), true);
                     DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error updating Player [{$player->player_name}] in World [{$player->world_id}]", $log_msg, 1);
                 }
             }
         }
         // Set the player ID on the hex tile to the local database ID
         if (isset($hex->player_id) || isset($hex->npc_player_id)) {
             $hex->player_id = $player_id;
         }
         // Insert or update the hex record
         if ($hex_exists == false) {
             $hex_id = WorldMapDAO::insertHex($this->db, $hex);
             if ($this->db->hasError()) {
                 echo 'Error inserting Hex: ';
                 print_r($this->db->getError());
                 echo "\r\n";
                 $log_msg = var_export($hex, true) . "\r\n\r\n" . print_r($this->db->getError(), true);
                 DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error inserting hex into World {$hex->world_id}, X: {$hex->hex_x}, Y: {$hex->hex_y}", $log_msg);
             }
         } else {
             $hex_id = WorldMapDAO::updateHex($this->db, $hex);
             if ($this->db->hasError()) {
                 echo 'Error updating Hex: ';
                 print_r($this->db->getError());
                 echo "\r\n";
                 $log_msg = var_export($hex, true) . "\r\n\r\n" . print_r($this->db->getError(), true);
                 DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error updating hex in World {$hex->world_id}, X: {$hex->hex_x}, Y: {$hex->hex_y}", $log_msg, 1);
             }
         }
     }
     DataLoadLogDAO::completeFunction($this->db, $func_log_id, "Created {$hex_count} Hexes");
     echo "Created {$hex_count} Hexes\r\n";
     return true;
 }
 function SaveGuildLeaderboard($leader_data)
 {
     $log_seq = 0;
     $func_args = func_get_args();
     $func_args[0] = 'Removed Hex Array. See WS Request Log.';
     $func_log_id = DataLoadLogDAO::startFunction($this->db, $this->data_load_id, __CLASS__, __FUNCTION__, $func_args);
     $count = 0;
     foreach ($leader_data as $key => $leader) {
         $guild = new Guild();
         $guild->world_id = $this->auth->world_id;
         $guild->data_load_id = $this->data_load_id;
         $guild->game_guild_id = $leader['guild_id'];
         if (array_key_exists('owner_id', $leader) && ($leader_id = PlayerDAO::getLocalIdFromGameId($this->db, $leader['owner_id']))) {
             $guild->leader_id = $leader_id;
         }
         $guild->guild_name = $leader['guild_name'];
         $guild->battle_points = $leader['score'];
         $guild->glory_points = $leader['glory_points'];
         $guild->members = $leader['member_count'];
         $id = GuildDAO::getLocalIdFromGameId($this->db, $guild->game_guild_id);
         if ($id) {
             $guild->id = $id;
             GuildDAO::updateGuild($this->db, $guild);
         } else {
             GuildDAO::insertGuild($this->db, $guild);
         }
         if ($this->db->hasError()) {
             echo 'Error saving guild: ';
             print_r($this->db->getError());
             echo "\r\n";
             $log_msg = var_dump($player) . "\r\n\r\n" . print_r($this->db->getError(), true);
             DataLoadLogDAO::logEvent2($this->db, $func_log_id, $log_seq++, 'ERROR', "Error Saving Guild: World [{$guild->world_id}], Guild: [{$guild->guild_name}]", $log_msg, 1);
         } else {
             $count++;
         }
     }
     DataLoadLogDAO::completeFunction($this->db, $func_log_id, "Saved {$count} Guilds");
 }