Definition: [
"autonomous agent",
{
"init": "{
// FOREVER CONSTANT
$AA_NAME= "HOLLAA"; // Holder Of Linked List AA;
$AA_OWNER = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ";
$INSTRUCTIONS = "For every operation 'list_name = <list name>' is needed. To add an element at the end of the list use ', 'add = <item name>' and optionaly 'value = <item value>'. To remove 'remove = <item name>'. You can insert with 'insert = <item name>', 'after/before = <ref item name>' and optionaly 'value = <item value>'. When the list s empty, you can delete it with 'delete = true'. If you need to iterate you might find useful to be triggered back with the next or previous item, use 'get_next/get_previous = <actual item name>'.";
// get info from state or input data
$list_name = trigger.data.list_name otherwise bounce ("Need 'list = <list_name>'!");
$list_owner = var[$list_name||"_owner"] otherwise trigger.address;
$list_items_count = var[$list_name||"_items_count"] otherwise 0;
// access checking
$list_is_public = var[$list_name||"_is_public"] otherwise trigger.data.public;
if (!$list_is_public) if (trigger.address != $list_owner)
bounce ("You don't own this private list");
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.add }",
"init": "{
$item_key = $list_name||"_"||trigger.data.add;
}",
"messages": [
{
"app": "state",
"state": "{
// update list state
var[$list_name||"_owner"] = $list_owner;
var[$list_name||"_items_count"] = $list_items_count + 1;
var[$list_name||"_is_public"] = $list_is_public;
// update last item
$last_item = var[$list_name||"_last_item"];
if (!!$last_item) var[$last_item||"_next_item"] = trigger.data.add;
var[$list_name||"_last_item"] = trigger.data.add;
if (var[$list_name||"_items_count"] == 1 ) var[$list_name||"_first_item"] = trigger.data.add;
// add new item at the end of the linked list
var[$item_key] = trigger.data.value otherwise true;
var[$item_key||"_previous_item"] = $last_item;
var[$item_key||"_next_item"] = false;
response['message']= $item_key||" added ^^.";
}"
}
]
},
{
"if": "{ !!trigger.data.remove }",
"init": "{
if (!(var[$list_name||"_owner"])) bounce ("'"||$list_name||"' did not exist !");
$item_key = $list_name||"_"||trigger.data.remove;
}",
"messages": [
{
"app": "state",
"state": "{
// update list state
var[$list_name||"_items_count"] = $list_items_count - 1;
// update link in previous item
$previous_item = var[$item_key||"_previous_item"];
var[$previous_item||"_next_item"] = $next_item;
// update link in next item
$next_item = var[$item_key||"_next_item"];
var[$next_item||"_previous_item"] = $previous_item;
// remove item
var[$item_key] = false;
var[$item_key||"_previous_item"] = false;
var[$item_key||"_next_item"] = false;
response['message']= trigger.data.remove||" removed ^^.";
}"
}
]
},
{
"if": "{ !!trigger.data.insert }",
"init": "{
$item_key = $list_name||"_"||trigger.data.insert;
// identify previosu and next items
if (!!trigger.data.after)
{
$previous_item = $list_name||"_"||trigger.data.after;
if (!var[$previous_item]) bounce ($previous_item||" do NOT exist !");
$next_item = var[$previous_item||"_next_item"];
}
else
{
if (!!trigger.data.before)
{
$next_item = $list_name||"_"||trigger.data.before;
if (!var[$next_item]) bounce ($next_item||" do NOT exist !");
$previous_item = var[$next_item||"_previous_item"];
}
else
{
bounce ("Please, specify 'after' or 'before'");
}
}
}",
"messages": [
{
"app": "state",
"state": "{
// update list state
var[$list_name||"_items_count"] = $list_items_count + 1;
// update previous item link
if (!!$previous_item)
var[$previous_item||"_next_item"] = trigger.data.insert;
else
var[$list_name||"_first_item"] = trigger.data.insert;
// update next item link
if (!!$next_item)
var[$next_item||"_previous_item"] = trigger.data.insert;
else
var[$list_name||"_last_item"] = trigger.data.insert;
// add new item
var[$item_key] = trigger.data.value otherwise true;
var[$item_key||"_previous_item"] = $previous_item;
var[$item_key||"_next_item"] = $next_item;
response['message']= trigger.data.insert||" added ^^.";
}"
}
]
},
{
"if": "{ trigger.data.delete }",
"init": "{
if (!var[$list_name||"_owner"]) bounce ("'"||$i.list_name||"' did not exist at the first place!");
if (var[$list_name||"_items_count"] > 0) bounce ("List must be empty to be deleted, use 'remove_last_item' as much as needed");
}",
"messages": [
{
"app": "state",
"state": "{
var[$list_name||"_owner"] = false;
var[$list_name||"_items_count"] = false;
var[$list_name||"_is_public"] = false;
response['message']= $list_name||" delete ^^.";
}"
}
]
},
{
"if": "{ !!trigger.data.get_next }",
"init": "{
$starting_key = var[$list_name||"_"||trigger.data.get_next];
$next_item = !!$starting_key ? var[$starting_key||"_next_item"] : var[$list_name||"_first_item"];
//$item_name = substring($next_item, length($list_name) +1);
}",
"messages": [
{
"app": "data",
"payload": {
"item_name": "{ $next_item }",
"item_value": "{ var[$next_item] }",
"token": "{ trigger.data.token }"
}
},
{
"app": "payment",
"payload": {
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{4}"
}
]
}
}
]
},
{
"if": "{ !!trigger.data.get_previous }",
"init": "{
$starting_key = var[$list_name||"_"||trigger.data.get_previous];
$previous_item = !!$starting_key ?
var[$starting_key||"_previous_item"] otherwise bounce ("Was first item!") :
var[$list_name||"_last_item"];
// $item_name = substring($previous_item, length($list_name) +1);
}",
"messages": [
{
"app": "data",
"payload": {
"item_name": "{ $previous_item }",
"item_value": "{ var[$previous_item] }",
"token": "{ trigger.data.token }"
}
},
{
"app": "payment",
"payload": {
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{4}"
}
]
}
}
]
},
{
"messages": [
{
"app": "state",
"state": "{ bounce ($INSTRUCTIONS);}"
}
]
}
]
}
}
]