diff --git a/client/datastore.js b/client/datastore.js index a412d3e..99d3485 100644 --- a/client/datastore.js +++ b/client/datastore.js @@ -23,6 +23,47 @@ module.exports = class { //console.log(""); } + calculateGlobal(client){ + let guilds = this.map.keys(); + let stations = client.stations; + let statistics = {}; + + if(!client.stations) return; + + let calculation = guilds.next(); + + while (!calculation.done) { + let currentGuild = this.getEntry(calculation.value); + if(calculation.value == 'global'){ + return calculation = guilds.next(); + } + + if(stations){ + Object.keys(stations).forEach(function(station) { + if(currentGuild.statistics[stations[station].name] && currentGuild.statistics[stations[station].name].time && !parseInt(currentGuild.statistics[stations[station].name].time) == 0 && currentGuild.statistics[stations[station].name].used && !parseInt(currentGuild.statistics[stations[station].name].used) == 0){ + if(!statistics[stations[station].name]){ + statistics[stations[station].name] = {}; + statistics[stations[station].name].time = 0; + statistics[stations[station].name].used = 0; + } + + statistics[stations[station].name].time = parseInt(statistics[stations[station].name].time)+parseInt(currentGuild.statistics[stations[station].name].time); + statistics[stations[station].name].used = parseInt(statistics[stations[station].name].used)+parseInt(currentGuild.statistics[stations[station].name].used); + } + }); + } + calculation = guilds.next(); + } + + let newData = {}; + newData.guild = {}; + newData.guild.id = "global"; + newData.guild.name = "global"; + newData.statistics = statistics; + this.updateEntry(newData.guild, newData); + } + + checkEntry(id){ if(!this.map.has(id)){ this.createEntry(id); diff --git a/client/events/ready.js b/client/events/ready.js index 1ca6f8e..81fac5c 100644 --- a/client/events/ready.js +++ b/client/events/ready.js @@ -23,7 +23,6 @@ module.exports = { client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') .then(res => res.json()); } catch (err) { - client.stations = null; console.error(err); } @@ -32,6 +31,12 @@ module.exports = { .then(res => res.json()); }, 3600000); + if(!client.stations) { + client.user.setStatus('dnd'); + } + + client.datastore.calculateGlobal(client); require(`../emojis.js`).execute(client); + } } \ No newline at end of file