Skip to content

Commit 2a9890c

Browse files
committed
Cleanup, comments, moved building service to city
1 parent f90fbf8 commit 2a9890c

File tree

4 files changed

+73
-135
lines changed

4 files changed

+73
-135
lines changed

foe/main.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,10 @@
5959
refresh = count + random.randrange(config['settings']['update']['min'], config['settings']['update']['max'])
6060

6161
print "Checking... (%s)" % (count)
62+
# NOTE: The full update should adjust for any coins/supplies/resources gained from these pickups
63+
account.city.pickup()
6264

63-
buildingService = BuildingService()
64-
response = buildingService.multipickup(account.city.buildings)
65-
66-
account.updateFromResponse(response)
67-
68-
for building in account.city.buildings:
69-
sleep = random.uniform(0.5, 2)
70-
time.sleep(sleep)
71-
building.produce()
65+
account.city.produce()
7266

7367
session.commit()
7468

foe/models/building.py

+17-36
Original file line numberDiff line numberDiff line change
@@ -94,78 +94,55 @@ def populate(self, *args, **kwargs):
9494
elif self.state == 'UnconnectedState':
9595
self.collection_time = 0
9696
else:
97+
# State we don't now about... so print it
9798
pprint.pprint(state)
9899

99-
100100
return super(Building, self).populate(*args, **kwargs)
101101

102102
def produce(self):
103103
"""
104+
Starts production in the building
104105
"""
105106

106107
if self.type == 'residential':
107-
return
108+
return None
108109

109110
if self.collection_time:
110-
return
111+
return None
111112

112113
if self.state in ['ProducingState', 'ProductionFinishedState', 'ConstructionState', 'UnconnectedState']:
113-
return
114+
return None
114115

116+
# NOTE: '1' means the first slot, which is 5 minutes for supplies or 4 hours for resources
115117
response = self.request('startProduction', [self.id, 1])
116118

117119
print "%s started production" % (self)
118120

119-
#
121+
# TODO: Resources should be 4 hours ... but should be corrected by the full update
120122
self.collection_time = time.time() + (5 * 60)
121123
self.state = 'ProducingState'
122124

123125
return response
124126

125127
def pickup(self):
126128
"""
129+
Picks up/gather the coins/supplies/resources from the building
127130
"""
128131

129132
if not self.pickupable():
130-
return
133+
return None
131134

132135
response = self.request('pickupProduction', [[self.id]])
133136

134137
print "%s picked up production" % (self)
135138

136-
self.pickupUpdateState()
137-
138-
return response
139-
140-
def multipickup(self, buildings):
141-
"""
142-
"""
143-
144-
updateIds = []
145-
146-
for building in buildings:
147-
if not building.pickupable():
148-
continue
149-
150-
updateIds.append(building.id)
151-
152-
if len(updateIds) == 0:
153-
return []
154-
155-
response = self.request('pickupProduction', [updateIds])
156-
157-
for building in buildings:
158-
if building.id not in updateIds:
159-
continue
160-
161-
print "%s picked up production" % (building)
162-
163-
building.pickupUpdateState()
139+
self.pickedup()
164140

165141
return response
166142

167143
def pickupable(self):
168144
"""
145+
Returns True if the build is ready to be picked up
169146
"""
170147

171148
if not self.collection_time:
@@ -179,8 +156,9 @@ def pickupable(self):
179156

180157
return True
181158

182-
def pickupUpdateState(self):
159+
def pickedup(self):
183160
"""
161+
Marks the building as being picked up, resetting state and timers
184162
"""
185163

186164
if self.type == 'residential':
@@ -190,12 +168,15 @@ def pickupUpdateState(self):
190168
self.collection_time = 0
191169
self.state = 'IdleState'
192170

171+
return self
172+
193173
def cancel(self):
194174
"""
175+
Cancels the current prodution of the building, reverting it to the idle state
195176
"""
196177

197178
if self.type == 'residential':
198-
return
179+
return None
199180

200181
response = self.request('cancelProduction', [[self.id]])
201182

foe/models/buildingService.py

-87
This file was deleted.

foe/models/city.py

+53-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
import pprint
88
import json
9+
import random
910
from collections import OrderedDict
1011

1112
# 3rd-Party
@@ -14,7 +15,7 @@
1415

1516
import pydash
1617

17-
#
18+
# Proprietary
1819
from request import Request
1920
from models.model import Model
2021
from models.building import Building
@@ -53,14 +54,12 @@ def __init__(self, *args, **kwargs):
5354

5455
return super(City, self).__init__(*args, **kwargs)
5556

56-
5757
def __repr__(self):
5858
"""
5959
"""
6060

6161
return "City"
6262

63-
6463
def populate(self, *args, **kwargs):
6564
"""
6665
"""
@@ -84,3 +83,54 @@ def populate(self, *args, **kwargs):
8483

8584

8685
return super(City, self).populate(*args, **kwargs)
86+
87+
def pickup(self):
88+
"""
89+
Picks up (gather resources) all the builds in the city
90+
"""
91+
92+
# Get the IDs of all the buildings that can be picked up
93+
ids = [building.id for building in self.buildings if building.pickupable()]
94+
# No point in going any futher if there is nothing to pickup
95+
if not ids:
96+
print "No buildings need picking up"
97+
return self
98+
99+
# Pick a random sample (~70%) of the of these buildings
100+
count = int(len(ids) * 0.7)
101+
# Sample the builds at random
102+
sample = random.sample(ids, count)
103+
# Pickup all of them at once
104+
response = self.request('pickupProduction', [sample], klass='CityProductionService')
105+
106+
print "Picked up %s/%s buildings at once" % (len(sample), len(ids))
107+
# Get the buildings that weren't all picked up at once
108+
difference = set(ids).difference(set(sample))
109+
# Now pick up the rest one-by-one
110+
for building in self.buildings:
111+
112+
if building.id in difference:
113+
# Add a built of time so it looks like a human ;)
114+
sleep = random.uniform(0.5, 1)
115+
time.sleep(sleep)
116+
117+
building.pickup()
118+
else:
119+
# This building was picked up as part of the multi/batch pickup so just update the state
120+
building.pickedup()
121+
122+
return self
123+
124+
def produce(self):
125+
"""
126+
Starts production of all building in the city
127+
"""
128+
129+
for building in self.buildings:
130+
# Add a built of time so it looks like a human ;)
131+
sleep = random.uniform(0.5, 2)
132+
time.sleep(sleep)
133+
134+
building.produce()
135+
136+
return self

0 commit comments

Comments
 (0)