Author Topic: [Database] Tabledesign question  (Read 784 times)

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
[Database] Tabledesign question
« on: August 22, 2009, 10:06:11 AM »
Ok, so I finally have the time to get back to making games.

Currently I'm working on the battle engine. Two players and their units (1-16 per player) are going to duke it out. During combat, certain things can happen to a unit. They can be set on fire, put to sleep, paralyzed, slowed, etc (you get the idea).

Now my question is how to design the user-owned units table.
I was thinking of doing the following:
- unitid (which unit this is)
- playerid (to which player this belongs)
- ... (different fields like level, etc.)
- on_fire (1 if the unit is on fire, 0 if not)
- sleep (1 it it's asleep, 0 if awake)
- etc.

Of course there is the other option of having a table with:
- statusid (which status this is)
- unitid (which unit is affected)
- affected_by (by which status this is affected 1 = on fire, 2 = asleep...)

Now I'm wondering which one would be better?
The first one uses more space in the playerunits table, but no additional tables, the second one uses smaller but multiple tables.

Now a bit of background maybe: as I said, each battle can be fought from 2 up to 32 units. Every player can have an (as of now) unlimited amount of units. Each unit can be affected by 4-6,7 (still to be decided) different things at once.

I know, generally normalization is better, but what about a case where there could be a new table with n*6 (n = number of units in the whole game) entries vs. extending the units-table by 6 fields?

[edit] Oh, by better I mean which one of the two approaches (or a third approach I didn't list here) would be faster and more importantly more stable for a lot of queries.
« Last Edit: August 22, 2009, 10:10:32 AM by Sinzygy »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: [Database] Tabledesign question
« Reply #1 on: August 22, 2009, 10:56:02 AM »
Use bits, 32 values in one field then :D

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: [Database] Tabledesign question
« Reply #2 on: August 22, 2009, 01:12:55 PM »
I would go with option #1 first and make the table an INNODB so you get the speed of row locking during the updates/inserts. Once the game was going and you actually had players playing then I would look at performance issues, if there are any :)

Creating online addictions, one game at a time:

Offline Scion

  • Level 27
  • **
  • Posts: 402
  • Reputation: +11/-0
    • View Profile
Re: [Database] Tabledesign question
« Reply #3 on: August 24, 2009, 02:38:32 AM »
while im normally a fan of denormalisation, i too would probably go with the one table.

Especially if you keep the number of different affected_by types down..... At the begining id go with a small fixed number of affected_by types stored in their own individual boolean columns...to keep it as simple as possible in the begining.

However later if you find yourself wanting to add additional types then chris's suggestion of using bit operators is also a good idea. Id hide any complexity inside an object or behind some methods.


Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: [Database] Tabledesign question
« Reply #4 on: August 24, 2009, 04:01:54 AM »
Yeah, I went with Chris' idea and just stored all status as a string.
Thanks for the quick help. Now back to coding :D (Spent a whole day yesterday and managed to get my inventory working :P)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: [Database] Tabledesign question
« Reply #5 on: August 30, 2009, 04:08:58 AM »
I was suggesting bit operations (within one byte/word/long word), not strings (within a set of several bytes). The former is much better performance wise.
http://pl2.php.net/manual/en/language.operators.bitwise.php

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal