Splinterlands Api/Bot Learning: 3. How to write a program to realize automatic card (rent multi cards)

image.png
If you don't know how to rent a card through API, please go to my last blog
https://peakd.com/hive-13323/@wohefengyiyang/splinterlands-apibot-learning-2-how-to-write-a-program-to-realize-automatic-card-rental-by-using-python

In my last blog, I explained how to rent a card. So how to rent multi cards?

1. The easiest way is to rent a card many times

cards_sorted_one = get_rent_cards_xp(335, 3, False, 1, 3)
for card_sorted in cards_sorted:
    if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
        rent_card(username, [card_sorted.market_id], 1, 'DEC')
        break

cards_sorted_two = get_rent_cards_xp(339, 3, False, 1, 3)
for card_sorted in cards_sorted_two:
    if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
        rent_card(username, [card_sorted.market_id], 1, 'DEC')
        break

cards_sorted_three = get_rent_cards_xp(333, 3, False, 1, 3)
for card_sorted in cards_sorted_three:
    if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
        rent_card(username, [card_sorted.market_id], 1, 'DEC')
        break

2. Using the for loop

HaHaHa, the way of 1 seems too stupid, so use the loop

# Define a Class an array to save your rental card settings
class RentalSetting:
    card_id = ''
    edition = 3
    gold = False
    xp = 1
    price = 0
    
    def __init__(self, c, e, g, x, p):
        self.card_id = c
        self.edition = e
        self.gold = g
        self.xp = x
        self.price = p

rental_setting = [RentalSetting(333,3,False,1,1),RentalSetting(335,3,True,1,1),RentalSetting(339,3,False,1,1)]

for r_setting in rental_setting:
    cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp, r_setting.price)
    for card_sorted in cards_sorted:
        if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
            rent_card(username, [card_sorted.market_id], 1, 'DEC')
            break

3. Rent multiple identical cards

What should we do iff we want to rent multiple identical cards?

# Define a Class an array to save your rental card settings and amounts
class RentalSetting:
    card_id = ''
    edition = 3
    gold = False
    xp = 1
    price = 0
    amount = 1
    
    def __init__(self, c, e, g, x, p, a):
        self.card_id = c
        self.edition = e
        self.gold = g
        self.xp = x
        self.price = p
        self.amount = a


rental_setting = [RentalSetting(333,3,False,1,1,3),RentalSetting(335,3,True,1,1,4),RentalSetting(339,3,False,1,1,5)]
for r_setting in rental_setting:
    cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp, r_setting.price)
    cards_amount = []
    amount = r_setting.amount
    index = 0
    for card_sorted in cards_sorted:
        index = index + 1
        if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
            cards_amount.append(card_sorted.market_id)
            amount = amount - 1
            if amount == 0 or index ==len(cards_sorted):
                if len(cards_amount) > 0:
                    rent_card(username, cards_amount, 1, 'DEC')
                    break

4. All codes: you can copy and test it

import requests
from beem import Hive

API2 = "https://api2.splinterlands.com"

# 👇👇👇👇👇👇👇👇👇👇👇 your username
username = 'username'

# 👇👇👇👇👇👇👇👇👇👇👇 your posting keyword and active keyword
passwords = ['aaa', 'bbb']

hive = Hive(keys=passwords)


class Card:
    market_id = ''
    uid = ''
    detail_id = 0
    price = 0

    def __init__(self, m, u, d, p):
        self.market_id = m
        self.uid = u
        self.detail_id = d
        self.price = p

    def __lt__(self, other):
        return self.price < other.price


# Define a Class an array to save your rental card settings and amounts
class RentalSetting:
    card_id = ''
    edition = 3
    gold = False
    xp = 1
    price = 0
    amount = 1

    def __init__(self, c, e, g, x, p, a):
        self.card_id = c
        self.edition = e
        self.gold = g
        self.xp = x
        self.price = p
        self.amount = a


def get_rent_cards_xp(card_id: int, edition: int, gold: bool, xp: int, price: float) -> list:
    url = API2 + "/market/for_rent_by_card"
    request: dict = {"card_detail_id": card_id,
                     "gold": gold,
                     "edition": edition}
    rent_cards = requests.get(url, params=request).json()
    cards = [card for card in rent_cards if card.get("xp") >= xp and float(card.get("buy_price")) <= price]
    v_cards = []
    for c in cards:
        v_cards.append(Card(c.get('market_id'), c.get('uid'), c.get('card_detail_id'), float(c.get('buy_price'))))
    v_cards.sort()
    return v_cards


