flake8 all the things.
[naja.git] / naja / actions.py
1 from naja.constants import ACTION_GLYPHS, BITS, CHESS_PIECES
2 from naja.utils import bit_glyphs, move_glyph
3
4
5 class LocationAction(object):
6     """
7     An action that may be performed on a location.
8     """
9
10     TEXT = None
11     GLYPHS = (ACTION_GLYPHS.NOTHING,)
12     MSB_GLYPH = None
13
14     def __init__(self, required_bits, **data):
15         self.required_bits = required_bits
16         self.data = data
17
18     def get_text(self, location=None):
19         substitutions = self.data.copy()
20
21         if 'direction' in self.data:
22             substitutions['rowcol'] = {
23                 'NORTH': 'column',
24                 'SOUTH': 'column',
25                 'EAST': 'row',
26                 'WEST': 'row',
27             }[self.data['direction']]
28             substitutions['direction'] = '{%s}' % (substitutions['direction'],)
29
30         if 'chesspiece' in self.data:
31             substitutions['chesspiece_name'] = move_glyph(
32                 self.data['chesspiece'])
33
34         if location is None:
35             substitutions['location_bits'] = 'bits specified by this location'
36         else:
37             substitutions['location_bits'] = bit_glyphs(
38                 location.bitwise_operand)
39
40         return self.TEXT % substitutions
41
42     def check_available(self, player):
43         return player.bits.check_bits(self.required_bits)
44
45     def perform_action(self, board, location):
46         raise NotImplementedError(
47             "%s does not implement perform_action()." % (type(self).__name__,))
48
49     def check_and_clear_MSB(self, player):
50         if player.bits.check_bit(BITS.MSB):
51             player.bits.clear_bit(BITS.MSB)
52             return True
53         else:
54             return False
55
56     def export(self):
57         return {'required_bits': list(self.required_bits),
58                 'data': self.data,
59                 'action_class': self.__class__.__name__}
60
61
62 class DoNothing(LocationAction):
63     TEXT = "No effect."
64
65     def perform_action(self, board, location):
66         pass
67
68
69 class LoseHealthOrMSB(LocationAction):
70     TEXT = "Lose {HEALTH} or {MSB}."
71     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
72
73     def perform_action(self, board, location):
74         if not self.check_and_clear_MSB(board.player):
75             board.lose_health()
76
77
78 class SetBits(LocationAction):
79     TEXT = "Set %(location_bits)s."
80     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
81
82     def perform_action(self, board, location):
83         board.player.bits.set_bits(location.bitwise_operand)
84
85
86 class ToggleBits(LocationAction):
87     TEXT = "Toggle %(location_bits)s."
88     GLYPHS = (ACTION_GLYPHS.TOGGLE_BITS,)
89
90     def perform_action(self, board, location):
91         board.player.bits.toggle_bits(location.bitwise_operand)
92
93
94 class LoseHealthOrMSBAndSetBits(LocationAction):
95     TEXT = "Lose {HEALTH} or {MSB}, then set %(location_bits)s."
96     GLYPHS = (ACTION_GLYPHS.SET_BITS,)
97     MSB_GLYPH = ACTION_GLYPHS.DAMAGE
98
99     def perform_action(self, board, location):
100         if not self.check_and_clear_MSB(board.player):
101             board.lose_health()
102         board.player.bits.set_bits(location.bitwise_operand)
103
104
105 class AcquireWinToken(LocationAction):
106     TEXT = "Gain {WINTOKEN}, then clear {RED,GREEN,BLUE}."
107     GLYPHS = (ACTION_GLYPHS.WINTOKEN,)
108
109     def perform_action(self, board, location):
110         board.acquire_win_token()
111         board.player.bits.clear_bits(set([
112             BITS.RED, BITS.GREEN, BITS.BLUE,
113         ]))
114
115
116 class GainHealth(LocationAction):
117     TEXT = "Gain {HEALTH}."
118     GLYPHS = (ACTION_GLYPHS.HEAL,)
119
120     def perform_action(self, board, location):
121         board.gain_health()
122
123
124 class GainHealthAndClearBitsOrMSB(LocationAction):
125     TEXT = "Gain {HEALTH}, then clear %(location_bits)s or {MSB}."
126     GLYPHS = (ACTION_GLYPHS.HEAL,)
127     MSB_GLYPH = ACTION_GLYPHS.CLEAR_BITS
128
129     def perform_action(self, board, location):
130         board.gain_health()
131         if not self.check_and_clear_MSB(board.player):
132             board.player.bits.clear_bits(location.bitwise_operand)
133
134
135 class ShiftLocations(LocationAction):
136     TEXT = "Shift current %(rowcol)s %(direction)s."
137     GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
138
139     def perform_action(self, board, location):
140         board.shift_locations(self.data['direction'])
141
142
143 class AllowChessMove(LocationAction):
144     TEXT = "Move like a %(chesspiece_name)s for one turn."
145     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
146
147     def perform_action(self, board, location):
148         if self.data['chesspiece'] in CHESS_PIECES:
149             chesspiece = CHESS_PIECES[self.data['chesspiece']]
150             board.allow_chess_move(chesspiece)