/
sm_build.py
293 lines (248 loc) · 14.3 KB
/
sm_build.py
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'''
Created on 02.05.2014
@author: Tobias Ruck
'''
from state import State, StateMachine
from event import listen_to, Event
from trigger import trigger, TriggerEnoughResources, TriggerBuildSlotAvailable,\
TriggerEnoughSpaceForResources
from datetime import datetime
from job import Job, JobBuild
from condition import condition_changes, ConditionEnoughResources,\
ConditionEnoughSpaceForResources, ConditionBuildSlotAvailable,\
ConditionQuestFulfilled
import db
from log import logger
order_file = open('building_jobs.txt', 'w')
build_roman = (
# World quests
[
{ 'type': 'http', 'url': '/statistiken.php' },
({ 'type': 'quest', 'name': 'World_01', 'space': (90, 120, 60, 30) },),
{ 'type': 'rename_village', 'new_name': 'Bubber Duckey' },
({ 'type': 'quest', 'name': 'World_02' },),
],
[
{ 'type': 'build', 'name': 'clay_pit', 'level': 1 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 1 },
{ 'type': 'build', 'name': 'woodcutter', 'level': 1 },
{ 'type': 'build', 'name': 'iron_mine', 'level': 1 },
({ 'type': 'quest', 'name': 'Economy_01'},),
{ 'type': 'build', 'name': 'cropland', 'level': 1 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 1, 'quest_event': 'Economy_02' },
{ 'type': 'build', 'name': 'cropland', 'level': 1 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 1 },
{ 'type': 'build', 'name': 'cropland', 'level' : 1 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 1 },
{ 'type': 'build', 'name': 'cropland', 'level' : 1 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 1 },
{ 'type': 'build', 'name': 'cropland', 'level' : 1 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 1 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 1, 'quest_event': 'Economy_04' },
{ 'type': 'build', 'name': 'clay_pit', 'level': 2 },
{ 'type': 'build', 'name': 'cropland', 'level' : 2 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 2 },
{ 'type': 'build', 'name': 'cropland', 'level' : 2 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 2, 'quest_event': 'Economy_05' },
{ 'type': 'build', 'name': 'clay_pit', 'level': 2 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 2 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 2 },
{ 'type': 'build', 'name': 'cropland', 'level': 2 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 2 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 2 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 2 },
{ 'type': 'build', 'name': 'cropland', 'level': 2 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 2 },
{ 'type': 'build', 'name': 'cropland', 'level' : 2 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 2, 'quest_event': 'Economy_08' },
{ 'type': 'build', 'name': 'clay_pit', 'level': 3 },
{ 'type': 'build', 'name': 'cropland', 'level' : 3 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 3 },
{ 'type': 'build', 'name': 'cropland', 'level' : 3 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 3 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 3 },
{ 'type': 'build', 'name': 'cropland', 'level' : 3 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 3 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 3 },
{ 'type': 'build', 'name': 'cropland', 'level': 3 },
{ 'type': 'build', 'name': 'woodcutter', 'level' : 3 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 3 },
{ 'type': 'build', 'name': 'cropland', 'level': 3 },
{ 'type': 'build', 'name': 'clay_pit', 'level': 3 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 3 },
{ 'type': 'build', 'name': 'cropland', 'level' : 4 },
{ 'type': 'build', 'name': 'iron_mine', 'level' : 3 },
{ 'type': 'build', 'name': 'cropland', 'level': 5 },
{ 'type': 'build', 'name': 'grain_mill', 'level': 1, 'quest_event': 'Economy_11' },
{ 'type': 'build', 'name': 'cropland', 'level': 3 },
{ 'type': 'build_fields' }
],
[
{ 'type': 'build', 'name': 'main_building', 'level': 2 },
{ 'type': 'build', 'name': 'cranny', 'level': 1, 'quest_event': 'Battle_02' },
{ 'type': 'build', 'name': 'main_building', 'level': 3, 'quest_event': 'World_03' },
{ 'type': 'build', 'name': 'granary', 'level': 1, 'quest_event': 'Economy_03' },
{ 'type': 'http', 'url': '/karte.php' },
({ 'type': 'quest', 'name': 'World_05', 'space': (90, 160, 90, 95) },),
{ 'type': 'read_inbox' },
({ 'type': 'quest', 'name': 'World_06', 'space': (280, 315, 200, 145) },),
{ 'type': 'open_gold_menu' },
{ 'type': 'quest', 'name': 'World_07b' },
{ 'type': 'build', 'name': 'embassy', 'level': 1, 'quest_event': 'World_04' },
{ 'type': 'build', 'name': 'warehouse', 'level': 1 },
{ 'type': 'build', 'name': 'marketplace', 'level': 1, 'quest_event': 'Economy_06' },
{ 'type': 'build', 'name': 'warehouse', 'level': 2 },
{ 'type': 'build', 'name': 'barracks', 'level': 1, 'quest_event': 'Battle_03' },
{ 'type': 'build', 'name': 'warehouse', 'level': 3, 'quest_event': 'Economy_09' },
{ 'type': 'build', 'name': 'main_building', 'level': 4 },
{ 'type': 'build', 'name': 'main_building', 'level': 5, 'quest_event': 'World_09' },
{ 'type': 'build', 'name': 'granary', 'level': 2 },
{ 'type': 'build', 'name': 'granary', 'level': 3, 'quest_event': 'Economy_10' },
],
[
{ 'type': 'quest', 'name': 'Economy_06', 'space': (600, 0, 0, 0), 'is_event_based': True }, # marketplace
{ 'type': 'sell_resources', 'sell_res': 1, 'buy_res': 2, 'sell_amount': 100, 'buy_amount': 200 }, # sell 100 wood for 200 clay
{ 'type': 'quest', 'name': 'Economy_07', 'space': (100, 99, 99, 99) }
],
{ 'type': 'quest', 'name': 'Economy_02', 'space': (160, 190, 150, 70), 'is_event_based': True }, # 4 of each resource at lvl 1
{ 'type': 'quest', 'name': 'Economy_03', 'space': (250, 290, 100, 130), 'is_event_based': True }, # granary
{ 'type': 'quest', 'name': 'Economy_04', 'space': (400, 460, 330, 270), 'is_event_based': True }, # everything at lvl 1
{ 'type': 'quest', 'name': 'Economy_05', 'space': (240, 255, 190, 160), 'is_event_based': True }, # 4 of each resource at lvl 2
{ 'type': 'quest', 'name': 'Economy_08', 'space': (400, 400, 400, 200), 'is_event_based': True }, # everything at lvl 2
{ 'type': 'quest', 'name': 'Economy_09', 'space': (620, 730, 560, 230), 'is_event_based': True }, # warehouse lvl 3
{ 'type': 'quest', 'name': 'Economy_10', 'space': (880, 1020,590, 320), 'is_event_based': True }, # granary lvl 3
{ 'type': 'quest', 'name': 'Economy_11', 'is_event_based': True }, # grain mill
{ 'type': 'quest', 'name': 'Battle_02', 'space': (130, 150, 120, 100), 'is_event_based': True }, # cranny
{ 'type': 'quest', 'name': 'Battle_03', 'space': (110, 140, 160, 30), 'is_event_based': True }, # barracks
{ 'type': 'quest', 'name': 'World_03', 'space': (170, 100, 130, 70), 'is_event_based': True }, # main building lvl 3
{ 'type': 'quest', 'name': 'World_04', 'space': (215, 145, 195, 50), 'is_event_based': True }, # embassy
{ 'type': 'quest', 'name': 'World_09', 'space': (570, 470, 560, 265), 'is_event_based': True }, # main building lvl 5
)
class JobManager():
def __init__(self, village):
self.village = village
#self.jobs = []
self.state_machines = []
self.root = Job.create({'type':'root'})
def init_from_def(self, job_definition):
self._recursive_add_jobs(self.root, job_definition)
def init_from_db(self):
state_data = db.states.find( { 'sm_village': self.village.village_id } )
for data in state_data:
sm = StateMachine.create_from_db(self.village, data)
sm.current_state.transition()
self.state_machines.append( sm )
def _recursive_add_jobs(self, parent, job_def):
if isinstance(job_def, tuple):
for jd in job_def:
self._recursive_add_jobs(parent, jd)
elif isinstance(job_def, list):
for jd in job_def:
p = self._recursive_add_jobs(parent, jd)
if p is not None:
parent = p
elif isinstance(job_def, dict):
job = Job.create(job_def)
#self.jobs.append(job)
sm = StateMachine_Job(self.village, {'start': { 'job': job } })
sm.current_state['after_job_id'] = parent['_jid']
sm.current_state.save_data()
self.state_machines.append(sm)
return job
def __on_event__(self, event):
'''
Dispatches the event to all children state machines
'''
for sm in self.state_machines:
sm.__on_event__(event)
class StateMachine_Job(metaclass = StateMachine):
class start(State):
@listen_to('job_completed')
def on_job_completed(self, event):
#if self['job'].get('name', None) == 'main_building':
# print('* main build:', self.data, event.job)
if event.job['_jid'] == self['after_job_id']:
self.wait_for_conditions['job'] = self['job']
self.current_state = self.wait_for_conditions
def data_updated(self):
self['job'] = Job.create(self['job'])
class wait_for_conditions(State):
def transition(self):
'''
Store the conditions of the job and assign the triggers
'''
self['conditions'] = self['job'].get_conditions(self.village)
self.cond_instances = {}
self.ready = False
if 'resources' in self['conditions']:
self.cond_instances['resources'] = self.cond_enough_resources().enqueue(self.village, self['conditions']['resources'])
if 'space' in self['conditions']:
self.cond_instances['space'] = self.cond_enough_space().enqueue(self.village, self['conditions']['space'])
if 'build_slot_id' in self['conditions']:
self.cond_instances['build_slot_id'] = self.cond_build_slot_available().enqueue(self.village, self['conditions']['build_slot_id'])
if 'quest_event' in self['conditions']:
self.cond_instances['quest_event'] = self.cond_quest_fulfilled().enqueue(self.village, self['conditions']['quest_event'])
self.ready = True
self.save_data()
self.check_execution()
@condition_changes(ConditionQuestFulfilled)
def cond_quest_fulfilled(self):
self.check_execution()
@condition_changes(ConditionEnoughResources)
def cond_enough_resources(self):
self.check_execution()
@condition_changes(ConditionEnoughSpaceForResources)
def cond_enough_space(self):
self.check_execution()
@condition_changes(ConditionBuildSlotAvailable)
def cond_build_slot_available(self):
self.check_execution()
def data_updated(self):
self['job'] = Job.create(self['job'])
def check_execution(self):
if not self.ready:
return
self.ready = False
c = True
cn = {}
for k, cond in self.cond_instances.items():
c = c and cond.is_true()
cn[k] = cond.is_true()
logger.log_note("check condition", "Job: %s\n Fulfilled: %s\n %s\n %s" % (self['job'], cn, self['conditions'], self.cond_instances))
if c:
logger.log_note("execute job", "Execute job: %s" % self['job'])
for cond in self.cond_instances.values():
cond.terminate()
self['job'].execute(self.village)
if 'repeat' in self['job'] and self['job']['repeat']:
self.current_state = self.wait_for_conditions
elif 'quest_event' in self['job'] and isinstance(self['job'], JobBuild):
self.wait_for_build['quest_event'] = self['job']['quest_event']
self.wait_for_build['building'] = self['job'].get_build_id()
self.wait_for_build['level'] = self['job']['level']
self.current_state = self.wait_for_build
else:
self.current_state = self.terminated
self.village.fire_event( Event(self.village, 'job_completed', datetime.now(), job=self['job']) )
else:
self.save_data()
self.ready = True
class wait_for_build(State):
def transition(self):
self.save_data()
@listen_to('build')
def on_build(self, event):
if event.building == self['building'] and event.level == self['level']:
self.village.fire_event( Event(self.village, 'quest_fulfilled', datetime.now(), quest_name=self['quest_event']) )
self.current_state = self.terminated
class terminated(State):
pass
if __name__ == '__main__':
import pprint
from village import Village
from account import Account
vill = Village(Account((0,0), None), None, None, None)
jm = JobManager(vill)
pprint.pprint([ sm.current_state.data for sm in jm.state_machines ])
print("-"*10)
#jm.__on_event__(Event(vill, 'job_completed', None, job=jm.root))