Exemplo n.º 1
0
 /**
  * Performs the work of inserting or updating the row in the database.
  *
  * If the object is new, it inserts it; otherwise an update is performed.
  * All related objects are also updated in this method.
  *
  * @param PropelPDO $con
  * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
  * @throws PropelException
  * @see        save()
  */
 protected function doSave(PropelPDO $con)
 {
     $affectedRows = 0;
     // initialize var to track total num of affected rows
     if (!$this->alreadyInSave) {
         $this->alreadyInSave = true;
         // We call the save method on the following object(s) if they
         // were passed to this object by their corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aCorporation !== null) {
             if ($this->aCorporation->isModified() || $this->aCorporation->isNew()) {
                 $affectedRows += $this->aCorporation->save($con);
             }
             $this->setCorporation($this->aCorporation);
         }
         if ($this->isNew() || $this->isModified()) {
             // persist changes
             if ($this->isNew()) {
                 $this->doInsert($con);
             } else {
                 $this->doUpdate($con);
             }
             $affectedRows += 1;
             $this->resetModified();
         }
         if ($this->playersScheduledForDeletion !== null) {
             if (!$this->playersScheduledForDeletion->isEmpty()) {
                 $pks = array();
                 $pk = $this->getPrimaryKey();
                 foreach ($this->playersScheduledForDeletion->getPrimaryKeys(false) as $remotePk) {
                     $pks[] = array($remotePk, $pk);
                 }
                 PlayerCourtQuery::create()->filterByPrimaryKeys($pks)->delete($con);
                 $this->playersScheduledForDeletion = null;
             }
             foreach ($this->getPlayers() as $player) {
                 if ($player->isModified()) {
                     $player->save($con);
                 }
             }
         } elseif ($this->collPlayers) {
             foreach ($this->collPlayers as $player) {
                 if ($player->isModified()) {
                     $player->save($con);
                 }
             }
         }
         if ($this->playerCourtsScheduledForDeletion !== null) {
             if (!$this->playerCourtsScheduledForDeletion->isEmpty()) {
                 PlayerCourtQuery::create()->filterByPrimaryKeys($this->playerCourtsScheduledForDeletion->getPrimaryKeys(false))->delete($con);
                 $this->playerCourtsScheduledForDeletion = null;
             }
         }
         if ($this->collPlayerCourts !== null) {
             foreach ($this->collPlayerCourts as $referrerFK) {
                 if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                     $affectedRows += $referrerFK->save($con);
                 }
             }
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
 /**
  * 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.";
     }
 }