public static function checkCurrentGame()
 {
     // check if a user is logged in
     if (ModelUser::getCurrentUser()->getId() <= 0) {
         unset($_SESSION['id_game']);
         return;
     }
     // check if user selected another game
     if (isset($_POST['select_game'])) {
         $_SESSION['id_game'] = (int) $_POST['select_game'];
     }
     if (!isset($_SESSION['id_game'])) {
         return;
     }
     // check if game exists and is running/started
     try {
         $game = ModelGame::getGame($_SESSION['id_game']);
     } catch (NullPointerException $ex) {
         unset($_SESSION['id_game']);
         return;
     }
     if ($game->getStatus() !== GAME_STATUS_STARTED && $game->getStatus() !== GAME_STATUS_RUNNING) {
         unset($_SESSION['id_game']);
         return;
     }
     // check if user is in this game
     try {
         ModelIsInGameInfo::getIsInGameInfo(ModelUser::getCurrentUser()->getId(), $_SESSION['id_game']);
     } catch (NullPointerException $ex) {
         unset($_SESSION['id_game']);
         return;
     }
     // set current game
     ModelGame::setCurrentGame($_SESSION['id_game']);
 }
 private function showGame(array &$data)
 {
     $gameInfo = array();
     $gameInfo['name'] = $this->game->getName();
     $gameInfo['creator'] = $this->game->getCreator()->getLogin();
     $gameInfo['id'] = $this->game->getId();
     $gameInfo['status'] = $this->game->getStatus();
     $data['game'] = $gameInfo;
     if (isset($_POST['delete'])) {
         $data['delete'] = true;
     }
     if (ModelUser::getCurrentUser() === $this->game->getCreator()) {
         $data['isCreator'] = true;
     }
     $players = array();
     $iter = ModelUser::iterator(STATUS_USER_ALL, $this->game->getId());
     while ($iter->hasNext()) {
         $user = $iter->next();
         $player = array();
         $player['login'] = $user->getLogin();
         $player['color'] = ModelIsInGameInfo::getIsInGameInfo($user->getId(), $this->game->getId())->getColor()->getName();
         $player['id'] = $user->getUserId();
         $players[] = $player;
     }
     $data['game']['player'] = $players;
 }
 /**
  * @param $id_user int
  * @param $id_game int
  * @return array (money => int, countries => int, resproduction => int, traderoutes => int, trproduction => int, combos => int, comboproduction => int, sum => int)
  * @throws NullPointerException
  */
 public static function getCurrentProductionForUserInGame($id_user, $id_game)
 {
     $id_user = (int) $id_user;
     $id_game = (int) $id_game;
     $output = array();
     // money on bank
     $ingame = ModelIsInGameInfo::getIsInGameInfo($id_user, $id_game);
     $output['money'] = $ingame->getMoney();
     $output['moneySpendable'] = min($output['money'], MAX_MONEY_SPENDABLE);
     // money from resources
     $output['countries'] = 0;
     $output['resproduction'] = 0;
     $combos = array();
     $combos[RESOURCE_OIL] = 0;
     $combos[RESOURCE_TRANSPORT] = 0;
     $combos[RESOURCE_INDUSTRY] = 0;
     $combos[RESOURCE_MINERALS] = 0;
     $combos[RESOURCE_POPULATION] = 0;
     $iter = ModelGameArea::iterator($id_user, $id_game);
     while ($iter->hasNext()) {
         $area = $iter->next();
         ++$output['countries'];
         $output['resproduction'] += $area->getProductivity();
         ++$combos[$area->getIdResource()];
     }
     // money from traderoutes
     $output['traderoutes'] = 0;
     $output['trproduction'] = 0;
     // money from combos
     $combo_count = $combos[RESOURCE_OIL];
     foreach ($combos as $res_count) {
         if ($res_count < $combo_count) {
             $combo_count = $res_count;
         }
     }
     $output['combos'] = $combo_count;
     $output['comboproduction'] = $combo_count * 4;
     // sum
     $output['sum'] = $output['moneySpendable'] + $output['resproduction'] + $output['trproduction'] + $output['comboproduction'];
     return $output;
 }
 public function run(array &$data)
 {
     $data['template'] = $this->getTemplate();
     $this->addCurrentGameInfo($data);
     // get Model Data
     /** @var $iig ModelIsInGameInfo */
     $iig = ModelIsInGameInfo::getIsInGameInfo(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     $this->id_set = $iig->getIdStartingSet();
     $this->possibleStartRegions = ModelStartRegion::getRegionsForSet($this->id_set);
     // array(int id_opttype => array(int option_number => array(int id_area => ModelStartRegion)))
     // update moves
     if (isset($_POST['selectstart'])) {
         $this->selectOption($data);
     }
     if (isset($_POST['fixate_start'])) {
         $this->fixateMove($data);
     }
     // parse moves
     $this->checkFixate($data, PHASE_SELECTSTART);
     $this->checkCurrentPhase($data, PHASE_SELECTSTART);
     $this->parseOptions($data);
 }
 /**
  * fixates the move if no error occured
  *
  * @throws ControllerException
  * @return void
  */
 public function finishMove()
 {
     if ($this->error) {
         return;
     }
     // check if every option has taken countries
     $move = ModelSelectStartMove::getSelectStartMoveForUser($this->id_user, $this->id_game);
     $regions_selected = $move->getRegions();
     // array(int option_number => array(int id_zarea))
     // get Model Data
     /** @var $iig ModelIsInGameInfo */
     $iig = ModelIsInGameInfo::getIsInGameInfo($this->id_user, $this->id_game);
     $id_set = $iig->getIdStartingSet();
     $regions = ModelStartRegion::getRegionsForSet($id_set);
     // array(int id_opttype => array(int option_number => array(int id_area => ModelStartRegion)))
     foreach ($regions as $opttype) {
         foreach ($opttype as $opt_number => $areas) {
             if (!isset($regions_selected[$opt_number])) {
                 throw new ControllerException('Choose countries first.');
             }
         }
     }
     $this->fixatePhase(true);
 }
 /**
  * run the game logic
  *
  * @throws LogicException
  * @return void
  */
 public function run()
 {
     if (!$this->checkIfValid()) {
         throw new LogicException('Game ' . $this->id_game . ' not valid for processing.');
     }
     $this->startProcessing();
     try {
         // run through moves for each user
         $iter = ModelSelectStartMove::iterator($this->id_game);
         while ($iter->hasNext()) {
             // areas to select for user
             $selectStartMove = $iter->next();
             $regions_selected = $selectStartMove->getRegions();
             // array(int option_number => array(int id_zarea))
             $id_user = $selectStartMove->getIdUser();
             /* @var $iigi ModelIsInGameInfo */
             $iigi = ModelIsInGameInfo::getIsInGameInfo($id_user, $this->id_game);
             $id_set = $iigi->getIdStartingSet();
             foreach ($regions_selected as $option_number => $areas) {
                 $regions = ModelStartRegion::getRegionsForSetAndOption($id_set, $option_number);
                 // array(int id_area => ModelStartRegion)
                 foreach ($areas as $id_zarea) {
                     $gameArea = ModelGameArea::getGameArea($this->id_game, $id_zarea);
                     $id_area = $gameArea->getIdArea();
                     /* @var $region ModelStartRegion */
                     $region = $regions[$id_area];
                     $id_option = $region->getIdOptionType();
                     $unit_count = ModelOptionType::getOptionType($id_option)->getUnits();
                     // set user for game area
                     $gameArea->setIdUser($id_user);
                     // create units for user
                     $iterUnits = ModelLandUnit::iterator();
                     while ($iterUnits->hasNext()) {
                         $landUnit = $iterUnits->next();
                         $id_unit = $landUnit->getId();
                         $inGameLandUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_zarea, $id_user, $id_unit);
                         $inGameLandUnit->setCount($unit_count);
                     }
                 }
             }
         }
         // add units to all empty game-areas
         $iter = ModelGameArea::iterator(NEUTRAL_COUNTRY, $this->id_game);
         while ($iter->hasNext()) {
             /* @var $gameArea ModelGameArea */
             $gameArea = $iter->next();
             if ($gameArea->getIdType() !== TYPE_LAND) {
                 continue;
             }
             $count = $gameArea->getProductivity();
             if ($gameArea->getIdResource() === RESOURCE_OIL) {
                 ++$count;
             }
             $iterUnits = ModelLandUnit::iterator();
             while ($iterUnits->hasNext()) {
                 $landUnit = $iterUnits->next();
                 $id_unit = $landUnit->getId();
                 $inGameLandUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $gameArea->getId(), NEUTRAL_COUNTRY, $id_unit);
                 $inGameLandUnit->setCount($count);
             }
         }
         $this->finishProcessing();
     } catch (\Exception $ex) {
         $this->logger->fatal($ex);
         $this->rollback();
     }
 }
 private function updateUserMoney()
 {
     foreach ($this->spent_production as $id_user => $spent_production_value) {
         $current_production = UserViewHelper::getCurrentProductionForUserInGame($id_user, $this->id_game);
         $iig = ModelIsInGameInfo::getIsInGameInfo($id_user, $this->id_game);
         $iig->setMoney($current_production['sum'] - $spent_production_value);
     }
 }