Jump to content

Recommended Posts

Imagine a turn-based 1 vs 1 browsergame.

I need to be able to detect when a user goes offline (exits the browser, shuts down the PC, disables JS), and perform some actions.

Since I already have to costantly poll the server with AJAX to get the data, I thought that maybe I could add a line to my code that records in the DB the current time, and check if the other user's last connection time is < than (current time - 3 seconds), in wich case I consider him offline and declare the first user winner (add exp and money etc.)

But, what if one user exits, and after 1 second, the other does the same? Then he does not have time to detect the other user's logout. Nobody gets the Exp, but, most important, the entry in the "active fights" database does not get deleted.

Any ideas?

EDIT: Cron-jobs are a no-go, since Altervista, my PHP hosting website, requires me to pay for them and I can execute them every 60 minutes at best, while I would need them to run max every 5 seconds or so

Edited by ricvail
Link to comment
https://forums.phpfreaks.com/topic/279524-detecting-user-activity/
Share on other sites

Timing out the user as you described is the only way to effectively determine if they have gone offline. You just need to make sure your timeout code handles all the possible scenarios.

 

It sounds like what you are talking about is an instance where both users timeout at the same time, so there is no clear winner because you can't determine who timed out first. What you need to do is just decide what you want to do in such a case, and code that. A few possible options are

1) Consider the fight a draw

2) Consider the fight void and just remove it.

3) Consider who last made a successful move the winner

...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.