Many web based text games have tasks which take real world time to complete. A basic example would be a game where you need to process some resource. You could "upgrade" your processing plant, but doing so would take, say 6 hours. If you chose to do this upgrade, you would be unable to do anything else in game until your 6 hours had elapsed, other than see a remaining time screen. Does anybody have any suggestions as to how this could be achieved in a game written in PHP?
It's pretty straightforward actually:
Now when a user starts a new task you record the time the task was started and which task it is and add the time when it will be finished. You store that in the database. The next time the user logs into the game, you check whether there is any active tasks for that user and if so, display the remaining time.
So you could have a table Tasks
with
and another table for the User
and one for Active_Tasks
with
And in your PHP script you have corresponding code to query and update those tables.
Try to get something working from that description and when you hit any roadblocks, ask a new question about the specific parts.
The answer would depend on a few different things. There are multiple ways of doing this in php. Examples:
If you don't want anything else to happen while the "processing plant" is being upgraded you could use the sleep or usleep functions of php. I would read the documentation from php.net, but basically they delay further execution of a php function for x milliseconds.
If you still need the user to be able to perform other tasks while the "processing plant" is being upgraded you can you the time function to grab the exact unix timestamp for when the function was started. As you run other functions in the game you can one of those functions check the timestamp against a the variable that holds the amount of time needed to wait for the upgrade process to be complete.
Hope this helps.
Well first off, most of these games are not written in php (I'm familiar with what you are talking about as I have played them).
But if doing it in php is a must, you will need to also use some type of database (which it's good that you also have mysql as one of your tags).
I'm not sure what all you want your game to do but for example: Say the object of your game is for users to have competing businesses against each other or something.
Table: users
Table: business_types
Table: users_businesses (one user can own multiple business)
Table: users_resources
That's just an example...You would need cron jobs to award resources for each business a user owns every hour that way they can collect the money even when they are not logged in.
But again, I would recommend that this is not in php.