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; }
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 executeProduction(ModelProductionMove $move) { $id_user = $move->getIdUser(); $id_zarea = $move->getIdZArea(); foreach ($move->getUnits() as $id_unit => $count) { $inGameLandUnits = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_zarea, $id_user, $id_unit); $inGameLandUnits->addCount($count); } if (!isset($this->spent_production[$id_user])) { $this->spent_production[$id_user] = 0; } $this->spent_production[$id_user] += $move->getCost(); }