Empty slots for building in towns

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Empty slots for building in towns

Post by kibo95 »

In my game every player will have few towns in which he could build and upgrade buildings spend resources and get points for that.
As you probably know in many online browser games town is empty in beggining,with empty slots for buildings,but when you build building on place you want one slot is replaced with image of that particular building.
I'm wondering how to make that. I was thinking that upload image of empty city and then images of city with 1,2,3 buildings and so on. Last image would be with all buildings in town.
And when player upgrade building old picture is replaced with new one what contains that building.
But i guess that's not best way to do that.
So can anybody explain me which is the bst way to carry out my plan.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

Re: Empty slots for building in towns

Post by Gunner »

that's pretty much all you need, actually..

1.png -> empty slot building
2.png -> barrack

then just replace the empty slot with barrack once the player updates his buildings..

you might need an index for each building in your database
something like

Code: Select all

$barrack = 2;
$empty = 1;
<img src="<?php echo $barrack; ?>.png" />
of course the code above is just a prototype..
Skype: mrvictordiaz
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: Empty slots for building in towns

Post by kibo95 »

ok even that is the easiest way,i still don't understand how to manage that. I mean once when player build building, how to save that new picture,so when he click on his town again,he see that new picture,without getting back on an old one with empty slots.
Do i need something like database for pictures connected to player database? (because every player would have different pictures on his town).
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Empty slots for building in towns

Post by Chris »

kibo95 wrote:ok even that is the easiest way,i still don't understand how to manage that. I mean once when player build building, how to save that new picture,so when he click on his town again,he see that new picture,without getting back on an old one with empty slots.
Do i need something like database for pictures connected to player database? (because every player would have different pictures on his town).
Different to what extent? Different buildings in different locations?

Yes you would need to store player information in some form of database.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: Empty slots for building in towns

Post by kibo95 »

Different to what extent? Different buildings in different locations?

Yes you would need to store player information in some form of database.
yes,on different locations
how could i store that,i know how to store data,i mean numbers or different variables,varchar,int etc, is there variable for images
User avatar
Verahta
Posts: 441
Joined: Wed Aug 24, 2011 1:50 am

Re: Empty slots for building in towns

Post by Verahta »

Go to the tutorial section and do some PHP/MySQL tutorials. I honestly think you need to "fill out" your foundational knowledge before you can tackle the project you are wanting to.

Also, there are tons of tutorials that guide you step-by-step through how to build exactly the type of game you are talking about.
"In order to understand recursion, one must first understand recursion".
User avatar
kibo95
Posts: 35
Joined: Wed Jul 16, 2014 6:59 am

Re: Empty slots for building in towns

Post by kibo95 »

Go to the tutorial section and do some PHP/MySQL tutorials. I honestly think you need to "fill out" your foundational knowledge before you can tackle the project you are wanting to.

Also, there are tons of tutorials that guide you step-by-step through how to build exactly the type of game you are talking about.

I definitely need to fill out my knowlege,because i'm just starting my "career". But it's going really nice,i have already done almost everything i wanted to (believe it or not). Only problem is graphic design,I'm not really good in it (what is completely understandable,i have no experience,no talent) so my pictures are not very convincing. But rest of it is going well. This is part that i'm not sure how to do,but i'm sure that i can figoure it out,i didn't think about it a lot. I just thought you can give me some fast answers so i could continue without bodering with that.
User avatar
Verahta
Posts: 441
Joined: Wed Aug 24, 2011 1:50 am

Re: Empty slots for building in towns

Post by Verahta »

I haven't built that kind of game, so I don't know the answer, but it should be as simple as swapping images. A database is just a text file basically, it has a structure and order to it, but it's just a complex list essentially. You don't literally store images in it, but you store file names in it, which point to the images with the same file name. LIke the images path or URI/URL. Then just swap them, like Gunner did in the code snippet with the PHP call.

Take a few hours to do some graphics tutorials, you will be amazed at what you can do if you follow a nice tutorial on the type of art/graphics you want to make after just a few hours of effort.
"In order to understand recursion, one must first understand recursion".
User avatar
Verahta
Posts: 441
Joined: Wed Aug 24, 2011 1:50 am

Re: Empty slots for building in towns

Post by Verahta »

I should add, I'm pretty sure Halls has a video tutorial he made that makes this kind of game in the youtube tutorial videos section.
"In order to understand recursion, one must first understand recursion".
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Empty slots for building in towns

Post by Chris »

You could use blob, or text and store them as base64 strings, but it would be better to stick to using image files for what they are built for, for a lot of different reasons, don't get me started on that :arrow: . Make your table data relate to the images in the file structure programmatically.

I think this is roughly what you are looking to make, I added the image_file column to the `building_type` table.

