5đź‘Ť
Perhaps you might consider separating a skill and it’s associated effect. More that likely, skills will end up having one or more effect associated with them, and that effect could potentially be used by multiple skills.
For example, an effect could be “Does N frost damage to current target”. That effect could be used by the skills “Blizzard Bolt”, “Frost Blast”, and “Icy Nova”.
models.py
class Skill(models.Model):
name = models.CharField()
cost = models.PositiveIntegerField()
effects = models.ManyToManyField(Effect)
class Effect(models.Model):
description = models.CharField()
action = models.CharField()
# Each Django model has a ContentType. So you could store the contenttypes of
# the Player, Enemy, and Breakable model for example
objects_usable_on = models.ManyToManyField(ContentType)
def do_effect(self, **kwargs):
// self.action contains the python module to execute
// for example self.action = 'effects.spells.frost_damage'
// So when called it would look like this:
// Effect.do_effect(damage=50, target=target)
// 'damage=50' gets passed to actions.spells.frost_damage as
// a keyword argument
action = __import__(self.action)
action(**kwargs)
effects\spells.py
def frost_damage(**kwargs):
if 'damage' in kwargs:
target.life -= kwargs['damage']
if target.left <= 0:
# etc. etc.
1đź‘Ť
I’m kind of tired (late here in Sweden), so I am sorry if i misunderstood, but the first thing that popped into my head was extra fields on many-to-many relationships.
- [Django]-Serializer return incorrect output
- [Django]-ImportError : no module named 'models'
- [Django]-Framework/Language for new web 2.0 sites (2008 and 2009)
1đź‘Ť
I would set up some inheritance.
class BaseSkill(models.Model):
name = models.CharField()
cost = models.PositiveIntegerField()
type = models.CharField()
....
class FireSkill(BaseSkill):
burn_time = models.PositiveIntegerField()
def save():
self.type = 'fire_skill'
return super(FireSkill, self).save()
class IceSkill(BaseSkill):
freeze_time = models.PositiveIntegerField()
def save():
self.type = 'ice_skill'
return super(IceSkill, self).save()
The advantages of this are when you just want to list a player skills you all need to work with the BaseSkill class. If a vendor is selling skills you only need to list prices from the BaseSkill class. When you need more detailed attributes of a skill it is easy to take the type to access it. E.g. If you have: skill = BaseSkill.objects().get(pk=1) you can access ice skill by doing skill.ice_skill.freeze_time or more generally get_attribute(skill, skill.type).field_name
- [Django]-How to install the gviz_api library?
- [Django]-Verbose_name_plural unexpected in a model?
- [Django]-How can I disable some options in the Select widget of a mptt's TreeForeignKey field in some model admin?
- [Django]-Django giving error ImportError: No module named 'corsheaders'
1đź‘Ť
When I’ve seen this, there have been two classes: one for the Skill as an abstract instance (e.g. a skill in speaking Swedish, a skill in Excel development) and then the actual skills possessed by a person with a foreign key to the Skill.
- [Django]-Django query for many to many relationship
- [Django]-How do you access/configure summaries/snippets in Django Haystack
- [Django]-Django clean-up code
- [Django]-Formatting Time Django Template
0đź‘Ť
You could also use single table and save inner model based off object in a pickled field.
- [Django]-Django cors headers and server error
- [Django]-Chords of Chords Timeout
- [Django]-Check if a value is equal to the modulo of another number in a Django template