"""Random event config player."""
from mpf.core.config_player import ConfigPlayer
from mpf.core.randomizer import Randomizer
from mpf.core.utility_functions import Util


class RandomEventPlayer(ConfigPlayer):

    """Plays a random event based on config."""

    config_file_section = 'random_event_player'
    show_section = 'random_events'
    device_collection = None

    def __init__(self, machine):
        """Initialise random event player."""
        super().__init__(machine)
        self._machine_wide_dict = {}

    def _get_randomizer(self, settings, context, calling_context):
        key = "random_{}.{}".format(context, calling_context)
        if settings['scope'] == "player":
            if not self.machine.game.player[key]:
                self.machine.game.player[key] = Randomizer(settings['events'])
            return self.machine.game.player[key]
        else:
            if key not in self._machine_wide_dict:
                self._machine_wide_dict[key] = Randomizer(settings['events'])
            return self._machine_wide_dict[key]

    def play(self, settings, context, calling_context, priority=0, **kwargs):
        """Play a random event from list based on config."""
        del priority
        randomizer = self._get_randomizer(settings, context, calling_context)
        self.machine.events.post(randomizer.get_next(), **kwargs)

    def validate_config_entry(self, settings, name):
        """Validate one entry of this player."""
        config = self._parse_config(settings, name)
        return config

    def get_express_config(self, value):
        """Parse express config."""
        return {"events": self.get_list_config(Util.string_to_list(value))}

    def get_list_config(self, value):
        """Parse list."""
        return {"events": value}


player_cls = RandomEventPlayer

@@ -809,32 +809,249 @@
 ings%5B'events'%5D)%0A
+%0A            if settings%5B'force_all'%5D:%0A                self.machine.game.player%5Bkey%5D.force_all = True%0A%0A            if settings%5B'force_different'%5D:%0A                self.machine.game.player%5Bkey%5D.force_different = True%0A%0A
             retu
@@ -1083,16 +1083,17 @@
 er%5Bkey%5D%0A
+%0A
         
@@ -1149,16 +1149,16 @@
 e_dict:%0A
-
         
@@ -1227,16 +1227,231 @@
 ents'%5D)%0A
+%0A            if settings%5B'force_all'%5D:%0A                self._machine_wide_dict%5Bkey%5D.force_all = True%0A%0A            if settings%5B'force_different'%5D:%0A                self._machine_wide_dict%5Bkey%5D.force_different = True%0A%0A
         

add events to random event player