public function index($request)
 {
     $currentPage = 1;
     if (!empty($request['page'])) {
         $currentPage = (int) preg_replace('/[^0-9]+/', '', $request['page']);
         if ($currentPage < 1) {
             $this->sendErrorAndQuit('Page must be a positive value larger than one');
         }
     }
     $killsPerPage = 20;
     $skip = ($currentPage - 1) * $killsPerPage;
     $count = Kingboard_Kill::count();
     $lastPage = ceil($count / $killsPerPage);
     if ($currentPage > $lastPage) {
         $this->sendErrorAndQuit('Page does not exist');
     }
     $data = Kingboard_Kill::find()->sort(array('killTime' => -1))->skip($skip)->limit($killsPerPage);
     $templateVars = array('data' => $data, 'next' => $skip + $killsPerPage < $count ? $currentPage + 1 : false, 'prev' => $currentPage > 1 ? $currentPage - 1 : false, 'currentPage' => $currentPage, 'lastPage' => $lastPage, 'action' => '/home');
     if (!empty($request['ajax'])) {
         return $this->render('home_killspage.html', $templateVars);
     }
     $stats = Kingboard_Kill_MapReduce_KillsByShip::find();
     $info = array();
     $template = "index.html";
     $stats = $stats->sort(array("value.value" => -1));
     $templateVars['count'] = $count;
     $templateVars['stats'] = $stats;
     $templateVars['info'] = $info;
     $templateVars['reports'] = Kingboard_BattleSettings::find()->limit(20)->sort(array('enddate' => -1));
     return $this->render($template, $templateVars);
 }
 public function create(array $params)
 {
     if (!Kingboard_BattleCreate_Form::validate($_POST)) {
         // @todo handle invalid
         die;
     }
     $user = Kingboard_Auth::getUser();
     list($key, $character) = explode('|', $_POST['character']);
     $key = $user["keys"][$key];
     $pheal = new Pheal($key['apiuserid'], $key['apikey'], 'corp');
     $contacts = $pheal->ContactList(array('characterID' => $character));
     $positives = array();
     foreach ($contacts->corporateContactList as $contact) {
         // accumulate postive standings
         if ($contact->standing > 0) {
             $positives[$contact->contactID] = $contact->contactName;
         }
     }
     // alliance standings override corp standings
     foreach ($contacts->allianceContactList as $contact) {
         if ($contact->standing > 0) {
             $positives[$contact->contactID] = $contact->contactName;
         } else {
             // negative standings, we only need those if corp has positive, but alliance negative
             if (isset($positives[$contact->contactID])) {
                 unset($positives[$contact->contactID]);
             }
         }
     }
     $battleSetting = new Kingboard_BattleSettings();
     $battleSetting->startdate = new MongoDate(strtotime($_POST['startdate']));
     $battleSetting->user = $user->_id;
     $battleSetting->enddate = new MongoDate(strtotime($_POST['enddate']));
     $battleSetting->system = $_POST['system'];
     $battleSetting->key = $key;
     $battleSetting->character = $character;
     $battleSetting->positives = $positives;
     $battleSetting->runs = 0;
     $battleSetting->nextRun = new MongoDate(time());
     $battleSetting->save();
     // we are done here, lets redirect to the battle!
     $this->redirect("/battle/" . $battleSetting->_id);
 }
 /**
  * display a certain battle
  * @param array $parameters
  * @return void
  */
 public function show(array $parameters)
 {
     $battleSetting = Kingboard_BattleSettings::getById($parameters['id']);
     if (is_null($battleSetting)) {
         $this->sendErrorAndQuit("Battle with Id " . $parameters['id'] . " does not exist");
     }
     $battle = Kingboard_Battle::getByBattleSettings($battleSetting);
     //print_r($battle->data);
     $this->_context['battleSetting'] = $battleSetting;
     $this->render("battle.html", $battle->data);
 }