def add_player(self, player_num): if player_num > len(self.player_list): player = Player(self, len(self.player_list)) self.player_list.append(player) self.events.post('player_added', player=player, num=player_num) # no events docstring as this event is also in mpf # Enable player var events and send all initial values player.enable_events(True, True)
def device_loaded_in_mode(self, mode: Mode, player: Player): """Restore internal state from player if persist_state is set or create new state.""" super().device_loaded_in_mode(mode, player) if self.config['persist_state']: if not player.is_player_var(self.player_state_variable): player[self.player_state_variable] = LogicBlockState( self.get_start_value()) # enable device ONLY when we create a new entry in the player if self._start_enabled: mode.add_mode_event_handler("mode_{}_started".format( mode.name), self.enable, priority=100) self._state = player[self.player_state_variable] else: self._state = LogicBlockState(self.get_start_value()) if self._start_enabled: mode.add_mode_event_handler("mode_{}_started".format( mode.name), self.enable, priority=100) mode.add_mode_event_handler("mode_{}_started".format(mode.name), self.post_update_event)
def add_player(self, player_num): if player_num > len(self.player_list): player = Player(self, len(self.player_list)) self.player_list.append(player) self.events.post('player_add_success', player=player, num=player_num)
def _player_add(self, ev_result=True): # This is the callback from our request player add event. # Don't call it directly. if ev_result is False: self.debug_log("Request to add player has been denied.") return False else: player = Player(self.machine, len(self.player_list)) self.player_list.append(player) self.num_players = len(self.player_list) self.machine.create_machine_var(name='player{}_score'.format( player.number), value=player.score, persist=True) '''machine_var: player(x)_score desc: Holds the numeric value of a player's score. The "x" is the player number, so this actual machine variable is ``player1_score`` or ``player2_score``. Since these are machine variables, they are maintained even after a game is over. Therefore you can use these machine variables in your attract mode display show to show the scores of the last game that was played. These machine variables are updated at the end of each player's turn, and they persist on disk so they are restored the next time MPF starts up. ''' return player
def __init__(self, machine: MachineController, name: str, player: Player, config: dict): """Initialize logic block.""" self.machine = machine self.name = name self.player = player self.handler_keys = set() # LogicBlocks are loaded multiple times and config_validator changes the config # therefore we have to copy the config config = copy.deepcopy(config) self.config = self.machine.config_validator.validate_config( 'logic_blocks:{}'.format(self.config_section_name), config, base_spec='logic_blocks:common') self.configure_logging('Logicblock.' + name, self.config['console_log'], self.config['file_log']) self.player_state_variable = "{}_state".format(self.name) '''player_var: (logic_block)_state desc: A dictionary that stores the internal state of the logic block with the name (logic_block). (In other words, a logic block called *mode1_hit_counter* will store its state in a player variable called ``mode1_hit_counter_state``). The state that's stored in this variable include whether the logic block is enabled and whether it's complete. The actual value of the logic block is stored in another player variable whose name you can specify via the ``player_variable:`` setting in the individual logic block config. ''' if not player.is_player_var(self.player_state_variable ) or not self.config['persist_state']: player[self.player_state_variable] = { "enabled": False, "completed": False } if not self.config['enable_events']: self.enabled = True if not self.config['events_when_complete']: self.config['events_when_complete'] = [ 'logicblock_' + self.name + '_complete' ] if not self.config['events_when_hit']: self.config['events_when_hit'] = [ 'logicblock_' + self.name + '_hit' ]
def device_loaded_in_mode(self, mode: Mode, player: Player): """Load extra ball in mode and initialise player. Args: mode: Mode which is loaded player: Current player """ del mode self.player = player if not player.is_player_var('extra_ball_{}_num_awarded'.format( self.name)): player['extra_ball_{}_num_awarded'.format(self.name)] = 0 '''player_var: extra_ball_(name)_awarded
def device_loaded_in_mode(self, mode: Mode, player: Player): """Load extra ball in mode and initialise player. Args: ---- mode: Mode which is loaded player: Current player """ del mode self.player = player if not player.is_player_var(self._player_var_name): player[self._player_var_name] = 0 '''player_var: extra_ball_(name)_awarded
def _create_player_logic_blocks(self, player: Player, **kwargs): """Create the game-wide logic blocks for this player. Args: player: The player object. **kwargs: Does nothing. Just here to allow this method to be called via an event handler. Note that this method is automatically added as a handler to the 'player_add_success' event. """ del kwargs player.logic_blocks = set() if 'logic_blocks' in self.machine.config: self._create_logic_blocks( config=self.machine.config['logic_blocks'], player=player)
def _player_add_request_complete(self, ev_result=True, **kwargs) -> bool: """Handle result of player_add_request callback. Callback from our request player add event. Don't call it directly. """ del kwargs if ev_result is False: self.debug_log("Request to add player has been denied.") return False new_player_number = len(self.player_list) + 1 self.machine.events.post('player_will_add', number=new_player_number) '''event: player_will_add desc: A new player will be added to this game. This event is sent immediately prior to the player_adding event. args: number: The new player number that will be added ''' # Actually create the new player object player = Player(self.machine, len(self.player_list)) self.player_list.append(player) self.num_players = len(self.player_list) self.machine.events.post_queue('player_adding', player=player, number=player.number, callback=partial( self._player_adding_complete, player=player)) '''event: player_adding desc: A new player is in the process of being added to this game. This is a queue event, and the player won't actually be finished adding until the queue is cleared. args: player: The player object for the player being added number: The player number ''' return True
def _create_player_logic_blocks(self, player: Player, **kwargs): """Create the game-wide logic blocks for this player. Args: player: The player object. **kwargs: Does nothing. Just here to allow this method to be called via an event handler. Note that this method is automatically added as a handler to the 'player_add_success' event. """ del kwargs player.logic_blocks = set() '''player_var: logic_blocks desc: A set which contains references to all the logic blocks which exist for this player. There's nothing useful in here for you, we just include it so you know what this player variable does. ''' if 'logic_blocks' in self.machine.config: self._create_logic_blocks( config=self.machine.config['logic_blocks'], player=player)
def __init__(self, machine: MachineController, name: str, player: Player, config: dict): """Initialise logic block.""" self.machine = machine self.name = name self.player = player self.handler_keys = set() self.log = None # LogicBlocks are loaded multiple times and config_validator changes the config # therefore we have to copy the config config = copy.deepcopy(config) self.config = self.machine.config_validator.validate_config( 'logic_blocks:{}'.format(self.config_section_name), config, base_spec='logic_blocks:common') self.player_state_variable = "{}_state".format(self.name) if not player.is_player_var(self.player_state_variable ) or not self.config['persist_state']: player[self.player_state_variable] = { "enabled": False, "completed": False } if not self.config['enable_events']: self.enabled = True if not self.config['events_when_complete']: self.config['events_when_complete'] = [ 'logicblock_' + self.name + '_complete' ] if not self.config['events_when_hit']: self.config['events_when_hit'] = [ 'logicblock_' + self.name + '_hit' ]