/** * creates new ship and corresponding move * * @param $id_unit int * @param $name string * @param $zarea_in_port int * @param $zarea int * @throws ControllerException * @throws ModelException */ public function setNewShip($id_unit, $name, $zarea_in_port, $zarea) { // 1. regex name $name = trim($name); if (!preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+){1,}?\$/", $name)) { throw new ControllerException('Invalid ship name. Only letters, numbers, spaces and -\' allowed'); } // 2. check if id_zarea_in_port belongs to user $port_area = ModelGameArea::getGameArea($this->id_game, (int) $zarea_in_port); if ($port_area->getIdUser() !== $this->id_user) { throw new ControllerException('Area doesn\'t belong to user.'); } // 3. check if zarea and id_zarea_in_port are adjacent if (!in_array((int) $zarea, $port_area->getAdjecents())) { throw new ControllerException('Area not adjacent do port area.'); } // 4. check if ship id is in still available ships $stillAvailableShips = $this->getStillAvailableShips(); if (!isset($stillAvailableShips[$id_unit])) { throw new ControllerException('No ships of this type available.'); } else { if ($stillAvailableShips[$id_unit] <= 0) { throw new ControllerException('No ships of this type available anymore.'); } } // 5. create new move ModelSetShipsMove::createSetShipsMove($this->id_user, $this->id_game, (int) $zarea_in_port, (int) $zarea, (int) $id_unit, $name); }