I'm developing a card game and am currently working on a system that allows players to preselect a card which is to be played automatically (from the server) as soon as it is the card's owner's turn to play. I set it up as follows.
- Clicking a card out of turn preselects that card, meaning an out of turn action (with corresponding ajax call) is carried out which updates the id of the preselected card for that player in the database. If the card is deselected, the same action resets the preselected card id for that player to -1.
- Before each playerTurn state, there is a "game" type state prePlayerTurn which checks whether the active player has a preselected card and, if allowed by the rules, plays that card and skips to the prePlayerTurn for the next player.
- "Playing a card" means that a method playCard is called, which among other things, sets the preselected card id of the card owner back to -1. (After all, there is no longer any preselected card after it was played.)
- If the player has no eligible preselected card, the state prePlayerTurn transitions into an "activeplayer" type state named playerTurn, which gives the turn to the player and waits till he clicks a card which is subsequently played.
The problem comes when two subsequent players have preselected an eligible card and it is the first of these two to play. In such a case, sometimes the second preselected card is not played, and sometimes this is accopanied by an sql deadlock error message. I'm quite certain that the cause of this problem is the fact that the playCard method is called twice with very little delay, which causes two subsequent database updates to reset the preselected card id to -1 for these two players, creating a deadlock for the second update.
Question: how can I make sure the update of the preselected card id for the second player is put on hold until the first player's update is done?
Or if you have another solution, that would of course also be appreciated
Thanks!
- Clicking a card out of turn preselects that card, meaning an out of turn action (with corresponding ajax call) is carried out which updates the id of the preselected card for that player in the database. If the card is deselected, the same action resets the preselected card id for that player to -1.
- Before each playerTurn state, there is a "game" type state prePlayerTurn which checks whether the active player has a preselected card and, if allowed by the rules, plays that card and skips to the prePlayerTurn for the next player.
- "Playing a card" means that a method playCard is called, which among other things, sets the preselected card id of the card owner back to -1. (After all, there is no longer any preselected card after it was played.)
- If the player has no eligible preselected card, the state prePlayerTurn transitions into an "activeplayer" type state named playerTurn, which gives the turn to the player and waits till he clicks a card which is subsequently played.
The problem comes when two subsequent players have preselected an eligible card and it is the first of these two to play. In such a case, sometimes the second preselected card is not played, and sometimes this is accopanied by an sql deadlock error message. I'm quite certain that the cause of this problem is the fact that the playCard method is called twice with very little delay, which causes two subsequent database updates to reset the preselected card id to -1 for these two players, creating a deadlock for the second update.
Question: how can I make sure the update of the preselected card id for the second player is put on hold until the first player's update is done?
Or if you have another solution, that would of course also be appreciated
Thanks!