Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DigitalScoreReel Device #1545

Merged
20 changes: 20 additions & 0 deletions mpf/config_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ digital_outputs:
platform_settings: single|dict|None
type: single|enum(light,driver)|
light_subtype: single|str|None
digital_score_reels:
__valid_in__: machine
__type__: device
reel_count: single|num|0
start_value: single|str|0
frames: list|subconfig(digital_score_reel_frame)|None
include_player_number: single|bool|False
digital_score_reel_frame:
character: single|str|None
frame: single|int|None
dual_wound_coils:
__valid_in__: machine
__type__: device
Expand Down Expand Up @@ -678,11 +688,21 @@ info_lights:
image_pools:
__valid_in__: machine, mode # todo add to validator
__type__: config_dict
image_templates:
__valid_in__: machine
__type: config
images:
__valid_in__: machine, mode
__type__: config_dict
file: single|str|None
frame_skips: list|subconfig(images_frame_skips)|None
image_template: single|subconfig(images,device)|
load: single|str|None
images_frame_skips:
__valid_in__: machine, mode
__type__: config_dict
from: single|int|None
to: single|int|None
keyboard:
__valid_in__: machine # todo add to validator
__type__: config_dict
Expand Down
35 changes: 35 additions & 0 deletions mpf/devices/digital_score_reel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Contains the base class for digital (image-based) score reels."""

from mpf.core.system_wide_device import SystemWideDevice


class DigitalScoreReel(SystemWideDevice):
config_section = 'digital_score_reels'
collection = 'digital_score_reels'
class_label = 'digital_score_reel'

def __init__(self, machine, name):
super().__init__(machine, name)
self._frames = {}
self._reel_count = 0
self._include_player_number = False

async def _initialize(self):
await super()._initialize()

self._reel_count = self.config["reel_count"]
self._include_player_number = self.config["include_player_number"]
for frame in self.config["frames"]:
self._frames[str(frame["character"])] = str(frame["frame"])

self.machine.events.add_handler(self.name, self._post_reel_values)

def _post_reel_values(self, **kwargs):
# Pad the string up to the necessary number of characters in the reel
score = str(kwargs["value"]).rjust(self._reel_count, self.config["start_value"])
# Create a dict of reel name keys to target frame values
result = { str(i + 1): self._frames[score[i]] for i in range(self._reel_count)}
# Post the event
event_name = "score_reel_{}_player{}".format(self.name, self.machine.game.player.number) if \
self._include_player_number else "score_reel_{}".format(self.name)
self.machine.events.post(event_name, **result)
1 change: 1 addition & 0 deletions mpf/mpfconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ mpf:
- mpf.devices.servo.Servo
- mpf.devices.achievement.Achievement
- mpf.devices.achievement_group.AchievementGroup
- mpf.devices.digital_score_reel.DigitalScoreReel
- mpf.devices.dmd.Dmd
- mpf.devices.rgb_dmd.RgbDmd
- mpf.devices.light_group.LightStrip
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#config_version=5

digital_score_reels:
player_score:
reel_count: 4
include_player_number: true
frames:
- character: 1
frame: 2
- character: 2
frame: 4
- character: 3
frame: 6
- character: 4
frame: 8
- character: 5
frame: 10
- character: 6
frame: 12
- character: 7
frame: 14
- character: 8
frame: 16
- character: 9
frame: 18
- character: 0
frame: 20
arbitrary_event:
reel_count: 3
start_value: X
frames:
- character: A
frame: 1
- character: B
frame: 2
- character: C
frame: 3
- character: D
frame: 4
- character: E
frame: 5
- character: X
frame: 6
44 changes: 44 additions & 0 deletions mpf/tests/test_DigitalScoreReels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test digital score reels."""

from mpf.tests.MpfFakeGameTestCase import MpfFakeGameTestCase

class TestDigitalScoreReels(MpfFakeGameTestCase):

def get_config_file(self):
return 'test_digital_score_reels.yaml'

def get_machine_path(self):
return 'tests/machine_files/digital_score_reels'

def test_player_score(self):
self.mock_event('score_reel_player_score_player1')
self.mock_event('score_reel_player_score_player2')
self.start_two_player_game()
self.assertGameIsRunning()

self.assertPlayerNumber(1)
self.advance_time_and_run()
self.machine.game.player.score = 123
self.advance_time_and_run()
self.assertEqual({"1": "20", "2": "2", "3": "4", "4": "6"}, self._last_event_kwargs['score_reel_player_score_player1'])
self.assertEventNotCalled('score_reel_player_score_player2')

self.drain_all_balls()
self.assertPlayerNumber(2)
self.machine.game.player.score = 9876
self.advance_time_and_run()
self.assertEqual({"1": "18", "2": "16", "3": "14", "4": "12"}, self._last_event_kwargs['score_reel_player_score_player2'])

def test_arbitrary_event(self):
self.mock_event('score_reel_arbitrary_event')
self.post_event_with_params('arbitrary_event', value='AB')
self.advance_time_and_run()
self.assertEqual({"1": "6", "2": "1", "3": "2"}, self._last_event_kwargs['score_reel_arbitrary_event'])

self.post_event_with_params('arbitrary_event', value='DXB')
self.advance_time_and_run()
self.assertEqual({"1": "4", "2":"6", "3": "2"}, self._last_event_kwargs['score_reel_arbitrary_event'])

self.post_event_with_params('arbitrary_event', value='EC')
self.advance_time_and_run()
self.assertEqual({"1": "6", "2":"5", "3": "3"}, self._last_event_kwargs['score_reel_arbitrary_event'])