Skip to content

Instantly share code, notes, and snippets.

@andytudhope
Created November 3, 2017 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andytudhope/594ac49370a81c2712fec3e4799a307f to your computer and use it in GitHub Desktop.
Save andytudhope/594ac49370a81c2712fec3e4799a307f to your computer and use it in GitHub Desktop.
var price = 50000000000000000;
status.command({
name: 'hours',
description: 'How many hours do you want to book the ...?',
color: '#CCCCCC',
sequentialParams: true,
params: [{
name: 'hours',
type: status.types.NUMBER,
placeholder: 'e.g. 12 hours',
suggestions: hoursSuggestions
}],
handler: function(params) {
return {'text-message': 'OK'};
}
});
function hoursLabel(params) {
var value = params.value;
return "Book the ... for " + value + " hours";
}
function priceLabel(params) {
var value = parseInt(params.value)*parseInt(price);
return "Price: " + web3.fromWei(value, "ether") + " ETH";
}
function validationTextLabel(params) {
var value = parseInt(params.value)*parseInt(price);
var balance = parseInt(web3.toWei(params.balance));
if (value>balance) {
return "Insufficient Balance!";
} else {
return "";
}
}
status.defineSubscription(
// the name of subscription and the name of the value in bot-db
// associated with this subscription
"setHours",
// the map of values on which subscription depends: keys are arbitrary names
// and values are db paths to another value
{value: ["sliderValue"]},
// the function which will be called as reaction on changes of values above,
// should be pure. Returned result will be associated with subscription in bot-db
hoursLabel
);
status.defineSubscription(
"setPrice",
{value: ["sliderValue"]},
priceLabel
);
status.defineSubscription(
"setValidationText",
{value: ["sliderValue"], balance: ["balance"]},
validationTextLabel
);
function hoursSuggestions(params, context) {
var balance = parseFloat(web3.fromWei(web3.eth.getBalance(context.from), "ether"));
var defaultSliderValue = 1;
var view = ["view", {style: {margin:10}},
["text", {}, "Set the maximum time you will be using the ... here. Don't worry, you will be refunded any time you don't use!"],
["text", {}, "Your Balance " + balance + " ETH"],
["text", {}, ["subscribe", ["setPrice"]]],
["text", {}, ["subscribe", ["setHours"]]],
["subscibe"],
["slider", {
maximumValue: 24,
value: defaultSliderValue,
minimumValue: 1,
onSlidingComplete: ["dispatch", ["set", "sliderValue"]],
step: 1
}],
['dispatch', [status.events.SET_COMMAND_ARGUMENT, [0 /arg idx/, value, false /move-to-next? true or false/]]]
['touchable',
{onPress: ['dispatch', [status.events.SET_COMMAND_ARGUMENT, [0, "12", false]]]},
["view", {}, ["text", {}, "Press here to set Value!"]]
],
["text", {style: {color: "red"}}, ["subscribe", ["setValidationText"]]]
];
status.setDefaultDb({
sliderValue: defaultSliderValue,
balance: balance,
setHours: hoursLabel({value: defaultSliderValue}),
setPrice: priceLabel({value: defaultSliderValue}),
setValidationText: validationTextLabel({value: defaultSliderValue, balance: balance})
});
status.updateDb({
balance: balance,
});
return {markup: view};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment