Skip to content

Commit fb80e03

Browse files
authored
Merge pull request #1833 from avanwinkle/bugfix-bonus-unset-player-vars
Bugfix bonus unset player vars
2 parents 48b6e09 + f73e649 commit fb80e03

File tree

8 files changed

+48
-21
lines changed

8 files changed

+48
-21
lines changed

mpf/modes/bonus/code/bonus.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ def _bonus_next_item(self):
9191
self._subtotal()
9292
return
9393

94-
# Calling player.vars.get() instead of player.get() bypasses the
95-
# auto-fill zero and will throw if there is no player variable.
96-
# The fallback value of 1 is used for bonus entries that don't use
97-
# a player score, which are multiplied by one to get the bonus.
98-
hits = self.player.vars.get(entry['player_score_entry'], 1)
94+
# If a player_score_entry is provided, use player getattr to get a
95+
# fallback value of zero if the variable is not set. Otherwise
96+
# use 1 as the multiplier for non-player-score bonuses.
97+
hits = self.player[entry['player_score_entry']] if entry['player_score_entry'] else 1
9998
score = entry['score'].evaluate([]) * hits
10099

101100
if (not score and entry['skip_if_zero']) or (score < 0 and entry['skip_if_negative']):

mpf/modes/high_score/code/high_score.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ async def _run(self) -> None:
196196
# ask player for initials if we do not know them
197197
if not player.initials:
198198
try:
199-
player.initials = await self._ask_player_for_initials(player, award_names[i], value, category_name)
199+
player.initials = await self._ask_player_for_initials(player, award_names[i],
200+
value, category_name)
200201
except asyncio.TimeoutError:
201202
del new_list[i]
202203
# no entry when the player missed the timeout

