Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1358 from missionpinball/tilt_placeholders
use placeholders in tilt. fix #1031
  • Loading branch information
jabdoa2 committed May 5, 2019
2 parents e258ae4 + be0efca commit 573e71b
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 28 deletions.
6 changes: 3 additions & 3 deletions mpf/config_spec.yaml
Expand Up @@ -1507,7 +1507,7 @@ tic_stepper_settings:
poll_ms: single|ms|100ms
current_limit: single|int|192
tilt:
__valid_in__: machine, mode
__valid_in__: mode
tilt_slam_tilt_events: event_handler|str:ms|None
tilt_warning_events: event_handler|str:ms|None
tilt_events: event_handler|str:ms|None
Expand All @@ -1516,8 +1516,8 @@ tilt:
slam_tilt_switch_tag: single|str|slam_tilt
warnings_to_tilt: single|template_int|3
reset_warnings_events: event_handler|str:ms|ball_will_end
multiple_hit_window: single|ms|300ms
settle_time: single|ms|5s
multiple_hit_window: single|template_ms|300ms
settle_time: single|template_ms|5s
tilt_warnings_player_var: single|str|tilt_warnings
timed_switches:
__valid_in__: machine, mode
Expand Down
15 changes: 15 additions & 0 deletions mpf/core/config_validator.py
Expand Up @@ -64,6 +64,7 @@ def __init__(self, machine, load_cache, store_cache):
"template_int": self._validate_type_template_int,
"template_bool": self._validate_type_template_bool,
"template_secs": self._validate_type_template_secs,
"template_ms": self._validate_type_template_ms,
"boolean": self._validate_type_bool,
"ms": self._validate_type_ms,
"ms_or_token": self._validate_type_or_token(self._validate_type_ms),
Expand Down Expand Up @@ -515,6 +516,20 @@ def _validate_type_template_secs(self, item, validation_failure_info):

return self.machine.placeholder_manager.build_float_template(item)

def _validate_type_template_ms(self, item, validation_failure_info):
if item is None:
return None
if not isinstance(item, (str, int)):
self.validation_error(item, validation_failure_info, "Template has to be string/int.")

# try to convert to int. if we fail it will be a template
try:
item = Util.string_to_ms(item)
except ValueError:
pass

return self.machine.placeholder_manager.build_int_template(item)

def _validate_type_template_int(self, item, validation_failure_info):
if item is None:
return None
Expand Down
14 changes: 0 additions & 14 deletions mpf/core/mode.py
Expand Up @@ -126,20 +126,6 @@ def configure_mode_settings(self, config: dict) -> None:
priority=self.config['mode']['priority'] +
self.config['mode']['start_priority'])

def _get_merged_settings(self, section_name: str) -> dict:
"""Return a dict of a config section from the machine-wide config with the mode-specific config merged in."""
if section_name in self.machine.config:
return_dict = copy.deepcopy(self.machine.config[section_name])
else:
return_dict = dict()

if section_name in self.config:
return_dict = Util.dict_merge(return_dict,
self.config[section_name],
combine_lists=False)

return return_dict

@property
def is_game_mode(self) -> bool:
"""Return true if this is a game mode."""
Expand Down
2 changes: 1 addition & 1 deletion mpf/modes/credits/code/credits.py
Expand Up @@ -41,7 +41,7 @@ def mode_init(self):

self.credits_config = self.machine.config_validator.validate_config(
config_spec='credits',
source=self._get_merged_settings('credits'),
source=self.machine.config.get('credits', {}),
section_name='credits')

# add setting
Expand Down
2 changes: 1 addition & 1 deletion mpf/modes/high_score/code/high_score.py
Expand Up @@ -33,7 +33,7 @@ def mode_init(self):

self.high_score_config = self.machine.config_validator.validate_config(
config_spec='high_score',
source=self._get_merged_settings('high_score'),
source=self.config.get('high_score', {}),
section_name='high_score')

# if data is invalid. do not use it
Expand Down
22 changes: 14 additions & 8 deletions mpf/modes/tilt/code/tilt.py
Expand Up @@ -16,7 +16,7 @@ class Tilt(Mode):
"""

__slots__ = ["_balls_to_collect", "_last_warning", "ball_ending_tilted_queue", "tilt_event_handlers",
"last_tilt_warning_switch", "tilt_config"]
"last_tilt_warning_switch", "tilt_config", "_settle_time", "_warnings_to_tilt", "_multiple_hit_window"]

def __init__(self, machine: MachineController, config: dict, name: str, path) -> None:
"""Create mode."""
Expand All @@ -26,6 +26,9 @@ def __init__(self, machine: MachineController, config: dict, name: str, path) ->
self.tilt_event_handlers = None # type: Set[EventHandlerKey]
self.last_tilt_warning_switch = None # type: int
self.tilt_config = None # type: Any
self._settle_time = None
self._warnings_to_tilt = None
self._multiple_hit_window = None
super().__init__(machine, config, name, path)

def mode_init(self):
Expand All @@ -38,7 +41,7 @@ def mode_init(self):

self.tilt_config = self.machine.config_validator.validate_config(
config_spec='tilt',
source=self._get_merged_settings('tilt'),
source=self.config.get('tilt', {}),
section_name='tilt')

def mode_start(self, **kwargs):
Expand All @@ -57,6 +60,10 @@ def mode_start(self, **kwargs):
for event in self.tilt_config['tilt_slam_tilt_events']:
self.add_mode_event_handler(event, self.slam_tilt)

self._settle_time = self.tilt_config['settle_time'].evaluate([])
self._warnings_to_tilt = self.tilt_config['warnings_to_tilt'].evaluate([])
self._multiple_hit_window = self.tilt_config['multiple_hit_window'].evaluate([])

def mode_stop(self, **kwargs):
"""Stop mode."""
self._remove_switch_handlers()
Expand Down Expand Up @@ -118,15 +125,14 @@ def tilt_warning(self, **kwargs):

warnings = self.machine.game.player[self.tilt_config['tilt_warnings_player_var']]

if warnings >= self.tilt_config['warnings_to_tilt'].evaluate([]):
warnings_to_tilt = self._warnings_to_tilt
if warnings >= warnings_to_tilt:
self.tilt()
else:
self.machine.events.post(
'tilt_warning',
warnings=warnings,
warnings_remaining=(
self.tilt_config['warnings_to_tilt'].evaluate([]) -
warnings))
warnings_remaining=warnings_to_tilt - warnings)
'''event: tilt_warning
desc: A tilt warning just happened.
args:
Expand Down Expand Up @@ -204,7 +210,7 @@ def _tilt_switch_handler(self):

def _tilt_warning_switch_handler(self):
if (not self._last_warning or
(self._last_warning + (self.tilt_config['multiple_hit_window'] * 0.001) <=
(self._last_warning + (self._multiple_hit_window * 0.001) <=
self.machine.clock.get_time())):

self.tilt_warning()
Expand Down Expand Up @@ -250,7 +256,7 @@ def tilt_settle_ms_remaining(self):
if not self.last_tilt_warning_switch:
return 0

delta = (self.tilt_config['settle_time'] -
delta = (self._settle_time -
(self.machine.clock.get_time() -
self.last_tilt_warning_switch) * 1000)
if delta > 0:
Expand Down
5 changes: 4 additions & 1 deletion mpf/tests/machine_files/tilt/config/config.yaml
@@ -1,5 +1,8 @@
#config_version=5

config:
- settings.yaml

modes:
- tilt
- base
Expand Down Expand Up @@ -61,4 +64,4 @@ ball_devices:
flippers:
f_test:
main_coil: c_flipper
activation_switch: s_flipper
activation_switch: s_flipper
@@ -1,5 +1,8 @@
#config_version=5

config:
- settings.yaml

modes:
- tilt
- base
Expand Down
@@ -1,5 +1,8 @@
#config_version=5

config:
- settings.yaml

game:
balls_per_game: 3
allow_start_with_ball_in_drain: True
Expand Down
34 changes: 34 additions & 0 deletions mpf/tests/machine_files/tilt/config/settings.yaml
@@ -0,0 +1,34 @@
#config_version=5

settings:
warnings_to_tilt:
label: Number of tilt warnings
values:
0: "no warnings"
1: "1"
2: "2"
3: "3"
5: "5"
10: "10"
default: 3
key_type: int
sort: 600
settle_time:
label: Time to wait on tilt to settle bob
values:
3000: "3s"
5000: "5s"
10000: "10s"
default: 5000
key_type: int
sort: 610
multiple_hit_window:
label: Tilt sensitivity
values:
150: "sensitive"
300: "normal"
500: "insensitive"
1000: "very insensitive"
default: 300
key_type: int
sort: 620
3 changes: 3 additions & 0 deletions mpf/tests/machine_files/tilt/modes/tilt/config/tilt.yaml
Expand Up @@ -3,3 +3,6 @@
tilt:
reset_warnings_events: tilt_reset_warnings
tilt_events: tilt_event
multiple_hit_window: settings.multiple_hit_window
settle_time: settings.settle_time
warnings_to_tilt: settings.warnings_to_tilt

0 comments on commit 573e71b

Please sign in to comment.