changeset 46:d3d277a42ac6

Less getattr/isinstance silliness.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 07 May 2012 21:07:48 +0200
parents 1e8f7e694f0c
children 3e3bed2ce248
files gamelib/game_base.py gamelib/lab.py
diffstat 2 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/game_base.py	Mon May 07 20:44:27 2012 +0200
+++ b/gamelib/game_base.py	Mon May 07 21:07:48 2012 +0200
@@ -24,3 +24,7 @@
 
     def can_spend(self, lab):
         return True
+
+    @classmethod
+    def save_name(cls):
+        return "%s.%s" % (cls.SCIENCE_TYPE, cls.__name__)
--- a/gamelib/lab.py	Mon May 07 20:44:27 2012 +0200
+++ b/gamelib/lab.py	Mon May 07 21:07:48 2012 +0200
@@ -23,21 +23,21 @@
             self._choose_initial_science()
 
     def _load_data(self, init_data):
-        for name, points in init_data['science'].iteritems():
-            science_type, cls = name.split('.')
-            if science_type == 'product':
-                science = getattr(products, cls)
-            elif science_type == 'research':
-                science = getattr(research, cls)
-            else:
-                raise ValueError("Unknown science type: %s" % (science_type,))
-            self._gain_science(science(points))
+        sciences = init_data['science'].copy()
+        for science in self.new_products + self.new_research:
+            # Check if this science is one we should know.
+            points = sciences.pop(science.save_name(), None)
+            if points is not None:
+                # It is! Learn it.
+                self._gain_science(science(points))
+        if sciences:
+            # We're supposed to know an unknowable thing. :-(
+            raise ValueError("Unknown science: %s" % (sciences.keys(),))
 
     def save_data(self):
         data = {'science': {}}
         for science in self.science:
-            name = "%s.%s" % (science.SCIENCE_TYPE, type(science).__name__)
-            data['science'][name] = science.points
+            data['science'][science.save_name()] = science.points
         return data
 
     def _choose_initial_science(self):