cs50x avatar

[Python] List the lowest rental prices for each CP

cs50x

Published: 13 Nov 2021 › Updated: 13 Nov 2021[Python] List the lowest rental prices for each CP

[Python] List the lowest rental prices for each CP

Code

import enum
import requests
import pandas as pd

CP_MAP = {
    "regular": {
        "alpha": {"legendary": 3000, "epic": 600, "rare": 120, "common": 30},
        "beta": {"legendary": 1500, "epic": 300, "rare": 60, "common": 15},
        "dice": {"legendary": 1000, "epic": 200, "rare": 40, "common": 10},
        "promo": {"legendary": 3000, "epic": 600, "rare": 120, "common": 30},
        "untamed": {"legendary": 1000, "epic": 200, "rare": 40, "common": 10},
        "chaos": {"legendary": 500, "epic": 100, "rare": 20, "common": 5}
    },
    "gold": {
        "alpha": {"legendary": 150000, "epic": 30000, "rare": 6000, "common": 1500},
        "beta": {"legendary": 75000, "epic": 15000, "rare": 3000, "common": 750},
        "dice": {"legendary": 50000, "epic": 10000, "rare": 2000, "common": 500},
        "promo": {"legendary": 150000, "epic": 30000, "rare": 6000, "common": 1500},
        "untamed": {"legendary": 50000, "epic": 10000, "rare": 2000, "common": 500},
        "chaos": {"legendary": 12500, "epic": 2500, "rare": 500, "common": 125}
    }
}

class Rarity(enum.IntEnum):
    Common = 1
    Rare = 2
    Epic = 3
    Legendary = 4

class Edition(enum.IntEnum):
    Alpha = 0
    Beta = 1
    Promo = 2
    Reward = 3
    Untamed = 4
    Dice = 5

    def to_slug(self, card_id):
        slug = self.name.lower()
        if slug == "reward":
            if card_id <= 223:
                return "beta"
            elif 224 <= card_id and card_id <= 298:
                return "dice"
            elif 299 <= card_id and card_id <= 330:
                return "gradius"
            elif 331 <= card_id and card_id <= 351:
                return "chaos"
        else:
            return slug

def get_data():
    market = requests.get(f"https://api2.splinterlands.com/market/for_rent_grouped").json()
    card_details = requests.get("https://api.splinterlands.io/cards/get_details").json()
    result = {}
    for card in market:
        gold_slug = "gold" if card["gold"] else "regular"
        edition_slug = Edition(card["edition"]).to_slug(card["card_detail_id"])
        rarity_id = [card_detail["rarity"] for card_detail in card_details if card_detail["id"] == card["card_detail_id"]][0]
        rarity_slug = Rarity(rarity_id).name.lower()
        card["cp"] = CP_MAP[gold_slug][edition_slug][rarity_slug]
        card["cp_per_dec"] = round(float(card["cp"]) / float(card["low_price"]), 3)
        if card["cp"] in result and float(card["low_price"]) < float(result[card["cp"]]["low_price"]):
            result[card["cp"]] = card
        else:
            result[card["cp"]] = card
    return result

data = get_data()
df = pd.DataFrame(data.values())
df = df.set_index("cp")
df = df.sort_index()
df[15:][["card_detail_id", "gold", "edition", "low_price", "cp_per_dec"]]

Result

Screenshot

You can run the code in Google Colab.

https://colab.research.google.com/drive/16tsx765O_Zl9txHNlA8Qe1bY5QMnUIzg?usp=sharing

Leave [Python] List the lowest rental prices for each CP to:

Written by

Read more #hive-13323 posts


Best Posts From cs50x

We have not curated any of cs50x's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From cs50x