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
|
49
index.js
49
index.js
@ -1,30 +1,43 @@
|
||||
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();
|
||||
|
Reference in New Issue
Block a user