]> git.mxchange.org Git - city.git/blob
941dc023c4eed7b42712898d481d7d539450a83d
[city.git] /
1 <?php
2 /**
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.
8  *
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.
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2015 City Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class CitySectionsDatabaseWrapper extends BaseDatabaseWrapper implements CitySectionsWrapper, Registerable {
33         // Constants for database table names
34         const DB_TABLE_CITY_SECTIONS = 'city_sections';
35
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';
39
40         /*
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.
43          */
44         const DB_COLUMN_LOT_ID                 = 'lot_id';
45
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';
49
50         // X-Y-Z position
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';
54
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';
62
63         // Other settings:
64         // Reserved section (see documentation)
65         const DB_COLUMN_SECTION_RESERVED       = 'section_reserved';
66
67         /**
68          * Protected constructor
69          *
70          * @return      void
71          */
72         protected function __construct () {
73                 // Call parent constructor
74                 parent::__construct(__CLASS__);
75         }
76
77         /**
78          * Creates an instance of this database wrapper by a provided user class
79          *
80          * @return      $wrapperInstance        An instance of the created wrapper class
81          */
82         public static final function createCitySectionsDatabaseWrapper () {
83                 // Get a new instance
84                 $wrapperInstance = new CitySectionsDatabaseWrapper();
85
86                 // Set (primary!) table name
87                 $wrapperInstance->setTableName(self::DB_TABLE_CITY_SECTIONS);
88
89                 // Return the instance
90                 return $wrapperInstance;
91         }
92
93         /**
94          * Checks if the given city id is found in sections table
95          *
96          * @param       $cityId         City id to check
97          * @return      $isFound        Whether the city id is found
98          */
99         public function ifCityHasSections ($cityId) {
100                 // Get search instance
101                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
102
103                 // Search for 'city_id'
104                 $searchInstance->addCriteria(self::DB_COLUMN_CITY_ID, $cityId);
105
106                 /*
107                  * Only one entry is enough to find, else this query could run very\
108                  * long on large maps.
109                  */
110                 $searchInstance->setLimit(1);
111
112                 // Execute it on database instance
113                 $resultInstance = $this->doSelectByCriteria($searchInstance);
114
115                 // Check if there is one entry
116                 $isFound = ($resultInstance->next() == 1);
117
118                 // Return result
119                 return $isFound;
120         }
121
122         /**
123          * Expands the sections table with initial data for given city id
124          *
125          * @param       $cityId         City id to check
126          * @return      $ids            Sections ids from initial expansion
127          */
128         public function doInitialCityExpansion ($cityId) {
129                 // Make sure this city has no sections
130                 assert(!$this->ifCityHasSections($cityId));
131
132                 // @TODO Unfinished
133                 $this->partialStub('cityId=' . $cityId . ' - UNFINISHED!');
134         }
135 }
136
137 // [EOF]
138 ?>