[
    "autonomous agent",
    {
        "messages": {
            "cases": [
                {
                    "if": "{
      // Obotic / MicroGrid by 
[email protected]
      // My best entry, combines techniques from all my previous AA, structure based on REDvsBLUE
      // 1) Scientist send args to be processed by obots
      
      $project = "
5IZAANLACSTY5ILLCJ6N5P4OX73ZWRJY" ;  // Select Random Registered Address AA
      $min_poolsize = 3 ;
      $processors_per_round = 3 ;
      $storage = 1000 ;
      $min_round_cost = $processors_per_round * 21000 + $storage  * 3 ;
      if( !var[ $project ][ "count" ] ) bounce( "$project AA missing 'count'") ;
      if( var[ $project ][ "count" ] < $min_poolsize ) bounce( "waiting for " || $project || " to have " || $min_poolsize || " registered obots" ) ;
      if( trigger.data.computation_rounds ) bounce( " computation_rounds = " || var[ 'computation_rounds' ] ) ;
      $min_poolsize >= 3
      }",
                    "messages": {
                        "cases": [
                            {
                                "if": "{ // REQUEST NEW ROUND
            if( !trigger.data.message AND !trigger.data.round AND trigger.output[[asset=base]] < $min_round_cost ) bounce( 'need to send ' || $min_round_cost || ' to request a new round');
            // TODO check for trigger.data.args
            ( !trigger.data.message AND !trigger.data.round ) // means not messages from project nor obot
         }",
                                "init": "{
            $process = '{ "args": "' || 'obotic' || '", "processors": 1, "round": ' || ( var[ "round" ] + 1 ) || ' }' ;
         }",
                                "messages": [
                                    {
                                        "app": "data",
                                        "payload": {
                                            "message": "{ $process }",
                                            "nosend": true
                                        }
                                    },
                                    {
                                        "app": "payment",
                                        "payload": {
                                            "asset": "base",
                                            "outputs": [
                                                {
                                                    "address": "{ $project }",
                                                    "amount": "{ trigger.output[[asset=base]] - $storage }"
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "app": "state",
                                        "state": "{ var[ "round" ] += 1 ; }"
                                    }
                                ]
                            },
                            {
                                "if": "{ // START ROUND
            trigger.address == $project AND trigger.data.message AND trigger.data.choosen
         }",
                                "init": "{
            $process = json_parse( trigger.data.message ) ;
            $args = json_stringify( $process.args ) ;
            $processors = json_parse( $process.processors ) + 1 ;
            $round = $process.round ;
            $morebot = '{ "args": "' || $args || '", "processors": ' || $processors || ', "round": ' || $round || ' }' ;
            $obot = trigger.data.choosen ;
            $fee = var[ $obot ][ "fee" ] otherwise 21000 ;
         }",
                                "messages": [
                                    {
                                        "app": "state",
                                        "state": "{  // TASK ROUND TRACKER
                  response[ "processors" ] = $processors ;
                  /*
                  if( $round ){
                     var[ "g" || $round ] = timestamp + 43200; // start countdown
                     response[ "message" ] = $message;
                     response[ "round" ] = $round;
                     response[ "redplayer" ] = $sendred;
                     response[ "blueplayer" ] = $sendblue;
                  }
                  if( trigger.address == $project ){
                    var[ "redaddr" ] += 1;  // new red address
                    var[ "r" || var[ "redaddr" ] ] = trigger.data.choosen;  // save red address
                  }
                  if( trigger.address == $blue ){
                     var[ "blueaddr" ] += 1;  // new blue address
                     var[ "b" || var[ "blueaddr" ] ] = trigger.data.choosen;
                  }
                   */ 
               }"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }
]