This repository has been archived on 2024-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
exchange-bot/displayWorker.js

82 lines
1.8 KiB
JavaScript

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;
}
isSomethingChanged() {
for (let from in this.data) {
for (let dat of this.data[from]) {
if(dat.change!==0) {
return true;
}
}
}
return false;
}
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