require_once '../../server/classes/class.user.php'; require_once '../../server/config.php'; session_start(); if (count($_GET) != 0 && isset($_SESSION['userSession'])) { // Check that all parameters have valid values if (!is_numeric($_GET['buildingId']) || !is_numeric($_GET['x']) || !is_numeric($_GET['y']) || intval($_GET['x']) < 0 || intval($_GET['y']) < 0 || intval($_GET['x']) > GRID_X || intval($_GET['y']) > GRID_Y) { die('ERROR'); } $DB = new DBUtil(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // DB name if ($DB) { $UserUtil = new UserUtil($DB); $BuildingUtil = new BuildingUtil($DB); $OperUtil = new OperationsUtil($DB); // Check if it's a valid building $building = $BuildingUtil->getBuildingById((int) $_GET['buildingId']); if ($building) { $user = $UserUtil->getUserById($_SESSION['userSession']->getId()); // Does the user have enough money to purchase it? if ($user && $user->getBalance() >= $building->getCost()) { // Grab this user's building instances $inst = $OperUtil->findBuildingInstanceByXY($user->getId(), $_GET['x'], $_GET['y']); // Purchase if (!$inst) { // Create the building instance $bi = new BuildingInstance(null, $user->getId(), $building->getId(), (int) $_GET['x'], (int) $_GET['y']); $bi = $OperUtil->create($bi); // Take the money away $user->setBalance($user->getBalance() - $building->getCost()); // Update the user $UserUtil->update($user);
} } echo "<br /><br />"; $building = new Building(null, 'example', '*****@*****.**'); $building = $BuildingUtil->create($building); if ($building != null) { echo '<strong>BuildingUtil->create()</strong>'; echo '<pre>'; print_r($building); echo '</pre>'; } else { echo "A problem ocurred while trying to create the building"; } if ($building != null) { $id = $building->getId(); $building2 = $BuildingUtil->getBuildingById($id); if ($building2 != null) { echo '<strong>BuildingUtil->getBuildingById()</strong>'; echo '<pre>'; print_r($building2); echo '</pre>'; } else { echo "A problem ocurred while trying to get a building by ID"; } } if ($building != null) { if ($BuildingUtil->removeBuilding($building)) { echo '<strong>BuildingUtil->remove()</strong>'; echo '<br />OK<br/><br />'; } else { echo "A problem ocurred while trying to remove the building from the database";
* with JavaScript, but i found that it was terribly inefficient and took a very long time * and resources to initialize. Printing and initializing the objects directly (using an * approach similar to the one below is noticeably faster. * Additionally, the reason why it's being done here (on the same file as the rest of the * HTML code) is to minimize the number of requests (which is great for mobile devices with * high bandwidth and low latency) * * Although it looks a bit weird, I used this approach to appeal amateur developers as it * seemed to me that it'd be easier to understand. */ for ($i = 0, $len = count($arrBldInst); $i < $len; $i++) { // Position information of the building instance $xpos = $arrBldInst[$i]->getXPos(); $ypos = $arrBldInst[$i]->getYPos(); // Also, we need to access additional information about each building $building = $BuildingUtil->getBuildingById($arrBldInst[$i]->getBuildingId()); switch ($arrBldInst[$i]->getBuildingId()) { case 1: // Ice cream shop echo "obj = new IceCreamShop(" . $arrBldInst[$i]->getId() . ", icss);"; break; case 2: // Hotel echo "obj = new Hotel(" . $arrBldInst[$i]->getId() . ", hs);"; break; case 3: // Cinema echo "obj = new Cinema(" . $arrBldInst[$i]->getId() . ", cs);"; break; case 4: // Tree