| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $AA_NAME= "HOLAAAA"; |
| 6 | $AA_OWNER = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 7 | $INSTRUCTIONS = "To add an element at the end of the use 'list_name = <list name>'', 'add = <item name>' + 'value = <itemvalue>'"; |
| 8 | |
| 9 | |
| 10 | $list_name = trigger.data.list_name otherwise bounce ("Need 'list = <list_name>'!"); |
| 11 | |
| 12 | $list_owner = var[$list_name||"_owner"] otherwise trigger.address; |
| 13 | $list_count = var[$list_name||"_count"] otherwise 0; |
| 14 | $list_public = var[$list_name||"_public"] otherwise $i.public; |
| 15 | if (!$list_public) if (trigger.address != $list_owner) bounce ("You don't own this private list"); |
| 16 | }", |
| 17 | "messages": { |
| 18 | "cases": [ |
| 19 | { |
| 20 | "if": "{ !!trigger.data.add }", |
| 21 | "init": "{ |
| 22 | $item_key = $list_name||"_"||trigger.data.add; |
| 23 | }", |
| 24 | "messages": [ |
| 25 | { |
| 26 | "app": "state", |
| 27 | "state": "{ |
| 28 | |
| 29 | var[$list_name||"_owner"] = $list_owner; |
| 30 | var[$list_name||"_count"] = $list_count + 1; |
| 31 | var[$list_name||"_public"] = $list_public; |
| 32 | |
| 33 | |
| 34 | $last_item = var[$list_name||"_last"]; |
| 35 | if (!!$last_item) |
| 36 | var[$last_item||"_next"] = $item_key; |
| 37 | var[$list_name||"_last"] = $item_key; |
| 38 | if ($list_count == 1 ) var[$list_name||"_first"] = $item_key; |
| 39 | |
| 40 | |
| 41 | var[$item_key] = trigger.data.value; |
| 42 | var[$item_key||"_previous"] = $last_item; |
| 43 | var[$item_key||"_next"] = false; |
| 44 | |
| 45 | response['message']= $item_key||" added ^^."; |
| 46 | }" |
| 47 | } |
| 48 | ] |
| 49 | }, |
| 50 | { |
| 51 | "if": "{ !!trigger.data.remove }", |
| 52 | "init": "{ |
| 53 | if (!(var[$list_name])) bounce ("'"||$list_name||"' did not exist !"); |
| 54 | $item_key = $list_name||"_"||trigger.data.remove; |
| 55 | }", |
| 56 | "messages": [ |
| 57 | { |
| 58 | "app": "state", |
| 59 | "state": "{ |
| 60 | |
| 61 | var[$list_name||"_count"] = $list_count - 1; |
| 62 | |
| 63 | |
| 64 | $previous_item = var[$item_key||"_previous"]; |
| 65 | var[$previous_item||"_next"] = $next_item; |
| 66 | |
| 67 | |
| 68 | $next_item = var[$item_key||"_next"]; |
| 69 | var[$next_item||"_previous"] = $previous_item; |
| 70 | |
| 71 | |
| 72 | var[$item_key] = false; |
| 73 | var[$item_key||"_previous"] = false; |
| 74 | var[$item_key||"_next"] = false; |
| 75 | |
| 76 | response['message']= $item_key||" removed ^^."; |
| 77 | }" |
| 78 | } |
| 79 | ] |
| 80 | }, |
| 81 | { |
| 82 | "if": "{ trigger.data.remove_last }", |
| 83 | "init": "{ |
| 84 | if (!(var[$list_name])) bounce ("'"||$list_name||"' did not exist !"); |
| 85 | if (var[$list_name||"_count"] == 0) bounce ("List is empty!"); |
| 86 | }", |
| 87 | "messages": [ |
| 88 | { |
| 89 | "app": "state", |
| 90 | "state": "{ |
| 91 | |
| 92 | $last_item = var[$list_name||"_last"]; |
| 93 | $previous_item = var[$last_item||"_previous"]; |
| 94 | var[$previous_item||"_next"] = false; |
| 95 | |
| 96 | |
| 97 | var[$list_name||"_count"] = $list_count - 1; |
| 98 | var[$list_name||"_last"] = $previous_item; |
| 99 | |
| 100 | |
| 101 | var[$last_item||"_previous"] = false; |
| 102 | var[$last_item] = false; |
| 103 | var[$last_item||"_next"] = false; |
| 104 | |
| 105 | response['message']= $last_item||" removed ^^."; |
| 106 | }" |
| 107 | } |
| 108 | ] |
| 109 | }, |
| 110 | { |
| 111 | "if": "{ !!trigger.data.insert_first }", |
| 112 | "init": "{ |
| 113 | $item_key = $list_name||"_"||trigger.data.insert_first; |
| 114 | }", |
| 115 | "messages": [ |
| 116 | { |
| 117 | "app": "state", |
| 118 | "state": "{ |
| 119 | |
| 120 | var[$list_name||"_owner"] = $list_owner; |
| 121 | var[$list_name||"_count"] = $list_count + 1; |
| 122 | var[$list_name||"_public"] = $list_public; |
| 123 | |
| 124 | |
| 125 | $first_item = var[$list_name||"_first"]; |
| 126 | if (!!$first_item) |
| 127 | var[$first_item||"_previous"] = $item_key; |
| 128 | var[$list_name||"_first"] = $item_key; |
| 129 | |
| 130 | |
| 131 | var[$item_key] = trigger.data.value; |
| 132 | var[$item_key||"_previous"] = false; |
| 133 | var[$item_key||"_next"] = $first_item; |
| 134 | |
| 135 | response['message']= $item_key||" added ^^."; |
| 136 | }" |
| 137 | } |
| 138 | ] |
| 139 | }, |
| 140 | { |
| 141 | "if": "{ !!trigger.data.insert }", |
| 142 | "init": "{ |
| 143 | $item_key = $list_name||"_"||trigger.data.insert; |
| 144 | |
| 145 | |
| 146 | if (!!trigger.data.after) |
| 147 | { |
| 148 | $previous_item = $list_name||"_"||trigger.data.after; |
| 149 | if (!var[$previous_item]) bounce ($previous_item||" do NOT exist !"); |
| 150 | $next_item = var[$previous_item||"_next"]; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | if (!!trigger.data.before) |
| 155 | { |
| 156 | $next_item = $list_name||"_"||trigger.data.before; |
| 157 | if (!var[$next_item]) bounce ($next_item||" do NOT exist !"); |
| 158 | $previous_item = var[$next_item||"_previous"]; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | bounce ("Please, specify 'after' or 'before'"); |
| 163 | } |
| 164 | } |
| 165 | }", |
| 166 | "messages": [ |
| 167 | { |
| 168 | "app": "state", |
| 169 | "state": "{ |
| 170 | |
| 171 | var[$list_name||"_count"] = $list_count + 1; |
| 172 | |
| 173 | |
| 174 | if (!!$previous_item) |
| 175 | var[$previous_item||"_next"] = $item_key; |
| 176 | else |
| 177 | var[$list_name||"_first"] = $item_key; |
| 178 | |
| 179 | |
| 180 | if (!!$next_item) |
| 181 | var[$next_item||"_previous"] = $item_key; |
| 182 | else |
| 183 | var[$list_name||"_last"] = $item_key; |
| 184 | |
| 185 | |
| 186 | var[$item_key] = trigger.data.value; |
| 187 | var[$item_key||"_previous"] = $previous_item; |
| 188 | var[$item_key||"_next"] = $next_item; |
| 189 | |
| 190 | response['message']= $item_key||" added ^^."; |
| 191 | }" |
| 192 | } |
| 193 | ] |
| 194 | }, |
| 195 | { |
| 196 | "if": "{ trigger.data.delete }", |
| 197 | "init": "{ |
| 198 | if (!var[$list_name]) bounce ("'"||$i.list_name||"' did not exist at the first place!"); |
| 199 | if (var[$list_name||"_count"] > 0) bounce ("List must be empty to be deleted, use 'remove_last' as much as needed"); |
| 200 | }", |
| 201 | "messages": [ |
| 202 | { |
| 203 | "app": "state", |
| 204 | "state": "{ |
| 205 | var[$list_name||"_owner"] = false; |
| 206 | var[$list_name||"_count"] = false; |
| 207 | var[$list_name||"_public"] = false; |
| 208 | |
| 209 | response['message']= $list_name||" delete ^^."; |
| 210 | }" |
| 211 | } |
| 212 | ] |
| 213 | }, |
| 214 | { |
| 215 | "messages": [ |
| 216 | { |
| 217 | "app": "state", |
| 218 | "state": "{ bounce ($INSTRUCTIONS);}" |
| 219 | } |
| 220 | ] |
| 221 | } |
| 222 | ] |
| 223 | } |
| 224 | } |
| 225 | ] |