[
    "autonomous agent",
    {
        "init": "{
        //
        // ==========================================
        // OBYTE STACK GAME from Whistling Frogs
        // ==========================================
        // Written by Crypto Girl on 21 Sep 2019
        // ==========================================
        // Version: 0.4 - 30 Sept 2019
        // ==========================================
        // Can be used with OBYTE STACK GAME BOT v0.1
        // ===========================================
        //
        // The object of the game is to win the Stack.
        // Users make bids. Min bid is 100,000 bytes.
        // 1st bid initiates the Stack.
        // Each subsequent bid starts the 5 min timer.
        // If more than 5 min has passed since the previous
        // bid, the (stack - commission) is shared equally
        // between the last bidder and the 1st bidder.
        // The new stack is initiated with the current bid.
        // A commission of 10,000 bytes is charged on each bid.
        //
        $bot_address = '4H2FOFBP7ST6BLYWHZ3GUV5PHY626AM4'; //testnet
        $commission_payout_threshold = 100000;
        $fee = 10000;  // 10,000 for now to be replaced with 1% of the pot
        $seed_amnt = 10000;
        $min_bid_amnt = 100000;  // 100,000 is about £0.02 to be increaded to £0.50
        $bid_amnt = trigger.output[[asset=base]];
        $commission = var['commisson'] otherwise 0;
    $pay_commission = ($commission >= $commission_payout_threshold) otherwise false;
        $game_status = var['game_status'] otherwise 'not started';
        $game_finished = (trigger.initial_address == $bot_address) otherwise false;
        $bid_status = ($bid_amnt >= $min_bid_amnt) otherwise false;
        if ($bid_status == false AND $game_finished == false) bounce('Your bid is too small.');
    }",
        "messages": [
            {
                "if": "{$game_finished OR ($commission >= $commission_payout_threshold)}",
                "app": "payment",
                "payload": {
                    "cases": [
                        {
                            "if": "{$game_finished AND (var['author'] == var['leader']) AND $pay_commission}",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ var['leader'] }",
                                        "amount": "{ var['stack_amnt'] }"
                                    },
                                    {
                                        "address": "{ $bot_address }",
                                        "amount": "{ var['commission'] }"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$game_finished AND (var['author'] == var['leader']) AND ($pay_commission == false)}",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ var['leader'] }",
                                        "amount": "{ var['stack_amnt'] }"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$game_finished AND (var['author'] != var['leader']) AND $pay_commission}",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ var['author'] }",
                                        "amount": "{ var['stack_amnt'] / 2 }"
                                    },
                                    {
                                        "address": "{ var['leader'] }",
                                        "amount": "{ var['stack_amnt'] / 2 }"
                                    },
                                    {
                                        "address": "{ $bot_address }",
                                        "amount": "{ var['commission'] }"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$game_finished AND (var['author'] != var['leader']) AND ($pay_commission == false)}",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ var['author'] }",
                                        "amount": "{ var['stack_amnt'] / 2 }"
                                    },
                                    {
                                        "address": "{ var['leader'] }",
                                        "amount": "{ var['stack_amnt'] / 2 }"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{($game_finished == false) AND $pay_commission}",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $bot_address }",
                                        "amount": "{ var['commission'] }"
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {
                "app": "state",
                "state": "{
                // game not started
                if ($bid_status AND ($game_status == 'not started')) {
                    var['stack_amnt'] = $bid_amnt - $fee;
                    //var['winnings'] = var['stack_amnt'] / 2;
                    var['author'] = trigger.initial_address;
                    var['leader'] = trigger.initial_address;
                    var['game_status'] = 'running';
                    response['message'] = 'Thank you for playing';
                }
                // game is running
                if ($bid_status AND ($game_status == 'running')) {
                    var['stack_amnt'] += ($bid_amnt - $fee);
                    var['winnings'] = var['stack_amnt'] / 2;
                    var['leader'] = trigger.initial_address;
                    var['game_status'] = 'running';
                    response['message'] = 'Thank you for playing';
                   }
                // game is finished
                   if ($game_finished) {
                    var['commission'] = balance[base] - $seed_amnt;
                    //var['stack_amnt'] = 0;
                    //var['leader'] = '';
                    //var['author'] = '';
                    var['game_status'] = 'not started';
                    response['message'] = 'Game finished.';
                   }
            }"
            }
        ]
    }
]