tags:

views:

81

answers:

2

I'm attempting to write a browser game using Django but I'm getting a bit stuck on how to store the settings for the game. For example, the game is tick based and I want to store the current tick. I have decided that I want only one game per database to avoid problems with the built-in user authorisation system (e.g. I don't want to say username X is unavailable because it is already used in a different game). As far as I can tell, I still need to store this information in a database table, but I'm not sure how to best do that. I seem to have 2 options:

A) Have the game as a normal model referenced by other tables (e.g. my User profile), and just ignore the possibility that there can be more than 1 game. This would mean that it would be technically possible to have 2 game rows, but if there were things would break very easily.

B) I have a model that I always assume has one and only one row which stores all the configuration data for the game. This model only contains static methods and none of the other models have references to it. E.g.:

class Game(models.Model):
    current_slot = models.PositiveIntegerField(default=0)

    @staticmethod
    def slots_per_day(self):
        Genre.objects.get(id=1).current_slot

Neither of these options seem "right" to me but can anyone tell me if one is better than the other? Or if there is another option I haven't seen yet?

+3  A: 

You could be a bit more generic and just have a "Setting" record. This would allow you to expand the amount of settings you could store infinitely.

class Setting(models.Model):
    name = models.CharField(max_length=50)
    value = models.TextField()

# ...

# Get the current slot setting
current_slot = Setting.objects.get(name='current_slot').value

# ...

# Or wrap it in a helper method
def get_setting(name, default_value):
    try:
        return Setting.objects.get(name=name).value
    except:
        return default_value

current_slot = get_setting('current_slot', 0)
T. Stone
Here's the obvious solution I knew I was misisng. Funny thing is I've done this in the past.
Bonnici
A: 

I don't understand why you need one database per game. Why not have a Games table with one row per game, and have all other data tables reference the game_id of the game they're associated with?

Aaron
Mainly because of complications with the User model being database-wide. It's possible to do it that way but I just think it would get a bit messy.
Bonnici
I was also working on a game in django, and I had a global User model and then also a Player model tied to a particular game instance.
Aaron