create account

Auto Unblock for Rabona by emrebeyler

View this thread on: hive.blogpeakd.comecency.com
· @emrebeyler · (edited)
$14.79
Auto Unblock for Rabona
I've extended my auto-heal script to handle red-cards. This script now checks if there are blocked players in your team, and automatically unblock them via RBN.

config.py
***

```
ACCOUNTS = [
    {
        "username": "your_user",
        "posting_key": "<pkey>"
    },
    {
        "username": "other_user",
        "posting_key": "<pkey>",
    }
]

```
***
auto-heal-unlock.py
```
import json
import logging

from lighthive.client import Client
from lighthive.datastructures import Operation
from rabona_python import RabonaClient

from config import ACCOUNTS

logger = logging.getLogger("Auto heal")
logger.setLevel(logging.DEBUG)
logFormatter = logging.Formatter(
    '%(asctime)s %(name)s %(levelname)s: %(message)s')
consoleHandler = logging.StreamHandler()
fileHandler = logging.FileHandler("autoheal.log")
consoleHandler.setFormatter(logFormatter)
fileHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
logger.addHandler(fileHandler)

r = RabonaClient()


def create_custom_json_op(username, pid, heal=True):
    if heal:
        custom_json_type = "heal_for_RBN"
    else:
        custom_json_type = "unblock_for_RBN"
    train_json = json.dumps(
        {
            "username": username,
            "type": custom_json_type,
            "command": {"tr_var1": pid}
        }
    )

    train_op = Operation('custom_json', {
        'required_auths': [],
        'required_posting_auths': [username, ],
        'id': 'rabona',
        'json': train_json,
    })
    return train_op


def auto_heal_and_unblock(team, pk):
    rbn_balance = r.userinfo(user=team).get("currency")
    logger.info("Checking \"%s\" for injured and blocked players.", team)
    players = r.team(user=team).get("players", [])
    # players[0]["games_blocked"] = 1

    injured_players = [p for p in players if int(p["games_injured"]) > 0]
    blocked_players = [p for p in players if int(p["games_blocked"]) > 0]

    if not injured_players and not blocked_players:
        logger.info(
            "\t\"%s\" doesnt have any injured or blocked players. Skipping.",
            team,
        )
        return
    else:
        logger.info("\tAvailable RBN: %s", round(rbn_balance))
        max_heal_games = round(rbn_balance) / 10000
        for p in injured_players:
            logger.info(
                "\t%s (%s) is injured for %s matches. Trying to heal." % (
                    p["name"], p["uid"], p["games_injured"]
                ))
            if p["games_injured"] <= max_heal_games:
                op = create_custom_json_op(team, p["uid"])
                c = Client(keys=[pk, ])
                c.broadcast(op=op)
                logger.info("\t Transaction is broadcasted. Enjoy.")
                max_heal_games -= p["games_injured"] * 10000
            else:
                logger.info(
                    "\tNot enough RBN to heal %s (%s) for %s matches.",
                    p["name"], p["uid"], p["games_injured"]
                )

        max_unblock_games = round(rbn_balance) / 10000
        for p in blocked_players:
            logger.info(
                "\t%s (%s) is blocked for %s matches. Trying to unblock." % (
                    p["name"], p["uid"], p["games_blocked"]
                ))

            if p["games_blocked"] <= max_unblock_games:
                op = create_custom_json_op(team, p["uid"], heal=False)
                c = Client(keys=[pk, ])
                c.broadcast(op=op)
                logger.info("\t Transaction is broadcasted. Enjoy.")
                max_heal_games -= p["games_injured"] * 10000
            else:
                logger.info(
                    "\tNot enough RBN to unblock %s (%s) for %s matches.",
                    p["name"], p["uid"], p["games_blocked"]
                )


def main():
    for team in ACCOUNTS:
        auto_heal_and_unblock(team["username"], team["posting_key"])


main()
```
***

Example output:
***

![Screenshot 2021-02-16 at 15.21.26.png](https://images.hive.blog/DQma7jdV1HJHhJrJTxBC9wxpQJTEZpU87TfVf4z6Ab5BVdR/Screenshot%202021-02-16%20at%2015.21.26.png)

***

Requires Python3.6. `lighthive`, `rabona_client` python libraries must be installed. I use it with a crontab entry - so it checks injured/blocked players once every hour.

#### Vote for my witness
***
If you like what I do, consider casting a vote on [Hivesigner](https://hivesigner.com/sign/account-witness-vote?witness=emrebeyler&approve=1) or on [PeakD](https://peakd.com/witnesses).
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 118 others
properties (23)
authoremrebeyler
permlinkauto-unblock-for-rabona
categoryhive-169321
json_metadata{"tags":["rabona","python","programming"],"image":["https://images.hive.blog/DQma7jdV1HJHhJrJTxBC9wxpQJTEZpU87TfVf4z6Ab5BVdR/Screenshot%202021-02-16%20at%2015.21.26.png"],"app":"hiveblog/0.1","format":"markdown","links":["https://hivesigner.com/sign/account-witness-vote?witness=emrebeyler&approve=1"]}
created2021-02-16 14:22:24
last_update2021-02-16 14:24:30
depth0
children0
last_payout2021-02-23 14:22:24
cashout_time1969-12-31 23:59:59
total_payout_value8.660 HBD
curator_payout_value6.132 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,473
author_reputation448,535,049,068,622
root_title"Auto Unblock for Rabona"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id101,904,795
net_rshares34,207,128,525,356
author_curate_reward""
vote details (182)