Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mpf/config_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,7 @@ shots:
switches: list|machine(switches)|None
start_enabled: single|bool|None
delay_switch: dict|machine(switches):ms|None
delay_events: dict|event_handler:ms|None
persist_enable: single|bool|true
playfield: single|machine(playfields)|playfield
priority: single|int|0
Expand Down
32 changes: 20 additions & 12 deletions mpf/devices/shot.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ async def _initialize(self) -> None:

for switch in self.config['switches'] + list(self.config['delay_switch'].keys()):
# mark the playfield active no matter what
switch.add_handler(self._mark_active,
callback_kwargs={'playfield': switch.config['playfield']})
switch.add_handler(self._mark_active, callback_kwargs={'playfield': switch.config['playfield']})

def _mark_active(self, playfield, **kwargs):
"""Mark playfield active."""
Expand Down Expand Up @@ -85,17 +84,27 @@ def validate_and_parse_config(self, config: dict, is_mode_config: bool, debug_pr

def _register_switch_handlers(self):
self._handlers = []
priority = self.mode.priority + self.config['priority']
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.config['priority'],
priority=priority,
blocking_facility="shot"))

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

for event, ms in list(self.config['delay_events'].items()):
self._handlers.append(self.machine.events.add_handler(event,
self._delay_switch_hit,
name=event,
ms=ms,
priority=priority,
blocking_facility="shot"))

def _remove_switch_handlers(self):
Expand Down Expand Up @@ -406,18 +415,17 @@ def _notify_monitors(self, profile, state):
callback(name=self.name, profile=profile, state=state)

@event_handler(4)
def _delay_switch_hit(self, switch_name, **kwargs):
def _delay_switch_hit(self, name, ms, **kwargs):
del kwargs
if not self.enabled:
return

self.delay.reset(name=switch_name + '_delay_timer',
ms=self.config['delay_switch']
[self.machine.switches[switch_name]],
self.delay.reset(name=name + '_delay_timer',
ms=ms,
callback=self._release_delay,
switch=switch_name)
switch=name)

self.active_delays.add(switch_name)
self.active_delays.add(name)

def _release_delay(self, switch):
self.active_delays.remove(switch)
Expand Down
2 changes: 2 additions & 0 deletions mpf/tests/machine_files/shots/config/test_shots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ switches:
number:
switch_28:
number:
switch_29:
number:

lights:
light_1:
Expand Down
4 changes: 4 additions & 0 deletions mpf/tests/machine_files/shots/modes/base2/config/base2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ shots:
switch: switch_15
delay_switch:
switch_15: 2s
shot_delay_event:
switch: switch_29
delay_events:
my_delay_event: 2s
default_show_light:
switch: switch_5
show_tokens:
Expand Down
14 changes: 14 additions & 0 deletions mpf/tests/test_Shots.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def test_shot_with_multiple_switches(self):
def test_shot_with_delay(self):
self.mock_event("shot_delay_hit")
self.mock_event("shot_delay_same_switch_hit")
self.mock_event("shot_delay_event_hit")
self.start_game()

# test delay at the beginning. should not count
Expand All @@ -247,6 +248,14 @@ def test_shot_with_delay(self):
self.assertEventCalled("shot_delay_same_switch_hit")
self.mock_event("shot_delay_same_switch_hit")

# test delay with event. should not count
self.post_event("my_delay_event")
self.advance_time_and_run(.5)
self.hit_and_release_switch("switch_29")
self.advance_time_and_run(1)
self.assertEventNotCalled("shot_delay_event_hit")
self.advance_time_and_run(3)

# test that shot works without delay
self.hit_and_release_switch("switch_1")
self.advance_time_and_run(.5)
Expand All @@ -256,6 +265,11 @@ def test_shot_with_delay(self):
self.hit_and_release_switch("switch_1")
self.advance_time_and_run(.5)
self.assertEventCalled("shot_delay_hit")

self.hit_and_release_switch("switch_29")
self.advance_time_and_run(.5)
self.assertEventCalled("shot_delay_event_hit")

self.mock_event("shot_delay_hit")
self.hit_and_release_switch("s_delay")

Expand Down
Loading