public function run(array &$data) { $data['template'] = $this->getTemplate(); $this->addCurrentGameInfo($data); $this->moveController = new SetShipsController(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); // update moves if (isset($_POST['setship'])) { $this->setShip($data); } if (isset($_POST['fixate_start'])) { $this->fixateMove($data); } if (isset($_POST['delete'])) { $this->deleteMove($data); } // show already set ships $data['currentShips'] = array(); $iterator = ModelSetShipsMove::getSetShipMovesForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); while ($iterator->hasNext()) { /** @var ModelSetShipsMove $move */ $move = $iterator->next(); $zShip = ModelInGameShip::getShipById(ModelGame::getCurrentGame()->getId(), $move->getIdZunit()); $id_ship = $zShip->getIdUnit(); $ship = ModelShip::getModelById($id_ship); $zAreaInPort = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZareaInPort()); $zAreaAtSea = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZarea()); $data['currentShips'][] = array('id' => $move->getId(), 'ship_type' => $ship->getName(), 'ship_name' => $zShip->getName(), 'zarea_in_port' => $zAreaInPort->getName() . ' ' . $zAreaInPort->getNumber(), 'zarea_at_sea' => $zAreaAtSea->getName() . ' ' . $zAreaAtSea->getNumber()); } // show still available ships $data['availableShips'] = array(); $stillAvailableShips = $this->moveController->getStillAvailableShips(); foreach ($stillAvailableShips as $id_unit => $count) { if ($count <= 0) { continue; } $data['availableShips'][] = array('id' => $id_unit, 'count' => $count, 'name' => ModelShip::getModelById($id_unit)->getName()); } // show available countries $data['availableZAreasInPort'] = array(); $data['availableZAreasAtSea'] = array(); $iterator = ModelGameArea::iterator(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId()); while ($iterator->hasNext()) { /** @var ModelGameArea $zArea */ $zArea = $iterator->next(); $data['availableZAreasInPort'][] = array('id_zarea_in_port' => $zArea->getId(), 'name' => $zArea->getName(), 'number' => $zArea->getNumber()); } $iterator = ModelArea::iterator(TYPE_SEA); while ($iterator->hasNext()) { /** @var ModelArea $area */ $area = $iterator->next(); $data['availableZAreasAtSea'][] = array('id_zarea_at_sea' => ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $area->getId())->getId(), 'name' => $area->getName(), 'number' => $area->getNumber()); } $this->checkFixate($data, PHASE_SETSHIPS); $this->checkCurrentPhase($data, PHASE_SETSHIPS); }
/** * get list of all ships that are still placebale * * @return array - array(int id_unit => int numberof) */ public function getStillAvailableShips() { // 1. get list of all StartShips try { $startShips = ModelStartShips::getStartShipsForPlayers(ModelGame::getGame($this->id_game)->getNumberOfPlayers())->getShips(); } catch (NullPointerException $ex) { return array(); } // 2. get list of all StartShipMoves and reduce list of Startships $moves = ModelSetShipsMove::getSetShipMovesForUser($this->id_user, $this->id_game); while ($moves->hasNext()) { /** @var $move ModelSetShipsMove */ $move = $moves->next(); $ship = ModelInGameShip::getShipById($this->id_game, $move->getIdZunit()); $id_unit = $ship->getIdUnit(); if (isset($startShips[$id_unit])) { --$startShips[$id_unit]; } } // 3. return return $startShips; }
/** * permenantly remove move (and corresponding ship) from database * * @param ModelSetShipsMove $move * @throws NullPointerException */ public static function deleteSetShipsMove(ModelSetShipsMove $move) { $id_game = $move->getIdGame(); $id_move = $move->getId(); SQLCommands::init($id_game); // 1. delete ship from DB ModelInGameShip::deleteShip($id_game, $move->getIdZunit()); // 2. delete ship for move $query = 'delete_units_for_move'; $dict = array(); $dict[':id_move'] = $id_move; DataSource::Singleton()->epp($query, $dict); // 3. delete areas for move $query = 'delete_move_areas_for_move'; DataSource::Singleton()->epp($query, $dict); // 4. delete move $query = 'delete_move'; DataSource::Singleton()->epp($query, $dict); unset(self::$moves[$id_game][$id_move]); }