changeset 20:718d1ec382f7

Deserialise lab data.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 06 May 2012 18:12:51 +0200
parents 12085dfd9e69
children bdc6bfc34ef2
files gamelib/lab.py gamelib/products.py gamelib/tests/test_lab.py
diffstat 3 files changed, 43 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/lab.py	Sun May 06 17:39:38 2012 +0200
+++ b/gamelib/lab.py	Sun May 06 18:12:51 2012 +0200
@@ -9,11 +9,28 @@
     BASIC_RESEARCH_SUCCESS_RATE = 0.05
     BASIC_RESEARCH_SUCCESS_MULTIPLIER = 2
 
-    def __init__(self):
+    def __init__(self, init_data=None):
         self.science = []
         self.new_research = research.ResearchArea.__subclasses__()
         self.new_products = products.Product.__subclasses__()
-        self._choose_initial_science()
+
+        if init_data is not None:
+            # Load stored state.
+            self._load_data(init_data)
+        else:
+            # New game.
+            self._choose_initial_science()
+
+    def _load_data(self, init_data):
+        for name, points in init_data['science'].iteritems():
+            module, cls = name.split('.')
+            if module == 'products':
+                science = getattr(products, cls)
+            elif module == 'research':
+                science = getattr(research, cls)
+            else:
+                raise ValueError("Unknown science type: %s" % (module,))
+            self._gain_science(science(points))
 
     def _choose_initial_science(self):
         # We always get all starting products.
--- a/gamelib/products.py	Sun May 06 17:39:38 2012 +0200
+++ b/gamelib/products.py	Sun May 06 18:12:51 2012 +0200
@@ -25,7 +25,7 @@
     COST = 100
     STARTING_PRODUCT = True
 
-    def __init__(self):
+    def __init__(self, points=0):
         self.points = 1
 
     def spend_point(self):
--- a/gamelib/tests/test_lab.py	Sun May 06 17:39:38 2012 +0200
+++ b/gamelib/tests/test_lab.py	Sun May 06 18:12:51 2012 +0200
@@ -5,27 +5,34 @@
 
 
 class TestLab(TestCase):
-    def setUp(self):
-        self.lab = Lab()
+    def test_new_lab(self):
+        all_sciences = set()
+        for _ in range(10):
+            lab = Lab()
+            research_areas = [r for r in lab.science
+                              if isinstance(r, research.ResearchArea)]
+            self.assertEqual(3, len(research_areas))
+            for science in research_areas:
+                all_sciences.add(type(science))
+        self.assertTrue(len(all_sciences) > 3)
 
     def test_find_new_products(self):
-        # Set up the appropriate research.
-        if research.Tesla in self.lab.new_research:
-            self.lab._gain_science(research.Tesla(1))
-        self.lab._get_science(research.Tesla).points += 2
-        # Check breakthrough options.
-        new_products = self.lab.find_new_products(None)
+        lab = Lab({
+                'science': {
+                    'research.Tesla': 3,
+                    }
+                })
+        new_products = lab.find_new_products(None)
         self.assertTrue(products.TeslaTank in new_products)
         self.assertTrue(products.DoomsdayVirus not in new_products)
 
     def test_find_new_research(self):
-        # Set up the appropriate research.
-        if research.Robotics in self.lab.new_research:
-            self.lab._gain_science(research.Robotics(1))
-        if research.Rocketry in self.lab.new_research:
-            self.lab._gain_science(research.Rocketry(1))
-        self.lab._get_science(research.Rocketry).points += 1
-        # Check breakthrough options.
-        new_research = self.lab.find_new_research()
+        lab = Lab({
+                'science': {
+                    'research.Robotics': 1,
+                    'research.Rocketry': 2,
+                    }
+                })
+        new_research = lab.find_new_research()
         self.assertTrue(research.Space in new_research)
         self.assertTrue(research.ArtificialIntelligence not in new_research)