public function parseKills($kills)
 {
     $oldkills = 0;
     $newkills = 0;
     $errors = 0;
     $lastID = 0;
     $lastIntID = 0;
     foreach ($kills as $kill) {
         try {
             // this needs to be run before exit of loop, otherwise having all kills of this run
             // will cause the lastID not being updated
             if (!is_null($kill->killID) && $kill->killID > 0) {
                 $lastID = $kill->killID;
             }
             if (!is_null(@$kill->killInternalID) && @$kill->killInternalID > 0) {
                 $lastIntID = $kill->killInternalID;
             }
             /*if(!is_null(Kingboard_Kill::getByKillId($kill->killID)))
               {
                   $oldkills++;
                   continue;
               }*/
             $killdata = array("killID" => $kill->killID, "solarSystemID" => $kill->solarSystemID, "location" => array("solarSystem" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->itemName, "security" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->security, "region" => Kingboard_EveSolarSystem::getBySolarSystemId($kill->solarSystemID)->Region['itemName']), "killTime" => new MongoDate(strtotime($kill->killTime)), "moonID" => $kill->moonID, "victim" => array("characterID" => $this->ensureCharacterID($kill->victim->characterID, $kill->victim->characterName), "characterName" => $kill->victim->characterName, "corporationID" => $this->ensureCorporationID($kill->victim->corporationID, $kill->victim->corporationName), "corporationName" => $kill->victim->corporationName, "allianceID" => (int) $kill->victim->allianceID, "allianceName" => $kill->victim->allianceName, "factionID" => (int) $kill->victim->factionID, "factionName" => $kill->victim->factionName, "damageTaken" => $kill->victim->damageTaken, "shipTypeID" => (int) $kill->victim->shipTypeID, "shipType" => Kingboard_EveItem::getByItemId($kill->victim->shipTypeID)->typeName));
             $killdata['attackers'] = array();
             foreach ($kill->attackers as $attacker) {
                 $killdata['attackers'][] = array("characterID" => $attacker->characterID, "characterName" => $attacker->characterName, "entityType" => Kingboard_Helper_EntityType::getEntityTypeByEntityId((int) $attacker->characterID), "corporationID" => $this->ensureCorporationID($attacker->corporationID, $attacker->corporationName), "corporationName" => $attacker->corporationName, "allianceID" => (int) $attacker->allianceID, "allianceName" => $attacker->allianceName, "factionID" => (int) $attacker->factionID, "factionName" => $attacker->factionName, "securityStatus" => $attacker->securityStatus, "damageDone" => $attacker->damageDone, "finalBlow" => $attacker->finalBlow, "weaponTypeID" => (int) $attacker->weaponTypeID, "weaponType" => Kingboard_EveItem::getByItemId($attacker->weaponTypeID)->typeName, "shipTypeID" => (int) $attacker->shipTypeID, "shipType" => Kingboard_EveItem::getByItemId($attacker->shipTypeID)->typeName);
             }
             $killdata['items'] = array();
             if (isset($kill->items) && !is_null($kill->items)) {
                 foreach ($kill->items as $item) {
                     $killdata['items'][] = $this->ParseItem($item);
                 }
             }
             $hash = Kingboard_KillmailHash_IdHash::getByData($killdata);
             $killdata['idHash'] = (string) $hash;
             if (is_null(Kingboard_Kill::getInstanceByIdHash($killdata['idHash']))) {
                 $killObject = new Kingboard_Kill();
                 $killObject->injectDataFromMail($killdata);
                 $killObject->save();
                 $newkills++;
             } else {
                 $oldkills++;
             }
         } catch (Kingboard_KillmailParser_KillmailErrorException $e) {
             $errors++;
         }
     }
     return array('oldkills' => $oldkills, 'newkills' => $newkills, 'lastID' => $lastID, 'lastIntID' => $lastIntID, 'errors' => $errors);
 }
 public static function generateBattle(Kingboard_BattleSettings $battleSetting)
 {
     $okills = array();
     $olosses = array();
     $kills = Kingboard_Kill::find(array("killTime" => array('$gt' => $battleSetting->startdate, '$lt' => $battleSetting->enddate), "location.solarSystem" => $battleSetting->system, '$or' => array(array("attackers.corporationID" => array('$in' => array_keys($battleSetting->positives))), array("attackers.allianceID" => array('$in' => array_keys($battleSetting->positives)))), 'victim.corporationID' => array('$nin' => array_keys($battleSetting->positives)), 'victim.allianceID' => array('$nin' => array_keys($battleSetting->positives))));
     $losses = Kingboard_Kill::find(array("killTime" => array('$gt' => $battleSetting->startdate, '$lt' => $battleSetting->enddate), "location.solarSystem" => $battleSetting->system, '$or' => array(array("victim.corporationID" => array('$in' => array_keys($battleSetting->positives))), array("victim.allianceID" => array('$in' => array_keys($battleSetting->positives))))));
     $timeline = array();
     foreach ($kills as $kill) {
         $killTime = date("Y-m-d H:i:s", $kill->killTime->sec);
         if (!isset($timeline[$killTime])) {
             $timeline[$killTime] = array("kills" => array(), "losses" => array());
         }
         $timeline[$killTime]['kills'][] = $kill->toArray();
         $okills[] = $kill->toArray();
     }
     foreach ($losses as $kill) {
         $killTime = date("Y-m-d H:i:s", $kill->killTime->sec);
         if (!isset($timeline[$killTime])) {
             $timeline[$killTime] = array("kills" => array(), "losses" => array());
         }
         $timeline[$killTime]['losses'][] = $kill->toArray();
         $olosses[] = $kill->toArray();
     }
     ksort($timeline);
     return array("kills" => $okills, "losses" => $olosses, "timeline" => $timeline);
 }
 public function nameCorporation(array $params)
 {
     if (!empty($params['corpname'])) {
         $id = Kingboard_Kill::getCorporationIdFromName($params['corpname']);
         $this->redirect("/corporation/{$id}/");
         return;
     }
     die('unknown character');
 }
 public function alliance($request)
 {
     if (!empty($request['hid'])) {
         $count = Kingboard_Kill::find(array('attackers.allianceID' => (int) $request['hid']))->count();
         $killdata = Kingboard_Kill::find(array('attackers.allianceID' => (int) $request['hid']))->sort(array('killTime' => -1))->limit(20);
         $lossdata = Kingboard_Kill::find(array('victim.allianceID' => (int) $request['hid']))->sort(array('killTime' => -1))->limit(20);
         $killstats = Kingboard_Kill_MapReduce_KillsByShipByAlliance::mapReduceKills((int) $request['hid']);
         $lossstats = Kingboard_Kill_MapReduce_KillsByShipByAlliance::mapReduceLosses((int) $request['hid']);
         $template = "alliance_home.html";
         $info = Kingboard_Kill::getAllianceInfoFromId($request['hid']);
         return $this->render($template, array('killdata' => $killdata, 'lossdata' => $lossdata, 'count' => $count, 'killstats' => $killstats, 'lossstats' => $lossstats, 'info' => $info));
     } else {
         die('no alliance id specified');
     }
 }
 public function victims(array $parameters)
 {
     $term = $_GET['term'];
     // at least 4 characters here or that will get quite slow
     if (strlen($term) < 4) {
         echo json_encode(array());
         return;
     }
     $result = Kingboard_Kill::find(array('victim.characterName' => new MongoRegex('/^' . $term . '.*/i')), array('victim.characterName' => 1))->limit(50);
     $names = array();
     foreach ($result as $res) {
         $names[$res['victim']['characterName']] = true;
     }
     $names = array_keys($names);
     echo json_encode($names);
 }
 public function index($request)
 {
     $context = array();
     $context['killdata'] = Kingboard_Kill::getInstanceByIdHash($request['killID']);
     foreach ($context['killdata']['attackers'] as $attacker) {
         if (!isset($stats[$attacker['allianceName']])) {
             $stats[$attacker['allianceName']] = array();
         }
         if (!isset($stats[$attacker['allianceName']][$attacker['corporationName']])) {
             $stats[$attacker['allianceName']][$attacker['corporationName']] = 0;
         }
         $stats[$attacker['allianceName']][$attacker['corporationName']]++;
     }
     ksort($stats);
     $context['stats'] = $stats;
     return $this->render('kill.html', $context);
 }
 /**
  * Find an ID in the kill collections for name of type key
  *
  * @throws Kingboard_KillmailParser_KillmailErrorException
  * @param string $name
  * @param string $key
  * @return integer
  */
 protected function queryKillCollections($name, $key)
 {
     $keyName = $key . 'Name';
     $keyId = $key . 'ID';
     $result = Kingboard_Kill::findOne(array('$or' => array(array('victim.' . $keyName => $name), array('attackers.' . $keyName => $name))));
     if (!empty($kill['victim'][$keyId]) && $result['victim'][$keyName] == $name) {
         return (int) $kill['victim'][$keyId];
     } else {
         if (is_array($result['attackers'])) {
             foreach ($result['attackers'] as $attacker) {
                 if ($attacker[$keyName] == $name) {
                     return (int) $attacker[$keyId];
                 }
             }
         }
     }
     throw new Kingboard_KillmailParser_KillmailErrorException('No result for typeName ' . $name . ' in the kills collection');
 }
 public function testIfCollisionCanOccurWithTheCurrentIdHash()
 {
     // Comment out the next linge to enable the test
     $this->markTestSkipped('Not part of the build process');
     $found = array();
     $errors = array();
     $allKills = Kingboard_Kill::find();
     foreach ($allKills as $kill) {
         $hash = new Kingboard_KillmailHash_IdHash();
         $hash->setVictimId((int) (empty($kill['victim']['characterID']) ? $kill['victim']['corporationID'] : $kill['victim']['characterID']));
         $hash->setTime(new MongoDate(strtotime(str_replace('.', '-', $kill['killTime']))));
         $hash->setVictimShip($kill['victim']['shipTypeID']);
         foreach ($kill['attackers'] as $attacker) {
             $hash->pushAttackerData($attacker);
         }
         foreach ($kill['items'] as $item) {
             $hash->addItem($item);
         }
         try {
             $hash = $hash->generateHash();
             if (!isset($found[$hash])) {
                 $found[$hash] = array();
             }
             $found[$hash][] = $kill['killID'];
         } catch (Kingboard_KillmailHash_ErrorException $e) {
             $errors[] = "Error {$e->getMessage()}: at kill {$kill['killID']}\n";
         }
     }
     if (!empty($errors)) {
         echo implode('', $errors) . "\n";
     }
     $collisions = false;
     foreach ($found as $hash => $foundOnes) {
         if (count($foundOnes) > 1) {
             echo "Hash '{$hash}' generated for this kills: " . implode(', ', $foundOnes) . "\n";
             $collisions = true;
         }
     }
     if ($collisions) {
         $this->fail('Collisions detected');
     }
 }
 /**
  * Get the data model, create one if none set
  * It does not save a new one or any changes!
  *
  * @return Kingboard_Kill
  */
 public function getModel()
 {
     $model = Kingboard_Kill::getInstanceByIdHash($this->getIdHash());
     if (!$model) {
         $model = new Kingboard_Kill();
     }
     $model->injectDataFromMail($this->getDataArray());
     return $model;
 }