From 3beea676f3bd4fc9101e9de1c24e8007011e952b Mon Sep 17 00:00:00 2001
From: Jeremy Thurgood <firxen@gmail.com>
Date: Mon, 12 May 2014 13:52:47 +0200
Subject: [PATCH] Load location deck from file.

---
 data/location_decks/test.yaml | 10 ++++++++++
 naja/actions.py               |  6 +++++-
 naja/gamestate.py             | 19 +++++++++----------
 naja/tests/test_actions.py    |  4 ++++
 setup.py                      |  5 ++++-
 5 files changed, 32 insertions(+), 12 deletions(-)
 create mode 100644 data/location_decks/test.yaml

diff --git a/data/location_decks/test.yaml b/data/location_decks/test.yaml
new file mode 100644
index 0000000..75f8690
--- /dev/null
+++ b/data/location_decks/test.yaml
@@ -0,0 +1,10 @@
+- actions: []
+- actions:
+  - action_class: 'DoNothing'
+    required_bits: [CYAN]
+- actions:
+  - action_class: 'DoNothing'
+    required_bits: [YELLOW]
+- actions:
+  - action_class: 'DoNothing'
+    required_bits: [YELLOW, MAGENTA]
diff --git a/naja/actions.py b/naja/actions.py
index 2b4d212..32ac035 100644
--- a/naja/actions.py
+++ b/naja/actions.py
@@ -9,7 +9,11 @@ class LocationAction(object):
     TEXT = None
 
     def __init__(self, required_bits, **data):
-        self.required_bits = frozenset(required_bits)
+        bits = set()
+        for bit in required_bits:
+            # Convert names to numbers if applicable.
+            bits.add(BITS.get(bit, bit))
+        self.required_bits = frozenset(bits)
         self.data = data
 
     def check_available(self, player):
diff --git a/naja/gamestate.py b/naja/gamestate.py
index 8d00756..c812c56 100644
--- a/naja/gamestate.py
+++ b/naja/gamestate.py
@@ -2,8 +2,15 @@
 The current game state.
 """
 
+import yaml
+
 from naja.gameboard import GameBoard
-from naja.constants import BITS
+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)
 
 
 class GameState(object):
@@ -15,15 +22,7 @@ class GameState(object):
         # 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=[
-                {'actions': []},
-                {'actions': [{'required_bits': [BITS.CYAN],
-                              'action_class': 'DoNothing'}]},
-                {'actions': [{'required_bits': [BITS.YELLOW],
-                              'action_class': 'DoNothing'}]},
-                {'actions': [{'required_bits': [BITS.YELLOW, BITS.MAGENTA],
-                              'action_class': 'DoNothing'}]},
-                ])
+            locations_definition=load_location_deck('test'))
 
     @property
     def player(self):
diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py
index 5fe07be..fb2c370 100644
--- a/naja/tests/test_actions.py
+++ b/naja/tests/test_actions.py
@@ -32,6 +32,10 @@ class TestActions(TestCase):
         check_available(set([BITS.MSB]), [], False)
         check_available(set([BITS.MSB]), [BITS.MSB], True)
 
+    def test_bits_translation(self):
+        action = actions.LocationAction(set([BITS.NORTH, 'MSB']))
+        self.assertEqual(action.required_bits, set([BITS.NORTH, BITS.MSB]))
+
     def test_DoNothing(self):
         board = self.make_board()
         state_before = board.export()
diff --git a/setup.py b/setup.py
index bbfb908..c66bef4 100644
--- a/setup.py
+++ b/setup.py
@@ -61,7 +61,10 @@ setup(
     ],
 
     # Dependencies
-    install_requires=['pygame'],
+    install_requires=[
+        'pygame',
+        'PyYAML',
+    ],
 
     # Files
     packages=find_packages(),
-- 
2.34.1