From 1f03039fceccadb5715c6f444fadadde4127258a Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 23 Jun 2021 17:21:09 +0100 Subject: [PATCH] animation queue working --- lnbits/extensions/copilot/migrations.py | 2 +- lnbits/extensions/copilot/models.py | 2 +- .../copilot/templates/copilot/compose.html | 117 ++++++++++-------- .../copilot/templates/copilot/index.html | 20 +-- 4 files changed, 81 insertions(+), 60 deletions(-) diff --git a/lnbits/extensions/copilot/migrations.py b/lnbits/extensions/copilot/migrations.py index 8e4e72d2f..c0ec153f4 100644 --- a/lnbits/extensions/copilot/migrations.py +++ b/lnbits/extensions/copilot/migrations.py @@ -23,7 +23,7 @@ async def m001_initial(db): lnurl_title TEXT, show_message INTEGER, show_ack INTEGER, - show_price INTEGER, + show_price TEXT, amount_made INTEGER, fullscreen_cam INTEGER, iframe_url TEXT, diff --git a/lnbits/extensions/copilot/models.py b/lnbits/extensions/copilot/models.py index 46f75a808..8aa2ae068 100644 --- a/lnbits/extensions/copilot/models.py +++ b/lnbits/extensions/copilot/models.py @@ -25,7 +25,7 @@ class Copilots(NamedTuple): lnurl_title: str show_message: int show_ack: int - show_price: int + show_price: str amount_made: int timestamp: int fullscreen_cam: int diff --git a/lnbits/extensions/copilot/templates/copilot/compose.html b/lnbits/extensions/copilot/templates/copilot/compose.html index 8a28be55d..c3211aaa5 100644 --- a/lnbits/extensions/copilot/templates/copilot/compose.html +++ b/lnbits/extensions/copilot/templates/copilot/compose.html @@ -135,6 +135,11 @@ console.log('Something went wrong!') }) }, + pushAnim(content){ + document.getElementById('animations').style.width = content[0] + document.getElementById('animations').src = content[1] + setTimeout(function(){ document.getElementById('animations').src = '' }, 5000) + }, launch(){ self = this console.log("poo") @@ -152,7 +157,6 @@ LNbits.utils.notifyApiError(err) }) }, - }, mounted() { this.initCamera() @@ -160,7 +164,21 @@ created: function () { self = this self.copilot = JSON.parse(localStorage.getItem('copilot')) - + console.log(localStorage.getItem('inkey')) + LNbits.api + .request( + 'GET', + '/copilot/api/v1/copilot/' + + self.copilot.id, + localStorage.getItem('inkey') + ) + .then(function (response) { + self.copilot = response.data + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + this.connectionBitStamp = new WebSocket('wss://ws.bitstamp.net') const obj = JSON.stringify({ @@ -170,14 +188,49 @@ this.connectionBitStamp.onmessage = function (e) { console.log(JSON.parse(e.data).data.price) - self.price = String( - new Intl.NumberFormat('en-US', { - style: 'currency', - currency: 'USD' - }).format(JSON.parse(e.data).data.price) - ) + console.log(self.copilot) + if(self.copilot.show_price){ + if(self.copilot.show_price == "btcusd"){ + self.price = String( + new Intl.NumberFormat('en-US', { + style: 'currency', + currency: 'USD' + }).format(JSON.parse(e.data).data.price) + ) + } + else if(self.copilot.show_price == "btceur"){ + self.price = String( + new Intl.NumberFormat('en-US', { + style: 'currency', + currency: 'EUR' + }).format(JSON.parse(e.data).data.price) + ) + } + else if(self.copilot.show_price == "btcgbp"){ + self.price = String( + new Intl.NumberFormat('en-US', { + style: 'currency', + currency: 'GBP' + }).format(JSON.parse(e.data).data.price) + ) + } + } } this.connectionBitStamp.onopen = () => this.connectionBitStamp.send(obj) + + const fetch = (data) => new Promise(resolve => setTimeout(resolve, 5000, this.pushAnim(data))) + + const addTask = (() => { + let pending = Promise.resolve(); + const run = async (data) => { + try { + await pending; + } finally { + return fetch(data); + } + } + return (data) => (pending = run(data)) + })(); if (location.protocol !== 'http:') { localUrl = 'wss://' + @@ -195,58 +248,24 @@ this.connection = new WebSocket(localUrl) this.connection.onmessage = function (e) { console.log(e.data) - res = e.data.split('-') - if (res[0] != this.oldRes) { - this.oldRes = res[0] if (res[0] == 'rocket') { - document.getElementById('animations').style.width = '40%' - document.getElementById('animations').src = - '/copilot/static/rocket.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - }, 5000) + addTask(['40%', '/copilot/static/rocket.gif']) } if (res[0] == 'face') { - document.getElementById('animations').style.width = '35%' - document.getElementById('animations').src = - '/copilot/static/face.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - }, 5000) + addTask(['35%', '/copilot/static/face.gif']) } if (res[0] == 'bitcoin') { - document.getElementById('animations').style.width = '30%' - document.getElementById('animations').src = - '/copilot/static/bitcoin.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - }, 5000) + addTask(['30%', '/copilot/static/bitcoin.gif']) } if (res[0] == 'confetti') { - document.getElementById('animations').style.width = '100%' - document.getElementById('animations').style.height = '100%' - document.getElementById('animations').src = - '/copilot/static/confetti.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - document.getElementById('animations').style.height = '' - }, 5000) + addTask(['100%', '/copilot/static/confetti.gif']) } if (res[0] == 'martijn') { - document.getElementById('animations').style.width = '40%' - document.getElementById('animations').src = - '/copilot/static/martijn.gif' - setTimeout(function () { - document.getElementById('animations').src = '' - }, 5000) + addTask(['40%', '/copilot/static/martijn.gif']) } if (res[0] == 'rick') { - this.animQueue.push({size: '40%', location: '/copilot/static/rick.gif'}) - } - if(self.queue == false){ - for() - + addTask(['40%', '/copilot/static/rick.gif']) } if (res[0] == 'true') { document.getElementById('videoCamera').style.width = '20%' @@ -260,8 +279,6 @@ if (res[1] != 'none') { showNotif(res[1]) } - this.oldRes = '' - } } this.connection.onopen = () => this.launch } diff --git a/lnbits/extensions/copilot/templates/copilot/index.html b/lnbits/extensions/copilot/templates/copilot/index.html index 85bb86dda..0ce146277 100644 --- a/lnbits/extensions/copilot/templates/copilot/index.html +++ b/lnbits/extensions/copilot/templates/copilot/index.html @@ -323,13 +323,15 @@
-
- -
+ +
@@ -453,7 +455,8 @@ show: false, data: null }, - options: ['bitcoin', 'confetti', 'rocket', 'face', 'martijn', 'rick'] + options: ['bitcoin', 'confetti', 'rocket', 'face', 'martijn', 'rick'], + currencyOptions: ['btcusd', 'btceur', 'btcgbp'] } }, methods: { @@ -512,6 +515,7 @@ ) .then(function (response) { localStorage.setItem('copilot', JSON.stringify(response.data)) + localStorage.setItem('inkey', self.g.user.wallets[0].inkey) }) .catch(function (error) { LNbits.utils.notifyApiError(error)