views:

49

answers:

1

Hey guys,

I'm doing a multi-player text based card game for fun in Django where each card allows each player to make some standard actions (Draw more cards, get gold, get points etc.) and maybe some other abilities (Like destroy a card from an opponents hand, give an opponent minus points and many more).

I have created a Card class:

class Card(models.Model):
    name = models.CharField(max_length=255, verbose_name="Name")
    description = models.TextField(verbose_name="Description")
    victory = models.BooleanField("Victory Card")
    action = models.BooleanField("Action Card")
    reaction = models.BooleanField("Reaction Card")
    treasure = models.BooleanField("Treasure Card")
    attack = models.BooleanField("Attack Card")

    plus_action = models.IntegerField(max_length=2, verbose_name="Plus actions", null=True, blank=True)
    plus_card = models.IntegerField(max_length=2, verbose_name="Plus cards", null=True, blank=True)
    plus_buy = models.IntegerField(max_length=2, verbose_name="Plus buy", null=True, blank=True)
    plus_gold = models.IntegerField(max_length=2, verbose_name="Plus gold", null=True, blank=True)
    plus_victory = models.IntegerField(max_length=2, verbose_name="Plus victory", null=True, blank=True)

    cost = models.IntegerField(max_length=2, verbose_name="Cost")

My problem is that I don't know how to represent the other abilities. I have thought about properties but I'm not sure if that's the way to go or how to go about it.

Do you guys have any suggestions? Thanks in advance!

Regards,
Andri

A: 

If you want a lot of complex properties for your model and don't need any kind of search or filtration by their values, you can implement something like class CardPropetry(object): ... which will contain all needed values.

Then you can serialize/deserialize(django docs) instances of this class into/from db.TextField

Updated:

I don't test this code, but something similar should work

models.py:

import yaml
import pickle
from django.db import models

class CardProperties(onbject):
    pass

class Card(models.Model):

    properties = models.TextField()

    def set_properties(self, obj):
        self.properties = pickle.dumps(obj)

    def get_properties(self):
        return pickle.loads(self.properties)

views.py:

def view(request):

    card = Card.objects.get(pk=key)
    properties = card.get_properties()

    properties.god_mode = True

    card.set_properties(properties)

    card.save()
Tiendil
I've never done anything like that but how would that work? Could you maybe give me an example?
AndriJan
I updated my post, this code should demonstrate the main idea. You also can store properties in dict and save it in json or yaml formats.
Tiendil