/** * user selected areas * * @param $id_set int * @param $option_number int * @param $areas array(int id_area) * @throws ControllerException * @return void */ public function selectStartAreas($id_set, $option_number, $areas) { if ($this->error) { return; } // check if user has already finished this move if ($this->checkIfDone()) { $this->error = true; throw new ControllerException('You have already finished this move.'); } // check if regions are in the set option $supported_areas = ModelStartRegion::getRegionsForSetAndOption($id_set, $option_number); foreach ($areas as $id_area) { if (!isset($supported_areas[$id_area])) { $this->error = true; throw new ControllerException('The area ' . $id_area . ' is not in your set and option-number.'); } } // check if correct number of areas selected $startRegion = array_shift($supported_areas); $id_option_type = $startRegion->getIdOptionType(); $optionType = ModelOptionType::getOptionType($id_option_type); if ($optionType->getCountries() !== count($areas)) { $this->error = true; throw new ControllerException('Please choose the correct amount of areas.'); } // insert $move = ModelSelectStartMove::getSelectStartMoveForUser($this->id_user, $this->id_game); $zareas = array(); foreach ($areas as $id_area) { $gameArea = ModelGameArea::getGameAreaForArea($this->id_game, $id_area); $zareas[] = $gameArea->getId(); } $move->setRegions($option_number, $zareas); }
/** * 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(); } }