Add save-game loading.
[naja.git] / naja / gamestate.py
index c812c564e2e9c06be35a346f08eddb178af5612b..a32438170e3c5663366850cde744027507b57ceb 100644 (file)
@@ -2,15 +2,23 @@
 The current game state.
 """
 
-import yaml
+try:
+    import yaml
+except ImportError:
+    yaml = None
+    import json
 
 from naja.gameboard import GameBoard
 from naja.resources import resources
 
 
 def load_location_deck(name):
-    with resources.get_file('location_decks', '%s.yaml' % (name,)) as deck_fp:
-        return yaml.safe_load(deck_fp)
+    if yaml:
+        with resources.get_file('location_decks', '%s.yaml' % name) as deck_fp:
+            return yaml.safe_load(deck_fp)
+    else:
+        with resources.get_file('location_decks', '%s.json' % name) as deck_fp:
+            return json.load(deck_fp)
 
 
 class GameState(object):
@@ -18,11 +26,17 @@ class GameState(object):
     Naja game state.
     """
 
-    def __init__(self):
-        # This is a very simple deck to allow testing more drawing logic
-        # on tiles. These will need to be replaced with better stuff.
-        self.gameboard = GameBoard.new_game(
-            locations_definition=load_location_deck('test'))
+    def __init__(self, gameboard):
+        self.gameboard = gameboard
+
+    @classmethod
+    def new(cls, deck='standard', **kw):
+        deck = load_location_deck(deck)
+        return cls(GameBoard.new_game(deck, **kw))
+
+    @classmethod
+    def load(cls, data):
+        return cls(GameBoard.import_game(data))
 
     @property
     def player(self):