/**
  * Import zKillboard kills for the selected systems and alliances.
  */
 public function getZkillboard($systems = '')
 {
     // If this is the initial call to the function, retrieve the list of systems from the DB.
     if ($systems == '') {
         $systems_object = Setting::where('key', 'systems')->firstOrFail();
         $systems = $systems_object->value;
     }
     // Convert the comma-seperated string into an array.
     $systems_array = explode(',', $systems);
     // If there are more systems in the list than we want to pull at once, chop off the first X and call this function again.
     while (count($systems_array) > $this->api_system_limit) {
         $this->getZkillboard(implode(',', array_splice($systems_array, 0, $this->api_system_limit)));
     }
     // Retrieve the selected alliances from the database.
     $alliances = Setting::where('key', 'alliances')->firstOrFail();
     // Build the API URL.
     $url = 'https://zkillboard.com/api/xml/losses/no-attackers/' . 'allianceID/' . preg_replace('/\\s+/', '', $alliances->value) . '/' . 'solarSystemID/' . preg_replace('/\\s+/', '', $systems) . '/';
     // Send the request.
     $response = Request::get($url)->addHeader('Accept-Encoding', 'gzip')->addHeader('User-Agent', 'Eve Traders Handbook')->send();
     if (isset($response->body) && strlen($response->body) > 0) {
         $body = simplexml_load_string(gzdecode($response->body));
         $insert_count = 0;
         // Parse the response, inserting the losses into the database.
         foreach ($body->result->rowset->row as $row) {
             // First check whether this kill has not already been recorded.
             $kill = Kill::find($row['killID']);
             if (!isset($kill->killID)) {
                 // Create and save the new kill record.
                 $kill = new Kill();
                 $kill->killID = $row['killID'];
                 $kill->solarSystemID = $row['solarSystemID'];
                 $kill->characterID = $row->victim['characterID'];
                 $kill->characterName = $row->victim['characterName'];
                 $kill->allianceID = $row->victim['allianceID'];
                 $kill->corporationID = $row->victim['corporationID'];
                 $kill->shipTypeID = $row->victim['shipTypeID'];
                 $kill->killTime = $row['killTime'];
                 $kill->save();
                 $insert_count++;
                 // Insert the alliance information into the database unless it already exists.
                 $alliance = Alliance::find($kill->allianceID);
                 if (!isset($alliance->id)) {
                     $alliance = new Alliance();
                     $alliance->id = $kill->allianceID;
                     $alliance->allianceName = $row->victim['allianceName'];
                     $alliance->save();
                 }
                 // Insert the corporation information into the database unless it already exists.
                 $corporation = Corporation::find($kill->corporationID);
                 if (!isset($corporation->id)) {
                     $corporation = new Corporation();
                     $corporation->id = $kill->corporationID;
                     $corporation->corporationName = $row->victim['corporationName'];
                     $corporation->save();
                 }
                 // Insert the ship type that was lost into the database unless it already exists.
                 $ship = Ship::find($kill->shipTypeID);
                 $type = Type::find($kill->shipTypeID);
                 if (!isset($ship->id)) {
                     $ship = new Ship();
                     $ship->id = $kill->shipTypeID;
                     $ship->shipName = $type->typeName;
                     $ship->save();
                 }
                 // Insert the ship loss into the items database as well.
                 if (stristr($ship->shipName, 'Capsule') === FALSE) {
                     $item = new Item();
                     $item->killID = $row['killID'];
                     $item->typeID = $kill->shipTypeID;
                     $item->typeName = $type->typeName;
                     $item->categoryName = $type->group->category['categoryName'];
                     $item->metaGroupName = isset($type->metaType->metaGroup['metaGroupName']) ? $type->metaType->metaGroup['metaGroupName'] : '';
                     $item->allowManufacture = 1;
                     $item->qty = 1;
                     $item->save();
                 }
                 // Add the category to the list of filters available on the site.
                 $filter = Filter::find($type->group->category['categoryID']);
                 if (!isset($filter->categoryID)) {
                     $filter = new Filter();
                     $filter->categoryID = $type->group->category['categoryID'];
                     $filter->categoryName = $type->group->category['categoryName'];
                     $filter->iconID = $type->group->category['iconID'];
                     $filter->save();
                 }
                 // Loop through the items lost in the kill. Insert each one into the items table.
                 if (isset($row->rowset->row)) {
                     foreach ($row->rowset->row as $loss) {
                         $typeID = (int) $loss['typeID'];
                         $item = Item::where('typeID', '=', $typeID)->first();
                         // If this item already exists in the items table, we don't need to re-query all the additional
                         // information, we can just copy it from an existing row.
                         if (isset($item)) {
                             // This type has already been seen. Duplicate the record and save the new instance.
                             $clone = new Item();
                             $clone = $item->replicate();
                             // Update the right killID and quantity, and unset the primary key and date columns.
                             $clone->killID = $row['killID'];
                             $clone->qty = $loss['qtyDropped'] + $loss['qtyDestroyed'];
                             unset($clone->id);
                             unset($clone->created_at);
                             unset($clone->updated_at);
                             // Save the cloned row.
                             $clone->save();
                         } else {
                             // This is a never-before-seen lost item. Create a new row and look up all the related details.
                             $item = new Item();
                             $type = Type::find($typeID);
                             $item->killID = (int) $row['killID'];
                             $item->typeID = $typeID;
                             $item->typeName = $type->typeName;
                             $item->categoryName = $type->group->category['categoryName'];
                             $metaGroupName = isset($type->metaType->metaGroup['metaGroupName']) ? $type->metaType->metaGroup['metaGroupName'] : '';
                             if ($metaGroupName == 'Tech I' || $metaGroupName == '') {
                                 $metaLevel = DB::table('dgmTypeAttributes')->where('typeID', $typeID)->where('attributeID', 633)->first();
                                 if (isset($metaLevel)) {
                                     $metaGroupName = 'Meta ';
                                     $metaGroupName .= isset($metaLevel->valueInt) ? $metaLevel->valueInt : $metaLevel->valueFloat;
                                 }
                             }
                             $item->metaGroupName = $metaGroupName;
                             $blueprint = Type::where('typeName', $type->typeName . ' Blueprint')->count();
                             if ($blueprint > 0) {
                                 $item->allowManufacture = 1;
                             }
                             $item->qty = $loss['qtyDropped'] + $loss['qtyDestroyed'];
                             $item->save();
                             // Add the category to the list of filters available on the site.
                             $filter = Filter::find($type->group->category['categoryID']);
                             if (!isset($filter->categoryID)) {
                                 $filter = new Filter();
                                 $filter->categoryID = $type->group->category['categoryID'];
                                 $filter->categoryName = $type->group->category['categoryName'];
                                 $filter->iconID = $type->group->category['iconID'];
                                 $filter->save();
                             }
                         }
                     }
                 }
             }
         }
         echo "Inserted {$insert_count} new kills! ";
     } else {
         echo "No response received from zKillboard API.";
     }
 }
 /**
  * Attack a Mob.
  *
  * This function will auto attack a Mob if possible. After that it may give XP, lose HP, get loot on the current area_id.
  *
  * @param $areas_mobs_id the ID of mob/area combination @see `areas_mobs`.`id`
  * @see /app/models/mob.php battle()
  */
 function game_attack($areas_mobs_id = null)
 {
     if (empty($areas_mobs_id) || !is_numeric($areas_mobs_id)) {
         $this->render('/errors/notfound');
         return false;
     }
     $this->updateGame(array('Character', 'Stat'));
     if ($this->AreasMob->canAttack($areas_mobs_id, $this->characterInfo['id'], $this->characterInfo['area_id'])) {
         $this->AreasMob->contain(array('Mob' => array('Stat')));
         $areasMob = $this->AreasMob->find('first', array('conditions' => array('AreasMob.id' => $areas_mobs_id)));
         $areasMob['Mob']['Stat'] = $this->AreasMob->Mob->makeStats($areasMob['Mob']['Stat']);
         $areasMob['Mob']['Stat']['hp_org'] = $areasMob['Mob']['Stat']['hp'];
         $areasMob['Mob']['battle_key'] = 'mob';
         $currentCharacter = $this->characterInfo;
         $currentCharacter['Stat'] = $this->Session->read('Game.Stat');
         $currentCharacter['Stat']['hp_org'] = $currentCharacter['Stat']['hp'];
         $currentCharacter['battle_key'] = 'you';
         $battleInfo = $this->AreasMob->Mob->battle($currentCharacter, $areasMob['Mob']);
         $loot = array();
         // List of loot (items)
         if ($battleInfo['defender']['Stat']['hp'] <= 0) {
             #	debug($battleInfo);
             // Player wins
             $areasMob['AreasMob']['last_killed'] = date('Y-m-d H:i:s');
             $this->AreasMob->save($areasMob['AreasMob']);
             App::import('Model', 'Kill');
             $Kill = new Kill();
             $someKill['character_id'] = $this->characterInfo['id'];
             $someKill['target_id'] = $areasMob['AreasMob']['id'];
             $someKill['mob_id'] = $areasMob['Mob']['id'];
             $someKill['type'] = 'mob';
             $Kill->save($someKill);
             $addXp = $this->AreasMob->Mob->earnedXp($currentCharacter['Stat']['level'], $areasMob['Mob']['level']);
             $this->Character->addStat('xp', $addXp, $this->characterInfo['id']);
             // Show the loot
             $this->loadModel('Loot');
             $this->Loot->createLootFromMob($areasMob['Mob']['id'], $this->characterInfo['area_id'], $this->characterInfo['id']);
             $this->Loot->contain(array('Item'));
             $loot = $this->Loot->getLoot(array('Loot.character_id' => $this->characterInfo['id'], 'Loot.mob_id' => $areasMob['Mob']['id']));
             $this->loadModel('Quest');
             $this->Quest->update(null, $this->characterInfo['id']);
         } elseif ($battleInfo['attacker']['Stat']['hp'] <= 0) {
             // Player loses
             // @todo reset it?
         }
         // Update the stats
         $addHp = $battleInfo['attacker']['Stat']['hp'] - $this->Session->read('Game.Stat.hp');
         $this->loadModel('Character');
         $this->Character->addStat('hp', $addHp, $this->characterInfo['id']);
         $this->updateGame(array('Character', 'Stat'));
         $this->set('battleInfo', $battleInfo);
         $this->set('loot', $loot);
     } else {
         $this->render('/errors/notfound');
         return false;
     }
 }