Jump to content
  • Advertisement
Sign in to follow this  
Legendre

PHP/MySQL Game - Networking and Websockets?

This topic is 2481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been learning PHP and MySQL by writing simple games. The current game that I am working on works this way:

1) Players sign up for an account and login.
2) Once they are logged in, they get sent to this webpage with an ANSI tile-based map in which they can walk around (usual 4 directions).
3) There is a global chat which is done by players posting messages to a database, and latest few messages are displayed (old messages deleted).

I am thinking of turning this into a game where players try to find an item hidden in a maze. So, they will wander around the ANSI tile-based map, until they find the room with the item (a row in the table for each room, a column labeled "item" containing a "1" if the room has the item, "0" otherwise) and a button will show up allowing them to pick up the item. First player to do so wins the game, and everything resets, with the item in a new random location.

Question:

How do I handle two players clicking on the pick up item button at the same time? Is this where networking and websockets come in? Or, can I just write it this way: "Upon clicking the pick up item button, a check is made to the database to see if the "Item" entry is 0 or 1. If its 1, the item is picked up and the entry is set to 0. If 0, an error message is received."

And the PHP/MySQL will sort it out who clicked first? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
use a database engine that supports transactions (With older MySQL versions you should probably go with InnoDB for this, newer ones should have more options), do the check and update as a transaction.

Alternativly since you only have 1 item per game you could just lock the entire table when a user tries to pick the item up (and unlock if afterwards). (Locks will block access to the table while you do the pickup so its not something you want to use frequently if you have alot of users but for this specific case it works fairly well)

If you don't use transactions or locks then it is possible for user 1 and 2 to click at almost the same time and both pass the check (2nd check can happen before the first one has updated the value in the DB if both requests are processed in parallel)

Share this post


Link to post
Share on other sites

use a database engine that supports transactions (With older MySQL versions you should probably go with InnoDB for this, newer ones should have more options), do the check and update as a transaction.

Alternativly since you only have 1 item per game you could just lock the entire table when a user tries to pick the item up (and unlock if afterwards). (Locks will block access to the table while you do the pickup so its not something you want to use frequently if you have alot of users but for this specific case it works fairly well)

If you don't use transactions or locks then it is possible for user 1 and 2 to click at almost the same time and both pass the check (2nd check can happen before the first one has updated the value in the DB if both requests are processed in parallel)


Oh thanks. I found a chapter in the book I am using on MySQL Transactions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!