Add routine to play until last action
This commit is contained in:
@@ -127,6 +127,15 @@ class Gameplay():
|
|||||||
raise CannotChangeThePastException
|
raise CannotChangeThePastException
|
||||||
|
|
||||||
|
|
||||||
|
def _get_last_action_time(self):
|
||||||
|
try:
|
||||||
|
last_action_time = max(self.actions.keys())
|
||||||
|
except ValueError:
|
||||||
|
last_action_time = self.world.current_time
|
||||||
|
|
||||||
|
return last_action_time
|
||||||
|
|
||||||
|
|
||||||
def _get_next_action_time(self):
|
def _get_next_action_time(self):
|
||||||
try:
|
try:
|
||||||
next_action_time = min(self.actions.keys())
|
next_action_time = min(self.actions.keys())
|
||||||
@@ -159,6 +168,11 @@ class Gameplay():
|
|||||||
self._handle_actions()
|
self._handle_actions()
|
||||||
|
|
||||||
|
|
||||||
|
def play_until_last_action(self, extra_time=0):
|
||||||
|
last_action_time = self._get_last_action_time()
|
||||||
|
self.play_until(last_action_time + extra_time)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.world = World()
|
self.world = World()
|
||||||
self.actions = defaultdict(list)
|
self.actions = defaultdict(list)
|
||||||
|
|||||||
@@ -740,3 +740,26 @@ class TestWorld(unittest.TestCase):
|
|||||||
new_food = 500 + rate_per_60
|
new_food = 500 + rate_per_60
|
||||||
|
|
||||||
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
self.assertResourcesMatch([new_food, 500, 500, 500], game.world)
|
||||||
|
|
||||||
|
|
||||||
|
def test_play_until_last_action(self):
|
||||||
|
game = Gameplay()
|
||||||
|
|
||||||
|
game.build(0, Buildings.HOUSE, [Tasks.IDLE]*2, [Tasks.IDLE]*2)
|
||||||
|
game.build(0, Buildings.HOUSE, Tasks.IDLE, Tasks.IDLE)
|
||||||
|
game.build(100, Buildings.LUMBERCAMP, Tasks.IDLE, Tasks.IDLE)
|
||||||
|
|
||||||
|
game.play_until_last_action()
|
||||||
|
|
||||||
|
self.assertEqual(3, len(game.world.buildings))
|
||||||
|
|
||||||
|
game.assign_villager(200, Tasks.IDLE, Tasks.SHEEP)
|
||||||
|
game.assign_villager(200, Tasks.IDLE, Tasks.SHEEP)
|
||||||
|
|
||||||
|
game.play_until_last_action(extra_time=60)
|
||||||
|
|
||||||
|
sheep_rate_per_60 = 19.8
|
||||||
|
gathered = sheep_rate_per_60*2
|
||||||
|
|
||||||
|
self.assertEqual(4, len(game.world.buildings))
|
||||||
|
self.assertResourcesMatch([200 + gathered, 50, 100, 200], game.world)
|
||||||
|
|||||||
Reference in New Issue
Block a user