Mercurial > sypikslang
changeset 6:826b44731323
Start of basic lab implementation.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 May 2012 12:48:12 +0200 |
parents | dd9046d0680c |
children | eadc5c19d665 |
files | gamelib/lab.py gamelib/products.py |
diffstat | 2 files changed, 71 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/lab.py Sun May 06 12:48:12 2012 +0200 @@ -0,0 +1,70 @@ +from gamelib import research, products + + +def is_subclass(item, superclass): + return ( + isinstance(item, type) + and issubclass(item, superclass) + and not item is superclass) + + +def list_products(): + for item in dir(products): + # Ugh! + item = getattr(products, item) + if is_subclass(item, products.Product): + yield item + + +def list_research(): + for item in dir(research): + # Ugh! + item = getattr(research, item) + if is_subclass(item, research.ResearchArea): + yield item + + +class Lab(object): + def __init__(self): + self.science = [] + + def spend_points(self, things, basic_research): + for thing in things: + assert thing in self.science + thing.spend_points(1) + if isinstance(thing, research.ResearchArea): + self.find_new_product(thing) + self.try_basic_research(basic_research) + + def _get_science(self, science_class): + for science in self.science: + if isinstance(science, science_class): + return science + return None + + def _meet_requirements(self, science_class): + for science, level in science_class.PREREQUISITES: + my_science = self._get_science(science) + if my_science is None: + return False + if my_science.points < level: + return False + return True + + def find_new_products(self, research_area): + available_products = [] + for product_class in list_products(): + if self._get_science(product_class): + continue + if self._meet_requirements(product_class): + available_products.append(product_class) + return available_products + + def find_new_research(self, basic_research): + available_research = [] + for research_class in list_research(): + if self._get_science(research_class): + continue + if self._meet_requirements(research_class): + available_research.append(research_class) + return available_research