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); // Print the building instance die('OK:' . $bi->getId()); } } } die('ERROR'); } } else { die('ERROR'); }