def test_sort_for_storage(self): test_list = JobList(TestCollector(0, 0), JobList.order_by.for_storage_collector) test_list.append( Job(TestObject(1, 3, 3), [Job.ResListEntry(2, 4, False)])) test_list.append( Job(TestObject(2, 1, 1), [Job.ResListEntry(1, 2, False)])) test_list.append( Job(TestObject(3, 2, 2), [Job.ResListEntry(2, 3, False)])) test_list.append( Job(TestObject(4, 9, 0), [Job.ResListEntry(4, 9, target_inventory_full=True)])) test_list.append( Job(TestObject(BUILDINGS.CLAY_DEPOSIT, 10, 5), [Job.ResListEntry(4, 9, False)])) test_list.sort_jobs() # Make sure everything was sorted in order of distance with secondary # sorting by fewest available and as last the clay deposit as it has a producer in range self.assertEqual(test_list[0].object.id, 4) self.assertEqual(test_list[1].object.id, 2) self.assertEqual(test_list[2].object.id, 3) self.assertEqual(test_list[3].object.id, 1) self.assertEqual(test_list[4].object.id, BUILDINGS.CLAY_DEPOSIT) # Both give res 2, but TestObject with id 3 is closer self.assertTrue( self.distance(test_list, 1) <= self.distance(test_list, 2))
def get_job(self): """Returns the next job or None""" if self.home_building is None: return None collectable_res = self.get_collectable_res() if not collectable_res: return None jobs = JobList(self, self.job_ordering) # iterate all building that provide one of the resources for building in self.get_buildings_in_range(reslist=collectable_res): # check if we can pickup here on principle target_possible = self._target_possible_cache.get(building, None) if target_possible is None: # not in cache, we have to check target_possible = self.check_possible_job_target(building) self._target_possible_cache[building] = target_possible if target_possible: # check for res here reslist = (self.check_possible_job_target_for(building, res) for res in collectable_res) reslist = [i for i in reslist if i] if reslist: # we can do something here jobs.append(Job(building, reslist)) # TODO: find out why order of self.get_buildings_in_range(..) and therefore order of jobs differs from client to client # TODO: find out why WildAnimal.get_job(..) doesn't have this problem # for MP-Games the jobs must have the same ordering to ensure get_best_possible_job(..) returns the same result jobs.sort(key=lambda job: job.object.worldid) return self.get_best_possible_job(jobs)
def create_list(self, order): test_list = JobList(TestCollector(0, 0), order) test_list.append( Job(TestObject(1, 3, 3), [Job.ResListEntry(3, 4, False)])) test_list.append( Job(TestObject(2, 1, 1), [Job.ResListEntry(1, 2, False)])) test_list.append( Job(TestObject(3, 2, 2), [Job.ResListEntry(2, 3, False)])) return test_list
def test_sort_fewest_available_and_distance(self): test_list = JobList(TestCollector(0, 0), JobList.order_by.fewest_available_and_distance) test_list.append(Job(TestObject(1, 3, 3), [Job.ResListEntry(2, 4, False)])) test_list.append(Job(TestObject(2, 1, 1), [Job.ResListEntry(1, 2, False)])) test_list.append(Job(TestObject(3, 2, 2), [Job.ResListEntry(2, 3, False)])) test_list._sort_jobs_fewest_available_and_distance() # Make sure everything was sorted in order of distance with secondary # sorting by fewest available self.assertEqual(test_list[0].object.id, 2) self.assertEqual(test_list[1].object.id, 3) self.assertEqual(test_list[2].object.id, 1)