Web servers and user timing

The title is a little confusing so let me explain. I'm making a HTML text based game that sits on a web server and the web server connects to a MySQL database.

Let's say the user does something, but in order to do that action again, they have to wait 20 minutes. How do you handle counting down the time in the background when the user may not be on the website?

I've seen this used hundreds of hundreds of times in text based games and dynamic games based on user events. How is it done?
You wouldn't need the user to be connected to the website since the countdown should be handled server side anyways. Since if the countdowns were handled client side it would be quite simple for users to alter the timers to their liking.

How you handle tracking different countdowns for different users is generally up to your design and what suits your application best, but some simple ways might be to assign each user a GUID which you can use to associate each user with their different countdown times that you have running on the server.

Though I have very little to no experience with web development so there probably is better ways to go about doing something like this.
So you would have a GUID associated with a timer and a user account and then store the GUID in a database? How often would you query the database?

I was thinking this approach initially but let's say the event needs to recognize the timer has ended within 1 second of the timer expiring. That would require database querying every second for every GUID and user. That would be very performance heavy.

I've seen games such as Tribal Wars that recognizes a timer for an event has ended within 100ms.

For example in Tribal Wars a users sends an attack on another user, the attack can take a variable amount of time amount of time so a timer is created for that event and is associated with that user.
I guess it would depend solely on your specific project and use case.

Let's take Tribal Wars and others like it for example, since they are graphics based and not text based they probably need the timer information quite regularly in order to update the client's UI and such with the amount of time left before they can perform some action.

How they go about this could be any number of ways but one way I imagine would be to have the master timer reside in the server, then also offload some of the processing to the client side. For example when as user logs in for the first time the client will ask the server for all timer information associated for this user and then keep an internal client side timer based off that information.

The client then could use these client side timers to update all the UI elements so that it doesn't need to poll the server as much. It would still have to poll the server every so often to make sure that the client side timers are in sync with the servers and also to double check a user can do the action he wants to do (In case they altered the client side timer), but it wouldn't be nearly as often as relying solely on the server.

You could also think of incorporating callbacks into it also, for example you could rely on the above and only sync the server and client timers every minute or so. And then to handle that you need to know when a timer has ended within the second you could have a callback on the server timer that when it ends it notifies the respective user's client that the timer is done just in case they happened to be out of sync since the last time you synced the timers.

There is obviously a bit more to implementing it then just that but hopefully that can help give some basic ideas to how something like this might be handled.

Though since you mentioned text based in your first post (Not sure if it is purely text based or more like Tribal Wars) you might not have to do something as complicated as that. If you don't have a bunch of UI elements that are always on the screen and need to be updated every frame or ten you might be able to just get away with polling the server for updates. Only some performance testing will tell really.

Though it seems to me what you are dealing with is mainly a synchronization issue, so if you happen to have any networking books lying around or better yet a networking book that deals with games they should have a few chapters in it that could give you a lot better ideas how to handle it then I could.

Hopefully that helps a bit. Again I am not to familiar with browser based games so it could be totally different there.
Last edited on
Topic archived. No new replies allowed.