Skip to content

Commit

Permalink
Linting, Unit tests and naming updates based on PR
Browse files Browse the repository at this point in the history
Fixed linting, updated unit tests and renamed event kwarg names.
  • Loading branch information
bernarma committed Jan 11, 2025
1 parent 16b2a80 commit 2367590
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
18 changes: 8 additions & 10 deletions mpf/devices/multiball.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,20 @@ def _ball_drain_count_balls(self, balls, **kwargs):

restart_grace_period_ms = self.config['restart_grace_period'].evaluate([])

if not self.machine.game or (self.machine.game.balls_in_play - balls < 1 and restart_grace_period_ms==0):
if not self.machine.game or (self.machine.game.balls_in_play - balls < 1 and restart_grace_period_ms == 0):
self._multiball_end()
elif restart_grace_period_ms>0:
elif restart_grace_period_ms > 0:
if self.machine.game.balls_in_play == 0:
self.delay.run_now(f"{self.name}_restart_grace_period")
self.delay.run_now("restart_grace_period")
elif self.machine.game.balls_in_play - balls < 1:
self.machine.events.post(f"multiball_{self.name}_will_end", duration=restart_grace_period_ms)
self.machine.events.post(f"multiball_{self.name}_will_end", grace_period=restart_grace_period_ms)
'''event: multiball_(name)_will_end
desc: The multiball called (name) does not have multiple balls active, enabling grace period.
args:
duration: duration of the restart grace period
grace_period: restart duration grace period in ms
'''

self.delay.add(name=f"{self.name}_restart_grace_period",
self.delay.add(name="restart_grace_period",
ms=restart_grace_period_ms,
callback=self._multiball_end)

Expand All @@ -266,7 +266,6 @@ def _multiball_end(self):
'''
self.debug_log("Ball drained. MB ended.")


@event_handler(5)
def event_stop(self, **kwargs):
"""Event handler for stop event."""
Expand Down Expand Up @@ -304,10 +303,9 @@ def event_add_a_ball(self, **kwargs):

def add_a_ball(self):
"""Add a ball if multiball has started."""

# remove if it exists as multiball now has multiple balls
if self.delay.check(f"{self.name}_restart_grace_period"):
self.delay.remove(f"{self.name}_restart_grace_period")
if self.delay.check("restart_grace_period"):
self.delay.remove("restart_grace_period")
self.machine.events.post(f"multiball_{self.name}_will_resume")
'''event: multiball_(name)_will_resume
desc: Multiball (name) has been resumed during end of multiball grace period.
Expand Down
20 changes: 13 additions & 7 deletions mpf/tests/test_MultiBall.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,20 +1235,20 @@ def testEndMultiballGracePeriodNoRestart(self):
self.assertAvailableBallsOnPlayfield(1)
self.mock_event("multiball_mb_grace_period_restart_will_end")
self.mock_event("multiball_mb_grace_period_restart_ended")
self.mock_event("multiball_mb_grace_period_restart_will_resume")

# start mb, default ball save, 5s restart grace period
self.post_event("mb_grace_period_restart_start")
self.advance_time_and_run(10)
self.assertAvailableBallsOnPlayfield(2)

self.drain_one_ball()
self.advance_time_and_run(5)
self.advance_time_and_run(10)

self.assertEventCalled("multiball_mb_grace_period_restart_will_end", 1)
self.assertEventCalled("multiball_mb_grace_period_restart_ended", 0)

self.advance_time_and_run(5)
self.assertEventNotCalled("multiball_mb_grace_period_restart_will_resume")
self.assertEventCalled("multiball_mb_grace_period_restart_ended", 1)
self.assertAvailableBallsOnPlayfield(1)


def testEndMultiballGracePeriodAddABallRestart(self):
Expand All @@ -1268,12 +1268,18 @@ def testEndMultiballGracePeriodAddABallRestart(self):
self.drain_one_ball()
self.advance_time_and_run(3)

# we should now be in the grace period (set at 5 seconds)
self.assertEventCalled("multiball_mb_grace_period_restart_will_end", 1)
self.assertEventCalled("multiball_mb_grace_period_restart_ended", 0)
self.assertEventNotCalled("multiball_mb_grace_period_restart_ended")

# trigger the add a ball during grace period
self.post_event("add_ball")
self.assertEventCalled("multiball_mb_grace_period_restart_ended", 0)
self.advance_time_and_run()
self.assertEventNotCalled("multiball_mb_grace_period_restart_ended")
self.assertEventCalled("multiball_mb_grace_period_restart_will_resume", 1)
self.advance_time_and_run(5)

# fast-forward time past the grace period and ensure multiball has resumed (i.e. with 2 balls)
self.advance_time_and_run(10)
self.assertAvailableBallsOnPlayfield(2)
self.assertEventCalled("ball_save_mb_grace_period_restart_timer_start")
self.assertEventNotCalled("multiball_mb_grace_period_restart_ended")

0 comments on commit 2367590

Please sign in to comment.