~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to widelandslib/tribe.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
except:
10
10
    basedir = p.join(p.dirname(__file__), p.pardir, p.pardir)
11
11
 
 
12
 
12
13
class BaseDescr(object):
 
14
 
13
15
    def __init__(self, tribe, name, descname, json):
14
16
        self.tribe = tribe
15
17
        self._json = json
18
20
 
19
21
    @property
20
22
    def image(self):
21
 
        return p.abspath(p.join(WIDELANDS_SVN_DIR, "data", self._json['icon']))
 
23
        return p.abspath(p.join(WIDELANDS_SVN_DIR, 'data', self._json['icon']))
 
24
 
22
25
 
23
26
class Ware(BaseDescr):
 
27
 
24
28
    def __str__(self):
25
 
        return "Ware(%s)" % self.name
 
29
        return 'Ware(%s)' % self.name
 
30
 
26
31
 
27
32
class Worker(BaseDescr):
 
33
 
28
34
    @property
29
35
    def becomes(self):
30
 
                 if 'becomes' in self._json:
31
 
                        return self._json['becomes']['name']
32
 
                 else:
33
 
                        return None
 
36
        if 'becomes' in self._json:
 
37
            return self._json['becomes']['name']
 
38
        else:
 
39
            return None
34
40
 
35
41
    def __str__(self):
36
 
        return "Worker(%s)" % self.name
 
42
        return 'Worker(%s)' % self.name
 
43
 
37
44
 
38
45
class Building(BaseDescr):
 
46
 
39
47
    @property
40
48
    def enhanced_building(self):
41
 
                 if 'enhanced' in self._json:
42
 
                        return True
43
 
                 else:
44
 
                        return False
 
49
        if 'enhanced' in self._json:
 
50
            return True
 
51
        else:
 
52
            return False
45
53
 
46
54
    @property
47
55
    def base_building(self):
48
56
        if not self.enhanced_building:
49
57
            return None
50
 
        bases = [b for b in self.tribe.buildings.values() if b.enhancement == self.name]
 
58
        bases = [b for b in self.tribe.buildings.values()
 
59
                 if b.enhancement == self.name]
51
60
        if len(bases) == 0 and self.enhanced_building:
52
 
            raise Exception("Building %s has no bases in tribe %s" % (self.name, self.tribe.name))
 
61
            raise Exception('Building %s has no bases in tribe %s' %
 
62
                            (self.name, self.tribe.name))
53
63
        if len(bases) > 1:
54
 
            raise Exception("Building %s seems to have more than one base in tribe %s." % (self.name, self.tribe.name))
 
64
            raise Exception('Building %s seems to have more than one base in tribe %s.' % (
 
65
                self.name, self.tribe.name))
55
66
        return bases[0]
56
67
 
57
68
    @property
58
69
    def enhancement(self):
59
 
                 if 'enhancement' in self._json:
60
 
                        return self._json['enhancement']
61
 
                 else:
62
 
                        return None
 
70
        if 'enhancement' in self._json:
 
71
            return self._json['enhancement']
 
72
        else:
 
73
            return None
63
74
 
64
75
    @property
65
76
    def buildcost(self):
66
 
                 result = dict()
67
 
                 if 'buildcost' in self._json:
68
 
                        for buildcost in self._json['buildcost']:
69
 
                          result[buildcost['name']] = buildcost['amount']
70
 
                 return result
 
77
        result = dict()
 
78
        if 'buildcost' in self._json:
 
79
            for buildcost in self._json['buildcost']:
 
80
                result[buildcost['name']] = buildcost['amount']
 
81
        return result
71
82
 
72
83
    @property
73
84
    def size(self):
74
85
        return self._json['size']
75
86
 
 
87
 
76
88
class ProductionSite(Building):
77
 
    btype = "productionsite"
 
89
    btype = 'productionsite'
 
90
 
78
91
    @property
79
92
    def outputs(self):
80
 
                 result = set()
81
 
                 if 'produced_wares' in self._json:
82
 
                        for warename in self._json['produced_wares']:
83
 
                          result.add(warename)
84
 
                 return result
 
93
        result = set()
 
94
        if 'produced_wares' in self._json:
 
95
            for warename in self._json['produced_wares']:
 
96
                result.add(warename)
 
97
        return result
85
98
 
86
99
    @property
87
100
    def inputs(self):
88
 
                 result = dict()
89
 
                 if 'stored_wares' in self._json:
90
 
                        for ware in self._json['stored_wares']:
91
 
                          result[ware['name']] = ware['amount']
92
 
                 return result
 
101
        result = dict()
 
102
        if 'stored_wares' in self._json:
 
103
            for ware in self._json['stored_wares']:
 
104
                result[ware['name']] = ware['amount']
 
105
        return result
93
106
 
94
107
    @property
95
108
    def workers(self):
96
 
                 result = dict()
97
 
                 if 'workers' in self._json:
98
 
                        for worker in self._json['workers']:
99
 
                          result[worker['name']] = worker['amount']
100
 
                 return result
 
109
        result = dict()
 
110
        if 'workers' in self._json:
 
