Load location deck from file.
authorJeremy Thurgood <firxen@gmail.com>
Mon, 12 May 2014 11:52:47 +0000 (13:52 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Mon, 12 May 2014 11:52:47 +0000 (13:52 +0200)
data/location_decks/test.yaml [new file with mode: 0644]
naja/actions.py
naja/gamestate.py
naja/tests/test_actions.py
setup.py

diff --git a/data/location_decks/test.yaml b/data/location_decks/test.yaml
new file mode 100644 (file)
index 0000000..75f8690
--- /dev/null
@@ -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]
index 2b4d2128d4b8ecba5dd7a230524e75300305160d..32ac03509c88527cf8d5205d0bfa0a490456c674 100644 (file)
@@ -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):
index 8d00756f8c85c318f7d30a224683f75c6b274f76..c812c564e2e9c06be35a346f08eddb178af5612b 100644 (file)
@@ -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):
index 5fe07be4892406f0185ad78153fd3f4b45da282e..fb2c370b4cf11662543479a048564c1066a5951d 100644 (file)
@@ -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()
index bbfb908f62cf3dce634315dd2a6901fceddfc565..c66bef4045a1a147be3aa88ec5daa9952736571e 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -61,7 +61,10 @@ setup(
     ],
 
     # Dependencies
-    install_requires=['pygame'],
+    install_requires=[
+        'pygame',
+        'PyYAML',
+    ],
 
     # Files
     packages=find_packages(),