Skip to content

Instantly share code, notes, and snippets.

@ProbablePrime
Created February 29, 2016 12:10
Show Gist options
  • Save ProbablePrime/cf038f4744f7743c3020 to your computer and use it in GitHub Desktop.
Save ProbablePrime/cf038f4744f7743c3020 to your computer and use it in GitHub Desktop.
Easily update a control set on beam quickely.
var WsabiClient = require('wsabi-client-bacon');
var mcBoard = [
[
{
key: 'Replace Item',
cost: 50
//cooldown: 5 * 1000
},
{
key: 'Bonus Island',
cost: 100 * 3
//cooldown: 30 * 1000
},
{
key: 'Hostile Mob',
cost: 500 * 2
//cooldown: 60 * 1 * 1000
},
{
key: 'Friendly Mob',
cost: 250 * 2,
//cooldown: 60 * 1 * 1000
},
{
key: 'Tree',
cost: 300 * 2,
//cooldown: 60 * 1000
}
],
[
{
key: 'Fireworks',
cost: 100
//cooldown: 20 * 1000
},
{
key: 'Change Time',
cost: 500 * 2
//cooldown: 10 * 1000
},
{
key: 'Random Potion',
cost: 250 * 3
//cooldown: 60 * 1000
},
{
key: 'Heal User',
cost: 500 * 5
//cooldown: 60 * 1000
},
{
key: 'Spawn TNT',
cost: 1000 * 20
//cooldown: 60 * 5 * 1000
}
]
];
function createTactile(tactile) {
var tactileObj = {
id: tactile.id,
type: 'tactiles',
text: tactile.key,
help: tactile.key,
blueprint: [
{
width: tactile.width,
height: tactile.height,
grid: 'large',
state: 'default',
x: (tactile.column > 0) ? tactile.column * tactile.width : tactile.column,
y: tactile.row
}
],
analysis: {
holding: true,
frequency: true
},
cost: {
press: {
cost: tactile.cost
}
},
cooldown: {
press: (tactile.cooldown)? tactile.cooldown : 0
}
};
return tactileObj;
}
function createBaseObject(settings) {
var baseStr = '{"id":1173,"version":"0.1.0","state":"draft","versionOrder":256,"changelog":"Initial release","installation":null,"createdAt":"2016-02-27 03:15:47","updatedAt":"2016-02-27 03:16:05","gameId":711,"controls":{"reportInterval":50,"tactiles":[],"joysticks":[]},"download":null}';
var baseState = JSON.parse(baseStr);
baseState.id = settings.versionId;
baseState.version = settings.versionSemVer;
baseState.gameId = settings.gameId;
baseState.controls.tactiles = createTactiles(mcBoard);
return baseState;
}
function createTactiles(board) {
var length = board.length;
var i = 0;
var id = 0;
var tactiles = [];
var tactile;
while (i < length) {
var row = board[i];
var innerLength = row.length;
var j = 0;
while (j < innerLength) {
tactile = row[j];
tactile.column = j;
tactile.row = i;
tactile.width = 3;
tactile.height = 1;
tactile.id = id;
tactiles.push(createTactile(tactile));
id++;
j++;
}
i++;
}
console.log(tactiles);
return tactiles;
}
var wsabi = new WsabiClient('https://beam.pro');
wsabi.liveUrl = '/api/v1/live';
wsabi.socket.on('open', doIt);
function login(username, password) {
console.log('login');
return wsabi.post('/api/v1/users/login', {username, password});
}
function postData() {
var settings = {
versionId: 1173,
versionSemVer: '0.1.0',
gameId: 711
};
console.log('post');
return wsabi.put('/api/v1/tetris/versions/' + settings.versionId, createBaseObject(settings));
}
function doIt() {
login('ProbablePrime', '<password>').then(postData).then(function () {
console.log('done');
}).catch(function (err) {
console.log(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment