# HG changeset patch # User Jeremy Thurgood # Date 1336416267 -7200 # Node ID 1e8f7e694f0ce8caeb8ced88bd0dd4c1fb648572 # Parent d35a3762edda15452d199642cf465c665d9f1121 Refactor missions and sciences a bit to reduce duplication. diff -r d35a3762edda -r 1e8f7e694f0c gamelib/game_base.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/game_base.py Mon May 07 20:44:27 2012 +0200 @@ -0,0 +1,26 @@ + + +def get_subclasses(base_class, leaf_only=True): + subclasses = [] + for cls in base_class.__subclasses__(): + if leaf_only and cls.__subclasses__(): + # Not a leaf class, and only want leaves + continue + subclasses.append(cls) + return subclasses + + +class Science(object): + NAME = None + PREREQUISITES = () + ACQUISITION_CHANCE = 1.0 + SCIENCE_TYPE = None + + def __init__(self, points=0): + self.points = points + + def spend_point(self): + self.points += 1 + + def can_spend(self, lab): + return True diff -r d35a3762edda -r 1e8f7e694f0c gamelib/gamestate.py --- a/gamelib/gamestate.py Mon May 07 16:53:18 2012 +0200 +++ b/gamelib/gamestate.py Mon May 07 20:44:27 2012 +0200 @@ -3,7 +3,8 @@ """The actual game state object""" -from gamelib import missions, lab, products +from gamelib import missions, lab +from gamelib.game_base import get_subclasses class Game(object): @@ -24,11 +25,7 @@ else: self.lab = lab.Lab() # instantiate all the available missions - for cls in missions.Mission.__subclasses__(): - if cls.__subclasses__(): - # Not a leaf class, so base class for other missions - continue - # Add mission to the list + for cls in get_subclasses(missions.Mission): self.missions.append(cls()) def start_turn(self): @@ -40,7 +37,7 @@ def get_available_equipment(self): """Return a list of equipment we can produce and afford""" available = [x for x in self.lab.science - if isinstance(x, products.Product) and x.COST <= self.money] + if x.SCIENCE_TYPE == 'product' and x.COST <= self.money] return available def get_available_missions(self): diff -r d35a3762edda -r 1e8f7e694f0c gamelib/lab.py --- a/gamelib/lab.py Mon May 07 16:53:18 2012 +0200 +++ b/gamelib/lab.py Mon May 07 20:44:27 2012 +0200 @@ -3,6 +3,7 @@ from random import random, choice from gamelib import research, products +from gamelib.game_base import get_subclasses class Lab(object): @@ -11,8 +12,8 @@ def __init__(self, init_data=None): self.science = [] - self.new_research = research.ResearchArea.__subclasses__() - self.new_products = products.Product.__subclasses__() + self.new_research = get_subclasses(research.ResearchArea) + self.new_products = get_subclasses(products.Product) if init_data is not None: # Load stored state. diff -r d35a3762edda -r 1e8f7e694f0c gamelib/products.py --- a/gamelib/products.py Mon May 07 16:53:18 2012 +0200 +++ b/gamelib/products.py Mon May 07 20:44:27 2012 +0200 @@ -1,4 +1,5 @@ from gamelib import research +from gamelib.game_base import Science # Kinds of product. @@ -8,9 +9,7 @@ PATHOGEN = 'pathogen' -class Product(object): - NAME = None - PREREQUISITES = () +class Product(Science): ACQUISITION_CHANCE = 0.8 COST = None UPGRADE_REQUIREMENT = 1 @@ -18,12 +17,6 @@ SCIENCE_TYPE = 'product' CATEGORIES = () - def __init__(self, points=0): - self.points = points - - def spend_point(self): - self.points += 1 - def can_spend(self, lab): extra = self.UPGRADE_REQUIREMENT * self.points + 1 return lab.meet_requirements(self, extra) diff -r d35a3762edda -r 1e8f7e694f0c gamelib/research.py --- a/gamelib/research.py Mon May 07 16:53:18 2012 +0200 +++ b/gamelib/research.py Mon May 07 20:44:27 2012 +0200 @@ -1,16 +1,8 @@ -class ResearchArea(object): - NAME = None - PREREQUISITES = () - SCIENCE_TYPE = 'research' +from gamelib.game_base import Science + - def __init__(self, points=0): - self.points = points - - def spend_point(self): - self.points += 1 - - def can_spend(self, lab): - return True +class ResearchArea(Science): + SCIENCE_TYPE = 'research' class Tesla(ResearchArea):