Skip to content

Sample Code Fragments

Brandon Nguyen edited this page Nov 13, 2020 · 7 revisions

API Docs Project: Full list of APIs

Loading a database

  • How to create a character object with a genric swordie database object.
    from swordie_db.database import SwordieDB
    
    # Creating a swordie database object
    swordie = SwordieDB() # When provided with empty parameters, it will connect to localhost
    
    char = swordie.get_char_by_name("brandon") # Creating a character object from database
  • Creating a database object with specific login information.
    from swordie_db.database import SwordieDB
    # Default Values
    # host = "localhost"
    # user = "root"
    # password = ""
    # schema = "swordie"
    # port = 3306 (int)
    
    # Parameters for creating a Swordie database object
    swordie = SwordieDB(host="53.153.23.124", password="password", user="root", schema="swordie", port=3306)
    
    # All Parameters are optional
    swordie_with_schema = SwordieDB(schema="spirit") # This is also a legal/functional instantiation.

Fetching character information from database

  • Getting information from a character (getters).
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    char = swordie.get_char_by_name("brandon") # Creating character object
    
    name = char.name # getter for name
    meso = char.money # getter for mesos
    job = char.get_job_name() # this getter gives us the actual job name
    job_id = char.job # This one gives us job id
    level = char.level # getter for levels
    
    # getting a value from a column that isn't a class property
    honor_exp = char.get_stat_by_column("honorexp") 
    
    print("Character name:", name)
    print("Character mesos:", meso)
    print("Character job:", job)
    print("Character level:", level)
    print("Character honor exp:", honor_exp)

Writing character information to database

  • Writing new data to database from a character (setters).
    • Note: All setter methods in the character class automatically saves to database after setting.
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    char = swordie.get_char_by_name("brandon") # Creating character object
    
    char.money = 999999 # Sets money to 999,999 mesos in the database
    # character now has 999,999 mesos
    
    char.add_mesos(1) 
    # Adds to the current meso count, I.E 999999 + 1 and saves in the database
    # character now has 1,000,000
    
    char.fame = 2000 # Sets fame to 2000 and saves to database
    
    char.add_fame(1) # Adds 1 fame to the existing count and saves to database

Advanced character information manipulation

  • Setting stats that may not exist as a property in character class.

    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    char = swordie.get_char_by_name("brandon")
    
    char.set_stat_by_column("honorexp", 521) 
    # sets the column "honorexp" in SQL to value of 521 and save
  • Setting new stats without the use of a character object

    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    swordie.set_char_stat("brandon", "level", 250) 
    # setting the character brandon to be level 250
    # set_char_stat(name, column, value)

User Manipulation

  • Obtaining a user object
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    # Recommended
    char = swordie.get_char_by_name("brandon")
    user = char.user
    
    # Getting a user object from database
    user = swordie.get_user_by_username("admin")
  • Fetching User information
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    char = swordie.get_char_by_name("brandon")
    user = char.user
    
    ban_reason = user.ban_reason
    account_type = user.account_type
    vote_points = user.vote_points
    is_admin = user.is_admin()
    # Refer to API documentation for full list of getters
  • Writing to User Database with setters
    • Note: All setters will automatically make changes to database after being set.
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    char = swordie.get_char_by_name("brandon")
    user = char.user
    
    user.change_password("admin") # Changes password in database
    user.ban_reason = "For hacking" # Changes the ban reason in database
    user.vote_points = 632 # Sets the vote point to 632
    user.add_vote_points(1) # Adds 1 vote point to existing pool
  • Getting information from user table when it may not exist as an attribute in the class
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    char = swordie.get_char_by_name("brandon")
    user = char.user
    
    age = char.user.get_stat_by_column("age") # This is also legal
    gender = user.get_stat_by_column("gender")

Inventory Information

  • Obtaining an Inventory Object, and various inventory types.
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    char = swordie.get_char_by_name("brandon")
    
    # Getting an inventory object
    inventory = char.inventory
    
    # Getting specific inventory tabs
    equip_inventory = inventory.equip_inv
    consume_inventory = inventory.consume_inv
    etc_inventory = inventory.etc_inv
    setup_inventory = inventory.install_inv
    equipped_invnentory = inventory.equipped_inv
    cash_inventory = inventory.cash_inv
  • Getting specific item information from inventory.
    from swordie_db.database import SwordieDB
    
    # Boilerplate code (see above)
    swordie = SwordieDB()
    
    # Getting a user object from character
    char = swordie.get_char_by_name("brandon")
    
    # Getting an inventory object
    inventory = char.inventory
    
    # Getting consume inventory
    consume_inv = inventory.consume_inv
    item_id = consume_inv[1]["itemid"] # consume_inv[1] indicates we want to get the item in bag index 1
    quantity = consume_inv[1]["quantity"] # consume_inv[1] indicates we want to get the item in that bag index
    # Read API Docs to get more information on other item stats you can get!
    
    # Using it in loops
    for bag_index in consume_inv:
      itemid = consume_inv[bag_index]["itemid"]
      print(itemid)
    
    # Using it for conditionals
    if inventory.is_equipping(5100002): # checking if the character is current equiping item id: 5100002 (pseudo id num)
      print("Do something")