2

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

AndriJan
  • 183
  • 1
  • 11
  • Aww, I was hoping you meant a strategy card game ABOUT Django... :) – Nathan Long Aug 26 '10 at 13:37
  • What are some examples of "other abilities"? Does it involve data that needs to be persisted or things that happen based on data you already have available? – Matthew J Morrison Aug 26 '10 at 13:53
  • @Nathan Long Well, I was hoping the first comment would be something of a help to me :D So I guess we're even ;) – AndriJan Aug 26 '10 at 13:53
  • @Metthew J Morrison Examples would be like I mentioned at the top (destroy a card some other player has) or another one would be I can take a look at what card is next in an opponents deck (or even mine). I don't know if that is clear enough? – AndriJan Aug 26 '10 at 13:56

2 Answers2

0

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
  • 541
  • 3
  • 11
  • I've never done anything like that but how would that work? Could you maybe give me an example? – AndriJan Aug 26 '10 at 15:20
  • 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 Aug 26 '10 at 17:14
0

This answer is far too late to be of any direct use I guess, but for anyone else trying to do something similar...

I reckon you were doing an implementation of Dominion, and you are trying to represent text abilities of the cards. Obviously you don't want to implement each card's mechanics in the main engine, therefore what you need is to store scripts for card effects and execute those instead.

You can store the code in a TextField and do this:

How do I execute a string containing Python code in Python?

Community
  • 1
  • 1
mcyalcin
  • 2,074
  • 1
  • 15
  • 12