111
            for worker in self._json['workers']:
 
112
                result[worker['name']] = worker['amount']
 
113
        return result
101
114
 
102
115
    @property
103
116
    def recruits(self):
104
 
                 result = set()
105
 
                 if 'produced_workers' in self._json:
106
 
                        for workername in self._json['produced_workers']:
107
 
                          result.add(workername)
108
 
                 return result
 
117
        result = set()
 
118
        if 'produced_workers' in self._json:
 
119
            for workername in self._json['produced_workers']:
 
120
                result.add(workername)
 
121
        return result
 
122
 
109
123
 
110
124
class Warehouse(Building):
111
 
    btype = "warehouse"
 
125
    btype = 'warehouse'
112
126
    pass
113
127
 
 
128
 
114
129
class TrainingSite(ProductionSite):
115
 
    btype = "trainingsite"
 
130
    btype = 'trainingsite'
116
131
    pass
117
132
 
 
133
 
118
134
class MilitarySite(Building):
119
 
    btype = "militarysite"
 
135
    btype = 'militarysite'
 
136
 
120
137
    @property
121
138
    def conquers(self):
122
139
        return self._json['conquers']
131
148
 
132
149
 
133
150
class Tribe(object):
 
151
 
134
152
    def __init__(self, tribeinfo, json_directory):
135
153
        self.name = tribeinfo['name']
136
154
 
137
 
        wares_file = open(p.normpath(json_directory + "/" + self.name + "_wares.json"), "r")
 
155
        wares_file = open(p.normpath(json_directory + '/' +
 
156
                                     self.name + '_wares.json'), 'r')
138
157
        waresinfo = json.load(wares_file)
139
158
        self.wares = dict()
140
159
        for ware in waresinfo['wares']:
141
 
                          descname = ware['descname'].encode('ascii', 'xmlcharrefreplace')
142
 
                          self.wares[ware['name']] = Ware(self, ware['name'], descname, ware)
 
160
            descname = ware['descname'].encode('ascii', 'xmlcharrefreplace')
 
161
            self.wares[ware['name']] = Ware(self, ware['name'], descname, ware)
143
162
 
144
 
        workers_file = open(p.normpath(json_directory + "/" + self.name + "_workers.json"), "r")
 
163
        workers_file = open(p.normpath(
 
164
            json_directory + '/' + self.name + '_workers.json'), 'r')
145
165
        workersinfo = json.load(workers_file)
146
166
        self.workers = dict()
147
167
        for worker in workersinfo['workers']:
148
 
                          descname = worker['descname'].encode('ascii', 'xmlcharrefreplace')
149
 
                          self.workers[worker['name']] = Worker(self, worker['name'], descname, worker)
 
168
            descname = worker['descname'].encode('ascii', 'xmlcharrefreplace')
 
169
            self.workers[worker['name']] = Worker(
 
170
                self, worker['name'], descname, worker)
150
171
 
151
 
        buildings_file = open(p.normpath(json_directory + "/" + self.name + "_buildings.json"), "r")
 
172
        buildings_file = open(p.normpath(
 
173
            json_directory + '/' + self.name + '_buildings.json'), 'r')
152
174
        buildingsinfo = json.load(buildings_file)
153
175
        self.buildings = dict()
154
176
        for building in buildingsinfo['buildings']:
155
 
                          descname = building['descname'].encode('ascii', 'xmlcharrefreplace')
156
 
                          if building['type'] == "productionsite":
157
 
                                  self.buildings[building['name']] = ProductionSite(self, building['name'], descname, building)
158
 
                          elif building['type'] == "warehouse":
159
 
                                  self.buildings[building['name']] = Warehouse(self, building['name'], descname, building)
160
 
                          elif building['type'] == "trainingsite":
161
 
                                  self.buildings[building['name']] = TrainingSite(self, building['name'], descname, building)
162
 
                          elif building['type'] == "militarysite":
163
 
                                  self.buildings[building['name']] = MilitarySite(self, building['name'], descname, building)
164
 
                          else:
165
 
                                  self.buildings[building['name']] = Building(self, building['name'], descname, building)
 
177
            descname = building['descname'].encode(
 
178
                'ascii', 'xmlcharrefreplace')
 
179
            if building['type'] == 'productionsite':
 
180
                self.buildings[building['name']] = ProductionSite(
 
181
                    self, building['name'], descname, building)
 
182
            elif building['type'] == 'warehouse':
 
183
                self.buildings[building['name']] = Warehouse(
 
184
                    self, building['name'], descname, building)
 
185
            elif building['type'] == 'trainingsite':
 
186
                self.buildings[building['name']] = TrainingSite(
 
187
                    self, building['name'], descname, building)
 
188
            elif building['type'] == 'militarysite':
 
189
                self.buildings[building['name']] = MilitarySite(
 
190
                    self, building['name'], descname, building)
 
191
            else:
 
192
                self.buildings[building['name']] = Building(
 
193
                    self, building['name'], descname, building)
166
194
 
167
195
    def __str__(self):
168
 
        return "Tribe(%s)" % self.name
 
196
        return 'Tribe(%s)' % self.name