Terrain tiles #5

Open
wants to merge 9 commits into
from
Jump to file
+399 −9
Split
@@ -21,4 +21,12 @@
admin.site.register(models.SettlementTerrain)
admin.site.register(models.SettlementTerrainResourceCount,
list_display = ["pk", "kind", "terrain", "count", "timestamp", "natural_rate", "rate_adjustment", "rate"]
-)
+)
+admin.site.register(models.TileClass,
+ list_display = ['name', 'slug']
+)
+admin.site.register(models.Tile,
+ list_display = ['continent', 'content_type', 'content_object', 'name', 'cls'],
+ list_display_links = ('name', 'cls'),
+ ordering = ['content_type', 'name', 'cls']
+)
@@ -1,7 +1,45 @@
+import os.path
+from random import randint
+
+from django.conf import settings
from django.db import models
+from django.utils.functional import curry
+
+TILES_URL = getattr(settings, 'TILES_URL', os.path.join(settings.MEDIA_URL, 'img', 'tiles'))
class KindManager(models.Manager):
def get_by_natural_key(self, slug):
return self.get(slug=slug)
+
+
+class TileManager(models.Manager):
+ def get_random_tile(self):
+ num_tiles = self.count()
+ return os.path.join(TILES_URL, self.all()[randint(0, num_tiles-1)].filename)
+
+ def _get_random_CLASS_tile(self, tile_class):
+ qs = self.filter(cls=tile_class)
+ num_tiles = qs.count()
+ if qs:
+ return os.path.join(TILES_URL, qs[randint(0, num_tiles-1)].filename)
+ else:
+ return ''
+
+ def add_accessor_methods(self, *args, **kwargs):
+ #The import is here to avoid a circular dependency
+ #The try: except: block is to avoid error when the TileClass model doesn't exits
+ #such as when doing the initial syncdb
+ try:
+ from models import TileClass
+ for cls in TileClass.objects.all():
+ if not hasattr(self, 'get_random_%s_tile' % cls):
+ setattr(self, 'get_random_%s_tile' % cls, curry(self._get_random_CLASS_tile, tile_class=cls))
+ except:
+ pass
+
+
+ def __init__(self, *args, **kwargs):
+ self.add_accessor_methods()
+ super(TileManager, self).__init__(*args, **kwargs)
@@ -7,11 +7,12 @@
from django.db import models, transaction
from django.contrib.auth.models import User
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes import generic
-from manoria.managers import KindManager
+from manoria.managers import KindManager, TileManager
from manoria.utils import weighted_choices
-
class Player(models.Model):
"""
A single player associated to a User
@@ -442,6 +443,7 @@ class BuildingKind(BaseKind):
"""
build_time = models.IntegerField()
+ tiles = generic.GenericRelation('Tile')
class BuildingCost(models.Model):
@@ -709,6 +711,7 @@ class SettlementTerrainKind(BaseKind):
buildable_on = models.BooleanField(default=True)
produces = models.ManyToManyField(ResourceKind)
+ tiles = generic.GenericRelation('Tile')
class SettlementTerrain(models.Model):
@@ -746,3 +749,23 @@ class SettlementTerrainResourceCount(BaseResourceCount):
kind = models.ForeignKey(ResourceKind)
terrain = models.ForeignKey(SettlementTerrain)
+
+
+class TileClass(BaseKind):
+ def __unicode__(self):
+ return self.slug
+
+
+class Tile(BaseKind):
+
+ continent = models.ForeignKey(Continent)
+ content_type = models.ForeignKey(ContentType, limit_choices_to = {"model__in": ("settlementterrainkind", "buildingkind")})
+ object_id = models.PositiveIntegerField()
+ content_object = generic.GenericForeignKey('content_type', 'object_id')
+ filename = models.CharField(max_length = 32)
+ cls = models.ForeignKey(TileClass, verbose_name = u'class')
+
+ objects = TileManager()
+
+ def __unicode__(self):
+ return self.slug
Oops, something went wrong.