refactor score calculations for performance
parent
ad6f05409f
commit
5e8a6c8837
|
@ -183,6 +183,9 @@ export default class Queue {
|
||||||
return this.queues.score.add('score::inquiry', { inqID, userID, name, type, reason });
|
return this.queues.score.add('score::inquiry', { inqID, userID, name, type, reason });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) {
|
public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) {
|
||||||
return this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff });
|
return this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff });
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,208 +1,203 @@
|
||||||
/* eslint-disable no-plusplus */
|
/* eslint-disable no-plusplus */
|
||||||
/* eslint-disable no-continue */
|
/* eslint-disable no-continue */
|
||||||
/* eslint-disable one-var-declaration-per-line */
|
/* eslint-disable one-var-declaration-per-line */
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
import axios from 'axios';
|
import { Message, TextChannel } from 'eris';
|
||||||
import { Message, TextChannel } from 'eris';
|
import { Client } from '../class';
|
||||||
import { Client } from '../class';
|
import { CloudServicesUtil } from '../util';
|
||||||
|
|
||||||
interface CSResponse {
|
let interval: NodeJS.Timeout;
|
||||||
found: boolean,
|
|
||||||
tier: number,
|
export function getTotalMessageCount(client: Client): number {
|
||||||
totalReferrals?: number,
|
const projectChannels: TextChannel[] = [];
|
||||||
createdAt?: Date,
|
for (const c of client.guilds.get(client.config.guildID).channels.filter((ch) => ch.type === 0)) {
|
||||||
warns?: Date[],
|
const chan = <TextChannel> c;
|
||||||
locks?: Date[],
|
if (chan.parentID !== '701591772186411108') continue;
|
||||||
deletes?: Date[],
|
projectChannels.push(chan);
|
||||||
}
|
}
|
||||||
|
return (projectChannels.length * 200) + 2200 + 300 + 100 + 200;
|
||||||
let interval: NodeJS.Timeout;
|
}
|
||||||
|
|
||||||
export function getTotalMessageCount(client: Client): number {
|
export default async function calculateScore(client: Client): Promise<NodeJS.Timeout> {
|
||||||
const projectChannels: TextChannel[] = [];
|
const start = async () => {
|
||||||
for (const c of client.guilds.get(client.config.guildID).channels.filter((ch) => ch.type === 0)) {
|
const { members } = client.guilds.get(client.config.guildID);
|
||||||
const chan = <TextChannel> c;
|
|
||||||
if (chan.parentID !== '701591772186411108') continue;
|
const general = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('485680288123584525')).getMessages(2200);
|
||||||
projectChannels.push(chan);
|
const programmingSupport = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('506970598631538708')).getMessages(300);
|
||||||
}
|
const programmingSupport2 = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('554892820553531422')).getMessages(100);
|
||||||
return (projectChannels.length * 200) + 2200 + 300 + 100 + 200;
|
const cloudSupport = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('546457788184789013')).getMessages(200);
|
||||||
}
|
|
||||||
|
const projectChannels: TextChannel[] = [];
|
||||||
export default async function calculateScore(client: Client): Promise<NodeJS.Timeout> {
|
for (const c of client.guilds.get(client.config.guildID).channels.filter((ch) => ch.type === 0)) {
|
||||||
const start = async () => {
|
const chan = <TextChannel> c;
|
||||||
const { members } = client.guilds.get(client.config.guildID);
|
if (chan.parentID !== '701591772186411108') continue;
|
||||||
|
projectChannels.push(chan);
|
||||||
const general = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('485680288123584525')).getMessages(2200);
|
}
|
||||||
const programmingSupport = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('506970598631538708')).getMessages(300);
|
const projectMessages: Message<TextChannel>[] = [];
|
||||||
const programmingSupport2 = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('554892820553531422')).getMessages(100);
|
for (const chan of projectChannels) {
|
||||||
const cloudSupport = await (<TextChannel> client.guilds.get(client.config.guildID).channels.get('546457788184789013')).getMessages(200);
|
const msg = await chan.getMessages(200);
|
||||||
|
projectMessages.push(...msg);
|
||||||
const projectChannels: TextChannel[] = [];
|
}
|
||||||
for (const c of client.guilds.get(client.config.guildID).channels.filter((ch) => ch.type === 0)) {
|
for (const member of members.values()) {
|
||||||
const chan = <TextChannel> c;
|
if (member.bot) continue;
|
||||||
if (chan.parentID !== '701591772186411108') continue;
|
let score = await client.db.Score.findOne({ userID: member.user.id });
|
||||||
projectChannels.push(chan);
|
if (!score) {
|
||||||
}
|
const data: {
|
||||||
const projectMessages: Message<TextChannel>[] = [];
|
userID: string,
|
||||||
for (const chan of projectChannels) {
|
total: number,
|
||||||
const msg = await chan.getMessages(200);
|
activity: number,
|
||||||
projectMessages.push(...msg);
|
roles: number,
|
||||||
}
|
moderation: number,
|
||||||
for (const member of members.values()) {
|
cloudServices: number,
|
||||||
if (member.bot) continue;
|
other: number,
|
||||||
let score = await client.db.Score.findOne({ userID: member.user.id });
|
staff: boolean,
|
||||||
if (!score) {
|
locked: boolean,
|
||||||
const data: {
|
notify: boolean,
|
||||||
userID: string,
|
lastUpdated: Date,
|
||||||
total: number,
|
pin: number[],
|
||||||
activity: number,
|
} = {
|
||||||
roles: number,
|
userID: member.user.id,
|
||||||
moderation: number,
|
total: 0,
|
||||||
cloudServices: number,
|
activity: 0,
|
||||||
other: number,
|
roles: 0,
|
||||||
staff: boolean,
|
moderation: 0,
|
||||||
locked: boolean,
|
cloudServices: 0,
|
||||||
notify: boolean,
|
other: 0,
|
||||||
lastUpdated: Date,
|
staff: false,
|
||||||
pin: number[],
|
locked: false,
|
||||||
} = {
|
notify: false,
|
||||||
userID: member.user.id,
|
lastUpdated: new Date(),
|
||||||
total: 0,
|
pin: [client.util.randomNumber(100, 999), client.util.randomNumber(10, 99), client.util.randomNumber(1000, 9999)],
|
||||||
activity: 0,
|
};
|
||||||
roles: 0,
|
score = await (new client.db.Score(data)).save();
|
||||||
moderation: 0,
|
client.util.signale.debug(`SCORE INIT - ${member.username}`);
|
||||||
cloudServices: 0,
|
}
|
||||||
other: 0,
|
|
||||||
staff: false,
|
// eslint-disable-next-line prefer-const
|
||||||
locked: false,
|
// eslint-disable-next-line one-var-declaration-per-line
|
||||||
notify: false,
|
// eslint-disable-next-line one-var
|
||||||
lastUpdated: new Date(),
|
let total = 0, activity = 0, roles = 0, moderation = 0, cloudServices = 0, other = 0, staff = 0;
|
||||||
pin: [client.util.randomNumber(100, 999), client.util.randomNumber(10, 99), client.util.randomNumber(1000, 9999)],
|
cloudServices = 0;
|
||||||
};
|
|
||||||
score = await (new client.db.Score(data)).save();
|
roles = Math.floor(member.roles.length * 0.50);
|
||||||
client.util.signale.debug(`SCORE INIT - ${member.username}`);
|
if (roles > 54) roles = 54;
|
||||||
}
|
|
||||||
|
const moderations = await client.db.Moderation.find({ userID: member.user.id });
|
||||||
// eslint-disable-next-line prefer-const
|
let activeMods = 0;
|
||||||
// eslint-disable-next-line one-var-declaration-per-line
|
for (const mod of moderations) {
|
||||||
// eslint-disable-next-line one-var
|
if (mod.type === 1 || mod.type === 4) continue;
|
||||||
let total = 0, activity = 0, roles = 0, moderation = 0, cloudServices = 0, other = 0, staff = 0;
|
const testDate = (new Date(new Date(mod.date).setHours(2190)));
|
||||||
cloudServices = 0;
|
if (testDate > new Date()) { moderation -= 15; activeMods++; }
|
||||||
|
}
|
||||||
roles = Math.floor(member.roles.length * 0.50);
|
if (activeMods <= 0) moderation = 2;
|
||||||
if (roles > 54) roles = 54;
|
|
||||||
|
let messageCountTotal = 0;
|
||||||
const moderations = await client.db.Moderation.find({ userID: member.user.id });
|
const allMessagesArray = [...general, ...programmingSupport, ...programmingSupport2, ...cloudSupport, ...projectMessages];
|
||||||
let activeMods = 0;
|
|
||||||
for (const mod of moderations) {
|
for (const msg of allMessagesArray.filter((m) => m.member?.id === member.id)) {
|
||||||
if (mod.type === 1 || mod.type === 4) continue;
|
if (!msg.content) continue;
|
||||||
const testDate = (new Date(new Date(mod.date).setHours(2190)));
|
if (msg.content.length <= 5) continue;
|
||||||
if (testDate > new Date()) { moderation -= 15; activeMods++; }
|
// eslint-disable-next-line no-plusplus
|
||||||
}
|
messageCountTotal++;
|
||||||
if (activeMods <= 0) moderation = 2;
|
}
|
||||||
|
|
||||||
let messageCountTotal = 0;
|
const activityTotal = messageCountTotal;
|
||||||
const allMessagesArray = [...general, ...programmingSupport, ...programmingSupport2, ...cloudSupport, ...projectMessages];
|
activity = Math.floor(Math.log1p(activityTotal) * 12);
|
||||||
|
if (activity > (Math.log1p(getTotalMessageCount(client)) * 12)) activity = Math.floor((Math.log1p(getTotalMessageCount(client)) * 12));
|
||||||
for (const msg of allMessagesArray.filter((m) => m.member?.id === member.id)) {
|
if (member.roles.includes('446104438969466890') || member.roles.includes('701481967149121627')) staff = 20;
|
||||||
if (!msg.content) continue;
|
|
||||||
if (msg.content.length <= 5) continue;
|
const response = await CloudServicesUtil.fetchAccountStatus(member.user.id, client.config.internalKey);
|
||||||
// eslint-disable-next-line no-plusplus
|
if (response || response.found === true) {
|
||||||
messageCountTotal++;
|
let negatives = 0;
|
||||||
}
|
let positives = 0;
|
||||||
|
if (response.createdAt) {
|
||||||
const activityTotal = messageCountTotal;
|
const csCreatedTestDate = (new Date(new Date(response.createdAt).setHours(730)));
|
||||||
activity = Math.floor(Math.log1p(activityTotal) * 12);
|
if (csCreatedTestDate > new Date()) {
|
||||||
if (activity > (Math.log1p(getTotalMessageCount(client)) * 12)) activity = Math.floor((Math.log1p(getTotalMessageCount(client)) * 12));
|
negatives -= 10;
|
||||||
if (member.roles.includes('446104438969466890') || member.roles.includes('701481967149121627')) staff = 20;
|
}
|
||||||
|
}
|
||||||
const response = <CSResponse> (await axios.get(`https://api.cloud.libraryofcode.org/wh/score?id=${member.user.id}&authorization=${client.config.internalKey}`)).data;
|
let warns = 0;
|
||||||
if (response.found === true) {
|
for (const warn of response.warns) {
|
||||||
let negatives = 0;
|
const date = (new Date(new Date(warn).setHours(730)));
|
||||||
let positives = 0;
|
if (date > new Date()) { negatives -= 2; warns++; }
|
||||||
if (response.createdAt) {
|
}
|
||||||
const csCreatedTestDate = (new Date(new Date(response.createdAt).setHours(730)));
|
if (warns <= 0) positives += 2;
|
||||||
if (csCreatedTestDate > new Date()) {
|
if (response.warns.length === 0) positives += 3;
|
||||||
negatives -= 10;
|
|
||||||
}
|
let locks = 0;
|
||||||
}
|
for (const lock of response.locks) {
|
||||||
let warns = 0;
|
const date = (new Date(new Date(lock).setHours(1460)));
|
||||||
for (const warn of response.warns) {
|
if (date > new Date()) { negatives -= 5; locks++; }
|
||||||
const date = (new Date(new Date(warn).setHours(730)));
|
}
|
||||||
if (date > new Date()) { negatives -= 2; warns++; }
|
if (locks <= 0) positives += 3;
|
||||||
}
|
if (response.locks.length === 0) positives += 7;
|
||||||
if (warns <= 0) positives += 2;
|
|
||||||
if (response.warns.length === 0) positives += 3;
|
if (response.deletes.length > 0) {
|
||||||
|
for (const del of response.deletes) {
|
||||||
let locks = 0;
|
const date = (new Date(new Date(del).setHours(3650)));
|
||||||
for (const lock of response.locks) {
|
if (date > new Date()) negatives -= 20;
|
||||||
const date = (new Date(new Date(lock).setHours(1460)));
|
}
|
||||||
if (date > new Date()) { negatives -= 5; locks++; }
|
}
|
||||||
}
|
positives += Math.floor(response.totalReferrals * 1.17);
|
||||||
if (locks <= 0) positives += 3;
|
if (response.tier === 2) positives += 2;
|
||||||
if (response.locks.length === 0) positives += 7;
|
else if (response.tier === 3) positives += 3;
|
||||||
|
|
||||||
if (response.deletes.length > 0) {
|
if (negatives < 0) cloudServices = Math.floor((negatives * 1.2) + (positives * 0.06));
|
||||||
for (const del of response.deletes) {
|
else cloudServices = Math.floor(positives * 0.61);
|
||||||
const date = (new Date(new Date(del).setHours(3650)));
|
}
|
||||||
if (date > new Date()) negatives -= 20;
|
|
||||||
}
|
const inquiries = await client.db.Inquiry.find({ userID: member.user.id, type: 0 }).lean().exec();
|
||||||
}
|
const judgements = await client.db.Judgement.find({ userID: member.user.id }).lean().exec();
|
||||||
positives += Math.floor(response.totalReferrals * 1.17);
|
|
||||||
if (response.tier === 2) positives += 2;
|
if (inquiries?.length > 0) {
|
||||||
else if (response.tier === 3) positives += 3;
|
for (const inq of inquiries) {
|
||||||
|
const testDate = (new Date(new Date(inq.date).setHours(1460)));
|
||||||
if (negatives < 0) cloudServices = Math.floor((negatives * 1.2) + (positives * 0.06));
|
if (testDate > new Date()) other -= 1.65;
|
||||||
else cloudServices = Math.floor(positives * 0.61);
|
}
|
||||||
}
|
other = Math.floor(other * 1.15);
|
||||||
|
}
|
||||||
const inquiries = await client.db.Inquiry.find({ userID: member.user.id, type: 0 }).lean().exec();
|
|
||||||
const judgements = await client.db.Judgement.find({ userID: member.user.id }).lean().exec();
|
if (judgements?.length > 0) {
|
||||||
|
for (const judgement of judgements) {
|
||||||
if (inquiries?.length > 0) {
|
switch (judgement.severity) {
|
||||||
for (const inq of inquiries) {
|
default:
|
||||||
const testDate = (new Date(new Date(inq.date).setHours(1460)));
|
break;
|
||||||
if (testDate > new Date()) other -= 1.65;
|
case 0:
|
||||||
}
|
other -= 5;
|
||||||
other = Math.floor(other * 1.15);
|
break;
|
||||||
}
|
case 1:
|
||||||
|
other -= 10;
|
||||||
if (judgements?.length > 0) {
|
break;
|
||||||
for (const judgement of judgements) {
|
case 2:
|
||||||
switch (judgement.severity) {
|
other -= 20;
|
||||||
default:
|
break;
|
||||||
break;
|
}
|
||||||
case 0:
|
}
|
||||||
other -= 5;
|
}
|
||||||
break;
|
|
||||||
case 1:
|
const discordAccountCreatedAtTestDate = (new Date(new Date(new Date(member.user.createdAt)).setHours(730)));
|
||||||
other -= 10;
|
if (discordAccountCreatedAtTestDate > new Date()) {
|
||||||
break;
|
other -= 10;
|
||||||
case 2:
|
}
|
||||||
other -= 20;
|
|
||||||
break;
|
const joinedAtTestDate = (new Date(new Date(new Date(member.joinedAt)).setHours(420)));
|
||||||
}
|
if (joinedAtTestDate > new Date()) {
|
||||||
}
|
other -= 30;
|
||||||
}
|
}
|
||||||
|
|
||||||
const discordAccountCreatedAtTestDate = (new Date(new Date(new Date(member.user.createdAt)).setHours(730)));
|
total = Math.floor(((total + activity + roles + moderation + cloudServices + staff + other) * 5.13) * 1.87);
|
||||||
if (discordAccountCreatedAtTestDate > new Date()) {
|
|
||||||
other -= 10;
|
// client.queue.updateScore(score, total, activity, roles, moderation, cloudServices, other, staff);
|
||||||
}
|
|
||||||
|
client.db.Score.updateOne({ userID: score.userID }, { $set: { total, activity, roles, moderation, cloudServices, other, staff, lastUpdate: new Date() } }).exec();
|
||||||
const joinedAtTestDate = (new Date(new Date(new Date(member.joinedAt)).setHours(420)));
|
if (!score.pin || score.pin?.length < 1) {
|
||||||
if (joinedAtTestDate > new Date()) {
|
client.db.Score.updateOne({ userID: score.userID }, { $set: { pin: [this.client.util.randomNumber(100, 999), this.client.util.randomNumber(10, 99), this.client.util.randomNumber(1000, 9999)] } }).exec();
|
||||||
other -= 30;
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
total = Math.floor(((total + activity + roles + moderation + cloudServices + staff + other) * 5.13) * 1.87);
|
await start();
|
||||||
|
interval = setInterval(async () => {
|
||||||
client.queue.updateScore(score, total, activity, roles, moderation, cloudServices, other, staff);
|
await start();
|
||||||
}
|
}, 3600000);
|
||||||
};
|
return interval;
|
||||||
await start();
|
}
|
||||||
interval = setInterval(async () => {
|
|
||||||
await start();
|
|
||||||
}, 1800000);
|
|
||||||
return interval;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue