gachapy
A gacha engine built in Python for developing gacha games
Directory
- controller
The controller used for all high level management of the gacha game
- objects
Objects used for low level management and data storage of the gacha game
- loader
Library of loader functions for loading gachapy objects from json files
- keylang
The tokenizer, parser, and interpreter for KeyLang, the language used by
gachapy to save and load custom rarity to drop rate functions
Author: Jacob Kerr, 2021
Submodules
Package Contents
Classes
A representation of an item in the gacha game. |
|
A representation of a banner in the gacha game. |
|
A representation of a player in the gacha game. |
|
The base class of the abstract syntax tree |
|
An expression in the KeyLang grammar |
|
A term in the KeyLang grammar |
|
A factor in the KeyLang grammar |
|
A float literal in the KeyLang grammar |
|
A rarity variable in the KeyLang grammar |
|
A controller used to control an instance of a gacha game. Used to |
|
A representation of an item in the gacha game. |
|
A representation of a banner in the gacha game. |
|
A representation of a player in the gacha game. |
|
The base class of the abstract syntax tree |
|
An expression in the KeyLang grammar |
|
A term in the KeyLang grammar |
|
A factor in the KeyLang grammar |
|
A float literal in the KeyLang grammar |
|
A rarity variable in the KeyLang grammar |
|
A representation of an item in the gacha game. |
|
A representation of a banner in the gacha game. |
|
A representation of a player in the gacha game. |
|
The base class of the abstract syntax tree |
|
An expression in the KeyLang grammar |
|
A term in the KeyLang grammar |
|
A factor in the KeyLang grammar |
|
A float literal in the KeyLang grammar |
|
A rarity variable in the KeyLang grammar |
|
A controller used to control an instance of a gacha game. Used to |
|
The base class of the abstract syntax tree |
|
An expression in the KeyLang grammar |
|
A term in the KeyLang grammar |
|
A factor in the KeyLang grammar |
|
A float literal in the KeyLang grammar |
|
A rarity variable in the KeyLang grammar |
Functions
|
The function used to sort items in a list of items |
|
Returns the random weights of the items for the random function |
|
Parses the inputed KeyLang expression into a KeyLang abstract syntax |
|
Converts the inputed KeyLang expression into a list of tokens |
|
Parses the inputted tokens into a KeyLang abstract syntax tree |
|
Parses the inputted tokens as an expression |
|
Parses the inputted tokens as a term |
|
Parses the inputted tokens as a factor |
|
Parses the inputted tokens as a base |
|
Parses the inputted tokens as a constant |
|
Evaluates the KeyLang AST to a value using the rarity |
|
The function used to sort items in a list of items |
|
The key used to sort players in a list of players |
|
Returns the random weights of the items for the random function |
|
Parses the inputed KeyLang expression into a KeyLang abstract syntax |
|
Converts the inputed KeyLang expression into a list of tokens |
|
Parses the inputted tokens into a KeyLang abstract syntax tree |
|
Parses the inputted tokens as an expression |
|
Parses the inputted tokens as a term |
|
Parses the inputted tokens as a factor |
|
Parses the inputted tokens as a base |
|
Parses the inputted tokens as a constant |
|
Evaluates the KeyLang AST to a value using the rarity |
|
Creates a Controller object from the specified json files |
|
Saves the controller in json format into the specified files |
|
The function used to sort items in a list of items |
|
Returns the random weights of the items for the random function |
|
Parses the inputed KeyLang expression into a KeyLang abstract syntax |
|
Converts the inputed KeyLang expression into a list of tokens |
|
Parses the inputted tokens into a KeyLang abstract syntax tree |
|
Parses the inputted tokens as an expression |
|
Parses the inputted tokens as a term |
|
Parses the inputted tokens as a factor |
|
Parses the inputted tokens as a base |
|
Parses the inputted tokens as a constant |
|
Evaluates the KeyLang AST to a value using the rarity |
|
The key used to sort players in a list of players |
|
Parses the inputed KeyLang expression into a KeyLang abstract syntax |
|
Converts the inputed KeyLang expression into a list of tokens |
|
Parses the inputted tokens into a KeyLang abstract syntax tree |
|
Parses the inputted tokens as an expression |
|
Parses the inputted tokens as a term |
|
Parses the inputted tokens as a factor |
|
Parses the inputted tokens as a base |
|
Parses the inputted tokens as a constant |
|
Evaluates the KeyLang AST to a value using the rarity |
Attributes
The default key function converting rarity to drop rate (1 / rarity |
|
The default key function converting rarity to drop rate (1 / rarity |
- class gachapy.Item(name: str, id: str, rarity: float)[source]
A representation of an item in the gacha game. An item is something that players can collect and pull from banners. Each item must be unique
- namestring
name of the item
- idstring
description of the item Invariant: must be unique
- idint
rarity of the item where the higher the number, the higher the rarity Invariant: must be >= 1
- change_rarity(self, rarity: float) bool
Changes the rarity of the Item
- rarityfloat
new rarity of the item Precondition: rarity must be >= 1
- bool
True if the rarity successfully updated, false otherwise
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Banner(name: str, id: str, items: List[Item], price: float, key: str)[source]
A representation of a banner in the gacha game. A Banner is where players can choose to spend money in order to obtain a random item from the specific item pool presented in the Banner. Each Banner (should) contain different items and have different prices than other banners to distinguish their offerings
- namestr
name of the banner
- idstr
id of the banner Invariant: must be unique
- itemsList[Item]
the list of items in the banner
- pricefloat
the price of pulling from the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- _weightsList[float]
list of drop weights for items Invariant: weights[i] corresponds to items[i]
- remove_item(self, item: Item) bool
Removes the first occurence of an item from the banner
- itemItem
item to remove from the banner
- bool
True if item is found in banner, False if otherwise
- pull(self) Item
Returns a random item out of a banner randomized by weight
- Item
the random item from the pull
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Player(name: str, id: str, items: List[Item], money: float)[source]
A representation of a player in the gacha game. A Player is someone who can use money to purchase pulls from banners, receive items from banners, and collect items. Each Player is unique and may own different items from each other
- namestr
the name of the player
- idstr
the id of the player Invariant: must be unique
- itemsList[Item]
the list of items that the player owns
- moneyfloat
the amount of money that the player owns
- add_item(self, item: Item) None
Adds an item to the player’s inventory
- itemItem
item to add to player inventory
None
- remove_item(self, item: Item) bool
Removes an item from the player’s inventory
- itemItem
item to add to player inventory
- bool
True if item is found in inventory, False if not
- change_money(self, amount: float) bool
Adds or removes money from player
- amountfloat
the amount to add or remove (positive if add, negative if remove) Precondition: if amount removed, leftover amount must be nonnegative
- bool
True if the amount was able to be added or removed from account (does not create a negative money value), False otherwise
- get_net_worth(self) float
Returns the net worth of the player, calculated by the sum of the rarities of all of the items they own
- float
the net worth of the player
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- gachapy._sort_item_key(item: Item) float [source]
The function used to sort items in a list of items
- itemItem
the item to extract the key from
- float
the key of the item
- gachapy._get_random_weights(items, key: str) List[float] [source]
Returns the random weights of the items for the random function
- itemsList[Item]
list of items to find weights of
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- List[float]
the list of weights of the items
- exception gachapy.SyntaxError
Bases:
BaseException
The exception thrown when a syntax error is found
- class gachapy.Ast(left=None, right=None, data=None)
Bases:
object
The base class of the abstract syntax tree
- class gachapy._Expr(left=None, right=None, data=None)
Bases:
Ast
An expression in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Term(left=None, right=None, data=None)
Bases:
Ast
A term in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Factor(left=None, right=None, data=None)
Bases:
Ast
A factor in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Float(left=None, right=None, data=None)
Bases:
Ast
A float literal in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Rar(left=None, right=None, data=None)
Bases:
Ast
A rarity variable in the KeyLang grammar
- __str__(self) str
Return str(self).
- gachapy.parse(s: str) Ast
Parses the inputed KeyLang expression into a KeyLang abstract syntax tree
- sstr
the expression to parse
- Ast
the KeyLang AST representative of the expression
- gachapy._tokenize(s: str) List[str]
Converts the inputed KeyLang expression into a list of tokens
- sstr
the KeyLang expression to tokenize
- List[str]
the list of tokens
- gachapy._parse_tokens(tokens: List[str]) Ast
Parses the inputted tokens into a KeyLang abstract syntax tree
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the tokens
- gachapy._parse_expr(tokens: List[str]) Ast
Parses the inputted tokens as an expression
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the expression
- gachapy._parse_term(tokens: List[str]) Ast
Parses the inputted tokens as a term
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the term
- gachapy._parse_factor(tokens: List[str]) Ast
Parses the inputted tokens as a factor
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the factor
- gachapy._parse_base(tokens: List[str]) Ast
Parses the inputted tokens as a base
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the base
- gachapy._parse_const(tokens: str) Ast
Parses the inputted tokens as a constant
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the constant
- gachapy.interpret(ast: Ast, rarity: float) float
Evaluates the KeyLang AST to a value using the rarity
- astAst
the AST of the expression
- rarityfloat
the value for the rarity
- float
the result of the expression
- gachapy.DEFAULT_KEY = 1 / R[source]
The default key function converting rarity to drop rate (1 / rarity
- gachapy._sort_item_key(item: gachapy.objects.Item) float [source]
The key used to sort items in a list of items
- itemItem
the item to extract the key from
- float
the key of the item
- gachapy._sort_player_key(player: gachapy.objects.Player) float [source]
The key used to sort players in a list of players
- playerPlayer
the player to extract the key from
- float
the key of the player
- exception gachapy.PullError[source]
Bases:
Exception
An exception thrown when pulling from a banner
- class gachapy.Controller(items: Dict[str, gachapy.objects.Item] = {}, banners: Dict[str, gachapy.objects.Banner] = {}, players: Dict[str, gachapy.objects.Player] = {})[source]
A controller used to control an instance of a gacha game. Used to perform all actions needed in a game and manage game information in the form of gachapy objects
- itemsDict[str,Item]
the list of items that are in the item pool for the gacha
- bannersDict[str,Banner]
the list of banners that are available for the gacha
- playersDict[str,Player]
the list of players enrolled in the gacha
- find_item_by_name(self, item_name: str) Optional[gachapy.objects.Item]
Returns the Item object with the given name or None if not found DEPRICATED: consider using find_item_by_id
- item_namestr
the name of the item
- Optional[Item]
the item object with the given name or None if not found
- find_banner_by_name(self, banner_name: str) Optional[gachapy.objects.Banner]
Returns the Banner object with the given name or None if not found DEPRICATED: consider using find_banner_by_id
- banner_namestr
the name of the banner
- Optional[Banner]
the banner object with the given name or None if not found
- find_player_by_name(self, player_name: str) Optional[gachapy.objects.Player]
Returns the Player object with the given name or None if not found DEPRICATED: consider using find_player_by_id
- player_namestr
the name of the player
- Optional[player]
the player object with the given name or None if not found
- find_item_by_id(self, item_id: str) Optional[gachapy.objects.Item]
Returns the Item object with the given id or None if not found
- item_idstr
the id of the item
- Optional[Item]
the item object with the given id or None if not found
- find_banner_by_id(self, banner_id: str) Optional[gachapy.objects.Banner]
Returns the Banner object with the given id or None if not found
- banner_idstr
the id of the banner
- Optional[Banner]
the banner object with the given id or None if not found
- find_player_by_id(self, player_id: str) Optional[gachapy.objects.Player]
Returns the Player object with the given id or None if not found
- player_idstr
the id of the player
- Optional[player]
the player object with the given id or None if not found
- pull(self, player_id: str, banner_id: str) Optional[gachapy.objects.Item]
Pulls and returns an item from the specified banner for the specified player
- player_idstr
the id of the selected player Preconditon: must be a valid id
- banner_idstr
the id of the selected banner Preconditon: must be a valid id
- Optional[Item]
the item if the pull is successful or None if the player does not have enough money
PullError if player or banner are not valid
- add_new_item(self, name: str, id: str, rarity: float) Optional[gachapy.objects.Item]
Adds a new item to the gacha game
- namestr
the name of the new item
- idstr
the id of the new item Precondition: must be unique
- rarityfloat
rarity of the item where the higher the number, the higher the rarity Precondition: must be >= 1
- Optional[Item]
the Item object representing the new item or None if an item with the specified id already exists
- add_new_banner(self, name: str, id: str, items_str: gachapy.objects.List[str], price: float, key: str = DEFAULT_KEY) Optional[gachapy.objects.Banner]
Adds a new banner to the gacha game
- namestr
the name of the new banner
- idstr
the id of the new banner Precondition: must be unique
- items_strList[str]
the list of the ids of the items in the banner
- pricefloat
the price of pulling from the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- Optional[Banner]
the Banner object representing the new banner or None if a banner with the specified id already exists
- add_new_player(self, name: str, id: str, start_money: float, items_str: gachapy.objects.List[str] = []) Optional[gachapy.objects.Player]
Adds a new player to the gacha game
- namestr
the name of the new player
- idstr
the id of the new player
- start_moneyfloat
the amount of money the new player will start with
- items_strList[str]
the list of the ids of the items the player has
- Optional[Player]
the Player object representing the new player or None if a player with the specified id already exists
- remove_item(self, item_id: str) Optional[gachapy.objects.Item]
Removes the specified item from the gacha game WARNING: Will also remove from banners and players if found, costly operation
- item_idstr
the name of the item to remove
- Optional[Item]
the removed item or None if item does not exist
- remove_banner(self, banner_id: str) Optional[gachapy.objects.Banner]
Removes the specified banner from the gacha game
- banner_idstr
the id of the banner to remove
- Optional[Banner]
the removed banner or None if banner does not exist
- remove_player(self, player_id: str) Optional[gachapy.objects.Player]
Removes the specified player from the gacha game
- player_idstr
the id of the player to remove
- Optional[Player]
the removed player or None if player does not exist
- create_random_banner(self, name: str, id: str, num_items: int, price: float = - 1, key: str = DEFAULT_KEY) Optional[gachapy.objects.Banner]
- Creates a random banner with the given name and number of items
The price is automatically determined by the average of the rarities of the items selected if a price is not specified
- namestr
the name of the random banner
- idstr
the id of the random banner Precondition: must be unique
- num_itemsint
the number of items in the banner
- pricefloat
the price of the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- Optional[Banner]
the banner created or None if a banner with the name specified already exists
- remove_all_banners(self) None
Removes all of the banners in the game
None
- top_items(self, num_items: int) gachapy.objects.List[gachapy.objects.Item]
Returns the top specified number of items in the game sorted by rarity
- num_itemsint
the number of items to return Precondition: must be >= 1
- List[Item]
the list of top items
- top_players(self, num_players: int) gachapy.objects.List[gachapy.objects.Player]
Returns the top specified number of players in the game sorted by net worth
- num_playersint
the number of players to return Precondition: must be >= 1
- List[Player]
the list of top players
- __str__(self) str
Return str(self).
- class gachapy.Item(name: str, id: str, rarity: float)[source]
A representation of an item in the gacha game. An item is something that players can collect and pull from banners. Each item must be unique
- namestring
name of the item
- idstring
description of the item Invariant: must be unique
- idint
rarity of the item where the higher the number, the higher the rarity Invariant: must be >= 1
- change_rarity(self, rarity: float) bool
Changes the rarity of the Item
- rarityfloat
new rarity of the item Precondition: rarity must be >= 1
- bool
True if the rarity successfully updated, false otherwise
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Banner(name: str, id: str, items: List[Item], price: float, key: str)[source]
A representation of a banner in the gacha game. A Banner is where players can choose to spend money in order to obtain a random item from the specific item pool presented in the Banner. Each Banner (should) contain different items and have different prices than other banners to distinguish their offerings
- namestr
name of the banner
- idstr
id of the banner Invariant: must be unique
- itemsList[Item]
the list of items in the banner
- pricefloat
the price of pulling from the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- _weightsList[float]
list of drop weights for items Invariant: weights[i] corresponds to items[i]
- remove_item(self, item: Item) bool
Removes the first occurence of an item from the banner
- itemItem
item to remove from the banner
- bool
True if item is found in banner, False if otherwise
- pull(self) Item
Returns a random item out of a banner randomized by weight
- Item
the random item from the pull
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Player(name: str, id: str, items: List[Item], money: float)[source]
A representation of a player in the gacha game. A Player is someone who can use money to purchase pulls from banners, receive items from banners, and collect items. Each Player is unique and may own different items from each other
- namestr
the name of the player
- idstr
the id of the player Invariant: must be unique
- itemsList[Item]
the list of items that the player owns
- moneyfloat
the amount of money that the player owns
- add_item(self, item: Item) None
Adds an item to the player’s inventory
- itemItem
item to add to player inventory
None
- remove_item(self, item: Item) bool
Removes an item from the player’s inventory
- itemItem
item to add to player inventory
- bool
True if item is found in inventory, False if not
- change_money(self, amount: float) bool
Adds or removes money from player
- amountfloat
the amount to add or remove (positive if add, negative if remove) Precondition: if amount removed, leftover amount must be nonnegative
- bool
True if the amount was able to be added or removed from account (does not create a negative money value), False otherwise
- get_net_worth(self) float
Returns the net worth of the player, calculated by the sum of the rarities of all of the items they own
- float
the net worth of the player
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- gachapy._get_random_weights(items, key: str) List[float] [source]
Returns the random weights of the items for the random function
- itemsList[Item]
list of items to find weights of
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- List[float]
the list of weights of the items
- exception gachapy.SyntaxError
Bases:
BaseException
The exception thrown when a syntax error is found
- class gachapy.Ast(left=None, right=None, data=None)
Bases:
object
The base class of the abstract syntax tree
- class gachapy._Expr(left=None, right=None, data=None)
Bases:
Ast
An expression in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Term(left=None, right=None, data=None)
Bases:
Ast
A term in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Factor(left=None, right=None, data=None)
Bases:
Ast
A factor in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Float(left=None, right=None, data=None)
Bases:
Ast
A float literal in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Rar(left=None, right=None, data=None)
Bases:
Ast
A rarity variable in the KeyLang grammar
- __str__(self) str
Return str(self).
- gachapy.parse(s: str) Ast
Parses the inputed KeyLang expression into a KeyLang abstract syntax tree
- sstr
the expression to parse
- Ast
the KeyLang AST representative of the expression
- gachapy._tokenize(s: str) List[str]
Converts the inputed KeyLang expression into a list of tokens
- sstr
the KeyLang expression to tokenize
- List[str]
the list of tokens
- gachapy._parse_tokens(tokens: List[str]) Ast
Parses the inputted tokens into a KeyLang abstract syntax tree
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the tokens
- gachapy._parse_expr(tokens: List[str]) Ast
Parses the inputted tokens as an expression
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the expression
- gachapy._parse_term(tokens: List[str]) Ast
Parses the inputted tokens as a term
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the term
- gachapy._parse_factor(tokens: List[str]) Ast
Parses the inputted tokens as a factor
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the factor
- gachapy._parse_base(tokens: List[str]) Ast
Parses the inputted tokens as a base
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the base
- gachapy._parse_const(tokens: str) Ast
Parses the inputted tokens as a constant
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the constant
- gachapy.interpret(ast: Ast, rarity: float) float
Evaluates the KeyLang AST to a value using the rarity
- astAst
the AST of the expression
- rarityfloat
the value for the rarity
- float
the result of the expression
- gachapy.load_controller(filename: str) gachapy.controller.Controller [source]
Creates a Controller object from the specified json files
- filenamestr
the path of the json file
- Controller
the controller loaded from the specified files
- gachapy.save_controller(controller: gachapy.controller.Controller, filename: str) None [source]
Saves the controller in json format into the specified files
- controllerController
the controller to save into files
- filenamestr
the path to the json file
None
- class gachapy.Item(name: str, id: str, rarity: float)[source]
A representation of an item in the gacha game. An item is something that players can collect and pull from banners. Each item must be unique
- namestring
name of the item
- idstring
description of the item Invariant: must be unique
- idint
rarity of the item where the higher the number, the higher the rarity Invariant: must be >= 1
- change_rarity(self, rarity: float) bool
Changes the rarity of the Item
- rarityfloat
new rarity of the item Precondition: rarity must be >= 1
- bool
True if the rarity successfully updated, false otherwise
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Banner(name: str, id: str, items: List[Item], price: float, key: str)[source]
A representation of a banner in the gacha game. A Banner is where players can choose to spend money in order to obtain a random item from the specific item pool presented in the Banner. Each Banner (should) contain different items and have different prices than other banners to distinguish their offerings
- namestr
name of the banner
- idstr
id of the banner Invariant: must be unique
- itemsList[Item]
the list of items in the banner
- pricefloat
the price of pulling from the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- _weightsList[float]
list of drop weights for items Invariant: weights[i] corresponds to items[i]
- remove_item(self, item: Item) bool
Removes the first occurence of an item from the banner
- itemItem
item to remove from the banner
- bool
True if item is found in banner, False if otherwise
- pull(self) Item
Returns a random item out of a banner randomized by weight
- Item
the random item from the pull
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- class gachapy.Player(name: str, id: str, items: List[Item], money: float)[source]
A representation of a player in the gacha game. A Player is someone who can use money to purchase pulls from banners, receive items from banners, and collect items. Each Player is unique and may own different items from each other
- namestr
the name of the player
- idstr
the id of the player Invariant: must be unique
- itemsList[Item]
the list of items that the player owns
- moneyfloat
the amount of money that the player owns
- add_item(self, item: Item) None
Adds an item to the player’s inventory
- itemItem
item to add to player inventory
None
- remove_item(self, item: Item) bool
Removes an item from the player’s inventory
- itemItem
item to add to player inventory
- bool
True if item is found in inventory, False if not
- change_money(self, amount: float) bool
Adds or removes money from player
- amountfloat
the amount to add or remove (positive if add, negative if remove) Precondition: if amount removed, leftover amount must be nonnegative
- bool
True if the amount was able to be added or removed from account (does not create a negative money value), False otherwise
- get_net_worth(self) float
Returns the net worth of the player, calculated by the sum of the rarities of all of the items they own
- float
the net worth of the player
- __str__(self) str
Return str(self).
- __eq__(self, other) bool
Return self==value.
- gachapy._sort_item_key(item: Item) float [source]
The function used to sort items in a list of items
- itemItem
the item to extract the key from
- float
the key of the item
- gachapy._get_random_weights(items, key: str) List[float] [source]
Returns the random weights of the items for the random function
- itemsList[Item]
list of items to find weights of
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- List[float]
the list of weights of the items
- exception gachapy.SyntaxError
Bases:
BaseException
The exception thrown when a syntax error is found
- class gachapy.Ast(left=None, right=None, data=None)
Bases:
object
The base class of the abstract syntax tree
- class gachapy._Expr(left=None, right=None, data=None)
Bases:
Ast
An expression in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Term(left=None, right=None, data=None)
Bases:
Ast
A term in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Factor(left=None, right=None, data=None)
Bases:
Ast
A factor in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Float(left=None, right=None, data=None)
Bases:
Ast
A float literal in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Rar(left=None, right=None, data=None)
Bases:
Ast
A rarity variable in the KeyLang grammar
- __str__(self) str
Return str(self).
- gachapy.parse(s: str) Ast
Parses the inputed KeyLang expression into a KeyLang abstract syntax tree
- sstr
the expression to parse
- Ast
the KeyLang AST representative of the expression
- gachapy._tokenize(s: str) List[str]
Converts the inputed KeyLang expression into a list of tokens
- sstr
the KeyLang expression to tokenize
- List[str]
the list of tokens
- gachapy._parse_tokens(tokens: List[str]) Ast
Parses the inputted tokens into a KeyLang abstract syntax tree
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the tokens
- gachapy._parse_expr(tokens: List[str]) Ast
Parses the inputted tokens as an expression
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the expression
- gachapy._parse_term(tokens: List[str]) Ast
Parses the inputted tokens as a term
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the term
- gachapy._parse_factor(tokens: List[str]) Ast
Parses the inputted tokens as a factor
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the factor
- gachapy._parse_base(tokens: List[str]) Ast
Parses the inputted tokens as a base
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the base
- gachapy._parse_const(tokens: str) Ast
Parses the inputted tokens as a constant
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the constant
- gachapy.interpret(ast: Ast, rarity: float) float
Evaluates the KeyLang AST to a value using the rarity
- astAst
the AST of the expression
- rarityfloat
the value for the rarity
- float
the result of the expression
- gachapy.DEFAULT_KEY = 1 / R[source]
The default key function converting rarity to drop rate (1 / rarity
- gachapy._sort_player_key(player: gachapy.objects.Player) float [source]
The key used to sort players in a list of players
- playerPlayer
the player to extract the key from
- float
the key of the player
- exception gachapy.PullError[source]
Bases:
Exception
An exception thrown when pulling from a banner
- class gachapy.Controller(items: Dict[str, gachapy.objects.Item] = {}, banners: Dict[str, gachapy.objects.Banner] = {}, players: Dict[str, gachapy.objects.Player] = {})[source]
A controller used to control an instance of a gacha game. Used to perform all actions needed in a game and manage game information in the form of gachapy objects
- itemsDict[str,Item]
the list of items that are in the item pool for the gacha
- bannersDict[str,Banner]
the list of banners that are available for the gacha
- playersDict[str,Player]
the list of players enrolled in the gacha
- find_item_by_name(self, item_name: str) Optional[gachapy.objects.Item]
Returns the Item object with the given name or None if not found DEPRICATED: consider using find_item_by_id
- item_namestr
the name of the item
- Optional[Item]
the item object with the given name or None if not found
- find_banner_by_name(self, banner_name: str) Optional[gachapy.objects.Banner]
Returns the Banner object with the given name or None if not found DEPRICATED: consider using find_banner_by_id
- banner_namestr
the name of the banner
- Optional[Banner]
the banner object with the given name or None if not found
- find_player_by_name(self, player_name: str) Optional[gachapy.objects.Player]
Returns the Player object with the given name or None if not found DEPRICATED: consider using find_player_by_id
- player_namestr
the name of the player
- Optional[player]
the player object with the given name or None if not found
- find_item_by_id(self, item_id: str) Optional[gachapy.objects.Item]
Returns the Item object with the given id or None if not found
- item_idstr
the id of the item
- Optional[Item]
the item object with the given id or None if not found
- find_banner_by_id(self, banner_id: str) Optional[gachapy.objects.Banner]
Returns the Banner object with the given id or None if not found
- banner_idstr
the id of the banner
- Optional[Banner]
the banner object with the given id or None if not found
- find_player_by_id(self, player_id: str) Optional[gachapy.objects.Player]
Returns the Player object with the given id or None if not found
- player_idstr
the id of the player
- Optional[player]
the player object with the given id or None if not found
- pull(self, player_id: str, banner_id: str) Optional[gachapy.objects.Item]
Pulls and returns an item from the specified banner for the specified player
- player_idstr
the id of the selected player Preconditon: must be a valid id
- banner_idstr
the id of the selected banner Preconditon: must be a valid id
- Optional[Item]
the item if the pull is successful or None if the player does not have enough money
PullError if player or banner are not valid
- add_new_item(self, name: str, id: str, rarity: float) Optional[gachapy.objects.Item]
Adds a new item to the gacha game
- namestr
the name of the new item
- idstr
the id of the new item Precondition: must be unique
- rarityfloat
rarity of the item where the higher the number, the higher the rarity Precondition: must be >= 1
- Optional[Item]
the Item object representing the new item or None if an item with the specified id already exists
- add_new_banner(self, name: str, id: str, items_str: gachapy.objects.List[str], price: float, key: str = DEFAULT_KEY) Optional[gachapy.objects.Banner]
Adds a new banner to the gacha game
- namestr
the name of the new banner
- idstr
the id of the new banner Precondition: must be unique
- items_strList[str]
the list of the ids of the items in the banner
- pricefloat
the price of pulling from the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- Optional[Banner]
the Banner object representing the new banner or None if a banner with the specified id already exists
- add_new_player(self, name: str, id: str, start_money: float, items_str: gachapy.objects.List[str] = []) Optional[gachapy.objects.Player]
Adds a new player to the gacha game
- namestr
the name of the new player
- idstr
the id of the new player
- start_moneyfloat
the amount of money the new player will start with
- items_strList[str]
the list of the ids of the items the player has
- Optional[Player]
the Player object representing the new player or None if a player with the specified id already exists
- remove_item(self, item_id: str) Optional[gachapy.objects.Item]
Removes the specified item from the gacha game WARNING: Will also remove from banners and players if found, costly operation
- item_idstr
the name of the item to remove
- Optional[Item]
the removed item or None if item does not exist
- remove_banner(self, banner_id: str) Optional[gachapy.objects.Banner]
Removes the specified banner from the gacha game
- banner_idstr
the id of the banner to remove
- Optional[Banner]
the removed banner or None if banner does not exist
- remove_player(self, player_id: str) Optional[gachapy.objects.Player]
Removes the specified player from the gacha game
- player_idstr
the id of the player to remove
- Optional[Player]
the removed player or None if player does not exist
- create_random_banner(self, name: str, id: str, num_items: int, price: float = - 1, key: str = DEFAULT_KEY) Optional[gachapy.objects.Banner]
- Creates a random banner with the given name and number of items
The price is automatically determined by the average of the rarities of the items selected if a price is not specified
- namestr
the name of the random banner
- idstr
the id of the random banner Precondition: must be unique
- num_itemsint
the number of items in the banner
- pricefloat
the price of the banner
- keystr
function that takes in rarity and returns the drop rate of the item, written in KeyLang
- Optional[Banner]
the banner created or None if a banner with the name specified already exists
- remove_all_banners(self) None
Removes all of the banners in the game
None
- top_items(self, num_items: int) gachapy.objects.List[gachapy.objects.Item]
Returns the top specified number of items in the game sorted by rarity
- num_itemsint
the number of items to return Precondition: must be >= 1
- List[Item]
the list of top items
- top_players(self, num_players: int) gachapy.objects.List[gachapy.objects.Player]
Returns the top specified number of players in the game sorted by net worth
- num_playersint
the number of players to return Precondition: must be >= 1
- List[Player]
the list of top players
- __str__(self) str
Return str(self).
- exception gachapy.SyntaxError
Bases:
BaseException
The exception thrown when a syntax error is found
- class gachapy.Ast(left=None, right=None, data=None)
Bases:
object
The base class of the abstract syntax tree
- class gachapy._Expr(left=None, right=None, data=None)
Bases:
Ast
An expression in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Term(left=None, right=None, data=None)
Bases:
Ast
A term in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Factor(left=None, right=None, data=None)
Bases:
Ast
A factor in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Float(left=None, right=None, data=None)
Bases:
Ast
A float literal in the KeyLang grammar
- __str__(self) str
Return str(self).
- class gachapy._Rar(left=None, right=None, data=None)
Bases:
Ast
A rarity variable in the KeyLang grammar
- __str__(self) str
Return str(self).
- gachapy.parse(s: str) Ast
Parses the inputed KeyLang expression into a KeyLang abstract syntax tree
- sstr
the expression to parse
- Ast
the KeyLang AST representative of the expression
- gachapy._tokenize(s: str) List[str]
Converts the inputed KeyLang expression into a list of tokens
- sstr
the KeyLang expression to tokenize
- List[str]
the list of tokens
- gachapy._parse_tokens(tokens: List[str]) Ast
Parses the inputted tokens into a KeyLang abstract syntax tree
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the tokens
- gachapy._parse_expr(tokens: List[str]) Ast
Parses the inputted tokens as an expression
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the expression
- gachapy._parse_term(tokens: List[str]) Ast
Parses the inputted tokens as a term
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the term
- gachapy._parse_factor(tokens: List[str]) Ast
Parses the inputted tokens as a factor
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the factor
- gachapy._parse_base(tokens: List[str]) Ast
Parses the inputted tokens as a base
- tokensList[str]
the list of tokens to parse
- Ast
the abstract syntax tree representative of the base