2023-06-04 02:48:42 +00:00
|
|
|
import { Guild } from "discord.js";
|
2023-06-04 21:13:15 +00:00
|
|
|
import RadioClient from "../../Client";
|
2023-06-04 02:48:42 +00:00
|
|
|
|
2023-06-04 01:29:42 +00:00
|
|
|
export default class Statistics {
|
2023-06-04 02:48:42 +00:00
|
|
|
map: any;
|
|
|
|
|
2021-09-16 01:46:54 +00:00
|
|
|
constructor() {
|
|
|
|
this.map = new Map();
|
|
|
|
}
|
|
|
|
|
2023-06-04 21:13:15 +00:00
|
|
|
update(client: RadioClient, guild: Guild, radio: any) {
|
2021-09-16 01:46:54 +00:00
|
|
|
|
2023-06-04 21:13:15 +00:00
|
|
|
client.datastore?.checkEntry(guild.id);
|
2021-09-16 01:46:54 +00:00
|
|
|
|
2023-06-04 21:13:15 +00:00
|
|
|
radio.datastore = client.datastore?.getEntry(guild.id);
|
2021-09-16 01:46:54 +00:00
|
|
|
|
|
|
|
if(!radio.datastore.statistics[radio.station.name]){
|
|
|
|
radio.datastore.statistics[radio.station.name] = {};
|
|
|
|
radio.datastore.statistics[radio.station.name].time = 0;
|
|
|
|
radio.datastore.statistics[radio.station.name].used = 0;
|
2023-06-04 21:13:15 +00:00
|
|
|
client.datastore?.updateEntry(guild, radio.datastore);
|
2021-09-16 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let date = new Date();
|
|
|
|
radio.currentTime = date.getTime();
|
|
|
|
radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime);
|
|
|
|
radio.datastore.statistics[radio.station.name].time = parseInt(radio.datastore.statistics[radio.station.name].time)+parseInt(radio.playTime);
|
|
|
|
|
|
|
|
radio.datastore.statistics[radio.station.name].used = parseInt(radio.datastore.statistics[radio.station.name].used)+1;
|
2023-06-04 21:13:15 +00:00
|
|
|
client.datastore?.updateEntry(guild, radio.datastore);
|
2021-09-16 01:46:54 +00:00
|
|
|
this.calculateGlobal(client);
|
|
|
|
}
|
|
|
|
|
2023-06-04 21:13:15 +00:00
|
|
|
calculateGlobal(client: RadioClient){
|
2021-09-16 02:35:12 +00:00
|
|
|
if(!client.stations) return;
|
2023-06-04 21:13:15 +00:00
|
|
|
if(!client.datastore?.map) return;
|
2021-09-16 01:46:54 +00:00
|
|
|
|
|
|
|
let guilds = client.datastore.map.keys();
|
2021-09-16 02:35:12 +00:00
|
|
|
let stations = client.stations;
|
2023-06-04 02:48:42 +00:00
|
|
|
let statistics : any = {};
|
2021-09-16 01:46:54 +00:00
|
|
|
|
2021-09-16 02:35:12 +00:00
|
|
|
if(!client.stations) return;
|
2021-09-16 01:46:54 +00:00
|
|
|
|
|
|
|
let calculation = guilds.next();
|
|
|
|
|
|
|
|
while (!calculation.done) {
|
|
|
|
let currentGuild = client.datastore.getEntry(calculation.value);
|
|
|
|
if(calculation.value != 'global'){
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-06-04 02:48:42 +00:00
|
|
|
let newData : any = {};
|
2021-09-16 01:46:54 +00:00
|
|
|
newData.guild = {};
|
|
|
|
newData.guild.id = "global";
|
|
|
|
newData.guild.name = "global";
|
|
|
|
newData.statistics = statistics;
|
|
|
|
client.datastore.updateEntry(newData.guild, newData);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|