I'm working on an online muiltiplayer board game & have a SQL server question.
Lets assume the game allows two players. When the game is created, the creator is added as the first user.
At that point, two users could try to join the game simultaneously. One of these users should be blocked.
My database schema is as follows:
tbGame - contains a list of all games. PrimaryKey is GameId
tbPlayers - contains a list of all registered users. PrimaryKey is PlayerId
tbPlayersInGame - contains a list of all players in each game. Foreign key
relations to tbGame and tbPlayers.
I feel like I need two things:
- A way to lock tbPlayersInGame based on the GameId. This would be used while adding a player to a game. From what I've read it sounds like a key-range lock (on GameId) would be suitable.
- A way to enforce a 1-to-2 relationship so that attempts to add a 3rd player would fail.