# Bustabit Scripts This repository documents 's autobetter / scripting system. ## Table of Contents - [How to write a script](#how-to-write-a-script) * [Bits vs Satoshis](#bits-vs-satoshis) * [The UI Config](#the-ui-config) + [Input Objects](#input-objects) + [Required vs optional inputs](#required-vs-optional-inputs) * [Interacting with the game](#interacting-with-the-game) + [The Engine API](#the-engine-api) + [A Game object](#a-game-object) + [The UserInfo Store](#the-userinfo-store) + [The Chat Store](#the-chat-store) + [Displaying Output](#displaying-output) + [Notifications](#notifications) + [Stopping the Script](#stopping-the-script) + [Verifying Game Results](#verifying-game-results) ## How to write a script ### Bits vs Satoshis **Important:** The client and server talk in satoshis even though the user interface shows bits. - bits -> satoshis: `Math.round(bits * 100)` - bits -> bitcoins: `bits / 1e6` - satoshis -> bits: `satoshis / 100` - satoshis -> bitcoins: `satoshis / 1e8` - bitcoins -> bits : `Math.round(bitcoins * 1e8) / 1e6` - bitcoins -> satoshis : `Math.round(bitcoins * 1e8)` ### The UI Config One of the key features of bustabit's new scripting system is that scripts can specify a user interface for the user to fill out before clicking "Start Script". This allows the script to read user-defined variables, and it makes scripts more user-friendly for the player. ---- At the top of every script, you must define the **UI Config**. ```javascript var config = { }; // Simulation begins down here ... ``` Bustabit parses the config object into a user interface that is used to configure and drive the script. The user uses the generated form components to feed input into the script, and then your script can access these values while it's running. Here's a simple script: ```javascript var config = { name: { value: '', type: 'text', label: 'What is your name?' }, maxBet: { value: 1e8, type: 'balance', label: 'Max Bet' }, colors: { value: 'red', type: 'radio', label: 'Pick a color', options: { red: { value: 'red', type: 'noop', label: 'Red' }, blue: { value: 'blue', type: 'noop', label: 'Blue' }, } } }; log('hello', config.name.value, 'you chose the color', config.colors.value) ``` And here's the UI it generates: ![](img/img1.png) #### Input Objects As you can see, the UI Config contains values that we will call **Input Objects** since they get turned into form input components. Every Input Object has a `type` which is one of: - `text`: Maps to an `` field. ```javascript var config = { name: { value: '', type: 'text', label: 'What is your name?' }, name2: { value: 'initial value', type: 'text', label: 'Another field' } } ``` ![](img/img2.png) - `noop`: A noop input just means that it does not embed an interactive control. You usually use them inside a `radio` input. ```javascript var config = { noopExample: { type: 'noop', label: 'Demonstrating noop' }, // If you don't specify a label, the key is used. noopExample2: { type: 'noop' } } ``` ![](img/img3.png) - `radio`: Maps to HTML radio buttons. Notice that `value: 'red'` means that `options.red` will be the initially-selected radio button. ```javascript var config = { colors: { value: 'red', type: 'radio', label: 'Pick a color', options: { red: { value: 'red', type: 'noop', label: 'Red' }, blue: { value: 'blue', type: 'noop', label: 'Blue' }, } } } ``` ![](img/img4.png) - `checkbox`: Maps to HTML check boxes. ```javascript var config = { red: { type: 'checkbox', label: 'Red', value: true }, yellow: { type: 'checkbox', label: 'Yellow', value: false }, blue: { type: 'checkbox', label: 'Blue', value: true } }; ``` ![](img/img5.png) - `balance`: Creates a type="number" input that accepts values in bits but exposes the value as satoshis to the script. ```javascript var config = { baseBet: { value: 100, type: 'balance', label: 'base bet' } }; ``` **Important**: Notice how the value is `100` (satoshis), but it's exposed as `1` to the user in the UI. ![](img/img6.png) - `multiplier`: Creates a text field input that accepts payout multiplier inputs like `2.0`. ```javascript var config = { basePayout: { value: 2, type: 'multiplier', label: 'payout' } }; ``` ![](img/img7.png) - `combobox`: Maps to HTML `