Snippets
Created by
Riccardo Sibani
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
object info {
blockchain_rid: byte_array = x"AAAABBBCCCCA";
}
enum component_type {
TEXT,
INTEGER,
DECIMAL,
BYTE_ARRAY,
ENUM,
NFA,
GTV, // TODO
FT3 // TODO
}
class nfa { // all the NFAs in the system are grouped here
name; // e.g. ball
key id: byte_array,
version: decimal; // Figure out something for versioning
desc: text;
index amount: integer;
//logic: decG; link custom client side logic in DeclarativeG
}
class entity { // this is one type of asset (e.g. all the type of unique balls)
idx: integer;
nfa;
key idx, nfa;
}
class component { // e.g. color
nfa; // keep this?
component_structure;
}
class component_structure {
key id: byte_array;
name;
type: component_type;
required: boolean; // is this component compulsory in the nfa?
}
class component_structure_integer {
min_val: integer; // minimum value allowed (e.g. negative numbers not allowed)
max_val: integer; // max value allowed or byte_lenght
}
class component_structure_decimal {
min_val: integer; // minimum value allowed (e.g. negative numbers not allowed)
max_val: integer; // max value allowed or byte_lenght
decimals: integer; // how many decimals?
}
class component_structure_enum {
// this class is only used with enum types, assume enum are all text for now (but maybe forever)
key component_structure, text;
}
class component_structure_nfa {
// This could create a circular problem -- TODO Fix this by recursively check nfa.id
key component_structure, nfa;
}
class text_component {
key entity, component_structure;
value: text;
}
class int_component {
key entity, component_structure;
value: integer;
}
class dec_component {
key entity, component_structure;
value: decimal;
}
class bytes_component {
key entity, component_structure;
value: byte_array;
}
operation main() {
print("MAIN EXECUTION");
// use case
// NFA are balls, we have name, color, speed, position, acceleration and owner, skin
// - speed, position and acceleration are just integer / decimals
// - name is a text
// - skin is byte_array
// - color is an enum
// - owner is one NFA
val colorComponentStructure = createComponentStructure('color', component_type.ENUM, true, map<text, gtv>(['enum': ['yellow', 'blue', 'red', 'black', 'white', 'orange'].to_gtv()]));
val xAxisComponentStructure = createComponentStructure('x', component_type.DECIMAL, true, map<text, gtv>(['int_values': [integer.MIN_VALUE, integer.MAX_VALUE].to_gtv(), 'decimals': (4).to_gtv()]));
val yAxisComponentStructure = createComponentStructure('y', component_type.DECIMAL, true,map<text, gtv>(['int_values': [integer.MIN_VALUE, integer.MAX_VALUE].to_gtv(), 'decimals': (4).to_gtv()]));
val doggy = createNFA("doggy", "Cutest crypto assets", 100, [colorComponentStructure, xAxisComponentStructure, yAxisComponentStructure]);
val speedComponentStructure = createComponentStructure('speed', component_type.INTEGER, true, map<text, gtv>(['int_values': [0, 200].to_gtv()]));
val accelerationComponentStructure = createComponentStructure('acceleration', component_type.DECIMAL, true, map<text, gtv>(['int_values': [0, 100].to_gtv(), 'decimals': (2).to_gtv()]));
val skinComponentStructure = createComponentStructure('skin', component_type.BYTE_ARRAY, false, map<text, gtv>());
val ownerComponentStructure = createComponentStructure('owner', component_type.NFA, false, map<text, gtv>(['nfa_id': (doggy.id).to_gtv()]));
val ball = createNFA("ball", "Just a bouncing ball", 1000, [colorComponentStructure, xAxisComponentStructure, yAxisComponentStructure, speedComponentStructure,
accelerationComponentStructure, skinComponentStructure, ownerComponentStructure]);
// val doggy1 = createEntity(doggy);
}
function createEntity(nfa,
textComponents: map<component_structure, text>,
integerComponents: map<text, integer>,
decimalComponents: map<text, decimal>,
bytesComponents: map<text, byte_array>,
enumComponents: map<text, text>,
nfaComponents: map<text, nfa>
): entity {
val entityIdx = (nfa@*{}).size();
require(entityIdx < nfa.amount, "Maximum amount of nfa entities already created");
val entity = create entity(nfa, entityIdx);
// get the components of that nfa
val componentsStructure = component@*{nfa}.component_structure;
for(componentStructure in componentsStructure) {
when (componentStructure.type) {
component_type.INTEGER -> {
val componentValue = require(componentStructure.required or integerComponents.get(componentsStructure.id));
}
}
}
return entity;
}
function createComponentStructure(name: text, type: component_type, required: boolean, extra: map<text, gtv>): component_structure {
val newComponentStructure = create component_structure (
.name = name,
.id = (name, info.blockchain_rid).hash(),
type,
required
);
when(type) {
component_type.INTEGER -> {
require(extra.contains('int_values'));
val intValues = require_not_empty(list<integer>.from_gtv(extra.get('int_values')), "Only integer values allowed");
require(intValues.size() == 2, "Only min and max values are allowed");
require(intValues[0] < intValues[1]);
create component_structure_integer(.min_val = intValues[0], .max_val = intValues[1]);
};
component_type.DECIMAL -> {
require(extra.contains('int_values') and extra.contains('decimals'));
val intValues = require_not_empty(list<integer>.from_gtv(extra.get('int_values')), "Only integer values allowed");
val decimals = integer.from_gtv(extra.get('decimals'));
require(intValues.size() == 2, "Only min and max values are allowed");
require(intValues[0] < intValues[1]);
create component_structure_decimal(.min_val = intValues[0], .max_val = intValues[1], decimals);
};
component_type.ENUM -> {
require(extra.contains('enum'));
val allowed_values = list<text>.from_gtv(extra.get('enum'));
require(allowed_values.size() < 256, "You can create maximum 256 enum items");
for (allowed_value in allowed_values) {
create component_structure_enum(newComponentStructure, allowed_value.lower_case());
}
};
component_type.NFA -> {
require(extra.contains('nfa_id'), "No nfa_id parameter in extra");
val nfa_id = byte_array.from_gtv(extra.get('nfa_id'));
val nfa = (nfa@{.id == nfa_id}(-sort .version, nfa_ref = nfa) limit 1).nfa_ref; // maybe extra param in "extra" and ask for the version to select?
create component_structure_nfa(newComponentStructure, nfa);
};
}
return newComponentStructure;
}
function createNFA(name, desc: text, amount: integer, components_structure: list<component_structure>): nfa {
val nfa = create nfa(
.name = name.lower_case(),
(name, info.blockchain_rid).hash(),
desc,
amount,
.version = 0.00
);
for(component_structure in components_structure) {
create component(nfa, component_structure);
}
return nfa;
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.