Created
November 3, 2017 17:28
-
-
Save andytudhope/594ac49370a81c2712fec3e4799a307f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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