/**
  * checks if this move is valid, throws exception if not valid
  *
  * @param int $id_move
  * @throws NullPointerException
  * @throws ControllerException
  * @return boolean
  */
 public function validateLandMoveByid($id_move)
 {
     $id_move = intval($id_move);
     // check if move is not already over
     $game = ModelGame::getGame($this->id_game);
     $round = $game->getRound();
     $phase = $game->getIdPhase();
     if ($phase > PHASE_LANDMOVE) {
         ++$round;
     }
     $move = ModelLandMove::getLandMove($this->id_game, $id_move);
     $move_round = $move->getRound();
     if ($round != $move_round) {
         throw new ControllerException('Cannot validate move as it is not from the correct round.');
     }
     return $this->validateLandMove($id_move, $round, $move->getSteps(), $move->getUnits());
 }
 private function executeAttack($id_target_area, array $moves)
 {
     // 0. init empty arrays for attacker/defender units
     $units_attacker = array();
     $units_defender = array();
     // 1. get units for defender
     $target_area = ModelGameArea::getGameArea($this->id_game, $id_target_area);
     $id_defender = $target_area->getIdUser();
     $iter = ModelLandUnit::iterator();
     while ($iter->hasNext()) {
         /* @var ModelLandUnit $unit */
         $unit = $iter->next();
         $units_attacker[$unit->getId()] = 0;
         $landUnit_defender = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_defender, $unit->getId());
         $units_defender[$unit->getId()] = $landUnit_defender->getCount();
     }
     // 2. add up all units for attacker (multiple moves possible, check already finished moves from NML-fights)
     // 2.a subtract units from originating country
     $id_attacker = 0;
     foreach ($moves as $id_move) {
         $move = ModelLandMove::getLandMove($this->id_game, $id_move);
         $id_user = $move->getIdUser();
         $id_attacker = $id_user;
         $steps = $move->getSteps();
         $from = reset($steps);
         $units = $move->getUnits();
         foreach ($units as $id_unit => $count) {
             $landUnit_from = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $from, $id_user, $id_unit);
             $landUnit_from->addCount($count * -1);
             $units_attacker[$id_unit] += $count;
         }
     }
     // 3. calculate winner and remaining units
     $attacker_wins = $this->calculateFight($units_attacker, $units_defender);
     // 3.a update defender units
     $iter = ModelLandUnit::iterator();
     while ($iter->hasNext()) {
         /* @var ModelLandUnit $unit */
         $unit = $iter->next();
         $landUnit_defender = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_defender, $unit->getId());
         $landUnit_defender->setCount($units_defender[$unit->getId()]);
     }
     // 4. update target country units (and user if attacker won)
     if ($attacker_wins) {
         // 4.a update attacker units
         $iter = ModelLandUnit::iterator();
         while ($iter->hasNext()) {
             /* @var ModelLandUnit $unit */
             $unit = $iter->next();
             $landUnit_attacker = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_attacker, $unit->getId());
             $landUnit_attacker->setCount($units_attacker[$unit->getId()]);
         }
         // 4.b update country owner
         $area = ModelGameArea::getGameArea($this->id_game, $id_target_area);
         $area->setIdUser($id_attacker);
         // 4.c check ships
         // TODO : implement ship takeover or ship destruction
     }
     // 4. flag all moves as finished
     foreach ($moves as $id_move) {
         $this->finished_moves[] = $id_move;
     }
 }