private function validateMoves() { $game = ModelGame::getGame($this->id_game); $round = $game->getRound(); $move_iter = ModelProductionMove::iterator(null, $this->id_game, $round); $controllerForUser = array(); $controller = null; // run through moves while ($move_iter->hasNext()) { /* @var $move ModelProductionMove */ $move = $move_iter->next(); $id_user = $move->getIdUser(); // validate moves if (!isset($controllerForUser[$id_user])) { $controllerForUser[$id_user] = new ProductionController($id_user, $this->id_game); } try { $controller = $controllerForUser[$id_user]; /* @var $controller ProductionController */ $controller->validateProductionMove($move); } catch (ControllerException $ex) { $this->logger->error($ex); $move->flagMoveDeleted(); continue; } // add move to moves $this->moves[] = $move; } }
private function validateNewProductionMove($round, $id_zarea, array $units) { // 1. check if zarea belongs to user $gameArea = ModelGameArea::getGameArea($this->id_game, $id_zarea); if ($this->id_user !== $gameArea->getIdUser()) { throw new ControllerException('Unable to create production move. Area doesn\'t belong to the current user.'); } // 2. check if user has enough res left // 2.a check cost of previous productions $current_costs = 0; $moves = ModelProductionMove::iterator($this->id_user, $this->id_game, $round); while ($moves->hasNext()) { /* @var $move ModelProductionMove */ $move = $moves->next(); $current_costs += $move->getCost(); } // 2.b get available res $current_production = UserViewHelper::getCurrentProductionForUserInGame($this->id_user, $this->id_game); // 2.c get cost of new move $new_cost = 0; $unit_iter = ModelLandUnit::iterator(); while ($unit_iter->hasNext()) { /* @var $unit ModelLandUnit */ $unit = $unit_iter->next(); $id_unit = (int) $unit->getId(); if (!isset($units[$id_unit]) || $units[$id_unit] <= 0) { continue; } $new_cost += (int) $unit->getPrice() * $units[$id_unit]; } if ($current_production['sum'] - $current_costs < $new_cost) { throw new ControllerException('Insufficient funds!'); } }
private function showMoves(array &$data) { // show moves $costsSum = 0; $game = ModelGame::getCurrentGame(); $id_game = $game->getId(); $round = $game->getRound(); $phase = $game->getIdPhase(); if ($phase > PHASE_PRODUCTION) { ++$round; } $moves = ModelProductionMove::iterator(ModelUser::getCurrentUser()->getId(), $id_game, $round); $movesViewData = array(); while ($moves->hasNext()) { /* @var $move ModelProductionMove */ $move = $moves->next(); $moveViewData = array(); $moveViewData['id'] = $move->getIdMove(); $id_zarea = $move->getIdZArea(); $zArea = ModelGameArea::getGameArea((int) $id_game, $id_zarea); $area = ModelArea::getArea((int) $zArea->getIdArea()); $moveViewData['area'] = array('number' => $area->getNumber(), 'name' => $area->getName()); $units = $move->getUnits(); $unit_iter = ModelLandUnit::iterator(); $unitsViewData = array(); while ($unit_iter->hasNext()) { /* @var $unit ModelLandUnit */ $unit = $unit_iter->next(); $id_unit = (int) $unit->getId(); if (!isset($units[$id_unit]) || $units[$id_unit] <= 0) { continue; } $unitsViewData['name'] = $unit->getName(); $unitsViewData['count'] = $units[$id_unit]; $unitsViewData['cost'] = (int) $unit->getPrice() * $units[$id_unit]; $costsSum += $unitsViewData['cost']; break; } $moveViewData['units'] = $unitsViewData; $movesViewData[] = $moveViewData; } $data['moves'] = $movesViewData; $data['costsSum'] = $costsSum; }