- Home /
Players online counter
Hello, I'm making a smartphone game and currently I work on a panel, that would look something like this:
Players online: 10
Players in queue: 4
Players in game: 40
I've got a database, that has 3 booleans: isOnline, isInQueue, isInGame. When player logs in, the php script is called and this script also sets this player's boolean isOnline to true. But how could I possibly set this boolean to false when player quits the game? I was thinking about the coroutine call, which would call the php script, and this coroutine would start in the OnApplicationQuit(), but:
a) Would this coroutine end before the application quits?
b) I want to set isOnline to false even when player's network connection drops, not only when they quit the app.
c) especially on iOS the app could be just suspended, not quit, hence the OnApplicationQuit would not be called
So, I was thinking about sending every 10 seconds a query to the server informing about player's status. And when the server does not recieve any query from player who has isOnline or isInQueue or isInGame set to true within like 20 seconds, then these three variables would be set to false.
But how could I implement this? Or is there any better solution?
Thanks for any help in advance.
Answer by Bunny83 · Jul 08, 2016 at 04:46 PM
You could represent the state implicitly by using a logout timestamp. You simply set the timestamp to a certain time in the future (i.e. 10 seconds / 1 minute / ...). If the current time is larger than the timestamp the user is considered offline.
The boolean can be obtained implicit from an SQL query
"select (now() > logoutTimestamp) as "offline" from users;"
This will return a single column called "offline" and contains a "1" if the user is offline and a "0" if he's online. Of course you can turn that into an "online" field by reversing the comparison <
To update the timestamp you would simply call this frequently:
"update users set logoutTimestamp=AddTime(now(), "00:00:10") where id="+ userID+";"
This will set the timeout to the current time + 10 seconds. So the user is logged out automatically after 10 seconds unless you constantly update it.
In addition you know for how long a user hasn't been logged in as the timestamp holds the last logout date / time. I recommend more than 10 seconds. 1 minute minimum.
You can actively logout a user by simply doing
"update users set logoutTimestamp=now() where id="+ userID+";"
I recommend a similar approach for the other states as well.
For example to get the amount of players which are online / offline you can do this:
"select (now() < logoutTimestamp) as 'online', count(id) as 'count' from users group by online"
This will return either one or two rows, one for each "online state". If all users are online / offline, only one row is returned. So if there are 20 users in the table the result might look like this:
online count 0 11 1 9 // or is all users are offline
online | count
--------|-------
0 | 20
Yes, this is definitely gonna be helpful. Thank you very much buddy, great post. Appreciated.
Your answer
Follow this Question
Related Questions
Smart way of using servers and databases in unity 0 Answers
Database and Unity 0 Answers
Upload an image onto the database from Unity 1 Answer
php, sql security 3 Answers
Accessing a database though $_GET 1 Answer