Sign in to follow this  
Dom_152

PHP Strategy Game

Recommended Posts

Dom_152    476
I've been considering writing a little PHP Strategy Game and I was wondering what would be the best way to handle certain persistant data. I'll try to describe with an example. A player choose to build a structure with a build time of 5 minutes. The player then logs off 1 minute into the build. How could I then keep the strucuture building so that when the player logs back in or refreshes the page it will show that the construction is complete. My first thought would be to save the start time and the length for each of these sort of things in a database and then simply see if the start time + length >= current time and if that's true then mark it as complete. Does anyone have any other ideas because of course this means that the building will only be completed if the script is executed after the set time which could cause some problems with other things.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
I fail to see the problem: if the script is executed at 3:14pm, but an event was scheduled to be over at 2:28pm, simply run the scheduled event at 3:14pm "as if" it had been executed at the correct time, and only then display the result of the script execution.

Share this post


Link to post
Share on other sites
Wavesonics    330
Ya this idea works just fine and is really one of the only ways to accomplish it without having a server side process running.

Where this runs into a problem is:

Say the building is a resource plant which produces X # of resources per hour.

So they build it at 01:00 (1am), it takes 1 hour to build, and they log in again at 05:00. So it should have finished at 02:00, then given them X resources for every other hour ( 3 hours * X resources ). Yet, the Building didn't actually finishing building till they logged in again at 05:00. So they didn't get the 3 hours worth of resources they should have.

Even if your server doesn't let you run a server side process, it might let you run a cron job. Which, if every thing in the game was based off some time interval ( say, 1 hour ) you *could* set up a cron job to fire off a PHP file every hour which processed all events, such as check to see if a building is finished, or increment resources.

Really what it comes down to though is understanding the limitations of the system you are developing for. Without getting fancy with cron jobs or server side processes, you can only fire events and processes things when users are interacting with your web app.

Know that and design the game around these limitations. Thats the best way, rather then hacking in some 1 off solution that can fail or produce "weird" behavior.

Hope that helps, good luck man, PHP games can be very fun to code since they can be quicker to make then client side games :)

Share this post


Link to post
Share on other sites
Dom_152    476
Quote:
Original post by ToohrVyk
I fail to see the problem: if the script is executed at 3:14pm, but an event was scheduled to be over at 2:28pm, simply run the scheduled event at 3:14pm "as if" it had been executed at the correct time, and only then display the result of the script execution.


Ahh, thanks for that. Simple things like that have a tendancy to escape me :)

Quote:
Original post by Wavesonics
So they build it at 01:00 (1am), it takes 1 hour to build, and they log in again at 05:00. So it should have finished at 02:00, then given them X resources for every other hour ( 3 hours * X resources ). Yet, the Building didn't actually finishing building till they logged in again at 05:00. So they didn't get the 3 hours worth of resources they should have.


This is the sort of problem I was thinking about. I should be able to implement ToohrVyk's idea. Whilst I check whether something has completed I can also see how much time has elapsed since it should have been done and then perform the correct actions as though it had been completed at the correct time.

Quote:
Original post by Wavesonics
Hope that helps, good luck man, PHP games can be very fun to code since they can be quicker to make then client side games :)


My thoughts exactly! More time spent on writing the game rather than the technology that will power it ;)

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by Wavesonics
So they build it at 01:00 (1am), it takes 1 hour to build, and they log in again at 05:00. So it should have finished at 02:00, then given them X resources for every other hour ( 3 hours * X resources ). Yet, the Building didn't actually finishing building till they logged in again at 05:00. So they didn't get the 3 hours worth of resources they should have.


This is where your interpretation of the system is flawed.

The building finished building at 2:00am. Period. The fact that the script which actually commited this event to the database was executed at 5:00am is a coincidental detail that has no impact or relevance whatsoever on the fact that, for all in-game purposes, the building has been there ever since 2:00am, churning out resources for the player to use. And so, the resource-giving code will check for how long the building's been there, see that it's been there for three hours, and allocate the corresponding resources without any loss.

Share this post


Link to post
Share on other sites
Dom_152    476
Thanks for clearing that up, ToohrVyk. I appreciate the help from everyone. It's my first time writing this sort of game in PHP and using web technologies.

Share this post


Link to post
Share on other sites
Dom_152    476
OK take this situation:

A player sends his troops to another player's city. The journey will take 5 minutes. This player then logs out. The 5 minutes pass so by this time the troops should be at the other player's city however because the sending player has not logged in and hasn't allowed the troops status to be updated the troops won't appear on the other player's "People-in-my-city" list. How could I get around this?

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by Dom_152
OK take this situation:

A player sends his troops to another player's city. The journey will take 5 minutes. This player then logs out. The 5 minutes pass so by this time the troops should be at the other player's city however because the sending player has not logged in and hasn't allowed the troops status to be updated the troops won't appear on the other player's "People-in-my-city" list. How could I get around this?


Always update the entire simulation state at once, whenever necessary.

In this particular case, when the other player connects, the simulation state is advanced up until the current time, and the simulation rules state that there are troops inside the city. So, the troops will be displayed.



Share this post


Link to post
Share on other sites
Dom_152    476
Quote:
Original post by ToohrVyk
Always update the entire simulation state at once, whenever necessary.

In this particular case, when the other player connects, the simulation state is advanced up until the current time, and the simulation rules state that there are troops inside the city. So, the troops will be displayed.


If I update the entire simulation for every player and everything going on won't that be quite slow?

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by Dom_152
Quote:
Original post by ToohrVyk
Always update the entire simulation state at once, whenever necessary.

In this particular case, when the other player connects, the simulation state is advanced up until the current time, and the simulation rules state that there are troops inside the city. So, the troops will be displayed.


If I update the entire simulation for every player and everything going on won't that be quite slow?


Design your simulation so that it operates on differences. That is, whenever something should happen at a given time, store it in an event list that's sorted by time. Then, simply extract from that list the events that happen prior to the current time, in order, and execute them. This way, if few things happen (or if players connect often), then the computation load for each player will be small.

Of course, don't traverse the entire list of game objects every time a player connects—that would be insane in any scalable game, PHP or otherwise.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this