From: Neil Date: Sat, 17 May 2014 12:28:11 +0000 (+0200) Subject: Add a card_name parameter X-Git-Tag: 0.1~110 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=cccf26b59edcba80bcc68b58f0d1faa487576213;p=naja.git Add a card_name parameter --- diff --git a/naja/gameboard.py b/naja/gameboard.py index 7d8916e..944f21f 100644 --- a/naja/gameboard.py +++ b/naja/gameboard.py @@ -221,7 +221,8 @@ class LocationCard(object): A particular set of options available on a location. """ - def __init__(self, bitwise_operand, location_actions): + def __init__(self, card_name, bitwise_operand, location_actions): + self.card_name = card_name self.bitwise_operand = bitwise_operand self.actions = location_actions self.check_actions() @@ -230,7 +231,8 @@ class LocationCard(object): def import_location(cls, state): location_actions = [ cls.build_action(definition) for definition in state['actions']] - return cls(state['bitwise_operand'], location_actions) + return cls(state['card_name'], state['bitwise_operand'], + location_actions) @classmethod def build_action(cls, definition): @@ -245,9 +247,11 @@ class LocationCard(object): bits = cls.parse_bits(definition['bits']) else: bits = cls.generate_bitwise_operand() + card_name = definition['card_name'] return cls.import_location({ 'bitwise_operand': bits, 'actions': definition['actions'], + 'card_name': card_name, }) @classmethod @@ -259,6 +263,7 @@ class LocationCard(object): return { 'bitwise_operand': sorted(self.bitwise_operand), 'actions': [action.export() for action in self.actions], + 'card_name': self.card_name, } def check_actions(self): diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index d770f12..0717f0c 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -19,7 +19,7 @@ class TestActions(TestCase): def make_board(self, player_bits=None, locations=None): if locations is None: - locations = [{'actions': []}] + locations = [{'card_name': 'card', 'actions': []}] board = GameBoard.new_game({'cards': locations}) if player_bits is not None: board.player.bits.bits = 0 @@ -107,7 +107,7 @@ class TestActions(TestCase): def test_SetBits(self): board = self.make_board() state_before = board.export() - card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), []) actions.SetBits(set()).perform_action(board, card) state_after = board.export() self.assertEqual( @@ -117,7 +117,7 @@ class TestActions(TestCase): def test_ToggleBits(self): board = self.make_board(player_bits=[BITS.NORTH]) state_before = board.export() - card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), []) actions.ToggleBits(set()).perform_action(board, card) state_after = board.export() self.assertEqual(board.player.bits.check_bit(BITS.MSB), True) @@ -127,7 +127,7 @@ class TestActions(TestCase): def test_LoseHealthOrMSBAndSetBits_MSB_clear(self): board = self.make_board(player_bits=[]) state_before = board.export() - card = LocationCard(set([BITS.BLUE, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), []) actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) state_after = board.export() self.assertEqual(state_after['health'], state_before['health'] - 1) @@ -139,7 +139,7 @@ class TestActions(TestCase): def test_LoseHealthOrMSBAndSetBits_MSB_set(self): board = self.make_board(player_bits=[BITS.MSB]) state_before = board.export() - card = LocationCard(set([BITS.BLUE, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), []) actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) state_after = board.export() self.assert_player_bits(board, BITS.BLUE, BITS.NORTH) @@ -148,7 +148,7 @@ class TestActions(TestCase): def test_LoseHealthOrMSBAndSetBits_MSB_set_and_on_card(self): board = self.make_board(player_bits=[BITS.MSB]) state_before = board.export() - card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.MSB, BITS.NORTH]), []) actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) state_after = board.export() self.assert_player_bits(board, BITS.MSB, BITS.NORTH) @@ -170,7 +170,7 @@ class TestActions(TestCase): board = self.make_board(player_bits=[BITS.NORTH]) board.lose_health() state_before = board.export() - card = LocationCard(set([BITS.BLUE, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), []) actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card) state_after = board.export() self.assertEqual(state_after['health'], state_before['health'] + 1) @@ -183,7 +183,7 @@ class TestActions(TestCase): board = self.make_board(player_bits=[BITS.MSB, BITS.NORTH]) board.lose_health() state_before = board.export() - card = LocationCard(set([BITS.BLUE, BITS.NORTH]), []) + card = LocationCard('card', set([BITS.BLUE, BITS.NORTH]), []) actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card) state_after = board.export() self.assertEqual(state_after['health'], state_before['health'] + 1) diff --git a/naja/tests/test_gameboard.py b/naja/tests/test_gameboard.py index 2db02a7..5b38399 100644 --- a/naja/tests/test_gameboard.py +++ b/naja/tests/test_gameboard.py @@ -12,7 +12,7 @@ class TestGameBoard(TestCase): def make_deck(self, cards=None): if cards is None: - cards = [{'actions': []}] + cards = [{'card_name': 'card', 'actions': []}] return {'cards': cards} def assert_state(self, state1, state2, exclude=(), player_exclude=()): @@ -30,7 +30,8 @@ class TestGameBoard(TestCase): self.assertEqual(state1, state2) def test_export_new_board(self): - board = GameBoard.new_game({'cards': [{'actions': [ + board = GameBoard.new_game({'cards': [ + {'card_name' : 'card1', 'actions': [ { 'action_class': 'LoseHealthOrMSB', 'required_bits': [], @@ -46,7 +47,7 @@ class TestGameBoard(TestCase): 'health': 4, 'wins_required': 4, 'wins': 0, - 'locations': [{'actions': [ + 'locations': [{'card_name' : 'card1', 'actions': [ { 'action_class': 'LoseHealthOrMSB', 'required_bits': [], @@ -63,7 +64,8 @@ class TestGameBoard(TestCase): for position, location_state in board_locations: positions.append(position) self.assertEqual( - sorted(location_state.keys()), ['actions', 'bitwise_operand']) + sorted(location_state.keys()), ['actions', 'bitwise_operand', + 'card_name']) self.assertEqual(location_state['actions'], [ { 'action_class': 'LoseHealthOrMSB', @@ -244,14 +246,15 @@ class TestLocationCard(TestCase): self.assertEqual(bits, set(BITS.values())) def test_new_location_no_actions(self): - location = LocationCard.new_location({'actions': []}) + location = LocationCard.new_location({'card_name': 'card', + 'actions': []}) [action] = location.actions self.assertEqual(type(action), actions.DoNothing) self.assertEqual(action.required_bits, set()) def test_new_location_one_action(self): - location = LocationCard.new_location({'actions': [ - {'required_bits': [], 'action_class': 'DoNothing'}, + location = LocationCard.new_location({'card_name': 'card1', + 'actions': [{'required_bits': [], 'action_class': 'DoNothing'}, ]}) [action] = location.actions self.assertEqual(type(action), actions.DoNothing)