-
Notifications
You must be signed in to change notification settings - Fork 2
Sample Code Fragments
Amos Chua edited this page Jan 15, 2021
·
6 revisions
API Docs Project: Full list of APIs
Acknowledgements:
Adapted from SwordieDB Wiki
-
Option 1. Creating a generic database object with default values
from lazuli.database import Lazuli # Create an Azure database object (i.e. Lazuli) using the Lazuli class constructor azure = Lazuli() # When provided with empty parameters, it will connect to localhost # Default Values: # host = "localhost" # user = "root" # password = "" # schema = "kms_316" # port = 3306 # defaults to int, but it's intelligent enough to take Strings as well
-
Option 2. Creating a customised database object with specific login information
from lazuli.database import Lazuli # Parameters for creating an Azure database object, using the Lazuli class constructor: azure = Lazuli(host="53.153.23.124", password="foo", user="bar", schema="spirit", port=3306) # All parameters are optional - partial provision of login information is allowed: azure_with_schema = Lazuli(schema="spirit") # This is also a legal/functional instantiation.
- Getting generic information about the server status
from lazuli.database import Lazuli azure = Lazuli() number_of_players_online = azure.get_online_count() # gives number (int) of accounts currently connected to the server list_of_players_online = azure.get_online_players() # gives list of usernames of connected accounts
-
Create a Character object
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") # Creating a character object from database
-
Getting Character attributes
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") name = char.name # getter for name meso = char.meso # getter for mesos level = char.level # getter for levels job_id = char.job # getter for Job ID job = char.get_job_name() # fetches Job Name by matching Job ID in hashmap # Character::get_job_name() makes use of the Character.job getter - see API Docs for more details # ADVANCED: # Getting character data not modeled by Lazuli's Character class # Use the Character::get_stat_by_column(column_name) method to read from particular columns in the 'characters' table fatigue = char.get_stat_by_column("fatigue") # Print to console for testing purposes: print("Character name:", name) print("Character mesos:", meso) print("Character job:", job) print("Character level:", level) print("Character fatigue:", fatigue)
-
Modifying Character attributes (Basic)
- Note: All setter methods in the character class automatically saves to database after setting.
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") char.meso = 999999 # Sets meso count to 999,999 in the database # The character with IGN "KOOKIIE" now has 999,999 mesos in inventory # Lazuli setters have checks that raise errors with bad inputs (e.g. setting Meso past Meso cap) # You may use a try-catch if you'd like to feed this to another API. # Take for instance, an example with discord.py: try: char.meso = 9999999999999999 # 9 Quadrillion > 10 Billion except Exception as e: print("Error encountered") if type(e) is ValueError: channel = bot.get_channel(config['CHANNEL_ID']) # just an example of discord.py await channel.send({e.args[0]}) # This will send the following message via Discord: "You should not try to set meso to more than 10b!" char.add_mesos(1) # Adds to the current meso count (i.e. 999999 + 1), and saves the result in the database # The character with IGN "KOOKIIE" now has 1,000,000 mesos in inventory # Adders in Lazuli make use of setters, so the same value-checking occurs for bad inputs char.fame = 2000 # Sets fame to 2000 and saves to database char.add_fame(1) # Adds 1 fame to the existing count, and saves the result in the database
-
Advanced character information manipulation
-
Modifying character data not modeled by Lazuli's Character class
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") char.set_stat_by_column("fatigue", 0) # sets the column "fatigue" of the 'characters' table (in the DB) to a value of 0 # Use the Character::set_stat_by_column(column_name, value) method to modify particular columns in the 'characters' table
-
Setting new stats without the use of a character object
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() azure.set_char_stat("KOOKIIE", "level", 250) # The character with IGN "KOOKIIE" is now level 250 # Use the Character::set_char_stat(name, column, value) method to set the specified column to the specified value
-
-
Create an Account object
- Option 1. Getting an Account object from a Character object
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") user = char.account
- Option 2. Getting an Account object from a database (Lazuli) object
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() user = azure.get_user_by_username("foobar")
- Option 1. Getting an Account object from a Character object
-
Getting Account attributes
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") user = char.account ban_reason = user.ban_reason vote_points = user.vp # Refer to API docs for full list of getters
-
Modifying Account attributes (Basic)
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") user = char.account user.ban_reason = "For hacking" # Changes the ban reason in database user.vp = 632 # Sets the vote point to 632 user.add_vp(1) # Adds 1 vote point to existing pool
-
Advanced Account information manipulation
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() char = azure.get_char_by_name("KOOKIIE") user = char.account gender = user.get_stat_by_column("gender") creation_date = char.user.get_stat_by_column("createdat") # This is also legal user.set_stat_by_column("gender", 0) # sets the column "gender" of the 'characters' table (in the DB) to a value of 0 # Use the Account::set_stat_by_column(column_name, value) method to modify particular columns in the 'characters' table
-
Create an Inventory object
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() # Getting an Inventory object by IGN inventory = azure.get_inv_by_name("KOOKIIE") # Getting specific inventory tab contents equip_inventory = inventory.equip_inv use_inventory = inventory.consume_inv etc_inventory = inventory.etc_inv setup_inventory = inventory.install_inv cash_inventory = inventory.cash_inv equipped_invnentory = inventory.equipped_inv # Equipment window
-
Query inventory contents
from lazuli.database import Lazuli # Boilerplate code (see above) azure = Lazuli() # Getting an Inventory object by IGN inventory = azure.get_inv_by_name("KOOKIIE") # Getting USE inventory tab contents use_inventory = inventory.consume_inv # Querying item attributes via inventory slot index (bag index in Swordie) # use_inventory[1] gets the item in USE inventory, index 1 item_id = use_inventory[1]["itemid"] # Inventory tabs are Dicts of Dicts quantity = use_inventory[1]["quantity"] # Refer to API docs for full list of item attributes modeled in Lazuli # Able to query whether an item (by ID) is in a specified tab: if inventory.is_equipping(5100002): # checks if the character is current equipping an item of ID: 5100002 print("Do something") # Refer to API docs for full list of inventory methods
A TEAM SPIRIT Project • Lazuli PyPI Page • SpiritMS • SpiritSuite