/** * 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); }
private function parseOptions(array &$data) { $viewData = array(); foreach ($this->possibleStartRegions as $id_option_type => $option_types) { $optionType = ModelOptionType::getOptionType($id_option_type); foreach ($option_types as $option_number => $options) { $optionViewData = array(); $optionViewData['number'] = $option_number; $optionViewData['countrySelectUnitCount'] = $optionType->getUnits(); $optionViewData['countrySelectCount'] = $optionType->getCountries(); $optionViewData['areas'] = array(); foreach ($options as $id_area => $startRegion) { // get area infos $areaModel = ModelArea::getArea($id_area); $area = array(); $area['id_area'] = $id_area; $area['number'] = $areaModel->getNumber(); $area['name'] = $areaModel->getName(); // check if country already selected $gameArea = ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $id_area); $id_zarea = $gameArea->getId(); $modelMove = ModelSelectStartMove::getSelectStartMoveForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); if ($modelMove->checkIfAreaIsSelected($option_number, $id_zarea)) { $area['checked'] = true; } $optionViewData['areas'][] = $area; } $viewData[] = $optionViewData; } } $data['options'] = $viewData; }
/** * 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(); } }