~widelands-dev/widelands-website/trunk

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
#!/usr/bin/env python
# encoding: utf-8

import os.path as p
import json
try:
    from django.conf import settings
    basedir = settings.WIDELANDS_SVN_DIR
except:
    basedir = p.join(p.dirname(__file__), p.pardir, p.pardir)


class BaseDescr(object):

    def __init__(self, tribe, name, descname, json):
        self.tribe = tribe
        self._json = json
        self.name = name
        self.descname = descname

    @property
    def image(self):
        return p.abspath(p.join(settings.WIDELANDS_SVN_DIR, 'data', self._json['icon']))


class Ware(BaseDescr):

    def __str__(self):
        return 'Ware(%s)' % self.name


class Worker(BaseDescr):

    @property
    def becomes(self):
        if 'becomes' in self._json:
            return self._json['becomes']['name']
        else:
            return None

    def __str__(self):
        return 'Worker(%s)' % self.name


class Building(BaseDescr):

    @property
    def enhanced_building(self):
        if 'enhanced' in self._json:
            return True
        else:
            return False

    @property
    def base_building(self):
        if not self.enhanced_building:
            return None
        bases = [b for b in list(self.tribe.buildings.values())
                 if b.enhancement == self.name]
        if len(bases) == 0 and self.enhanced_building:
            raise Exception('Building %s has no bases in tribe %s' %
                            (self.name, self.tribe.name))
        if len(bases) > 1:
            raise Exception('Building %s seems to have more than one base in tribe %s.' % (
                self.name, self.tribe.name))
        return bases[0]

    @property
    def enhancement(self):
        if 'enhancement' in self._json:
            return self._json['enhancement']
        else:
            return None

    @property
    def buildcost(self):
        result = dict()
        if 'buildcost' in self._json:
            for buildcost in self._json['buildcost']:
                result[buildcost['name']] = buildcost['amount']
        return result

    @property
    def size(self):
        return self._json['size']


class ProductionSite(Building):
    btype = 'productionsite'

    @property
    def outputs(self):
        result = set()
        if 'produced_wares' in self._json:
            for warename in self._json['produced_wares']:
                result.add(warename)
        return result

    @property
    def inputs(self):
        result = dict()
        if 'stored_wares' in self._json:
            for ware in self._json['stored_wares']:
                result[ware['name']] = ware['amount']
        return result

    @property
    def workers(self):
        result = dict()
        if 'workers' in self._json:
            for worker in self._json['workers']:
                result[worker['name']] = worker['amount']
        return result

    @property
    def recruits(self):
        result = set()
        if 'produced_workers' in self._json:
            for workername in self._json['produced_workers']:
                result.add(workername)
        return result


class Warehouse(Building):
    btype = 'warehouse'
    pass


class TrainingSite(ProductionSite):
    btype = 'trainingsite'
    pass


class MilitarySite(Building):
    btype = 'militarysite'

    @property
    def conquers(self):
        return self._json['conquers']

    @property
    def max_soldiers(self):
        return self._json['max_soldiers']

    @property
    def heal_per_second(self):
        return self._json['heal_per_second']


class Tribe(object):

    def __init__(self, tribeinfo, json_directory):
        self.name = tribeinfo['name']

        with open(p.normpath(json_directory + '/' +
                             self.name + '_wares.json'), 'r') as wares_file:
            waresinfo = json.load(wares_file)
        self.wares = dict()
        for ware in waresinfo['wares']:
            descname = ware['descname']
            self.wares[ware['name']] = Ware(self, ware['name'], descname, ware)

        with open(p.normpath(
                json_directory + '/' + self.name + '_workers.json'), 'r') as workers_file:
            workersinfo = json.load(workers_file)
        self.workers = dict()
        for worker in workersinfo['workers']:
            descname = worker['descname']
            self.workers[worker['name']] = Worker(
                self, worker['name'], descname, worker)

        with open(p.normpath(
                json_directory + '/' + self.name + '_buildings.json'), 'r') as buildings_file:
            buildingsinfo = json.load(buildings_file)
        self.buildings = dict()
        for building in buildingsinfo['buildings']:
            descname = building['descname']
            if building['type'] == 'productionsite':
                self.buildings[building['name']] = ProductionSite(
                    self, building['name'], descname, building)
            elif building['type'] == 'warehouse':
                self.buildings[building['name']] = Warehouse(
                    self, building['name'], descname, building)
            elif building['type'] == 'trainingsite':
                self.buildings[building['name']] = TrainingSite(
                    self, building['name'], descname, building)
            elif building['type'] == 'militarysite':
                self.buildings[building['name']] = MilitarySite(
                    self, building['name'], descname, building)
            else:
                self.buildings[building['name']] = Building(
                    self, building['name'], descname, building)

    def __str__(self):
        return 'Tribe(%s)' % self.name