Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Magnomous · Jul 08, 2016 at 04:21 PM · serverdatabasephp

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 08, 2016 at 05:09 PM 0
Share

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

avatar image Magnomous Bunny83 · Jul 08, 2016 at 05:23 PM 0
Share

Yes, this is definitely gonna be helpful. Thank you very much buddy, great post. Appreciated.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges