Skip to content

Commit

Permalink
Your commit message here
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexify4103 authored and SxMAbel committed Oct 19, 2024
1 parent 403bd03 commit d4488a0
Showing 1 changed file with 30 additions and 51 deletions.
81 changes: 30 additions & 51 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Manager extends EventEmitter {
textChannel: state.options.textChannel,
voiceChannel: state.options.voiceChannel,
selfDeafen: state.options.selfDeafen,
volume: state.options.volume,
volume: lavaPlayer.volume || state.options.volume,
};

this.create(playerOptions);
Expand Down Expand Up @@ -303,72 +303,51 @@ export class Manager extends EventEmitter {
return this.options.usePriority ? this.priorityNode : this.options.useNode === "leastLoad" ? this.leastLoadNode.first() : this.leastPlayersNode.first();
}

private lastProcessedGuilds: Set<string> = new Set();
private lastSaveTimes: Map<string, number> = new Map();
private saveInterval: number = 1000;
private saveQueues: Map<string, Player[]> = new Map();
private eventBatchInterval: NodeJS.Timeout | null = null;
private eventBatchDuration: number = 1000;
private latestPlayerStates: Map<string, Player> = new Map();

/** Register savePlayerStates events */
private registerPlayerStateEvents(): void {
const events: (keyof ManagerEvents)[] = ["playerStateUpdate", "playerDestroy"];

for (const event of events) {
this.on(event, (player: Player) => this.handleEvent(event, player));
this.on(event, (player: Player) => this.collectPlayerStateEvent(event, player));
}
}

private handleEvent(event: keyof ManagerEvents, player: Player): void {
switch (event) {
case "playerDestroy":
this.lastSaveTimes.delete(player.guild);
this.players.delete(player.guild);
this.cleanupInactivePlayers();
break;
case "playerStateUpdate":
this.queuePlayerStateSave(player);
break;
default:
this.savePlayerState(player.guild);
break;
/** Collects player state events */
private collectPlayerStateEvent(event: keyof ManagerEvents, player: Player): void {
if (event === "playerDestroy") {
this.lastSaveTimes.delete(player.guild);
this.players.delete(player.guild);
this.cleanupInactivePlayers();
} else if (event === "playerStateUpdate") {
// Store the latest player state for the guild
this.latestPlayerStates.set(player.guild, player);
}
}

/** Queues a player state save */
private queuePlayerStateSave(player: Player): void {
const guildId = player.guild;

// If the current guild is not being processed, save immediately
if (!this.lastProcessedGuilds.has(guildId)) {
this.lastProcessedGuilds.add(guildId);
this.savePlayerState(guildId);

setTimeout(() => {
this.lastProcessedGuilds.delete(guildId);
this.processNextQueue(guildId);
}, this.saveInterval);
} else {
if (!this.saveQueues.has(guildId)) {
this.saveQueues.set(guildId, []);
}

this.saveQueues.get(guildId)!.push(player);
// Start the batch timer if it's not already running
if (!this.eventBatchInterval) {
this.eventBatchInterval = setTimeout(() => this.processBatchEvents(), this.eventBatchDuration);
}
}

/** Processes the next queued save for a specific guild */
private processNextQueue(guildId: string): void {
const queue = this.saveQueues.get(guildId);
if (queue && queue.length > 0) {
const player = queue.shift()!;
this.savePlayerState(player.guild);
/** Processes the collected player state events */
private processBatchEvents(): void {
if (this.eventBatchInterval) {
clearTimeout(this.eventBatchInterval);
this.eventBatchInterval = null;
}

if (queue.length === 0) {
this.saveQueues.delete(guildId);
}
// Save the latest player states for each guild
this.latestPlayerStates.forEach((player, guildId) => {
this.savePlayerState(guildId); // Perform a single write operation
});

setTimeout(() => this.processNextQueue(guildId), this.saveInterval);
} else {
this.lastProcessedGuilds.delete(guildId);
}
// Clear the latest player states after processing
this.latestPlayerStates.clear();
}

/**
Expand Down

0 comments on commit d4488a0

Please sign in to comment.