spltools functions and classes for battle data

in voilk •  4 months ago

    Image created with ChatGPT

    In this update, I have added some basic battle data processing to spltools. This is likely just the first update among several that will be concerned with battle data. A short summary of the update is:

    • A function for downloading and saving battle data
    • A class for storing and processing battle data
    • A class for storing info about a battle lineup (Team)
    • A function for generating the url for a scaled Splinterlands card image.
    • A method in the Battle class for generating a markdown summary of the battle

    separator.png

    Using the battle class is exceedingly simple for now. Simply initialize the Battle class with
    the battle id, and optionally a directory to save the data if you don't want to have to download it next time you want to use that battle for something:
    See examples/battle_markdown_summary.py

    We can then call the markdown_summary method. This prints text to the terminal that can be copied directly into a hive post.

    Copied text produces the following:
    ag26 vs. kalkulus
    TypeRanked
    Formatmodern
    Mana13
    ElementsBlue, Green, White, Black
    Winnerkalkulus
    ag26's team:

    kalkulus's team:

    separator.png

    Overview of added functionality:

    Downloading battle data

    Function for fetching battle data

    There is not that much to say about the battle data download function. It takes a battle queue id as input (you can find that in the url when viewing a Splinterlands battle, or from several api endpoints), downloads the data, and optionally saves it for later use.

    Battle class

    Class for storing and processing battle data

    The battle class takes the downloaded battle data and processes it into a more useful structure. For now, only a limited portion of the data is kept. This will likely be expanded later on when I use the other fields for something. You might notice that the Battle class instantiates two Team classes. These contain information about the two teams played in this battle.

    Team class

    Team class overview

    The team class simply stores information about which cards is in the team, and has two methods for returning readable summaries of that. get_names returns the summoner name and a list of the monster names, and hive images returns a string with urls for the summoner and card images.

    Markdown battle summary

    Function for summarizing the battle in markdown text

    In the markdown_summary method, we create a markdown table containing the match type, the format, mana cap, active elements and the winner. Also, there is an option (active by default) to also return hive image urls for the cards used in the battle. This option makes use of the hive_image function:

    Hive image

    Getting an image url for a Splinterlands card
    The hive_image function constructs an url that can be copied into a hive post to display a Splinterlands card at a given level, with an image of specified width and height. Note that the image will keep its aspect ratio, so only the most limiting of these two dimensions will be used. For now, the function will not give alpha borders since alpha and beta cards have the save id, and I default it to the beta border. I plan to enable alpha borders eventually.

    separator.png

    Final words

    That concludes the post for this update. Thank you for your attention. If you are interested in more posts about the spltools python package, you can find the previous ones here:

    spltools update posts:

    6. spltools setup guide
    5. Best brawler code
    4. spltools update: Classes for Brawl data
    3. spltools update: Reward chest classes
    2. spltools update: Checking which set a card belongs to
    1. Initial spltools commit


    If you have not yet joined Splinterlands please click the referral link below to get started.



    Join Splinterlands


    Best wishes
    @Kalkulus

    separator.png

      Authors get paid when people like you upvote their post.
      If you enjoyed what you read here, create your account today and start earning FREE VOILK!