diff gamelib/lab.py @ 20:718d1ec382f7

Deserialise lab data.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 06 May 2012 18:12:51 +0200
parents 10d3db1f1e08
children bdc6bfc34ef2
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.