Skip to content

Commit

Permalink
Separate FSLoop utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
TAEMBO committed May 8, 2024
1 parent 2b5b8f8 commit 0ae9427
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
39 changes: 1 addition & 38 deletions src/util/fsLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,41 +262,4 @@ export async function fsLoop(client: TClient, watchList: WatchListDocument[], se
fsCacheServer.graphPoints.push(dss.slots.used);
fsCacheServer.players = newPlayers;
fsCacheServer.isThrottled = false;
}

export async function fsLoopAll(client: TClient, watchList: WatchListDocument[]) {
const embed = new EmbedBuilder().setColor(client.config.EMBED_COLOR);
const pausedStatuses: (boolean | null)[] = [];
const totalCount: number[] = [];

for (const [serverAcro, server] of Object.entries(client.fsCache)) {
const playerInfo: string[] = [];
const serverSlots = server.players.length;

totalCount.push(serverSlots);
pausedStatuses.push(server.isThrottled);

for (const player of server.players) {
const playTimeHrs = Math.floor(player.uptime / 60);
const playTimeMins = (player.uptime % 60).toString().padStart(2, "0");
let decorators = player.isAdmin ? ":detective:" : ""; // Tag for if player is admin

decorators += client.fmList.cache.includes(player.name) ? ":farmer:" : ""; // Tag for if player is FM
decorators += client.tfList.cache.includes(player.name) ? ":angel:" : ""; // Tag for if player is TF
decorators += client.whitelist.cache.includes(player.name) ? ":white_circle:" : ""; // Tag for if player is on whitelist
decorators += watchList.some(x => x._id === player.name) ? ":no_entry:" : ""; // Tag for if player is on watchList

playerInfo.push(`\`${player.name.slice(0, 46)}\` ${decorators} **|** ${playTimeHrs}:${playTimeMins}`);
}

if (playerInfo.length) embed.addFields({ name: `${serverAcro.toUpperCase()} - ${serverSlots}/16`, value: playerInfo.join("\n") });
}

// Throttle message updating if no changes in cached data
if (pausedStatuses.every(x => x)) return;

await client.getChan("juniorAdminChat").messages.edit(client.config.mainServer.fsLoopMsgId, {
content: `\`\`\`js\n["${client.whitelist.cache.join(", ")}"]\`\`\`Updates every 30 seconds`,
embeds: [embed.setTitle(totalCount.reduce((a, b) => a + b, 0) + " online")]
}).catch(() => log("Red", "FSLoopAll invalid msg"));
}
}
41 changes: 41 additions & 0 deletions src/util/fsLoopAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { EmbedBuilder } from "discord.js";
import type TClient from "../client.js";
import { log } from "../util/index.js";
import type { WatchListDocument } from "../typings.js";

export async function fsLoopAll(client: TClient, watchList: WatchListDocument[]) {
const embed = new EmbedBuilder().setColor(client.config.EMBED_COLOR);
const pausedStatuses: (boolean | null)[] = [];
const totalCount: number[] = [];

for (const [serverAcro, server] of Object.entries(client.fsCache)) {
const playerInfo: string[] = [];
const serverSlots = server.players.length;

totalCount.push(serverSlots);
pausedStatuses.push(server.isThrottled);

for (const player of server.players) {
const playTimeHrs = Math.floor(player.uptime / 60);
const playTimeMins = (player.uptime % 60).toString().padStart(2, "0");
let decorators = player.isAdmin ? ":detective:" : ""; // Tag for if player is admin

decorators += client.fmList.cache.includes(player.name) ? ":farmer:" : ""; // Tag for if player is FM
decorators += client.tfList.cache.includes(player.name) ? ":angel:" : ""; // Tag for if player is TF
decorators += client.whitelist.cache.includes(player.name) ? ":white_circle:" : ""; // Tag for if player is on whitelist
decorators += watchList.some(x => x._id === player.name) ? ":no_entry:" : ""; // Tag for if player is on watchList

playerInfo.push(`\`${player.name.slice(0, 46)}\` ${decorators} **|** ${playTimeHrs}:${playTimeMins}`);
}

if (playerInfo.length) embed.addFields({ name: `${serverAcro.toUpperCase()} - ${serverSlots}/16`, value: playerInfo.join("\n") });
}

// Throttle message updating if no changes in cached data
if (pausedStatuses.every(x => x)) return;

await client.getChan("juniorAdminChat").messages.edit(client.config.mainServer.fsLoopMsgId, {
content: `\`\`\`js\n["${client.whitelist.cache.join(", ")}"]\`\`\`Updates every 30 seconds`,
embeds: [embed.setTitle(totalCount.reduce((a, b) => a + b, 0) + " online")]
}).catch(() => log("Red", "FSLoopAll invalid msg"));
}
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./formatString.js";
export * from "./formatTime.js";
export * from "./formatUser.js";
export * from "./fsLoop.js";
export * from "./fsLoopAll.js";
export * from "./hasProfanity.js";
export * from "./hasRole.js";
export * from "./isDCStaff.js";
Expand Down

0 comments on commit 0ae9427

Please sign in to comment.