Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programmatically adding cubes to IOTArt.io
const request = require('request');
const IOTA = require('iota.lib.js');
var node = 'http://INSERT_POW_NODE_HERE:14265';
var seed = 'SEED'
var IOTArtCubeHelper = function() {
var cubes = null
var address = null
var depth = 3;
var minWeightMagnitude = 14;
var iota = new IOTA({
'provider': node
});
function dataToTag(x, y, r, g, b, msg) {
if (msg.length > 8) {
console.log('message longer 8 chars')
return
}
const chars = String.fromCharCode(x) +
String.fromCharCode(y) +
String.fromCharCode(r) +
String.fromCharCode(g) +
String.fromCharCode(b) +
msg
function toTrytes(input) {
// If input is not a string, return null
if (typeof input !== 'string') return null
var TRYTE_VALUES = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var trytes = "";
for (var i = 0; i < input.length; i++) {
var char = input[i];
var asciiValue = char.charCodeAt(0);
// If not recognizable ASCII character, return null
if (asciiValue > 255) {
//asciiValue = 32
return null;
}
var firstValue = asciiValue % 27;
var secondValue = (asciiValue - firstValue) / 27;
var trytesValue = TRYTE_VALUES[firstValue] + TRYTE_VALUES[secondValue];
trytes += trytesValue;
}
return trytes;
}
return toTrytes(chars)
}
function getAddress(cb) {
request({
url: 'http://iotart.io/address',
method: 'GET',
json: true
}, function(error, response, data) {
cb(data.address)
})
}
function getCubes(cb) {
request({
url: 'http://iotart.io/cubes.json',
method: 'GET',
json: true
}, function(error, response, cubes) {
if (error) console.error(error)
var grid = []
for (var x = 0; x < 25; x++) {
if (!grid[x])
grid[x] = []
for (var z = 0; z < 25; z++) {
if (!grid[x][z])
grid[x][z] = []
}
}
for (const cube of cubes)
grid[cube.x][cube.z].push(cube)
cb(grid);
})
}
function getMaxValue(x, y) {
return cubes[x][y][cubes[x][y].length - 1].value
}
function getCubesPile(x, y) {
return cubes[x][y]
}
function addCube(x, y, value, color, message) {
if (value <= getMaxValue(x, y)) {
console.log('value to small ('+value+'<'+getMaxValue(x, y)+'), cant add cube')
return
}
var transfers = [{
'address': address,
'value': value,
'tag': dataToTag(x, y, color.r, color.g, color.b, message)
}]
console.log(`adding a cube to x:${x} y:${y} with color (r:${color.r}, g:${color.g}, b:${color.b}), value ${value} and message: ${message}`)
iota.api.sendTransfer(seed, depth, minWeightMagnitude, transfers, function(e, bundle) {
if (e) throw e;
console.log("sent transaction.\n bundle: ", bundle);
})
}
function init(cb) {
getCubes((c) => {
cubes = c
getAddress((a) => {
address = a
cb()
})
})
}
return {
init,
addCube,
getMaxValue,
getCubesPile
}
}
ICH = new IOTArtCubeHelper()
ICH.init(() => {
console.log('init')
// do stuff
let x = 0
let y = 0
// add cube issues a tx. beware of address reuse!! don't add cubes in a loop!!
return
ICH.addCube(x, y, ICH.getMaxValue(x, y) + 1, {
r: 0,
g: 255,
b: 0
}, 'huhu')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment