Snippets

Created by Riccardo Sibani last modified

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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.