Image
Download diagram: http://wikisend.com/download/955210/example.mwb
(http://dev.mysql.com/downloads/workbench/)

Code: Select all

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`player` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`city`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`city` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `player_id` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`, `player_id`),
  INDEX `fk_city_player_idx` (`player_id` ASC),
  CONSTRAINT `fk_city_player`
    FOREIGN KEY (`player_id`)
    REFERENCES `mydb`.`player` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`building_type`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_type` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `image_file` VARCHAR(45) NOT NULL,
  `level` VARCHAR(45) NOT NULL,
  `required_build_time` TIME NOT NULL,
  `resource_generation_time` TIME NOT NULL,
  `required_building_type_id` INT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_building_type_building_type1_idx` (`required_building_type_id` ASC),
  CONSTRAINT `fk_building_type_building_type1`
    FOREIGN KEY (`required_building_type_id`)
    REFERENCES `mydb`.`building_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`building`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `city_id` INT NOT NULL,
  `building_type_id` INT NOT NULL,
  `position` INT NOT NULL,
  `build_start_time` TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`, `city_id`),
  INDEX `fk_building_city1_idx` (`city_id` ASC),
  INDEX `fk_building_building_type1_idx` (`building_type_id` ASC),
  CONSTRAINT `fk_building_city1`
    FOREIGN KEY (`city_id`)
    REFERENCES `mydb`.`city` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_building_building_type1`
    FOREIGN KEY (`building_type_id`)
    REFERENCES `mydb`.`building_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`resource` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`building_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_resource` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `building_id` INT NOT NULL,
  `resource_id` INT NOT NULL,
  `amount` INT NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`, `building_id`, `resource_id`),
  INDEX `fk_building_resource_building1_idx` (`building_id` ASC),
  INDEX `fk_building_resource_resource1_idx` (`resource_id` ASC),
  CONSTRAINT `fk_building_resource_building1`
    FOREIGN KEY (`building_id`)
    REFERENCES `mydb`.`building` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_building_resource_resource1`
    FOREIGN KEY (`resource_id`)
    REFERENCES `mydb`.`resource` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`building_type_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_type_resource` (
  `resource_id` INT NOT NULL AUTO_INCREMENT,
  `building_type_id` INT NOT NULL,
  INDEX `fk_building_type_resource_resource1_idx` (`resource_id` ASC),
  INDEX `fk_building_type_resource_building_type1_idx` (`building_type_id` ASC),
  PRIMARY KEY (`resource_id`, `building_type_id`),
  CONSTRAINT `fk_building_type_resource_resource1`
    FOREIGN KEY (`resource_id`)
    REFERENCES `mydb`.`resource` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_building_type_resource_building_type1`
    FOREIGN KEY (`building_type_id`)
    REFERENCES `mydb`.`building_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`building_required_resource`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`building_required_resource` (
  `building_type_id` INT NOT NULL,
  `resource_id` INT NOT NULL,
  `amount` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`building_type_id`, `resource_id`),
  INDEX `fk_building_required_resource_resource1_idx` (`resource_id` ASC),
  CONSTRAINT `fk_building_required_resource_building_type1`
    FOREIGN KEY (`building_type_id`)
    REFERENCES `mydb`.`building_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_building_required_resource_resource1`
    FOREIGN KEY (`resource_id`)
    REFERENCES `mydb`.`resource` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
I made you the following to split it into steps. It is scripted and doesn't follow OOP so should be relatively easy to understand.

Model Logic

Code: Select all

/**
 * Creates the buildings image URL. Ideally make sure $path is coming from a config file.
 * @param string $buildingImageName The file name of the building image
 * @return string The url for the image of the building
 */
function getBuildingImageURL($buildingType = null, $path = '/images/buildings/', $emptySlotImageImageName = 'available_building_area.png') {
    if (null == $buildingType) {
        $buildingImageName = $emptySlotImageImageName;
    } else {
        $buildingImageName = $buildingType['image_file'];
    }
    return urlencode($path . $buildingImageName);
}

Code: Select all

/**
 * Gets the building name by its type
 * @param string $buildingType
 * @param string $emptySlotName
 * @return string A html safe building name
 */
function getBuildingName($buildingType = null, $emptySlotName = 'Empty Slot') {
    if (null == $buildingType) {
        $buildingName = $emptySlotName;
    } else {
        $buildingName = $buildingType['name'];
    }
    return htmlentities($buildingName);
}

Code: Select all

/**
 * Gets a building type by its id
 * @param integer $id
 * @return array The building type with structured by its column names
 */
function getBuildingTypeById($id) {
    $buildingTypeQuery = mysql_query("SELECT * FROM `building_type` WHERE `id` = {$building['building_type_id']}");
    $buildingType = mysql_fetch_assoc($buildingTypeQuery);
    if (empty($buildingType)) {
        $buildingType = null;
    }
    return $buildingType;
}

Code: Select all

/**
 * Gets a list of buildings related to the city id given.
 * If the amount of buildings does not match the allowed buildings per city.
 * The list is populated with empty values
 * @param integer $id The id of the city.
 * @param integer $buildingPerCity The amount of buildings allowed in a city.
 * @return mixed An array of buildings and/or nulls
 */
function getBuildingListByCityId($id, $buildingPerCity = 9) {
    $buildingQuery = mysql_query("SELECT * FROM `building` WHERE `city_id` = " . (int)$id ."");
    $buildingResult = mysql_fetch_assoc($buildingQuery);
    $buildingList = array();
    foreach($buildingResult as $building) {
        $building['type'] = getBuildingTypeById($building['building_type_id']);
        $buildingList[] = $building;
    }
    $builtBuildingCount = count($buildingList);
    if ($builtBuildingCount < $buildingPerCity) {
        // create empty slots based on the amount of building records found
        // minus the amount of buildings a city is allowed to have
        for ($i = 0; $i < ($buildingPerCity - $builtBuildingCount); $i++) {
            $buildingList[] = null;
        }
    }
    return $buildingList;
}
Controller

Code: Select all

// controller logic
// Casting to int is quicker than escaping for MySQL
// when passing strings always use mysql_real_escape_string()
$cityId = (int) $_GET['cityId'];
$buildingList = getBuildingListByCityId($cityId);
Helper

Code: Select all

/**
 * 
 * @param array $buildingType
 * @return string A HTML image tag
 */
function buildingImageTag($buildingType) {
    return '<img src="' . getBuildingImageURL($buildingType) . '" alt="'. getBuildingName($buildingType).'" />';
}
View

Code: Select all

// view logic
foreach ($buildingList as $building) {
   echo buildingImageTag($building['type']);
}
I haven't tested this it could be full of typos and errors. I would recommend looking into an MVC framework to help you build this, or at least some form of ORM as the relationships will be hell to program an maintain.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Beginner Help and Support”