Skip to content

Commit

Permalink
Merge pull request #1751 from avanwinkle/shot-priority-for-switch-han…
Browse files Browse the repository at this point in the history
…dlers

Shot priority for switch handlers
  • Loading branch information
avanwinkle authored Feb 4, 2024
2 parents 8888f30 + 7a1dc6f commit 298ce12
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mpf/config_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ shots:
delay_switch: dict|machine(switches):ms|None
persist_enable: single|bool|true
playfield: single|machine(playfields)|playfield
priority: single|int|0
mark_playfield_active: single|bool|true
enable_events: event_handler|event_handler:ms|None
disable_events: event_handler|event_handler:ms|None
Expand Down Expand Up @@ -2196,6 +2197,7 @@ widgets:
timing: single|enum(after_previous,with_previous)|after_previous
repeat: single|bool_or_token|false
easing: single|str|linear
step: single|float|0.0
bezier:
points: list|num_or_token|
thickness: single|float_or_token|1.0
Expand Down
8 changes: 5 additions & 3 deletions mpf/devices/shot.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ def _register_switch_handlers(self):
self._handlers = []
for switch in self.config['switches']:
self._handlers.append(self.machine.events.add_handler("{}_active".format(switch.name),
self.event_hit, priority=self.mode.priority,
self.event_hit,
priority=self.mode.priority + self.config['priority'],
blocking_facility="shot"))

for switch in list(self.config['delay_switch'].keys()):
self._handlers.append(self.machine.events.add_handler("{}_active".format(switch.name),
self._delay_switch_hit,
switch_name=switch.name,
priority=self.mode.priority,
priority=self.mode.priority + self.config['priority'],
blocking_facility="shot"))

def _remove_switch_handlers(self):
Expand Down Expand Up @@ -281,7 +282,8 @@ def event_hit(self, **kwargs):
if self.profile.config['block']:
min_priority = kwargs.get("_min_priority", {"all": 0})
min_shots = min_priority.get("shot", 0)
min_priority["shot"] = self.mode.priority if self.mode.priority > min_shots else min_shots
shot_priority = self.mode.priority + self.config["priority"]
min_priority["shot"] = shot_priority if shot_priority > min_shots else min_shots
return {"_min_priority": min_priority}

return None
Expand Down
1 change: 1 addition & 0 deletions mpf/tests/machine_files/shots/config/test_shots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ modes:
- base3
- mode1
- mode2
- mode3

switches:
switch_1:
Expand Down
21 changes: 21 additions & 0 deletions mpf/tests/machine_files/shots/modes/mode3/config/mode3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#config_version=6

mode:
priority: 100

shots:
mode3_shot_3_1:
switch: switch_3
mode3_shot_3_2:
switch: switch_3
priority: 10

variable_player:
mode3_shot_3_1_hit:
foo:
action: set
int: 100
mode3_shot_3_2_hit:
foo:
action: set
int: 50
10 changes: 10 additions & 0 deletions mpf/tests/test_Shots.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ def test_mode_priorities(self):
# check if color returns to mode1_shot_2 color
self.assertLightColor("light_2", "antiquewhite")

def test_switch_priorities(self):
self.start_game()
self.start_mode("mode3")

self.assertEqual(self.machine.game.player['foo'], 0)
self.hit_and_release_switch('switch_3')
self.advance_time_and_run()

self.assertEqual(self.machine.game.player['foo'], 100)

def test_hits(self):
self.assertFalse(self.machine.shots["mode1_shot_1"].enabled)

Expand Down

0 comments on commit 298ce12

Please sign in to comment.