1

I am new to DB design. I am trying to write a board game (4 players max) and was trying to come up with a way to communicate moves among each other. I am using a DB for this as per suggestions on stackoverflow.

My problem is this - When player A makes a move that move has to be read by B,C and D. So the fact that A made the move needs to be communicated to B,C and D. I am doing it the following way. Please tell me if there is a better way to do it. To me it seems all wrong and incredibly flaky.

I have a table with the following fields -

gameId, userMove, flagA, flagB, flagC, flagD

So when A makes the move I write among other things - (flagA=0, flagB=1, flagC=1, flagD=1)

When B,C or D read A's move they decrement their corresponding flag.

A will not update the table unless all flags are 0.

Same thing happens when others make their moves.

Comments? There has to be a better way for this. The things I am seeing wrong here -

  • I am looping on a select until all flags are 0 for A
  • I am looping on a select until the flag for the corresponding user is set to read the move.

That is a lot of server load and client timeouts I need to worry about.

I hope I have been able to explain my problem clearly. Please ask questions if needed.

Any help is appreciated.

EDIT: The game is web based (runs in a browser) and I am using php for the server side development and so I cannot use an in-memory cache though I would have loved to do that if possible.

Thanks, - Pav

user220201
  • 4,514
  • 6
  • 49
  • 69
  • Looping meaning polling, or looping meaning iterating? – Jé Queue Dec 28 '10 at 23:22
  • i don't quite understand why you would use a database at all ? to communicate the moves between players ? as Daniel pointed out, a DB is not the best choice for that purpose, you'd be much better off using local memory – yurib Dec 28 '10 at 23:23

3 Answers3

1

If the players of your game will be interacting with one game server during a single game session, it looks like you can keep all that state in memory.

Databases are great for durable storage of data with guarantees for atomicity, consistency and integrity. However, you don't seem to need any of these features for the temporal state that you are describing.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 1
    Exactly why I did not want to use a DB for this kind of data. But my game is web based (it runs in a browser) and I am using php for the implementation. I have not way to maintain an in-memory cache for this. It would be ideal though. Are you familiar with php development? – user220201 Dec 29 '10 at 00:01
  • @user220201: There are definitely ways to share data in memory between different requests to php. You may want to check this question: http://stackoverflow.com/questions/4089361/how-can-i-store-data-in-ram-memory-using-php. Feel free to post a new question if you want further info on this topic. Php experts will be willing to help. – Daniel Vassallo Dec 29 '10 at 00:04
  • Daniel, The other advantage with using a DB will allow me to scale without having to worry about having all the players in a game go to the same server instance. – user220201 Dec 29 '10 at 00:17
  • 1
    Well yes... You also get another advantage in that if the server crashes, you can restart and resume a game. – Daniel Vassallo Dec 29 '10 at 00:18
0

If flagA,B,C and D are all bits you might consider putting them all into one column and treating that column as a bit mask.

This will allow one column to control all flags. It can make your selects and updates much cleaner.

Read up on using bitmasks here:

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • That is true - and it would allow for expanding the number of other players, as well - but bear in mind that doing so would eliminate your ability to do indexed lookups on those columns. – TehShrike Dec 28 '10 at 23:26
  • Making it a flag is a good idea. The problem is I don't improve much on the looping part which is what I really want to remove. – user220201 Dec 29 '10 at 00:02
  • actually using bitwise operators you should be able to avoid looping. Can you provide an example where you think you wouldn't be able to? – Abe Miessler Dec 29 '10 at 00:28
0

Have you considered usng a file to store the info?

RC_Cleland
  • 2,274
  • 14
  • 16