[
    "autonomous agent",
    {
        "messages": {
            "cases": [
                {
                    "if": "{
      // Obotic / MicroGrid by 
[email protected]
      // Decentralized Distributed Incentivized Computing
            
      $project = "
55QKTO6TUZMLOGOGKQLC2DC3TCYSHSM3" ;  // Select Random Registered Address AA
      $min_poolsize = 1 ;  // use 1 for demo
      $processors_per_round = 3 ;  // limited by The total number of secondary AAs stemming from a single primary AA cannot exceed 10, otherwise the primary AA fails and bounces. Takes ~3 secondary AAs per processor, e.g. 3 processors_per_round takes 9 secondary AAs.
      $storage = 1000 ;
      $min_round_cost = $processors_per_round * ( 21000 + $storage * 4 ) ;
      if( !var[ $project ][ "count" ] ) bounce( "$project " || $project || " AA missing 'count'") ;
      if( var[ $project ][ "count" ] < $min_poolsize ) bounce( "waiting for " || $project || " to have " || $min_poolsize || " registered obots" ) ;
      
      $check = trigger.data.check ;
      if( $check ) bounce( "round=" || $check || " result= " || var[ $check || "_" || var[ $check || "_best_result" ] || "_result" ] ) ;
      
      // $min_poolsize >= $processors_per_round
      $min_poolsize >= 1  // Should be at least greater than processors_per_round, use 1 for demo
      }",
                    "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');
            ( !trigger.data.message AND !trigger.data.round ) // means not messages from project nor obot
         }",
                                "init": "{
            $process = '{ "args": ' || trigger.data.args || ', "processor": 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 ) ;
            $processor = json_parse( $process.processor ) ;
            $round = $process.round ;
            $morebot = '{ "args": ' || $args || ', "processor": ' || ( $processor + 1 ) || ', "round": ' || $round || ' }' ;
            // do morebot here
            $obot = trigger.data.choosen ;
            $fee = var[ $obot ][ "fee" ] otherwise 21000 ;
         }",
                                "messages": [
                                    {
                                        "if": "{ $processor < $processors_per_round AND trigger.output[[asset=base]] > ( $fee + $storage )
            }",
                                        "app": "payment",
                                        "payload": {
                                            "asset": "base",
                                            "outputs": [
                                                {
                                                    "address": "{ $project }",
                                                    "amount": "{ trigger.output[[asset=base]] - ( $fee + $storage ) }"
                                                },
                                                {
                                                    "address": "{ $obot }",
                                                    "amount": "{ $fee }"
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "if": "{ $processor < $processors_per_round AND trigger.output[[asset=base]] > ( $fee + $storage )
            }",
                                        "app": "data",
                                        "payload": {
                                            "message": "{ $morebot }",
                                            "args": "{ $args }",
                                            "round": "{ $round }",
                                            "nosend": true
                                        }
                                    },
                                    {
                                        "if": "{ $processor >= $processors_per_round AND trigger.output[[asset=base]] > $fee }",
                                        "app": "payment",
                                        "payload": {
                                            "asset": "base",
                                            "outputs": [
                                                {
                                                    "address": "{ $obot }",
                                                    "amount": "{ $fee }"
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "if": "{ $processor >= $processors_per_round AND trigger.output[[asset=base]] > $fee }",
                                        "app": "data",
                                        "payload": {
                                            "args": "{ $args }",
                                            "round": "{ $round }"
                                        }
                                    },
                                    {
                                        "app": "state",
                                        "state": "{  // ROUND ADMINISTRATOR
                  // debug
                  // response[ "processor" ] = $processor ;
                  // response[ "morebot" ] = $morebot ;
                  // response[ "args" ] = $args ;
                  // response[ "round" ] = $round ;
                  
                  var[ $round || "_" || $obot ] = "awaiting result" ;  // valid processor
                  var[ $round || "_deadline" ] = timestamp + 43200 ;  // start countdown
                  if( !var[ $round || "_highest_rank" ] ) var[ $round || "_highest_rank" ] = 0 ;
               }"
                                    }
                                ]
                            },
                            {
                                "if": "{ // RANK RESULTS
            // cant use app: poll because results are not pre-determined
            $round = trigger.data.round;
            if( $round ){
               $obot = trigger.address ;
               $ro = $round || "_" || $obot ;
               if( !var[ $ro ] ) bounce( "not a valid obot or round") ;
               if( var[ $ro ] != "awaiting result" ) bounce( "obot already submitted result" ) ;
               if( var[ $round || "_deadline" ] < timestamp ) bounce( "round is already over" ) ;
               $hash = sha256( trigger.data.result ) ;
               $rh = $round || "_" || $hash ;
            }
            $round
            }",
                                "messages": [
                                    {
                                        "app": "state",
                                        "state": "{
               var[ $ro ] = $hash ;  // save result hash
               var[ $rh || "_submitters" ] = var[ $rh || "_submitters" ] || ", " || $obot ;  // list of submitters
               var[ $rh || "_result" ] = trigger.data.result ;  // actual result
               var[ $rh ] += 1 ;  // votes
               if( var[ $rh ] > var[ $round || "_highest_rank" ] ){  // new highest rank
                     var[ $round || "_best_result" ] = $hash ;
                     var[ $round || "_highest_rank" ] = var[ $rh ] ;
               }
               
            }"
                                    }
                                ]
                            },
                            {
                                "messages": [
                                    {
                                        "app": "payment",
                                        "payload": {
                                            "asset": "base",
                                            "outputs": [
                                                {
                                                    "address": "
HFNBIQTUNVMRM7PDP6NT2QRKLR62FOGR"
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "app": "state",
                                        "state": "{ if( !var[ "owner" ] ) var[ "owner" ] = "
HFNBIQTUNVMRM7PDP6NT2QRKLR62FOGR"; }"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }
]