3 * A database wrapper for city sections. Sections are the smalles entity of a
4 * city. They can be connected with each other and form a lot. Therefore only
5 * sections of same type (and sub type) can be linked combined, else you will
6 * something really strange, a residential building with some industry parts
7 * for example is not possible in real world.
9 * Sure you can have a small shop (commercial) on ground level and some
10 * residentials on higher levels. This city simulation does support such
11 * creations as the levels (Z axis) is different.
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2015 City Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class CitySectionsDatabaseWrapper extends BaseDatabaseWrapper implements CitySectionsWrapper, Registerable {
33 // Constants for database table names
34 const DB_TABLE_CITY_SECTIONS = 'city_sections';
36 // Section id, an referenced city id and lot id
37 const DB_COLUMN_SECTION_ID = 'city_section_id';
38 const DB_COLUMN_CITY_ID = 'city_id';
41 * Lot id, the lot id is only set if the player has "aquired" the sections
42 * and has created a lot based on these sections.
44 const DB_COLUMN_LOT_ID = 'lot_id';
46 // Section and sub type (e.g. residential, hut)
47 const DB_COLUMN_SECTION_TYPE = 'section_type';
48 const DB_COLUMN_SECTION_SUB_TYPE = 'section_sub_type';
51 const DB_COLUMN_SECTION_POSITION_X = 'section_position_x';
52 const DB_COLUMN_SECTION_POSITION_Y = 'section_position_y';
53 const DB_COLUMN_SECTION_POSITION_Z = 'section_position_z';
55 // Connected neigbouring sections
56 const DB_COLUMN_SECTION_NEIGHBOUR_WEST_ID = 'section_neighbour_west_id';
57 const DB_COLUMN_SECTION_NEIGHBOUR_EAST_ID = 'section_neighbour_east_id';
58 const DB_COLUMN_SECTION_NEIGHBOUR_NORTH_ID = 'section_neighbour_north_id';
59 const DB_COLUMN_SECTION_NEIGHBOUR_SOUTH_ID = 'section_neighbour_south_id';
60 const DB_COLUMN_SECTION_NEIGHBOUR_UP_ID = 'section_neighbour_up_id';
61 const DB_COLUMN_SECTION_NEIGHBOUR_DOWN_ID = 'section_neighbour_down_id';
64 // Reserved section (see documentation)
65 const DB_COLUMN_SECTION_RESERVED = 'section_reserved';
68 * Protected constructor
72 protected function __construct () {
73 // Call parent constructor
74 parent::__construct(__CLASS__);
78 * Creates an instance of this database wrapper by a provided user class
80 * @return $wrapperInstance An instance of the created wrapper class
82 public static final function createCitySectionsDatabaseWrapper () {
84 $wrapperInstance = new CitySectionsDatabaseWrapper();
86 // Set (primary!) table name
87 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
89 // Return the instance
90 return $wrapperInstance;
94 * Checks if the given city id is found in sections table
96 * @param $cityId City id to check
97 * @return $isFound Whether the city id is found
99 public function ifCityHasSections ($cityId) {
100 // Get search instance
101 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
103 // Search for 'city_id'
104 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
107 * Only one entry is enough to find, else this query could run very\
108 * long on large maps.
110 $searchInstance->setLimit(1);
112 // Execute it on database instance
113 $resultInstance = $this->doSelectByCriteria($searchInstance);
115 // Check if there is one entry
116 $isFound = ($resultInstance->next() == 1);
123 * Expands the sections table with initial data for given city id
125 * @param $cityId City id to check
126 * @return $ids Sections ids from initial expansion
128 public function doInitialCityExpansion ($cityId) {
129 // Make sure this city has no sections
130 assert(!$this->ifCityHasSections($cityId));
133 $this->partialStub('cityId=' . $cityId . ' - UNFINISHED!');