基于PHP文本的游戏时间段[关闭]

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:

  • each task is associated with a time it takes to finish it
  • while the task is active, no other tasks may be started

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

  • ID
  • name
  • description
  • time_to_complete

and another table for the User and one for Active_Tasks with

  • user_id
  • task_id
  • started_at
  • ends_at

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:

  1. 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.

  2. 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

  • id
  • username
  • password

Table: business_types

  • id
  • title (examples: bakery, coffee shop, retail store
  • price (cost to build this type of business)

Table: users_businesses (one user can own multiple business)

  • id
  • user_id (obviously corresponds to user table)
  • business_type (corresponds to business_types table: bakery, coffee shop, retail store, etc)
  • level (the current level the business is at)
  • last activity (datetime, you can do a query where the user is unable to upgrade the business level until a certain amount of time has passed. And you can increase the time needed to wait depending on the business's level...higher levels would have longer wait times. No activity can be performed until the wait time is over.)
  • other fields like how much money this business produces per hour

Table: users_resources

  • id
  • user_id
  • amount (positive or negative integer. Users can gain money every hour from their businesses and can spend their money to upgrade or purchase new business)

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.