Author Topic: Adding time until completed building  (Read 1437 times)

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Adding time until completed building
« on: October 19, 2007, 12:25:33 PM »
I need to add an option to one of my projects where players can select a building type to upgrade and then have it take say 15 minutes to complete.  Thats not a big deal, but how to handle the delay in updating the database is the question.

Example:

Building: Town Hall Level 1
Player selects upgrade to level 2, pays costs, etc
Build time is 15 minutes.

15 minutes pass, now how do we update the building data in mySQL?

Offline vizion

  • Level 7
  • *
  • Posts: 32
  • Reputation: +1/-0
    • View Profile
    • Riddle Contest of Dooooom
Re: Adding time until completed building
« Reply #1 on: October 19, 2007, 05:03:18 PM »
Wouldn't it be easier to just update your table right away but add in some fields that track the time the building was started and the time it takes to build? Then just have your PHP code do a check to see if the user has access to the building...i.e. if 15 minutes has elapsed since the construction started.

This is how I do it in my puzzle contest for my penalty code which prevents users from entering a guess for 15 minutes. The field names are changed to reflect what you might use for your game:

$sql = "SELECT c.construction_start_time, b.construction_time_amount
      FROM " . CONSTRUCTION_TABLE . " c, " . BUILDINGS_TABLE . " b
      WHERE c.user_id = " . $userdata['user_id'];
   
   if (!$result = $db->sql_query($sql) )
      {
         message_die(GENERAL_ERROR, 'Error.', '', __LINE__, __FILE__, $sql);
      }
   
   $row = $db->sql_fetchrow($result);
   
   if ($row[construction_start_time'] >= (time() - ($row['construction_time_amount'] * 60)))
   {
      $time_left_to_completion = (($row['construction_start_time'] - (time() - ($row['construction_time_amount'] * 60))) / 60);
      echo $time_left_to_completion;
   }
   else
   {
         //allow access to the building
   }


You could set construction_time_amount to 15 or 30 or however minutes you want the building to take to build. no_use_time would just be a timestamp set when the construction is started.

Edit: I just noticed that my query isn't complete. You'd need to add in a line to pull up the information for the appropriate building/upgrade but that shouldn't be a problem.
« Last Edit: October 19, 2007, 05:09:47 PM by vizion »

Offline Equinox

  • Game Owner
  • Level 10
  • *
  • Posts: 58
  • Reputation: +2/-0
    • View Profile
    • Equinox Gaming Solutions
Re: Adding time until completed building
« Reply #2 on: October 20, 2007, 05:26:36 AM »
*nods in agreement* - I have a scouting feature that lasts for only an hour, and after that hour the scouting information deletes itself from the DB.

If you wanted, you could have two tables - your current one that the building is entered into, and another with the "hold" while it is building. This way, you dont need to do much as you can get the code to count all the necessary minutes and then allow the building to show as completed. You can just add something small to check if the building is in the "hold" table using its uniqueID, and if it is bring up a message saying "under construction" or something.

tbl_building_hold
  • buildingID (matches uniqueID of building in "buildings" table)
  • timebuilt (add to insert query using format: $timebuilt= date("Y-m-d H:i:s"))
  • minutes (how long for the building to complete)

then add this code on header/footer or essential page that is included with basically every page:

Code: [Select]
$minutes2= mysql_query("SELECT minutes FROM tbl_building_hold");
$minutes = mysql_fetch_array($minutes2);
$buildtime = date("Y-m-d H:i:s", strtotime("-$minutes minute"));
mysql_query("DELETE FROM tbl_building_hold WHERE timebuilt<='$buildtime'");

This way, it will take care of itself as will delete from the database after specified minutes and then the check wont find it in the holding DB and allow full use of the building.

If you want any help at all, just drop me a PM =]

-Equinox
Equinox Gaming Solutions Manager
http://www.equinoxgaming.co.uk

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Adding time until completed building
« Reply #3 on: October 22, 2007, 11:15:32 AM »
Thanks for the examples, pretty much how I figured it would be done.

Probably would be better if instead of pounding away at mySQL every page hit to check for completion, that a check is done only one time every few minutes.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Adding time until completed building
« Reply #4 on: October 22, 2007, 01:54:07 PM »
Isn't vizion's way more efficient? You won't need to add checks to every page, just the pages where the building is actually needed.

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Adding time until completed building
« Reply #5 on: October 22, 2007, 05:47:20 PM »
Yes, but I wouldn't have checks for completion on pages where it wasn't needed anyways :)

The only problem I see in the suggestions is that it requires a player to be online so the checks are made.  What happens if a player say upgrades something that effects turns based income and then logs off.  15 minutes pass and the upgrade is supposed to be completed.  Meanwhile no players login and play for several hours.  The player who made the upgrade that effects income doesn't get the benefits of the upgrade like they should have.  I guess the only way to solve that is run a cron every 15 minutes if no players are online.

Offline vizion

  • Level 7
  • *
  • Posts: 32
  • Reputation: +1/-0
    • View Profile
    • Riddle Contest of Dooooom
Re: Adding time until completed building
« Reply #6 on: October 23, 2007, 12:38:29 AM »
Well, since you're already storing the time building was started you could do a check against that time when the player logs in. If the player logs in two hours and fifteen minutes after construction started the check would then retroactively grant them two hours worth of gold (2:15 minus the build time) and the player would never notice. Maybe I'm misunderstanding the exact effects you're trying to achieve as well. You could write this check as a function and drop it in to run before every place that would be affected by the building upgrade. Hm, there's got to be a more efficient way to do it.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Adding time until completed building
« Reply #7 on: October 23, 2007, 05:08:56 AM »
If the building gives the player an extra 50% gold per tick, and that extra gold is given using cron jobs, you can just put the checks in the cronjob, right?

What I meant with my post before was that you add the checks where the calculations for the building are needed. There's no need to add new rows and deleting them when construction's finished. Just have a column with the completion time, and check the current time against that when you are calculating the effects.

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Adding time until completed building
« Reply #8 on: October 26, 2007, 06:06:09 PM »
Best way to do this is with a CRON job that runs every 5 minutes..
Creating online addictions, one game at a time:

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal