Fix Statistics class

This commit is contained in:
Christer Warén 2023-06-07 17:43:19 +03:00
parent cbae9ae38f
commit d260c6ea4f

View File

@ -25,36 +25,31 @@ export default class Statistics {
radio.datastore = client.datastore?.getEntry(guild.id); radio.datastore = client.datastore?.getEntry(guild.id);
//@ts-ignore if(radio.datastore === undefined) return;
if(!radio.datastore.statistics[radio.station.name]){ if(!radio.datastore.statistics[radio.station.name]){
//@ts-ignore
radio.datastore.statistics[radio.station.name] = { radio.datastore.statistics[radio.station.name] = {
time: 0, time: 0,
used: 0 used: 0
}; };
//@ts-ignore
client.datastore?.updateEntry(guild, radio.datastore); client.datastore?.updateEntry(guild, radio.datastore);
} }
let date = new Date(); let date = new Date();
radio.currentTime = date.getTime(); radio.currentTime = date.getTime();
radio.playTime = radio.currentTime - radio.startTime; radio.playTime = radio.currentTime - radio.startTime;
//@ts-ignore radio.datastore.statistics[radio.station.name] = {
radio.datastore.statistics[radio.station.name].time = parseInt(radio.datastore.statistics[radio.station.name].time) + radio.playTime; time: radio.datastore.statistics[radio.station.name].time + radio.playTime,
used: radio.datastore.statistics[radio.station.name].used + 1
//@ts-ignore }
radio.datastore.statistics[radio.station.name].used = parseInt(radio.datastore.statistics[radio.station.name].used)+1;
//@ts-ignore
client.datastore?.updateEntry(guild, radio.datastore); client.datastore?.updateEntry(guild, radio.datastore);
this.calculateGlobal(client); this.calculateGlobal(client);
} }
calculateGlobal(client: RadioClient){ calculateGlobal(client: RadioClient){
if(!client.stations) return;
if(!client.datastore?.map) return; if(!client.datastore?.map) return;
let guilds = client.datastore.map.keys(); let guilds = client.datastore.map.keys();
let stations = client.stations;
let statistics : statistics = {}; let statistics : statistics = {};
if(!client.stations) return; if(!client.stations) return;
@ -64,10 +59,10 @@ export default class Statistics {
while (!calculation.done) { while (!calculation.done) {
let currentGuild = client.datastore.getEntry(calculation.value); let currentGuild = client.datastore.getEntry(calculation.value);
if(calculation.value != 'global'){ if(calculation.value != 'global'){
if(stations){ if(client.stations){
for(const station of stations) { for(const station of client.stations) {
//@ts-ignore if(!currentGuild) return;
if(currentGuild.statistics[station.name] && currentGuild.statistics[station.name]?.time && parseInt(currentGuild.statistics[station.name].time) != 0 && currentGuild.statistics[station.name].used && parseInt(currentGuild.statistics[station.name].used) != 0){ if(currentGuild.statistics[station.name] && currentGuild.statistics[station.name]?.time && currentGuild.statistics[station.name].time != 0 && currentGuild.statistics[station.name].used && currentGuild.statistics[station.name].used != 0){
if(!statistics[station.name]){ if(!statistics[station.name]){
statistics[station.name] = { statistics[station.name] = {
time: 0, time: 0,
@ -75,10 +70,10 @@ export default class Statistics {
}; };
} }
//@ts-ignore statistics[station.name] = {
statistics[station.name].time = parseInt(statistics[station.name].time)+parseInt(currentGuild.statistics[station.name].time); time: statistics[station.name].time + currentGuild.statistics[station.name].time,
//@ts-ignore used: statistics[station.name].used + currentGuild.statistics[station.name].used
statistics[station.name].used = parseInt(statistics[station.name].used)+parseInt(currentGuild.statistics[station.name].used); }
} }
} }
} }
@ -91,7 +86,8 @@ export default class Statistics {
id: "global", id: "global",
name: "global" name: "global"
}, },
statistics: statistics statistics: statistics,
state: null
}; };
client.datastore.updateEntry(newData.guild, newData); client.datastore.updateEntry(newData.guild, newData);
} }