gachapy.controller

The controller used for all high level management of the gacha game. Instances of this class should be created for each instance of a gacha game you would like to run. These instances can then be used to access and manage the Item, Banner, and Player objects in the game directly. Once accessed, the objects can then be modified directly using class methods (see objects.py for more info).

This object operates as the Controller of a gachapy game

Author: Jacob Kerr, 2021

Module Contents

Classes

Controller

A controller used to control an instance of a gacha game. Used to

Functions

_sort_item_key(→ float)

The function used to sort items in a list of items

_sort_player_key(→ float)

The key used to sort players in a list of players

Attributes

DEFAULT_KEY

The default key function converting rarity to drop rate (1 / rarity)

gachapy.controller.DEFAULT_KEY = '1 / R'[source]

The default key function converting rarity to drop rate (1 / rarity)

gachapy.controller._sort_item_key(item: gachapy.objects.Item) float[source]

The key used to sort items in a list of items

Parameters

itemItem

the item to extract the key from

Returns

float

the key of the item

gachapy.controller._sort_player_key(player: gachapy.objects.Player) float[source]

The key used to sort players in a list of players

Parameters

playerPlayer

the player to extract the key from

Returns

float

the key of the player

exception gachapy.controller.PullError[source]

Bases: Exception

An exception thrown when pulling from a banner

class gachapy.controller.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

Fields

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

items: Dict[str, gachapy.objects.Item][source]

The list of items that are in the item pool for the gacha

banners: Dict[str, gachapy.objects.Banner][source]

The list of banners that are available for the gacha

players: Dict[str, gachapy.objects.Player][source]

The list of players enrolled in the gacha

find_item_by_name(item_name: str) gachapy.objects.Item | None[source]

Returns the Item object with the given name or None if not found DEPRICATED: consider using find_item_by_id

Parameters
item_namestr

the name of the item

Returns
Optional[Item]

the item object with the given name or None if not found

find_banner_by_name(banner_name: str) gachapy.objects.Banner | None[source]

Returns the Banner object with the given name or None if not found DEPRICATED: consider using find_banner_by_id

Parameters
banner_namestr

the name of the banner

Returns
Optional[Banner]

the banner object with the given name or None if not found

find_player_by_name(player_name: str) gachapy.objects.Player | None[source]

Returns the Player object with the given name or None if not found DEPRICATED: consider using find_player_by_id

Parameters
player_namestr

the name of the player

Returns
Optional[player]

the player object with the given name or None if not found

find_item_by_id(item_id: str) gachapy.objects.Item | None[source]

Returns the Item object with the given id or None if not found

Parameters
item_idstr

the id of the item

Returns
Optional[Item]

the item object with the given id or None if not found

find_banner_by_id(banner_id: str) gachapy.objects.Banner | None[source]

Returns the Banner object with the given id or None if not found

Parameters
banner_idstr

the id of the banner

Returns
Optional[Banner]

the banner object with the given id or None if not found

find_player_by_id(player_id: str) gachapy.objects.Player | None[source]

Returns the Player object with the given id or None if not found

Parameters
player_idstr

the id of the player

Returns
Optional[player]

the player object with the given id or None if not found

pull(player_id: str, banner_id: str) gachapy.objects.Item | None[source]

Pulls and returns an item from the specified banner for the specified player

Parameters
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

Returns
Optional[Item]

the item if the pull is successful or None if the player does not have enough money

Raises

PullError if player or banner are not valid

add_new_item(name: str, id: str, rarity: float) gachapy.objects.Item | None[source]

Adds a new item to the gacha game

Parameters
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

Returns
Optional[Item]

the Item object representing the new item or None if an item with the specified id already exists

add_new_banner(name: str, id: str, items_str: gachapy.objects.List[str], price: float, key: str = DEFAULT_KEY) gachapy.objects.Banner | None[source]

Adds a new banner to the gacha game

Parameters
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

Returns
Optional[Banner]

the Banner object representing the new banner or None if a banner with the specified id already exists

add_new_player(name: str, id: str, start_money: float, items_str: gachapy.objects.List[str] = []) gachapy.objects.Player | None[source]

Adds a new player to the gacha game

Parameters
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

Returns
Optional[Player]

the Player object representing the new player or None if a player with the specified id already exists

remove_item(item_id: str) gachapy.objects.Item | None[source]

Removes the specified item from the gacha game WARNING: Will also remove from banners and players if found, costly operation

Parameters
item_idstr

the name of the item to remove

Returns
Optional[Item]

the removed item or None if item does not exist

remove_banner(banner_id: str) gachapy.objects.Banner | None[source]

Removes the specified banner from the gacha game

Parameters
banner_idstr

the id of the banner to remove

Returns
Optional[Banner]

the removed banner or None if banner does not exist

remove_player(player_id: str) gachapy.objects.Player | None[source]

Removes the specified player from the gacha game

Parameters
player_idstr

the id of the player to remove

Returns
Optional[Player]

the removed player or None if player does not exist

create_random_banner(name: str, id: str, num_items: int, price: float = -1, key: str = DEFAULT_KEY) gachapy.objects.Banner | None[source]
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

Parameters
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

Returns
Optional[Banner]

the banner created or None if a banner with the name specified already exists

remove_all_banners() None[source]

Removes all of the banners in the game

Returns

None

top_items(num_items: int) gachapy.objects.List[gachapy.objects.Item][source]

Returns the top specified number of items in the game sorted by rarity

Parameters
num_itemsint

the number of items to return Precondition: must be >= 1

Returns
List[Item]

the list of top items

top_players(num_players: int) gachapy.objects.List[gachapy.objects.Player][source]

Returns the top specified number of players in the game sorted by net worth

Parameters
num_playersint

the number of players to return Precondition: must be >= 1

Returns
List[Player]

the list of top players

__str__() str[source]

Return str(self).