eximiabots-radiox/src/client/classes/Statistics.ts

96 lines
3.1 KiB
TypeScript
Raw Normal View History

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";
import { radio } from "./Radio";
2023-06-04 02:48:42 +00:00
2023-06-07 03:41:58 +00:00
export interface statistics {
[key: string]: statistic
}
interface statistic {
"time": number,
"used": number
}
2023-06-04 01:29:42 +00:00
export default class Statistics {
2023-06-07 03:41:58 +00:00
map: Map<string, statistics>;
2023-06-04 02:48:42 +00:00
2021-09-16 01:46:54 +00:00
constructor() {
this.map = new Map();
}
update(client: RadioClient, guild: Guild | null, radio: radio) {
2023-06-05 22:39:35 +00:00
if(!guild) return;
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
2023-06-07 14:43:19 +00:00
if(radio.datastore === undefined) return;
2021-09-16 01:46:54 +00:00
if(!radio.datastore.statistics[radio.station.name]){
2023-06-07 03:41:58 +00:00
radio.datastore.statistics[radio.station.name] = {
time: 0,
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 = radio.currentTime - radio.startTime;
2023-06-07 14:43:19 +00:00
radio.datastore.statistics[radio.station.name] = {
time: radio.datastore.statistics[radio.station.name].time + radio.playTime,
used: 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){
if(!client.datastore?.map) return;
2021-09-16 01:46:54 +00:00
let guilds = client.datastore.map.keys();
2023-06-07 03:41:58 +00:00
let statistics : statistics = {};
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'){
2023-06-07 14:43:19 +00:00
if(client.stations){
for(const station of client.stations) {
if(!currentGuild) return;
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){
2023-06-05 22:39:35 +00:00
if(!statistics[station.name]){
2023-06-07 03:41:58 +00:00
statistics[station.name] = {
time: 0,
used: 0
};
2021-09-16 01:46:54 +00:00
}
2023-06-07 14:43:19 +00:00
statistics[station.name] = {
time: statistics[station.name].time + currentGuild.statistics[station.name].time,
used: statistics[station.name].used + currentGuild.statistics[station.name].used
}
2021-09-16 01:46:54 +00:00
}
2023-06-05 22:39:35 +00:00
}
2021-09-16 01:46:54 +00:00
}
}
calculation = guilds.next();
}
2023-06-07 03:41:58 +00:00
let newData = {
guild: {
id: "global",
name: "global"
},
2023-06-07 14:43:19 +00:00
statistics: statistics,
state: null
2023-06-07 03:41:58 +00:00
};
2021-09-16 01:46:54 +00:00
client.datastore.updateEntry(newData.guild, newData);
}
};