~widelands-dev/widelands-website/django_staticfiles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from django.db import models


class Tribe(models.Model):
    name = models.CharField(max_length=100)
    displayname = models.CharField(max_length=100)
    descr = models.TextField()
    icon_url = models.CharField(max_length=256)
    network_pdf_url = models.CharField(max_length=256)
    network_gif_url = models.CharField(max_length=256)
    
    class Meta:
        ordering = ['name']


    def __unicode__(self):
        return u'%s' % self.name


class Worker(models.Model):

    name = models.CharField(max_length=100)
    displayname = models.CharField(max_length=100)
    tribe = models.ForeignKey(Tribe)
    # URL to include this, i wasn't able to feed django local images
    image_url = models.CharField(max_length=256)
    graph_url = models.CharField(max_length=256)  # URL to the help graph
    imagemap = models.TextField()  # the image map for the help graph

    # This limit shall probably cover the longest help (found 209, nothing
    # more)
    help = models.TextField(max_length=256)
    exp = models.TextField(max_length=8)  # Just in case
    becomes = models.OneToOneField(
        'self', related_name='trained_by_experience', blank=True, null=True)

    class Meta:
        ordering = ['name']


    def __unicode__(self):
        return u'%s' % self.name


class Ware(models.Model):
    name = models.CharField(max_length=100)
    displayname = models.CharField(max_length=100)
    tribe = models.ForeignKey(Tribe)
    # URL to include this, i wasn't able to feed django local images
    image_url = models.CharField(max_length=256)
    graph_url = models.CharField(max_length=256)  # URL to the help graph
    imagemap = models.TextField()  # the image map for the help graph

    # This limit shall probably cover the longest help (found 209, nothing
    # more)
    help = models.TextField(max_length=256)

    class Meta:
        ordering = ['name']


    def __unicode__(self):
        return u'%s' % self.name


class BuildingManager(models.Manager):

    def small(self):
        return self.all().filter(size='S')

    def medium(self):
        return self.all().filter(size='M')

    def big(self):
        return self.all().filter(size='B')

    def mine(self):
        return self.all().filter(size='I')

    def port(self):
        return self.all().filter(size='P')

    def headquarters(self):
        return self.all().filter(size='H')

        # return self.build_wares.count()

    pass


class Building(models.Model):
    SIZES = (
            ('S', 'small'),
            ('M', 'medium'),
            ('B', 'big'),
            ('I', 'mine'),
            ('P', 'port'),
            ('H', 'headquarters'),
    )
    TYPES = (
            ('P', 'productionsite'),
            ('W', 'warehouse'),
            ('M', 'militarysite'),
            ('T', 'trainingsite'),
            ('m', 'market'),
    )

    objects = BuildingManager()

    name = models.CharField(max_length=100)
    displayname = models.CharField(max_length=100)
    tribe = models.ForeignKey(Tribe)
    # URL to include this, i wasn't able to feed django local images
    image_url = models.CharField(max_length=256)
    graph_url = models.CharField(max_length=256)  # URL to the help graph
    imagemap = models.TextField()  # the image map for the help graph

    size = models.CharField(max_length=1, choices=SIZES)
    type = models.CharField(max_length=1, choices=TYPES)  # productionsite...

    help = models.TextField(blank=True)

    # Enhances to
    enhancement = models.OneToOneField(
        'self', related_name='enhanced_from', blank=True, null=True)

    # Build cost
    build_wares = models.ManyToManyField(
        Ware, related_name='build_ware_for_buildings', blank=True)
    # ' '.joined() integer strings
    build_costs = models.CharField(max_length=100, blank=True)

    # Workers
    workers_types = models.ManyToManyField(
        Worker, related_name='workers_for_buildings', blank=True)
    # ' '.joined() integer strings
    workers_count = models.CharField(max_length=100, blank=True)

    # Store
    store_wares = models.ManyToManyField(
        Ware, related_name='stored_ware_for_buildings', blank=True)
    # ' '.joined() integer strings
    store_count = models.CharField(max_length=100, blank=True)

    # Output
    output_wares = models.ManyToManyField(
        Ware, related_name='produced_by_buildings', blank=True)
    output_workers = models.ManyToManyField(
        Worker, related_name='trained_by_buildings', blank=True)
    
    class Meta:
        ordering = ['name']


    def save(self, *args, **kwargs):

        tdict = dict((b, a) for a, b in self.TYPES)
        sdict = dict((b, a) for a, b in self.SIZES)

        self.type = tdict.get(self.type, self.type)
        self.size = sdict.get(self.size, self.size)

        return models.Model.save(self, *args, **kwargs)

    def has_build_cost(self):
        return (self.build_wares.all().count() != 0)

    def get_build_cost(self):
        # Creating the relation between build_cost and build_wares
        # Querying the wares returns the wares in alphabetical order!
        count = map(int, self.build_costs.split())
        for c, w in zip(count, self.build_wares.all()):
            yield [w] * c

    def has_workers(self):
        return (self.workers_types.all().count() != 0)

    def get_workers(self):
        count = map(int, self.workers_count.split())
        for c, wor in zip(count, self.workers_types.all()):
            yield [wor] * c

    def produces(self):
        return (self.output_wares.all().count() != 0)

    def get_ware_outputs(self):
        return self.output_wares.all()

    def trains(self):
        return (self.output_workers.all().count() != 0)

    def get_worker_outputs(self):
        return self.output_workers.all()

    def has_outputs(self):
        return (self.output_workers.all().count() != 0 or self.output_wares.all().count() != 0)

    def has_stored_wares(self):
        return (self.store_wares.all().count() != 0)

    def get_stored_wares(self):
        count = map(int, self.store_count.split())
        for c, w in zip(count, self.store_wares.all()):
            yield [w] * c

    def __unicode__(self):
        return u"%s/%s" % (self.tribe.name, self.name)