/**
  * Returns whenever a Character can attack a mob
  *
  * @param int $character_id the ID of the character
  * @param int $area_id the ID of the current location of the character
  * @return boolean
  */
 function canAttack($areas_mobs_id = null, $character_id = null, $area_id = null)
 {
     $someMob = $this->find('first', array('conditions' => array('AreasMob.id' => $areas_mobs_id, 'AreasMob.area_id' => $area_id)));
     if (empty($someMob)) {
         return false;
     }
     // Check if Quest is needed...
     // When player_only = 1 we need to check the killing table if the player can kill this mob again
     if ($someMob['AreasMob']['player_only'] == '1') {
         App::import('Model', 'Kill');
         $Kill = new Kill();
         $lastKill = $Kill->find('first', array('conditions' => array('Kill.character_id' => $character_id, 'Kill.target_id' => $someMob['AreasMob']['id'], 'Kill.type' => 'mob', 'Kill.created >= ' => date('Y-m-d H:i:s', strtotime('-' . $someMob['AreasMob']['spawn_time'] . ' seconds')))));
         if (empty($lastKill)) {
             // Controleer de kill limit. Kill_limit is een limit op de areas_mobs.id en niet op de mob_id.
             if ($someMob['AreasMob']['kill_limit'] > 0) {
                 $amountKills = $Kill->find('count', array('conditions' => array('Kill.character_id' => $character_id, 'Kill.target_id' => $someMob['AreasMob']['id'], 'Kill.type' => 'mob')));
                 if ($someMob['AreasMob']['kill_limit'] > $amountKills) {
                     return true;
                 }
             } else {
                 return true;
             }
         }
     } elseif ($someMob['AreasMob']['last_killed'] < date('Y-m-d H:i:s', strtotime('-' . $someMob['AreasMob']['spawn_time'] . ' seconds'))) {
         if ($someMob['AreasMob']['kill_limit'] > 0) {
             $amountKills = $Kill->find('count', array('conditions' => array('Kill.target_id' => $someMob['AreasMob']['id'], 'Kill.type' => 'mob')));
             if ($someMob['AreasMob']['kill_limit'] < $amountKills) {
                 return true;
             }
         } else {
             return true;
         }
     }
     return false;
 }
 /**
  * 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.";
     }
 }
Example #3
0
 /**
  * Updates a Quest for a Character. If the Quest is completed and turned in
  * the Character may receive some Quest rewards.
  *
  * @param int $quest The ID of the quest
  * @param int $character_id The ID of the Character
  * @param boolean $turnIn Whenever this quest is turning in
  */
 function update($quest_id = null, $character_id = null, $turnIn = false)
 {
     App::import('Model', 'CharactersQuest');
     $CharactersQuest = new CharactersQuest();
     $conditions = array();
     $conditions['CharactersQuest.completed !='] = 'finished';
     if (isset($quest_id)) {
         $conditions['CharactersQuest.quest_id'] = $quest_id;
     }
     if (isset($character_id)) {
         $conditions['CharactersQuest.character_id'] = $character_id;
     }
     $quests = $CharactersQuest->find('all', array('conditions' => array($conditions)));
     if (!empty($quests)) {
         // Inventory, Kill maybe needed
         App::import('Model', 'Inventory');
         $Inventory = new Inventory();
         App::import('Model', 'Kill');
         $Kill = new Kill();
         $charactersQuestsData = array();
         $i = 0;
         foreach ($quests as $quest) {
             $charactersQuestsData[$i]['id'] = $quest['CharactersQuest']['id'];
             // Get the needed things (items, object, whatever) here
             $itemsNeeded = $this->ItemsQuest->find('list', array('conditions' => array('ItemsQuest.quest_id' => $quest['CharactersQuest']['quest_id'], 'ItemsQuest.type' => 'needed'), 'fields' => array('ItemsQuest.item_id', 'ItemsQuest.amount')));
             $mobsNeeded = $this->MobsQuest->find('list', array('conditions' => array('MobsQuest.quest_id' => $quest['CharactersQuest']['quest_id']), 'fields' => array('MobsQuest.mob_id', 'MobsQuest.amount')));
             $itemsRewards = $this->ItemsQuest->find('list', array('conditions' => array('ItemsQuest.quest_id' => $quest['CharactersQuest']['quest_id'], 'ItemsQuest.type' => 'reward'), 'fields' => array('ItemsQuest.item_id', 'ItemsQuest.amount')));
             $statsRewards = $this->QuestsStat->find('list', array('conditions' => array('QuestsStat.quest_id' => $quest['CharactersQuest']['quest_id']), 'fields' => array('QuestsStat.stat_id', 'QuestsStat.amount')));
             $completed = '';
             if (empty($itemsNeeded) && empty($mobsNeeded)) {
                 // Geen items nodig.. Omdat er op het moment niks anders is om te controleren updaten we deze quest...
                 if ($turnIn == true && isset($character_id) && isset($quest_id)) {
                     $charactersQuestsData[$i]['completed'] = 'finished';
                 } else {
                     $charactersQuestsData[$i]['completed'] = 'yes';
                 }
             } else {
                 $completed = 'yes';
                 // Default it's completed.
                 $itemList = array();
                 $mobList = array();
                 foreach ($itemsNeeded as $item_id => $amount) {
                     $itemList[] = $item_id;
                 }
                 foreach ($mobsNeeded as $mob_id => $amount) {
                     $mobList[] = $mob_id;
                 }
                 // Er zijn items nodig voor deze quest..  Kijken of deze al gehaald zijn...
                 // ItemsNeeded: item_id => aantal_needed
                 $characterInventories = $Inventory->find('all', array('conditions' => array('Inventory.character_id' => $quest['CharactersQuest']['character_id'], 'Inventory.item_id' => $itemList), 'fields' => array('Inventory.item_id', 'COUNT(Inventory.item_id) as amount'), 'group' => array('Inventory.item_id')));
                 // Er zijn mobs nodig voor deze quest. Kijken of ze gekilled zijn NADAT de quest is geaccepteerd...
                 $characterKills = $Kill->find('all', array('conditions' => array('Kill.character_id' => $quest['CharactersQuest']['character_id'], 'Kill.mob_id' => $mobList, 'Kill.type' => 'mob', 'Kill.created >=' => $quest['CharactersQuest']['created']), 'fields' => array('Kill.mob_id', 'COUNT(Kill.target_id) as amount'), 'group' => array('Kill.target_id')));
                 // We hebben een lijst met items wat nodig is..
                 // En dit wordt een lijst met wat we hebben...
                 $itemsHave = array();
                 foreach ($characterInventories as $characterInventory) {
                     $itemsHave[$characterInventory['Inventory']['item_id']] = $characterInventory[0]['amount'];
                 }
                 // Opslaan yes/no of de quest compleet is...
                 foreach ($itemsNeeded as $item_id => $amount) {
                     if (!isset($itemsHave[$item_id]) || isset($itemsHave[$item_id]) && $itemsHave[$item_id] < $itemsNeeded[$item_id]) {
                         $completed = 'no';
                     }
                 }
                 // We hebben nu een lijst met mobs die nodig zijn, en een lijst met kills
                 $mobsHave = array();
                 foreach ($characterKills as $characterKill) {
                     $mobsHave[$characterKill['Kill']['mob_id']] = $characterKill[0]['amount'];
                 }
                 // Opslaan yes/no of de quest compleet is...
                 foreach ($mobsNeeded as $mob_id => $amount) {
                     if (!isset($mobsHave[$mob_id]) || isset($mobsHave[$mob_id]) && $mobsHave[$mob_id] < $mobsNeeded[$mob_id]) {
                         $completed = 'no';
                     }
                 }
                 // Maybe the character is turning in the quest...
                 if ($completed == 'yes' && $turnIn == true && isset($character_id) && isset($quest_id)) {
                     $completed = 'finished';
                 }
                 // Als de quest 'finished' is, dan moeten eventuele items uit de inventory verwijderd worden...
                 // Dat kan hier.
                 if ($completed == 'finished' && !empty($itemsNeeded)) {
                     foreach ($itemsNeeded as $item_id => $amount) {
                         for ($j = 1; $j <= $amount; $j++) {
                             $Inventory->deleteAll(array('Inventory.character_id' => $quest['CharactersQuest']['character_id'], 'Inventory.item_id' => $item_id));
                         }
                     }
                 }
                 $charactersQuestsData[$i]['completed'] = $completed;
             }
             // En voor het mooi, natuurlijk ook even de rewards geven...
             if ($completed == 'finished' && !empty($itemsRewards)) {
                 App::import('Model', 'Drop');
                 $Drop = new Drop();
                 foreach ($itemsRewards as $item_id => $amount) {
                     for ($j = 1; $j <= $amount; $j++) {
                         $data = array();
                         // Bagid en index opvragen
                         $bagIndex = $Drop->hasFreeSpace($quest['CharactersQuest']['character_id'], $item_id, true);
                         $data['character_id'] = $quest['CharactersQuest']['character_id'];
                         $data['item_id'] = $item_id;
                         $data['index'] = $bagIndex['index'];
                         $data['bag_id'] = $bagIndex['bag_id'];
                         $Inventory->create();
                         $Inventory->save($data);
                     }
                 }
             }
             if ($completed == 'finished' && !empty($statsRewards)) {
                 App::import('Model', 'CharactersStat');
                 $CharactersStat = new CharactersStat();
                 foreach ($statsRewards as $stat_id => $amount) {
                     $statData = array();
                     $statData['character_id'] = $quest['CharactersQuest']['character_id'];
                     // Kijken of deze stat al in de database bestaat
                     $someStat = $CharactersStat->find('first', array('conditions' => array('CharactersStat.stat_id' => $stat_id, 'CharactersStat.character_id' => $quest['CharactersQuest']['character_id'])));
                     if (!empty($someStat)) {
                         $statData['id'] = $someStat['CharactersStat']['id'];
                         $statData['amount'] = $someStat['CharactersStat']['amount'] + $amount;
                     } else {
                         $statData['amount'] = $amount;
                         $statData['stat_id'] = $stat_id;
                     }
                     $CharactersStat->create();
                     $CharactersStat->save($statData);
                 }
             }
             $i++;
         }
         $CharactersQuest->saveAll($charactersQuestsData);
     }
 }