From 46d29dc9d3a5a7df20da7949b68b86d061875cba Mon Sep 17 00:00:00 2001 From: "kostya1661@gmail.com" Date: Sat, 26 Mar 2022 23:42:17 +0300 Subject: [PATCH] Gut text formatting --- displayWorker.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 51 +++++++++++++++++++++------------- 2 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 displayWorker.js diff --git a/displayWorker.js b/displayWorker.js new file mode 100644 index 0000000..b7684cd --- /dev/null +++ b/displayWorker.js @@ -0,0 +1,71 @@ +class DisplayWorker { + constructor(title, footer) { + this.title = title + this.footer = footer; + this.data = {} + } + + addFollowing(from, to, newValue, oldValue) { + if (!this.data.hasOwnProperty(from)) + this.data[from] = [] + + this.data[from].push({ + to: to, + newValue: newValue, + oldValue: oldValue, + change: newValue - oldValue, + percent: Math.max(newValue, oldValue) / Math.min(newValue, oldValue) * 100 - 100 + }) + } + + setTitle(title) { + this.title = title; + } + + setFooter(footer) { + this.footer = footer; + } + + build() { + var sortable = []; + + for (var i in this.data) { + sortable.push([i, this.data[i]]); + } + + sortable.sort(function(a, b) { + if (a[1].to > b[1].to) { + return 1; + } + if (b[1].to > a[1].to) { + return -1; + } + + return 0; + }); + + var result = `${this.title}:\n` + var lastFrom = "" + + sortable.forEach((v) => { + if (lastFrom !== v[0]){ + result += '\n' + lastFrom = v[0] + } + + v[1].forEach((t) => { + let change = (t.change <= 0 ? "" : "+") + t.change.toFixed(3) + let percent = t.percent.toFixed(3) + + result += ` ${v[0]} => ${t.to}: ${change} (${percent}%)\n` + }) + + }) + + result += `\n${this.footer}` + + return result; + } +} + +module.exports = DisplayWorker \ No newline at end of file diff --git a/index.js b/index.js index 6272808..35d898c 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,44 @@ +const DisplayWorker = require("./displayWorker.js") + const HilSu = require("hilsu-api"); const Telegraf = require("telegraf"); -const namesMap = {gems:"Салоцветы",money:"Самородки",balance:"Червонцы"} +const namesMap = { + gems: "Самоцветы", + money: "Самородки", + balance: "Червонцы" +} let exchangeClient = new HilSu.ExchangeClient(process.env.HILTOKEN); let bot = new Telegraf(process.env.BOTTOKEN); -/** - * rates - Новое значение курса - * oldRates - Предыдущее значение курса - */ -exchangeClient.on("ratesUpdate", (rates,oldRates) => { - if(oldRates===rates) return; //пропускаем если ничего не изменилось - if(oldRates===undefined) return; //и пропускаем если это первое получение данных (при старте) - let message = "Обновление курса:\n"; - for (let rate of rates.rates) { - //получаем старый курс для этого обмена - let old = oldRates.rates.find(r => r.to===rate.to&&r.from===rate.from); - message+=`${namesMap[rate.from]} -> ${namesMap[rate.to]}: ${rate.value.toFixed(3)} (${getPercentageChange(old.value, rate.value).toFixed(2)}%)\n`; - } - message+=`\nПул валют:\nЧервонцы: ${rates.maxExchange.balance.toFixed(3)}\nСалоцветы: ${rates.maxExchange.gems.toFixed(3)}\nСамородки: ${rates.maxExchange.money.toFixed(3)}`; - bot.telegram.sendMessage("-765759105",message); +exchangeClient.on("ratesUpdate", (rates, oldRates) => { + if (oldRates === undefined || oldRates === rates) + return; + + handleChange(rates, oldRates) }); -function getPercentageChange(oldNumber, newNumber){ - return ((oldNumber - newNumber) / oldNumber) * 100; +function handleChange(rates, oldRates){ + let message = new DisplayWorker("Обновление курса") + + for (let rate of rates.rates) { + let old = oldRates.rates.find(r => r.to === rate.to && r.from === rate.from); + + message.addFollowing( + namesMap[rate.from], + namesMap[rate.to], + rate.value, + old.value + ) + } + + message.setFooter( + `Пул валют:\n Червонцы: ${rates.maxExchange.balance.toFixed(3)}\n Салоцветы: ${rates.maxExchange.gems.toFixed(3)}\n Самородки: ${rates.maxExchange.money.toFixed(3)}` + ) + + bot.telegram.sendMessage("-765759105", message.build()); } bot.startPolling(); -exchangeClient.connect(); +exchangeClient.connect(); \ No newline at end of file