Results 1 to 3 of 3

Thread: PHP MySQL give something in time increments without cron jobs?

  1. #1
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default PHP MySQL give something in time increments without cron jobs?

    I have a MySQL table shown as the following (there are more fields but remover for the purpose of illustrating):

    Code:
    +----+-------------+--------+--------------+--------+--------------+
    | id | username    | energy | energy_total | health | health_total |
    +----+-------------+--------+--------------+--------+--------------+
    |  1 | Opalelement |     10 |           10 |     10 |           10 |
    |  2 | DougFresh   |     10 |           10 |     10 |           10 |
    +----+-------------+--------+--------------+--------+--------------+
    There are things you can do that take away energy, but I don't wnat to give it back all at once at any given time. What I wnat to do is maybe every 5 minutes after the last action that took some away, give one back until it is full.

    My idea was to make a database structure like the following:

    Code:
    +----+-------------+--------+--------------+-------------+--------+--------------+-------------+
    | id | username    | energy | energy_total | energy_used | health | health_total | health_used |
    +----+-------------+--------+--------------+-------------+--------+--------------+-------------+
    |  1 | *********** |     10 |           10 |             |     10 |           10 |             |
    |  2 | *********   |     10 |           10 |             |     10 |           10 |             |
    +----+-------------+--------+--------------+-------------+--------+--------------+-------------+
    *_used will be a unix timestamp value
    Then use a query to select the *_used fields and do "time() - *_used"
    Then divide the result by however long I want the increment to be
    After doing that I will just do something like UPDATE users SET energy=energy+$add_energy because if it hasn't been enough time the $add_energy will be 0

    That will of course be set to max out at the users energy_total

    My questions are:
    Would this be a good way to do this?
    Is there a better way to do this?

    Also, would this structure work instead and jsut compare everything to last_revoked?
    Code:
    +----+-------------+--------+--------------+--------+--------------+--------------+
    | id | username    | energy | energy_total | health | health_total | last_revoked |
    +----+-------------+--------+--------------+--------+--------------+--------------+
    |  1 | *********** |     10 |           10 |     10 |           10 |              |
    |  2 | *********   |     10 |           10 |     10 |           10 |              |
    +----+-------------+--------+--------------+--------+--------------+--------------+

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Without a cron job, that is the best way to do it. Congrats.

  3. #3
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •