~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlhelp/models.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.db import models
2
2
 
3
 
import settings
4
 
if settings.USE_SPHINX:
5
 
    from djangosphinx.models import SphinxSearch
6
3
 
7
4
class Tribe(models.Model):
8
5
    name = models.CharField(max_length=100)
9
6
    displayname = models.CharField(max_length=100)
 
7
    descr = models.TextField()
 
8
    icon_url = models.CharField(max_length=256)
 
9
    network_pdf_url = models.CharField(max_length=256)
 
10
    network_gif_url = models.CharField(max_length=256)
 
11
    
 
12
    class Meta:
 
13
        ordering = ['name']
 
14
 
 
15
 
 
16
    def __str__(self):
 
17
        return '%s' % self.name
10
18
 
11
19
 
12
20
class Worker(models.Model):
13
 
    if settings.USE_SPHINX:
14
 
        search          = SphinxSearch(
15
 
            weights = {
16
 
                'displayname': 100,
17
 
                'help': 60,
18
 
                'name': 20,
19
 
                }
20
 
        )
21
21
 
22
22
    name = models.CharField(max_length=100)
23
23
    displayname = models.CharField(max_length=100)
24
24
    tribe = models.ForeignKey(Tribe)
25
 
    image_url = models.CharField( max_length=256 ) # URL to include this, i wasn't able to feed django local images
26
 
    graph_url = models.CharField( max_length=256 ) # URL to the help graph
27
 
    imagemap = models.TextField() # the image map for the help graph
28
 
 
29
 
    help = models.TextField(max_length=256) # This limit shall probably cover the longest help (found 209, nothing more)
30
 
    exp = models.TextField(max_length=8) # Just in case
31
 
    becomes = models.OneToOneField('self', related_name="trained_by_experience", blank=True, null=True)
32
 
 
33
 
    def __unicode__(self):
34
 
        return u'%s' % self.name
 
25
    # URL to include this, i wasn't able to feed django local images
 
26
    image_url = models.CharField(max_length=256)
 
27
    graph_url = models.CharField(max_length=256)  # URL to the help graph
 
28
    imagemap = models.TextField()  # the image map for the help graph
 
29
 
 
30
    # This limit shall probably cover the longest help (found 209, nothing
 
31
    # more)
 
32
    help = models.TextField(max_length=256)
 
33
    exp = models.TextField(max_length=8)  # Just in case
 
34
    becomes = models.OneToOneField(
 
35
        'self', related_name='trained_by_experience', blank=True, null=True)
 
36
 
 
37
    class Meta:
 
38
        ordering = ['name']
 
39
 
 
40
 
 
41
    def __str__(self):
 
42
        return '%s' % self.name
35
43
 
36
44
 
37
45
class Ware(models.Model):
38
46
    name = models.CharField(max_length=100)
39
47
    displayname = models.CharField(max_length=100)
40
48
    tribe = models.ForeignKey(Tribe)
41
 
    image_url = models.CharField( max_length=256 ) # URL to include this, i wasn't able to feed django local images
42
 
    graph_url = models.CharField( max_length=256 ) # URL to the help graph
43
 
    imagemap = models.TextField() # the image map for the help graph
44
 
 
45
 
    help = models.TextField(max_length=256) # This limit shall probably cover the longest help (found 209, nothing more)
46
 
 
47
 
    if settings.USE_SPHINX:
48
 
        search          = SphinxSearch(
49
 
            weights = {
50
 
                'displayname': 100,
51
 
                'help': 60,
52
 
                'name': 20,
53
 
                }
54
 
        )
55
 
 
56
 
 
57
 
    def __unicode__(self):
58
 
        return u'%s' % self.name
 
49
    # URL to include this, i wasn't able to feed django local images
 
50
    image_url = models.CharField(max_length=256)
 
51
    graph_url = models.CharField(max_length=256)  # URL to the help graph
 
52
    imagemap = models.TextField()  # the image map for the help graph
 
53
 
 
54
    # This limit shall probably cover the longest help (found 209, nothing
 
55
    # more)
 
56
    help = models.TextField(max_length=256)
 
57
 
 
58
    class Meta:
 
59
        ordering = ['name']
 
60
 
 
61
 
 
62
    def __str__(self):
 
63
        return '%s' % self.name
59
64
 
60
65
 
61
66
class BuildingManager(models.Manager):
 
67
 
62
68
    def small(self):
63
 
        return self.all().filter(size="S")
 
69
        return self.all().filter(size='S')
 
70
 
64
71
    def medium(self):
65
 
        return self.all().filter(size="M")
 
72
        return self.all().filter(size='M')
 
73
 
66
74
    def big(self):
67
 
        return self.all().filter(size="B")
 
75
        return self.all().filter(size='B')
 
76
 
68
77
    def mine(self):
69
 
        return self.all().filter(size="I")
70
 
 
 
78
        return self.all().filter(size='I')
 
79
 
 
80
    def port(self):
 
81
        return self.all().filter(size='P')
 
82
 
 
83
    def headquarters(self):
 
84
        return self.all().filter(size='H')
71
85
 
72
86
        # return self.build_wares.count()
73
87
 
74
88
    pass
75
89
 
 
90
 
76
91
class Building(models.Model):
77
92
    SIZES = (
78
93
            ('S', 'small'),
79
94
            ('M', 'medium'),
80
95
            ('B', 'big'),
81
96
            ('I', 'mine'),
 
97
            ('P', 'port'),
 
98
            ('H', 'headquarters'),
82
99
    )
83
100
    TYPES = (
84
101
            ('P', 'productionsite'),
85
102
            ('W', 'warehouse'),
86
 
            ('M', 'military site'),
87
 
            ('T', 'trainings site'),
 
103
            ('M', 'militarysite'),
 
104
            ('T', 'trainingsite'),
 
105
            ('m', 'market'),
88
106
    )
89
 
    
 
107
 
90
108
    objects = BuildingManager()
91
 
    
92
 
    if settings.USE_SPHINX:
93
 
        search          = SphinxSearch(
94
 
            weights = {
95
 
                'displayname': 100,
96
 
                'help': 60,
97
 
                'name': 20,
98
 
                }
99
 
        )
100
 
 
101
109
 
102
110
    name = models.CharField(max_length=100)
103
111
    displayname = models.CharField(max_length=100)
104
112
    tribe = models.ForeignKey(Tribe)
105
 
    image_url = models.CharField( max_length=256 ) # URL to include this, i wasn't able to feed django local images
106
 
    graph_url = models.CharField( max_length=256 ) # URL to the help graph
107
 
    imagemap = models.TextField() # the image map for the help graph
 
113
    # URL to include this, i wasn't able to feed django local images
 
114
    image_url = models.CharField(max_length=256)
 
115
    graph_url = models.CharField(max_length=256)  # URL to the help graph
 
116
    imagemap = models.TextField()  # the image map for the help graph
108
117
 
109
 
    size = models.CharField(max_length=1,choices=SIZES)
110
 
    type = models.CharField( max_length=1, choices=TYPES) # productionsite...
 
118
    size = models.CharField(max_length=1, choices=SIZES)
 
119
    type = models.CharField(max_length=1, choices=TYPES)  # productionsite...
111
120
 
112
121
    help = models.TextField(blank=True)
113
122
 
114
123
    # Enhances to
115
 
    enhancement = models.OneToOneField('self', related_name='enhanced_from', blank=True, null=True)
 
124
    enhancement = models.OneToOneField(
 
125
        'self', related_name='enhanced_from', blank=True, null=True)
116
126
 
117
127
    # Build cost
118
 
    build_wares = models.ManyToManyField(Ware, related_name="build_ware_for_buildings", blank=True)
119
 
    build_costs = models.CharField(max_length=100, blank=True) # ' '.joined() integer strings
 
128
    build_wares = models.ManyToManyField(
 
129
        Ware, related_name='build_ware_for_buildings', blank=True)
 
130
    # ' '.joined() integer strings
 
131
    build_costs = models.CharField(max_length=100, blank=True)
120
132
 
121
133
    # Workers
122
 
    workers_types = models.ManyToManyField(Worker, related_name="workers_for_buildings", blank=True)
123
 
    workers_count = models.CharField(max_length=100, blank=True) # ' '.joined() integer strings
 
134
    workers_types = models.ManyToManyField(
 
135
        Worker, related_name='workers_for_buildings', blank=True)
 
136
    # ' '.joined() integer strings
 
137
    workers_count = models.CharField(max_length=100, blank=True)
124
138
 
125
139
    # Store
126
 
    store_wares = models.ManyToManyField(Ware, related_name="stored_ware_for_buildings", blank=True)
127
 
    store_count = models.CharField(max_length=100, blank=True) # ' '.joined() integer strings
 
140
    store_wares = models.ManyToManyField(
 
141
        Ware, related_name='stored_ware_for_buildings', blank=True)
 
142
    # ' '.joined() integer strings
 
143
    store_count = models.CharField(max_length=100, blank=True)
128
144
 
129
145
    # Output
130
 
    output_wares = models.ManyToManyField(Ware, related_name="produced_by_buildings", blank=True)
131
 
    output_workers = models.ManyToManyField(Worker, related_name="trained_by_buildings", blank=True)
 
146
    output_wares = models.ManyToManyField(
 
147
        Ware, related_name='produced_by_buildings', blank=True)
 
148
    output_workers = models.ManyToManyField(
 
149
        Worker, related_name='trained_by_buildings', blank=True)
 
150
    
 
151
    class Meta:
 
152
        ordering = ['name']
 
153
 
132
154
 
133
155
    def save(self, *args, **kwargs):
134
156
 
135
 
        tdict = dict((b,a) for a,b in self.TYPES)
136
 
        sdict = dict((b,a) for a,b in self.SIZES)
 
157
        tdict = dict((b, a) for a, b in self.TYPES)
 
158
        sdict = dict((b, a) for a, b in self.SIZES)
137
159
 
138
160
        self.type = tdict.get(self.type, self.type)
139
161
        self.size = sdict.get(self.size, self.size)
142
164
 
143
165
    def has_build_cost(self):
144
166
        return (self.build_wares.all().count() != 0)
 
167
 
145
168
    def get_build_cost(self):
146
 
        count = map(int,self.build_costs.split( ))
147
 
        for c,w in zip(count,self.build_wares.all()):
148
 
            yield [w]*c
 
169
        # Creating the relation between build_cost and build_wares
 
170
        # Querying the wares returns the wares in alphabetical order!
 
171
        count = list(map(int, self.build_costs.split()))
 
172
        for c, w in zip(count, self.build_wares.all()):
 
173
            yield [w] * c
149
174
 
150
175
    def has_workers(self):
151
176
        return (self.workers_types.all().count() != 0)
 
177
 
152
178
    def get_workers(self):
153
 
        count = map(int,self.workers_count.split( ))
154
 
        for c,wor in zip(count,self.workers_types.all()):
155
 
            yield [wor]*c
 
179
        count = list(map(int, self.workers_count.split()))
 
180
        for c, wor in zip(count, self.workers_types.all()):
 
181
            yield [wor] * c
156
182
 
157
183
    def produces(self):
158
184
        return (self.output_wares.all().count() != 0)
159
 
    def get_outputs(self):
 
185
 
 
186
    def get_ware_outputs(self):
160
187
        return self.output_wares.all()
 
188
 
161
189
    def trains(self):
162
190
        return (self.output_workers.all().count() != 0)
 
191
 
163
192
    def get_worker_outputs(self):
164
193
        return self.output_workers.all()
 
194
 
165
195
    def has_outputs(self):
166
196
        return (self.output_workers.all().count() != 0 or self.output_wares.all().count() != 0)
167
197
 
168
198
    def has_stored_wares(self):
169
199
        return (self.store_wares.all().count() != 0)
 
200
 
170
201
    def get_stored_wares(self):
171
 
        count = map(int,self.store_count.split( ))
172
 
        for c,w in zip(count,self.store_wares.all()):
173
 
            yield [w]*c
174
 
 
175
 
 
176
 
    def __unicode__(self):
177
 
        return u"%s/%s" %(self.tribe.name,self.name)
178
 
 
 
202
        count = list(map(int, self.store_count.split()))
 
203
        for c, w in zip(count, self.store_wares.all()):
 
204
            yield [w] * c
 
205
 
 
206
    def __str__(self):
 
207
        return "%s/%s" % (self.tribe.name, self.name)