mpf/platforms/fast/communicators/net_neuron.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def update_switches_from_hw_data(self):
293293
This will silently sync the switch.hw_state. If the logical state changes,
294294
it will process it like any switch change.
295295
"""
296-
for switch in self.machine.switches:
296+
for switch in self.machine.switches.values():
297297
hw_state = self.platform.hw_switch_data[switch.hw_switch.number]
298298

299299
if hw_state != switch.hw_state:

mpf/platforms/fast/fast_exp_board.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
from base64 import b16decode
5+
from binascii import Error as binasciiError
56
from importlib import import_module
67

78
from packaging import version
@@ -176,7 +177,7 @@ def update_leds(self):
176177

177178
try:
178179
self.communicator.send_bytes(b16decode(f'{msg_header}{msg}'), log_msg)
179-
except Exception as e:
180+
except binasciiError as e:
180181
self.log.error(
181182
f"Error decoding the following message for board {breakout_address} : {msg_header}{msg}")
182183
self.log.info("Attempted update that caused this error: %s", dirty_leds)

mpf/platforms/virtual.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ async def get_hw_switch_states(self):
105105

106106
if 'virtual_platform_start_active_switches' in self.machine.config:
107107
initial_active_switches = []
108-
for switch in Util.string_to_list(self.machine.config['virtual_platform_start_active_switches']):
109-
if switch not in self.machine.switches:
110-
if " " in switch:
108+
for switch_name in Util.string_to_list(self.machine.config['virtual_platform_start_active_switches']):
109+
if switch_name not in self.machine.switches.keys():
110+
if " " in switch_name:
111111
self.raise_config_error("MPF no longer supports lists separated by space in "
112112
"virtual_platform_start_active_switches. Please separate "
113-
"switches by comma: {}.".format(switch), 1)
113+
"switches by comma: {}.".format(switch_name), 1)
114114
else:
115115
self.raise_config_error("Switch {} used in virtual_platform_start_active_switches was not "
116-
"found in switches section.".format(switch), 1)
117-
initial_active_switches.append(self.machine.switches[switch].hw_switch.number)
116+
"found in switches section.".format(switch_name), 1)
117+
initial_active_switches.append(self.machine.switches[switch_name].hw_switch.number)
118118

119119
for k in self.hw_switches:
120120
if k in initial_active_switches:

mpf/plugins/platform_integration_test_runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ async def move_ball_from_drain_to_trough(self, **kwargs):
227227
drain_switches = self.machine.ball_devices.items_tagged('drain')[0].config.get('ball_switches')
228228
self.info_log("Found drain switches: %s of type %s", drain_switches, type(drain_switches))
229229
# If there's only one drain switch it might be a single value, rather than a list
230-
drain_switch = drain_switches if type(drain_switches) is str else drain_switches[-1]
230+
drain_switch = drain_switches if isinstance(drain_switches, str) else drain_switches[-1]
231231
self.info_log("Setting drain switch '%s' to zero", drain_switch)
232232
self.set_switch_sync(drain_switch, 0)
233233
await asyncio.sleep(0.25)

mpf/tests/machine_files/bonus/modes/bonus/config/bonus.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ mode_settings:
1212
score: 5000
1313
player_score_entry: modes
1414
reset_player_score_entry: False
15+
- event: bonus_undefined_var
16+
score: 5000
17+
skip_if_zero: false
18+
player_score_entry: undefined_var
19+
- event: bonus_static
20+
score: 2000

mpf/tests/test_Bonus.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def test_slam_tilt_in_service(self):
3838
def testBonus(self):
3939
self.mock_event("bonus_ramps")
4040
self.mock_event("bonus_modes")
41+
self.mock_event("bonus_undefined_var")
42+
self.mock_event("bonus_static")
4143
self.mock_event("bonus_subtotal")
4244
self.mock_event("bonus_multiplier")
4345
self.mock_event("bonus_total")
@@ -78,10 +80,14 @@ def testBonus(self):
7880
self.assertEqual(3, self._last_event_kwargs["bonus_ramps"]["hits"])
7981
self.assertEqual(10000, self._last_event_kwargs["bonus_modes"]["score"])
8082
self.assertEqual(2, self._last_event_kwargs["bonus_modes"]["hits"])
81-
self.assertEqual(13000, self._last_event_kwargs["bonus_subtotal"]["score"])
83+
self.assertEqual(0, self._last_event_kwargs["bonus_undefined_var"]["score"])
84+
self.assertEqual(0, self._last_event_kwargs["bonus_undefined_var"]["hits"])
85+
self.assertEqual(2000, self._last_event_kwargs["bonus_static"]["score"])
86+
self.assertEqual(1, self._last_event_kwargs["bonus_static"]["hits"])
87+
self.assertEqual(15000, self._last_event_kwargs["bonus_subtotal"]["score"])
8288
self.assertEqual(5, self._last_event_kwargs["bonus_multiplier"]["multiplier"])
83-
self.assertEqual(65000, self._last_event_kwargs["bonus_total"]["score"])
84-
self.assertEqual(66337, self.machine.game.player.score)
89+
self.assertEqual(75000, self._last_event_kwargs["bonus_total"]["score"])
90+
self.assertEqual(76337, self.machine.game.player.score)
8591

8692
# check resets
8793
self.assertEqual(0, self.machine.game.player.ramps)
@@ -102,10 +108,10 @@ def testBonus(self):
102108
self.assertEqual(0, self._last_event_kwargs["bonus_ramps"]["hits"])
103109
self.assertEqual(10000, self._last_event_kwargs["bonus_modes"]["score"])
104110
self.assertEqual(2, self._last_event_kwargs["bonus_modes"]["hits"])
105-
self.assertEqual(10000, self._last_event_kwargs["bonus_subtotal"]["score"])
111+
self.assertEqual(12000, self._last_event_kwargs["bonus_subtotal"]["score"])
106112
self.assertEqual(5, self._last_event_kwargs["bonus_multiplier"]["multiplier"])
107-
self.assertEqual(50000, self._last_event_kwargs["bonus_total"]["score"])
108-
self.assertEqual(116337, self.machine.game.player.score)
113+
self.assertEqual(60000, self._last_event_kwargs["bonus_total"]["score"])
114+
self.assertEqual(136337, self.machine.game.player.score)
109115

110116
# multiplier should stay the same
111117
self.assertEqual(0, self.machine.game.player.ramps)
@@ -128,6 +134,8 @@ def testBonus(self):
128134
self.mock_event("bonus_start")
129135
self.mock_event("bonus_ramps")
130136
self.mock_event("bonus_modes")
137+
self.mock_event("bonus_undefined_var")
138+
self.mock_event("bonus_static")
131139
self.mock_event("bonus_subtotal")
132140
self.mock_event("bonus_multiplier")
133141
self.mock_event("bonus_total")
@@ -157,6 +165,18 @@ def testBonus(self):
157165
self.assertEventNotCalled('bonus_multiplier')
158166
self.assertEventNotCalled('bonus_total')
159167

168+
self.advance_time_and_run(.5)
169+
self.assertEventCalled('bonus_undefined_var')
170+
self.assertEventNotCalled('bonus_subtotal')
171+
self.assertEventNotCalled('bonus_multiplier')
172+
self.assertEventNotCalled('bonus_total')
173+
174+
self.advance_time_and_run(.5)
175+
self.assertEventCalled('bonus_static')
176+
self.assertEventNotCalled('bonus_subtotal')
177+
self.assertEventNotCalled('bonus_multiplier')
178+
self.assertEventNotCalled('bonus_total')
179+
160180
self.advance_time_and_run(.5)
161181
self.assertEventCalled('bonus_subtotal')
162182
self.assertEventNotCalled('bonus_multiplier')

0 commit comments

Comments
 (0)