Author Topic: Updating resources  (Read 2184 times)

Offline perfecti0n

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Updating resources
« on: June 09, 2007, 06:46:28 PM »
hello guys :) i think i finally found a forum with some guys who have the knowledge to help me :D (lol at least i hope so) anyway im building a game and i've run into a problem. i have 4 resources

gold, stone, wood, food

the game is planned in a way that players would gain resources buy building farm fields and upgrading them. and in lvl 1 field they would have 40 food / hour. how could i make it so that the number in the base keeps updating (even when the user is not online) ?

Offline Ki Adi Mundi

  • Level 8
  • *
  • Posts: 38
  • Reputation: +0/-0
    • View Profile
Re: Updating resources
« Reply #1 on: June 09, 2007, 07:08:12 PM »
Is this a game like Command and Conquer?

Offline perfecti0n

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Updating resources
« Reply #2 on: June 09, 2007, 08:59:41 PM »
is there a browser based game called C&C ? :)

sorry i forgot to mention im making this game in php&mysql

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Updating resources
« Reply #3 on: June 10, 2007, 05:46:26 AM »
You could use cron jobs so that every so often the script is run and a certain amount is added to each person's resources.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Updating resources
« Reply #4 on: June 10, 2007, 09:54:58 AM »
Here's a non-cronjob way:

create a new field called last_res in your user table

When the user logs in for the first time, set this field to the current time.
From now on, you can check on each pageload the time that has passed between the last resource update and the current time

Code: [Select]
$now = date("U");

$time_passed = $now - $user[last_res];

If you have 40 units of a new resource each hour, this means 1 resource unit every 90 seconds. So if the time that has passed is greater than 90, you can add one unit of this resource

Code: [Select]
if($time_passed >= 90)
{
   $units = floor($time_passed / 90);
   $time_left = $now - $units*90;
   $last_res = $now - $time_left;
}

this code above checks the amount of units of a resource you get ($units) and calculates the time that is in excess. So if you check the page every 100 seconds, you get 1 unit each time. If you don't add the extra-time, the user will not get the full amount of resource-units

Now all you gotta do is update the resources and the last_res field to the new $last_res value.

Offline perfecti0n

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Updating resources
« Reply #5 on: June 10, 2007, 10:28:26 AM »
yes but what if in the time between the user has upgraded the building and that would raise his production per hour...? would i just start the script again or what ?

does anyone know the game travian ? how do you think they've done it...?

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Updating resources
« Reply #6 on: June 10, 2007, 12:41:07 PM »
Haven't played travian.

But here's a way to do it: the player has to manually activate the upgraded building (sending units, click 'activate', whatever), make the above calculation, and use a new value from then on.

And if you don't want to do this: you will need a time for the building to be finished. I guess this is stored somewhere. Just check if the building level has changed since the last login and if yes, when. Then calculate the resoruces for the time until upgrade with the old values, and then from the time of the upgrade with the new values.

It's pretty simple actually, but a lot of work.

Offline perfecti0n

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Updating resources
« Reply #7 on: June 10, 2007, 12:49:58 PM »
you mean a lot of code :) thx for your help i really appreciate it :) is there a +rep button here somewhere ?

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Updating resources
« Reply #8 on: June 10, 2007, 01:38:27 PM »
you need 100 posts to get the possibility to give or take away reputation.

Btw: what are you planning on doing? I'd be interested :)

Offline perfecti0n

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
    • View Profile
Re: Updating resources
« Reply #9 on: June 10, 2007, 04:44:09 PM »
its a build,attack,defend game...
4 nations:

Sparta
Egypt
Persia
Rome

but the project is really big and will take a lot of time :)
i got a lot of ideas to add in ;) if you're interested you can add me to msn :) write me a pm ;)

Offline Wakish

  • Level 14
  • *
  • Posts: 111
  • Reputation: +0/-1
    • View Profile
    • Wakish Wonderz
Re: Updating resources
« Reply #10 on: June 28, 2007, 01:59:59 PM »
yes but what if in the time between the user has upgraded the building and that would raise his production per hour...? would i just start the script again or what ?

does anyone know the game travian ? how do you think they've done it...?

I played Travian and I'm sure they use crons because everything needs to be well calculated ON the RIGHT time irrespective of whether the user logs in or not.. It internal time ticker needs to go on..

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Updating resources
« Reply #11 on: June 28, 2007, 02:08:52 PM »
Uhm yeah, I'd say exactly the opposite, wakish. If the were using Cronjobs, they'd need to executed every second to calculate everything for all the users.... And this would put a lot of stress on the server.

They probably update each user individually when they are part of an action (being attacked, etc.). It's not really difficult to do, it's mostly just a lot of work.

Offline Wakish

  • Level 14
  • *
  • Posts: 111
  • Reputation: +0/-1
    • View Profile
    • Wakish Wonderz
Re: Updating resources
« Reply #12 on: June 28, 2007, 02:37:45 PM »
Uhm yeah, I'd say exactly the opposite, wakish. If the were using Cronjobs, they'd need to executed every second to calculate everything for all the users.... And this would put a lot of stress on the server.

They probably update each user individually when they are part of an action (being attacked, etc.). It's not really difficult to do, it's mostly just a lot of work.

I'm yet to have an experience in PHP game programming, so I think you might be more versed and more experienced than me.. so for the time being I agree with you  :D

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal