def replace_reaction(target: dict, action: str, reaction_ind, reaction_new): """ Replace reaction for specified keyword for the {object} Args: target: Target {object} or {player}; action: Trigger action keyword; reaction_ind: Index of reaction that should be replaced; reaction_new: A new function that should be executed; Return: updated {item} objects """ # Check that passed target is an {item} or {player} object: if not (general.is_object(target) or general.is_player(target)): print(f"Error: {target['name']} is not an Object or Player") else: try: target['reactions'][action][reaction_ind] = reaction_new print( f"Reaction on action: {action} was successfully replaced for" + f"{target['name']}") except KeyError: print( f'There is no such action: {action} or reaction index: {reaction_ind}' ) return target
def add_action(target: dict, keyword: str, commands: tuple): """ Add action keyword to the {item} or Player Args: target: Target {item} or Player; keyword: Action word. commands: Tuple with acceptable commands Return: updated {item} objects """ # Check that passed target is an {item} or {player}: if not (general.is_item(target) or general.is_player(target)): print( f'{target["name"]} is not an appropriate object. Only Items or Player can have actions' ) # Check that keyword is in command list: elif keyword not in commands: print(f'{keyword} is not an appropriate command') else: target['actions'].add(keyword) # Print info message: print(f'successfully added {keyword} action to {target["name"]}') return target
def remove_reaction(target: dict, action: str, remove_all=False, reaction_ind=-1): """ Remove reaction for specified keyword from the {object} Args: target: Target {object} or {player}; action: Trigger action keyword; remove_all: (optional) If True, removes all reactions for specified action. False by default reaction_ind: If remove_all is set to False, remove only action from specified position (last by default). Return: updated {item} objects """ # Check that passed target is an {item} or {player} object: if not (general.is_object(target) or general.is_player(target)): print(f"Error: {target['name']} is not an Object or Player") else: if remove_all: try: del target['reactions'][action] print( f"All reactions were successfully deleted for action: {action}" ) except KeyError: print(f'There is no reactions for such action: {action}') else: try: target['reactions'][action].pop(reaction_ind) print( f"Reaction was successfully deleted for action: {action}") except KeyError: print(f'There is no such reaction index{reaction_ind}') return target
def pickup_item(player: dict, item: dict, place: dict): """Move {item} from it current location and place it in Player's Inventory. Args: player: {player} object; item: {item} to be moved; place: {place} where {item} is located now Return: Updated {player}, {item} and {place} objects """ # Check that {player} and {item} objects was passed and that: if not (general.is_player(player) and general.is_item(item) and general.is_place(place)): print( f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.' ) # Check if the {item} is located in {place}: elif item.get('location') == place: # Remove {item} from its current location: place['items'].remove(item) # Move the {item} to Inventory: item['location'] = player player['items'].append(item) # Update player's actions: player = update_actions(player) else: print(f"There is no {item['name']} in {place['name']}") return player, item, place
def set_abilities(player: dict, abilities: set, commands: set = None): """Update set of abilities available to Player. Args: player: {player} object; abilities: set of actions that would be available to Player by default; commands: a set of acceptable command keywords. Return: Updated {player} object """ # If no set of acceptable command keywords was passed, use the default one: if commands is None: commands = language.create_vocabulary() # Check that {player} object was passed: if not general.is_player(player): print(f'{player["name"]} is not an appropriate object.') # Check that all actions in abilities are acceptable commands: elif not abilities.issubset(commands): print( f"Following actions are unknown: {', '.join(abilities.difference(commands))}" ) else: player['abilities'] = abilities player = update_actions(player) # Print info message: print(f"{player}\'s abilities were set successfully") return player
def drop_item(player: dict, item: dict, place: dict): """Move {item} from Player's Inventory to a specified location. Args: player: {player} object; item: {item} to be moved; place: {place} where {item} should be placed Return: Updated {player}, {item} and {place} objects """ # Check that {player} and {item} objects was passed and that: if not (general.is_player(player) and general.is_item(item) and general.is_place(place)): print( f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.' ) else: # Remove {item} from Inventory: player['items'].remove(item) # Move the {item} to Inventory: item['location'] = place place['items'].append(item) # Update player's actions: player = update_actions(player) print(f"You have dropped {item['name']} in {place['name']}") return player, item, place
def print_reactions(target: dict, action: str): """ Show all set reactions for specified action for {object}. Args: target: Target {object}; action: Trigger action keyword; """ # Check that passed target is an {item} or {player} object: if not (general.is_object(target) or general.is_player(target)): print(f"Error: {target['name']} is not an Object or Player") else: try: # Print list of existing functions: for i, reaction in enumerate(target['reactions'][action]): print(f'Reaction #{i}: {reaction}') except KeyError: print(f'There is no reactions for such action: {action}')
def add_reaction(target: dict, action: str, reaction, ind=None, commands: set = None): """ Add reaction for specified keyword to the {object} Args: target: Target {object} or {player}; action: Trigger action keyword; reaction: A function that should be executed; ind: (optional) Reaction index; commands: Set of acceptable actions Return: updated {item} objects """ # If no set of acceptable command keywords was passed, use the default one: if commands is None: commands = language.create_vocabulary() # Check that passed target is an {item} or {player} object: if not (general.is_object(target) or general.is_player(target)): print(f"Error: {target['name']} is not an Object or Player") # Check that keyword is in command list: elif action not in commands: print(f'{action} is not an appropriate command') else: # Check if there is list object exists for passed action and if not, create an empty one: target['reactions'][action] = target['reactions'].get(action, []) if ind is None: target['reactions'][action].append(reaction) print( f"Reaction was successfully added into {target['name']} for action: {action}" ) else: try: target['reactions'][action].insert(ind, reaction) print( f"Reaction was successfully added into {target['name']} for action: {action}" ) except KeyError: print('Error: Incorrect index number') return target
def update_actions(player: dict): """Update set of actions available to Player. Args: player: {player} object; Return: Updated {player} object """ # Check that {player} object was passed: if general.is_player(player): # Get a set of actions provided by items in inventory: inv_actions = set() for item in player['items']: inv_actions.update(item['actions']) # Set Player's actions as union of inv_actions and abilities: player['actions'] = player['abilities'].union(inv_actions) else: print(f'{player["name"]} is not an appropriate object.') return player
def remove_action(target: dict, keyword: str): """ Add action keyword to the {item} or Player Args: target: Target {item} or Player; keyword: Action word that needs to be removed. Return: updated {item} objects """ # Check that passed target is an {item} or {player}: if not (general.is_item(target) or general.is_player(target)): print( f'{target["name"]} is not an appropriate object. Only Items or Player can have actions' ) # Check that keyword is in item actions: elif keyword not in target['actions']: print(f'{target["name"]} has no {keyword} action') else: target['actions'].remove(keyword) # Print info message: print(f'successfully removed {keyword} action from {target["name"]}')