Implementing a Django Based Skill

I am working on an RPG using django and am considering various options for implementing part of a skill system.

Let's say I have a base skill class, i.e. something like:

class Skill (models.Model):
      name = models.CharField()
      cost = models.PositiveIntegerField()
      blah blah blah

What will be some approaches to the implementation of specific skills? The first option that comes to mind:

1) Each skill increases the class of skills and redefines certain functions:

I don't know how this will work in django. It seems that having a db table for each skill would be redundant. Can a child class be abstract while the Skill class has an entry? That doesn't sound right. How about using a proxy class?

What are some other options. I would like to avoid an approach scenario for a pure django approach.

+3
5

, . , , , .

, " N ". "Blizzard Bolt", "Frost Blast" "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)

\spells.py

def frost_damage(**kwargs):
    if 'damage' in kwargs:
        target.life -= kwargs['damage']

        if target.left <= 0:
            # etc. etc.
+5

( , ), , , , , "--" .

+1

.

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()

- , BaseSkill. , BaseSkill. , . . : skill = BaseSkill.objects(). Get (pk = 1), , skill.ice_skill.freeze_time , , get_attribute (skill, skill.type).field_name

+1

, : (, -, Excel), , .

+1

You can also use a single table and save the internal model based on the object in a pickled field.

0
source

Source: https://habr.com/ru/post/1724565/


All Articles