Author Topic: Quick question  (Read 651 times)

Offline toxin

  • Level 21
  • *
  • Posts: 231
  • Reputation: +4/-2
    • View Profile
    • Encore Montreal
Quick question
« on: February 26, 2011, 03:32:15 AM »
I am working on a war game. The game will have soldiers and upgrades that improve a users stats. My questions is should I store the users stats in the database? Number of solders and upgrade level will be in the database. With this I can get the total and display to user or get an outcome of a battle without calling extra info from the database.

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Quick question
« Reply #1 on: February 26, 2011, 03:40:23 AM »
Yes. What alternative ideas do you have in storing/remembering user information?

Offline toxin

  • Level 21
  • *
  • Posts: 231
  • Reputation: +4/-2
    • View Profile
    • Encore Montreal
Re: Quick question
« Reply #2 on: February 26, 2011, 05:02:49 AM »
Math. I would be keeping non stat totals in the database.
Say I have the stat Attack it uses attack solders and a siege upgrade in the database i have the number of soldiers and the siege upgrade stored.
If a user trains 50 attack soldiers I would update only the number of soldiers in the database.
To display what the stat is i would use a formula. This would be the same formula used to update a stat if stored in the database.

Offline DV8

  • Level 10
  • *
  • Posts: 63
  • Reputation: +0/-0
    • View Profile
    • Shadowrun: Corrosion
Re: Quick question
« Reply #3 on: February 26, 2011, 07:43:45 AM »
Ah, now I get it. In principle, the rules of normalised data say that you never store results of an equation if you also store the elements in the same equation. There are examples where it would become a pain to recalculate everything all the time, but depending on how complex things become it might be worth it.

Offline chrisjenkinson

  • Level 10
  • *
  • Posts: 61
  • Reputation: +0/-0
    • View Profile
    • Xiphos
Re: Quick question
« Reply #4 on: February 26, 2011, 08:23:33 AM »
You can do the calculations in SQL in most cases, which will normally be faster than doing them in PHP, Ruby or whatever script language you are using.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Quick question
« Reply #5 on: February 28, 2011, 10:05:07 AM »
You can do the calculations in SQL in most cases, which will normally be faster than doing them in PHP, Ruby or whatever script language you are using.
This is evil. Do not do it. Every single SQL optimization manual/site/article I ever read repeat the mantra "do not use SQL as a calculator".

Offline chrisjenkinson

  • Level 10
  • *
  • Posts: 61
  • Reputation: +0/-0
    • View Profile
    • Xiphos
Re: Quick question
« Reply #6 on: February 28, 2011, 11:21:07 AM »
As with so many questions, "it depends", actually. This StackOverflow topic is quite good:

http://programmers.stackexchange.com/questions/19392/when-is-it-better-to-offload-work-to-the-rdbms-rather-than-to-do-it-in-code

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal