4 Replies - 1652 Views - Last Post: 08 May 2012 - 06:27 AM Rate Topic: -----

#1 asdfowkw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 12

PHP Text Game "Resources per second/minute"

Posted 08 May 2012 - 05:31 AM

Hello!

So I'm yet another guy doing a text game with php.

But I have a problem that I need help solving.

How would I go about and make resources update every say 5 second ?

Cronjobs isnt really optimal for that and i would live to have it dynamically so if i have 500 stone and a stoneincome of 60 stone / minute i would like it to be 505 stone after 5 seconds.

=)
Is This A Good Question/Topic? 0
  • +

Replies To: PHP Text Game "Resources per second/minute"

#2 Duckington   User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: PHP Text Game "Resources per second/minute"

Posted 08 May 2012 - 05:49 AM

From the small amount of info, i'm assuming a kind of age of empires system of a town gathering wood, stone, etc...?

Anyway, personally I would just do a cron job, but do it every 30 mins or an hour.

You can store in the DB somewhere which users have what rate per minute, and then just multiply it by whatever value.

So if you had 2 users with:

userid----rateofstonepermin
1---------30
2---------60

Along comes Mr Cron job every 30 minutes and simply looks for their rate and multiplies it by 30, then updates their actual value of the resource.
Was This Post Helpful? 0
  • +
  • -

#3 asdfowkw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 12

Re: PHP Text Game "Resources per second/minute"

Posted 08 May 2012 - 06:02 AM

Well then they would have to wait 30 minutes even if its only missing 1 resource.

But i found a way with mysql event scheduler but the problem i have now is that the event is running twice in a row even tho it shoudlnt :S
Was This Post Helpful? 0
  • +
  • -

#4 Duckington   User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: PHP Text Game "Resources per second/minute"

Posted 08 May 2012 - 06:09 AM

Well you could have it update every minute or sooner using AJAX, but they would have to be logged in with a valid session, so they wouldn't be able to close the page and continue gaining the resource.

In some cases like this, you often just have to accept the limitations and make it a feature of the game, so that the way it works is they gain x amount of resources every y minutes. That's how most of these games work which are similar in design, they don't update every few seconds.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: PHP Text Game "Resources per second/minute"

Posted 08 May 2012 - 06:27 AM

Your users should only see their resources when they access the page, so I would use Ajax along with Javascript's setTimeout() function when they're connected to update. My PHP script would calculate changes based on the time of the last update. This way the user could be logged off for days or hours and their status might change significantly when they log back in, or they could be logged on and see the changes over time.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1