How to make a timed reward system in php and mysql

1.1k views Asked by At

How can I make a reward system in PHP that has a timer and sets a timer for a certain amount of time after they click on it and mysql inserts a random value into the table?

I am making a project and I want users to accumulate the in-game currency as if they were using a Bitcoin faucet.

1

There are 1 answers

0
techdude On BEST ANSWER

A simple way to do this would be to keep track of the users "currency" using a table that has both amounts and datetimes. The user's balance would only show the entries that have datetimes before the current time. That way, you can insert the entry right away and then it only goes live when you need it.

rowid |  user_id   |  amount  |      activetime       |
-------------------------------------------------------
90000 |     1      |  0.01    |   12:13:12 01-16-15   |
90001 |     1      |  0.01    |   12:13:12 02-16-15   |

On inserts you can set the active time to something like DATE_ADD(NOW(), INTERVAL 2 HOUR) so it will show up 2 hours later.

Example of adding a row:

INSERT INTO transactions 
    (user_id, amount, activetime) 
VALUES 
    (1, 0.01, DATE_ADD(NOW(), INTERVAL 2 HOUR))

Example of getting current balance for a user:

SELECT SUM(amount) AS balance FROM transactions WHERE activetime <= NOW()

Also, if you want to rate limit the user, it is easy now to check if they have already clicked the button because there will be an entry greater than the current time. You could query quickly like this:

SELECT 1 FROM transactions WHERE activetime > NOW() LIMIT 1

Then in php, if num_rows() is 1, they have already clicked the button and are waiting for payment. Otherwise it will return 0.

Hopefully this helps. Let me know if you have any other questions on this topic, or if you need some clarification.