views:

37

answers:

1

I'm creating a turn and text-based strategy game in Django on Google App Engine with app-engine-patch. The simplified concept is that each player build can several different units and buildings to improve their base and fight other players for points.

My question involves the designing of the Django models, it seems to me that the buildings and units, which will have different attack power, life and such, should be their own models like so:

class Unit(db.Model):
    name = db.StringProperty()
    type = db.ReferenceProperty(UnitType)
    targets = KeyListProperty(UnitType)
    attack = db.IntegerProperty()
    life = db.IntegerProperty()
    price = db.IntegerProperty()

My problem then comes to how to easiest be able to set the players amount of a specific unit/building. As an example a player should be able to purchase, say 15 airplanes.

I then could just set "airplane" as an IntegerProperty in the player model and use the unit name as an identifier when getting the attack power and life of the airplane. That would however not be a very dynamic design, since the player model does not know if the unit "airplane" is actually present yet. I would like the player model to mirror the existing Unit/Building-models in someway.

This is my first try on a web-based game so I might be totally off track, does anyone have input on this? Is there a better way to do it?

+2  A: 

How about deriving from the Unit class?

class Airplane(Unit):
    owner = db.ReferenceProperty(User)

That way your User class would automatically get an airplane_set collection (well, a query really) according to the documentation

Edit

Alternatively you could implement an the Airplane class like this:

class Airplane(db.Model):
    unit = ReferenceProperty(Unit)
    amount = IntegerProperty()
    user = db.ReferenceProperty(User)
klausbyskov
Thanks for your answer. This might work, but wouldn't this mean that a purchase of 15 planes creates 15 Airplane objects? Isn't that a waste when all I really need to know is the amount?
allmanheten
@allmanheten, good point, I have updated my answer.
klausbyskov
Thank you, I will try this approach. :)
allmanheten