def verify(market_id: str, uid: str, card_detail_id: int) -> bool:
    url = API2 + "/market/validateListing"
    request: dict = {"card_detail_id": card_detail_id,
                     "uid": uid,
                     "market_id": market_id}
    return requests.get(url, params=request).json().get('isValid')


def rent_card(player: str, card_ids: list[str], days: int, currency: str):
    data: dict = {"items": card_ids,
                  "currency": currency,
                  "days": days,
                  "app": "splinterlands/0.7.139"}
    hive.custom_json("sm_market_rent", data, required_auths=[player], required_posting_auths=[])
    print(player, 'rent cards success')


rental_setting = [RentalSetting(333, 3, False, 1, 1, 3), RentalSetting(335, 3, True, 1, 1, 4),
                  RentalSetting(339, 3, False, 1, 1, 5)]
for r_setting in rental_setting:
    cards_sorted = get_rent_cards_xp(r_setting.card_id, r_setting.edition, r_setting.gold, r_setting.xp,
                                     r_setting.price)
    cards_amount = []
    amount = r_setting.amount
    index = 0
    for card_sorted in cards_sorted:
        index = index + 1
        if verify(card_sorted.market_id, card_sorted.uid, card_sorted.detail_id):
            cards_amount.append(card_sorted.market_id)
            amount = amount - 1
            if amount == 0 or index == len(cards_sorted):
                if len(cards_amount) > 0:
                    rent_card(username, cards_amount, 1, 'DEC')
                    print(username, 'rent cards', cards_amount)
                    break

Next period notice

Setting a power and BCX(power/dec), and automatic card rental

Share today's luck

image.png

If you have any questions, please leave a message in the comment area
Sort:  

Congratulations @wohefengyiyang! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 50 upvotes.
Your next target is to reach 100 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!

I got the error log:" Error: HTTPSConnectionPool(host='api.hive.blog', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)')))
Lost connection or internal error on node: https://api.hive.blog (1/100) "
what happened ? And my vpn is from Singapore.

For this problem, there's somethong wrong with your vpn that can't sent a request to hive. you can set codes 'hive = Hive(keys=passwords)' to hive = Hive(keys=passwords, node='https://api.deathwing.me')

In most cases, VPN is not required to use this node

Perfect! This problem has been bothering me for two days,thanks a lot!
but there's a new problem,after changed the node and shutdown VPN, i can't play this game, do you have any suggestions?pls

you can use the card rental bot whether you have VPN or not, so you can restart you vpn that the game and bot will run

Hi,
Could I have your telegram to discuss more?
As I see your code, its just scan 1 time to find the card with settings (we set above). So, could we add more code to scan several times to find suitable card that meet our settings?
Thanks

On this issue, I will gradually update it in my blog in the future. And I‘m sorry that I don't have telegram.
I will update the code about splinterlands in my blog bit by bit

Well this would be actually a good contribution to all of us, but there's a problem with this cause it actually rent card 1x at the moment not 3x at the moment. with that problem we may encounter decreasing our RC's until we cant no longer rent a card, I suggest to add a line that could maybe increase also our RC's like transfering dec to someones account so that it could automate, you can check my modified version of this code guys. also thanks to @wohefengyiyang

Well this would be actually a good contribution to all of us, but there's a problem with this cause it actually rent card 1x at the moment not 3x at the moment. with that problem we may encounter decreasing our RC's until we cant no longer rent a card, I suggest to add a line that could maybe increase also our RC's like transfering dec to someones account so that it could automate, you can check my modified version of this code guys https://peakd.com/autorent/@vin-aledo2k/how-to-rent-multiple-cards-in-python . also thanks to @wohefengyiyang

I spend so much time reading, which is how I found the post on https://www.tmcnet.com/topics/articles/2022/02/24/451626-best-essay-writing-services-2021-everything-should-know.htm I learned a lot of new things, and I'm sure some of them will help me in the future.

Posted using Splintertalk