Last active
April 3, 2017 11:37
-
-
Save meeDamian/2e37b0cb701c745acbcf0a4fd456c73f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (function () { | |
| 'use strict'; | |
| const DATA_SOURCE = 'https://meedamian.keybase.pub/btc_data.js'; | |
| const COLORS = { | |
| '#': 3, // black | |
| ' ': 0, // white | |
| '!': 1, // light grey | |
| '%': 2, // dark grey | |
| 'o': 6, // orange | |
| '*': -10 // ignore | |
| }; | |
| const COLOR_NAMES = { | |
| 0: 'white', | |
| 1: 'light grey', | |
| 2: 'dark grey', | |
| 3: 'black', | |
| 6: 'orange' | |
| }; | |
| let image; | |
| window.process = function ({img, x, y, defX, defY}) { | |
| r.place.panX = defX; | |
| r.place.panY = defY; | |
| image = img.reduce((acc, row, i) => { | |
| acc.push(...row.split('').map((cell, j) => { | |
| return { | |
| color: COLORS[cell], | |
| x: x + j, | |
| y: y + i | |
| }; | |
| })); | |
| return acc; | |
| }, []).sort(() => .5 - Math.random()); // "cute shuffle" to make traversing less predictable; ref: http://stackoverflow.com/a/18650169/390493 | |
| }; | |
| const BTC_ID = 'btc-data'; | |
| function updateBitcoin() { | |
| if (document.getElementById(BTC_ID)) { | |
| document.getElementById(BTC_ID).remove(); | |
| } | |
| let script = document.createElement('script'); | |
| script.id = BTC_ID; | |
| script.src = DATA_SOURCE; | |
| console.log(`Fetching data from ${DATA_SOURCE}…`); | |
| document.getElementsByTagName('head')[0].appendChild(script); | |
| } | |
| // every 10 minutes | |
| setTimeout(updateBitcoin, 0); | |
| setInterval(updateBitcoin, 10 * 60 * 1000); | |
| r.placeModule('placePaintBot', loader => { | |
| var c = loader('canvasse'); | |
| setInterval(() => { | |
| if (!image) { | |
| console.log('data not yet ready…'); | |
| return; | |
| } | |
| if (r.place.getCooldownTimeRemaining() > 200) { | |
| console.log('cooldown'); | |
| return; | |
| } | |
| for (let {x, y, color} of image) { | |
| let currentColor = r.place.state[c.getIndexFromCoords(x, y)]; | |
| if (color !== currentColor) { | |
| console.log(`Drawing (${x}, ${y}) from ${COLOR_NAMES[currentColor] || color} to ${COLOR_NAMES[color]}`); | |
| r.place.setColor(color); | |
| r.place.drawTile(x, y); | |
| return; | |
| } | |
| } | |
| console.log('Everything\'s dandy; noop'); | |
| }, 1500); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment