require_once '../../server/classes/class.users.php';
require_once '../../server/classes/class.buildings.php';
require_once '../../server/classes/class.operations.php';
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
<?php

require_once 'classes/class.dbutil.php';
require_once 'classes/class.users.php';
require_once 'classes/class.user.php';
require_once 'classes/class.buildings.php';
require_once 'classes/class.building.php';
require_once 'config.php';
$DB = new DBUtil(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// DB name
if ($DB) {
    $UserUtil = new UserUtil($DB);
    $BuildingUtil = new BuildingUtil($DB);
    $user = new User(null, 'example', '*****@*****.**');
    $user->setPassword('a-password');
    $user = $UserUtil->create($user);
    if ($user != null) {
        echo '<strong>UserUtil->create()</strong>';
        echo '<pre>';
        print_r($user);
        echo '</pre>';
    } else {
        echo "A problem ocurred while trying to create the user";
    }
    if ($user != null) {
        $id = $user->getId();
        $user2 = $UserUtil->getUserById($id);
        if ($user2 != null) {
            echo '<strong>UserUtil->getUserById()</strong>';
            echo '<pre>';
            print_r($user2);
require_once '../server/classes/class.dbutil.php';
require_once '../server/classes/class.users.php';
require_once '../server/classes/class.buildings.php';
require_once '../server/classes/class.operations.php';
require_once '../server/classes/class.user.php';
require_once '../server/config.php';
session_start();
if (!isset($_SESSION['userSession'])) {
    // If the user is not logged in, redirect him to the index.
    header("location: index.php");
}
$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);
    $user = $UserUtil->getUserById($_SESSION['userSession']->getId());
    $buildings = $BuildingUtil->listBuildings();
}
if (!$DB || $user == null || $buildings == null) {
    die("An unrecoverable error ocurred while trying to load the user information. Try again later.");
} else {
    // Grab this user's building instances
    $arrBldInst = $OperUtil->getBuildingInstances($user->getId());
}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />