Parsers

Parsers in pyzork are powerful tools which allow you to understand what the player desires to do and return the necessary objects in order to perform that action.

pyzork.actions.attack_parser(choice: str, battle: Battle) → Union[Enemy, Player][source]

A robust system to handle the user attacking an enemy during battle. This performs a simple check to see if the user desire to perform a simple attack and who they desire to attack. The parser proceeds in a very short process:

  1. Check if the player desires to attack by checking it against the list of ACCEPTABLE_ATTACKS words and the name of the player’s weapon
  2. If a match a found then the parser seeks to find a target by passing the user input and context to :method:target_parser
Parameters:
  • choice (str) – The user input
  • battle (Battle) – The battle context for the attack
Returns:

The parsed target

Return type:

Union[Player, Enemy]

pyzork.actions.direction_parser(choice: str, current_location: Location) → pyzork.enums.Direction[source]

A bit more robust parser for picking a direction you want to go in. This parser works in the following way:

  1. Clean up user input and remove Stopwords.
  2. Check if the text contains any of the acceptable movement words such as “go”, “walk”, etc… If no matches are detected then the parser returns.
  3. If only one exit is possible then the direction of that exit is returned, else the parser continues below. (Could potentially be an issue if there are overlapping keywords)
  4. If one of the words is a cardinal direction or a number equivalent to one of the cardinal directions then return that, else the parser continues below
  5. Compare every location and pick the one where the most words of the user input match the name
Parameters:
  • choice (str) – User input
  • current_location (Location) – The location the user is currently at and against which to check the exits direction and names
Returns:

The direction the parser has determined the user is trying to go in

Return type:

Direction

Possible Improvements

  • Find a larger list of ACCEPTABLE_MOVEMENTS words
  • Take into consideration each exit’s print_interaction
pyzork.actions.equip_item_parser(choice: str, player: Player) → Equipment[source]

A more robust filter for deciphering what item the player desires to equip, wether it be Armor or Weapon. It works as follows:

  1. Clean up user input and remove Stopwords
  2. Check if any of the words in the user input match an acceptable word for equipping
  3. Compare every item and pick the one where the most words of the user input match the name
Parameters:
  • choice (str) – The user input
  • player (str) – The player trying to equip something
Returns:

The piece of equipment the parser had determined as the most likely the player is trying to equip

Return type:

Equipment

pyzork.actions.interact_parser(choice: str, location: Location) → Entity[source]

A bit more robust parser for picking a npc to interact with. This parser works in the following way:

  1. Clean up user input and remove Stopwords.
  2. Check if the text contains any of the acceptable interaction words such as “talk”, “interact”, etc… If no matches are detected then the parser returns.
  3. If only one interaction is possible then the npc of that interaction is returned, else the parser continues below. (Could potentially be an issue if there are overlapping keywords)
  4. Compare every npc and pick the one where the most words of the user input match the name
Parameters:
  • choice (str) – The user input
  • location (str) – The location in which the player is trying to interact
Returns:

The entity the parser has determined as most the most likely entity the player is trying to interact with

Return type:

Entity

Possible Improvements

  • Find a larger list of ACCEPTABLE_INTERACTS words
  • Take into consideration each npc’s print_interaction
pyzork.actions.shop_parser(choice: str, shop: Shop) → Tuple[str, Item][source]

A more robust parser for handling a player’s responses within the context of a shop. This parser works in the following way:

  1. Check if the player desires to exit the shop by checking if the input is a valid direction or parsable direction
  2. Clean up user input and remove Stopwords.
  3. For each possible alias in ALIASES_SHOP check if the user input contains, if it does then continue
  4. Compare every item and pick the one where the most words of the user input match the name
Parameters:
  • choice (str) – The user input
  • shop (Shop) – The shop the user is in and is trying to sell/buy from
Returns:

A tuple of the action the user is trying to do and the item they are trying to do it with

Return type:

Tuple[str, Item]

pyzork.actions.target_parser(choice: str, targets: List[Enemy], player: Optional[Player] = None) → Union[Enemy, Player][source]

This checks if the user input contains enough keywords that can be considered to target an enemy. This parser assumes that the input has already been cleaned up and that another parser has already validated the primary action. The parser works as follows:

  1. If the there is only one enemy then return that enemy
  2. Compare every enemy and pick the one where the most words of the user input match the name
  3. If there are multiple possible targets check for for position context from POSITIONS
Parameters:
  • choice (str) – The user input
  • targets (List[Enemy]) – The list of potential targets, can be empty
  • player (Optional[Player]) – The player casting the ability, used if the ability is castable on self
Returns:

The target

Return type:

Union[Enemy, Player]

Possible Improvements

  • Check if the user input includes index-based context for the target they wish to attack. e.g 3rd golbin
  • Check if the user input includes contextual clues such as “lowest health”, “weakest”, ect…
  • Allow the player as a valid attack target (Done, to be tested)
pyzork.actions.use_ability_parser(choice, ctx: Union[World, Battle]) → Tuple[Union[Enemy, Player], Ability][source]

A more robust parser for picking an ability to use and a target for that ability. The parser proceeds in the following way:

  1. Clean up user input and remove Stopwords.
  2. Check if any of the words in the user input match an acceptable word for casting an ability
  3. Compare every ability and pick the one where the most words of the user input match the name
  4. Hand the context over to :method:target_parser to look for a target
  5. If both an ability and a target have been parsed then return them
Parameters:choice (str) – The user input
Returns:The target and ability the user has determined as the most likely target
Return type:Tuple[Union[Enemy, Player], Ability]
pyzork.actions.use_item_parser(choice: str, ctx: Union[World, Battle]) → Tuple[Union[Enemy, Player], Consumable][source]

A more robust parser for picking an item to use and a target for that item. The parser proceeds in the following way:

  1. Clean up user input and remove Stopwords.
  2. Check if any of the words in the user input match an acceptable word for using an item
  3. Compare every item and pick the one where the most words of the user input match the name
  4. Hand the context over to :method:target_parser to look for a target
  5. If both an item and a target have been parsed then return them
Parameters:choice (str) – The user input
Returns:The target and item the parser has deemed to be the most likely intent by the user
Return type:Tuple[Union[Enemy, Player], Consumable]
pyzork.actions.view_parser(choice: str) → str[source]

A simple parser for picking a player property to view. This parser works in the following way:

  1. Clean up user input and remove Stopwords.
  2. For each possible alias in ALIASES_VIEW check if the user input contains any, if it does then return that alias
Parameters:choice (str) – The user input
Returns:The thing the player wishes to view
Return type:str
pyzork.actions.yes_or_no_parser(choice: str) → bool[source]

A simple parser to check for a yes or no answer, based on basic boolean checks, this is used within the yes_or_no utils function. The parser works as follow:

  1. Clean up user input and remove Stopwords.
  2. Check if any of the word match any of the YES or NO words, if they do return True or False respectively, if not return None
Parameters:choice (str) – The user input
Returns:True -> yes, False -> no, None -> neither
Return type:Optional[bool]