示例#1
0
def main():

    rm = ResourceManager()
    choice = None
    while choice != 6:
        print("Manage Chore Board")
        print("0. Demand tick")
        print("1. View resources")
        print("2. View board")
        print("3: Edit board")
        print("3. Add chore completed")
        print("4. Add reward redemption")
        print("5. Add consumption")
        print("6. Exit")
        try:
            choice = int(raw_input("Enter choice: "))
            assert (choice >= 0 and choice <= 6) or (choice in (10, 11))
        except (ValueError, AssertionError):
            continue
        if choice == 0:
            print("Done")
            rm.demand_tick()
        elif choice == 1:
            for resource in rm.resources.itervalues():
                print("%s - S: %s D: %s CR: %s" % (resource.name, resource.supply, resource.get_demand(), resource.collect_rate))
        elif choice == 2:
            pass
 def setUp(self):
     self.apples = Resource('apples', 1.0)
     self.oranges = Resource('oranges', 7.0)
     self.resource_manager = ResourceManager()
     self.resource_manager._clear()
class TestResourceManager(TestCase):

    def setUp(self):
        self.apples = Resource('apples', 1.0)
        self.oranges = Resource('oranges', 7.0)
        self.resource_manager = ResourceManager()
        self.resource_manager._clear()

    def test_add_resource(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.assertEqual(len(self.resource_manager.resources), 2)

    def test_remove_resource(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.remove_resource('apples')
        self.assertEqual(len(self.resource_manager.resources), 0)

    def test_add_resource_supply(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource_supply('apples', 3)
        self.assertEqual(self.resource_manager.resources['apples'].supply, 3)
        self.resource_manager.add_resource_supply('apples', 1)
        self.assertEqual(self.resource_manager.resources['apples'].supply, 4)

    def test_consume_resource(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource_supply('apples', 2)
        self.resource_manager.consume_resource('apples', 1)
        self.assertEqual(self.resource_manager.resources['apples'].supply, 1)
        self.assertEqual(self.resource_manager.resources['apples'].demand, -1)

    def test_demand_tick(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.demand_tick()
        self.assertEqual(self.resource_manager.resources['apples'].demand, 1)
        self.assertEqual(self.resource_manager.resources['oranges'].demand, 7)

    def test_get_demand(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        self.resource_manager.demand_tick()
        demand = self.resource_manager.get_demand()
        self.assertEqual(demand['apples'], 1)
        self.assertEqual(demand['oranges'], 7)
        self.assertEqual(demand['pear'], 1)
        self.resource_manager.demand_tick()
        demand = self.resource_manager.get_demand()
        self.assertEqual(demand['apples'], 2)
        self.assertEqual(demand['pear'], 1)

    def test_calculate_reward_resources(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        resource_requirements = self.resource_manager.calculate_reward_resources(4)
        self.assertEqual(resource_requirements, {
            'apples': 1,
            'oranges': 6,
            'pear': 1
        })
        self.resource_manager.demand_tick()
        resource_requirements = self.resource_manager.calculate_reward_resources(4)
        self.assertEqual(resource_requirements, {
            'apples': 1,
            'oranges': 21,
            'pear': 1
        })
        self.resource_manager.add_resource_supply('apples', 16)
        self.resource_manager.add_resource_supply('oranges', 28)
        self.resource_manager.add_resource_supply('pear', 2)
        self.resource_manager.consume_resource('oranges', 23)
        self.resource_manager.consume_resource('apples', 1)
        self.resource_manager.consume_resource('pear', 1)
        self.resource_manager.demand_tick()
        resource_requirements = self.resource_manager.calculate_reward_resources(4)
        self.assertEqual(resource_requirements, {
            'apples': 8,
            'oranges': 27,
            'pear': 1
        })

    def test_calculate_reward_resources_negative_supply(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        self.resource_manager.consume_resource('oranges', 23)
        self.resource_manager.consume_resource('apples', 1)
        self.resource_manager.consume_resource('pear', 1)
        resource_requirements = self.resource_manager.calculate_reward_resources(4)
        self.assertEqual(resource_requirements, {
            'apples': 1,
            'oranges': 6,
            'pear': 1
        })

    def test_calculate_reward_resources_large_supply(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        self.resource_manager.add_resource_supply('apples', 100)
        self.resource_manager.add_resource_supply('pear', 5)
        resource_requirements = self.resource_manager.calculate_reward_resources(1)
        self.assertEqual(resource_requirements, {
            'apples': 30,
            'oranges': 1,
            'pear': 1
        })

    def test_calculate_reward_resources_high_demand(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        for _ in xrange(100):
            self.resource_manager.demand_tick()
        resource_requirements = self.resource_manager.calculate_reward_resources(1)
        self.assertEqual(resource_requirements, {
            'apples': 2,
            'oranges': 15,
            'pear': 1
        })

    def test_calculate_reward_resources_high_demand_high_supply(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        for _ in xrange(100):
            self.resource_manager.demand_tick()
        self.resource_manager.add_resource_supply('apples', 10)
        self.resource_manager.add_resource_supply('oranges', 70)
        self.resource_manager.add_resource_supply('pear', 7)
        resource_requirements = self.resource_manager.calculate_reward_resources(1)
        self.assertEqual(resource_requirements, {
            'apples': 2,
            'oranges': 50,
            'pear': 1
        })

    def test_calculate_reward_resources_small_cadence(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 8))
        resource_requirements = self.resource_manager.calculate_reward_resources(1 / 7 / 3)
        self.assertEqual(resource_requirements, {
            'pear': 1
        })

    def test_calculate_reward_resources_more_resources_sim(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        self.resource_manager.add_resource(Resource('grape', 0.25))
        self.resource_manager.add_resource(Resource('banana', 6))
        self.resource_manager.demand_tick()
        reward1 = self.resource_manager.calculate_reward_resources(1 / 7 / 3)
        self.resource_manager.demand_tick()
        self.resource_manager.add_resource_supply('apples', 1)
        self.resource_manager.add_resource_supply('oranges', 1)
        self.resource_manager.add_resource_supply('banana', 2)
        self.resource_manager.consume_reward(reward1)
        reward2 = self.resource_manager.calculate_reward_resources(0.5)
        self.resource_manager.demand_tick()
        self.resource_manager.add_resource_supply('banana', 1)
        self.resource_manager.add_resource_supply('grape', 1)
        self.resource_manager.add_resource_supply('apples', 2)
        self.resource_manager.add_resource_supply('pear', 1)
        self.resource_manager.consume_reward(reward1)
        self.resource_manager.consume_reward(reward2)
        reward3 = self.resource_manager.calculate_reward_resources(2)
        pass

    def test_long_run_simulator_wrong_cadence(self):
        self.resource_manager.add_resource(self.apples)
        self.resource_manager.add_resource(self.oranges)
        self.resource_manager.add_resource(Resource("pear", 0.7))
        self.resource_manager.add_resource(Resource('grape', 0.25))
        self.resource_manager.add_resource(Resource('banana', 6))

        resources = ('apples', 'oranges', 'pear', 'grape', 'banana')
        rewards = list()

        for i in xrange(1000):
            if i % 10 > 7:
                self.resource_manager.demand_tick()
                res_name = random.choice(resources)
                self.resource_manager.add_resource_supply(res_name, 1)
            else:
                reward = self.resource_manager.calculate_reward_resources(random.randint(1, 7))
                rewards.append(reward)
            try:
                reward = random.choice(rewards)
            except IndexError:
                continue
            for itm, amnt in reward.iteritems():
                if self.resource_manager.resources[itm].supply < amnt * 1.5:
                    break
            else:
                self.resource_manager.consume_reward(reward)
        final_reward_resource = self.resource_manager.calculate_reward_resources(3)
        print(self.resource_manager.get_demand())
        print(self.resource_manager.get_supply())
        print(final_reward_resource)