Gut text formatting
This commit is contained in:
parent
d8a74d6c9e
commit
46d29dc9d3
71
displayWorker.js
Normal file
71
displayWorker.js
Normal file
@ -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
|
51
index.js
51
index.js
@ -1,31 +1,44 @@
|
|||||||
|
const DisplayWorker = require("./displayWorker.js")
|
||||||
|
|
||||||
const HilSu = require("hilsu-api");
|
const HilSu = require("hilsu-api");
|
||||||
const Telegraf = require("telegraf");
|
const Telegraf = require("telegraf");
|
||||||
|
|
||||||
const namesMap = {gems:"Салоцветы",money:"Самородки",balance:"Червонцы"}
|
const namesMap = {
|
||||||
|
gems: "Самоцветы",
|
||||||
|
money: "Самородки",
|
||||||
|
balance: "Червонцы"
|
||||||
|
}
|
||||||
|
|
||||||
let exchangeClient = new HilSu.ExchangeClient(process.env.HILTOKEN);
|
let exchangeClient = new HilSu.ExchangeClient(process.env.HILTOKEN);
|
||||||
let bot = new Telegraf(process.env.BOTTOKEN);
|
let bot = new Telegraf(process.env.BOTTOKEN);
|
||||||
|
|
||||||
/**
|
exchangeClient.on("ratesUpdate", (rates, oldRates) => {
|
||||||
* rates - Новое значение курса
|
if (oldRates === undefined || oldRates === rates)
|
||||||
* oldRates - Предыдущее значение курса
|
return;
|
||||||
*/
|
|
||||||
exchangeClient.on("ratesUpdate", (rates,oldRates) => {
|
handleChange(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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function getPercentageChange(oldNumber, newNumber){
|
function handleChange(rates, oldRates){
|
||||||
return ((oldNumber - newNumber) / oldNumber) * 100;
|
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();
|
bot.startPolling();
|
||||||
exchangeClient.connect();
|
exchangeClient.connect();
|
Reference in New Issue
Block a user