Skip to content

Battlemetrics Upcoming Wipe v2 #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions src/discordTools/SetupSettingsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ async function setupGeneralSettings(client, guildId, channel) {
Path.join(__dirname, '..', 'resources/images/settings_logo.png'))]
});

await client.messageSend(channel, {
embeds: [DiscordEmbeds.getEmbed({
color: Constants.COLOR_SETTINGS,
title: client.intlGet(guildId, 'displayInformationBattlemetricsUpcomingWipes'),
thumbnail: `attachment://settings_logo.png`
})],
components: [DiscordButtons.getDisplayInformationBattlemetricsUpcomingWipesButton(guildId,
instance.generalSettings.displayInformationBattlemetricsUpcomingWipes)],
files: [new Discord.AttachmentBuilder(
Path.join(__dirname, '..', 'resources/images/settings_logo.png'))]
});

await client.messageSend(channel, {
embeds: [DiscordEmbeds.getEmbed({
color: Constants.COLOR_SETTINGS,
Expand Down
10 changes: 10 additions & 0 deletions src/discordTools/discordButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,16 @@ module.exports = {
style: enabled ? SUCCESS : DANGER
}));
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little space please :D

getDisplayInformationBattlemetricsUpcomingWipesButton: function (guildId, enabled) {
return new Discord.ActionRowBuilder().addComponents(
module.exports.getButton({
customId: 'DisplayInformationBattlemetricsUpcomingWipes',
label: enabled ?
Client.client.intlGet(guildId, 'enabledCap') :
Client.client.intlGet(guildId, 'disabledCap'),
style: enabled ? SUCCESS : DANGER
}));
},

getSubscribeToChangesBattlemetricsButtons: function (guildId) {
const instance = Client.client.getInstance(guildId);
Expand Down
46 changes: 41 additions & 5 deletions src/discordTools/discordEmbeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,40 @@ module.exports = {
const guildId = rustplus.guildId;
const instance = Client.client.getInstance(guildId);

const emptyFieldValue = { name: '\u200B', value: '\u200B', inline: true };
let firstWipe = emptyFieldValue;
let secondWipe = emptyFieldValue;

const battlemetricsId = instance.activeServer !== null ?
instance.serverList[instance.activeServer].battlemetricsId : null;

if (battlemetricsId && instance.generalSettings.displayInformationBattlemetricsUpcomingWipes){
const bmInstance = Client.client.battlemetricsInstances[battlemetricsId];
const upcomingWipes = bmInstance.getUpcomingWipesOrderedByTime();

if (upcomingWipes.length > 0){
const closestWipe = upcomingWipes[0];
if (closestWipe.type === 'map' || closestWipe.type === 'full') {
/* try match next map or full wipe*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some indentation here please

const nextMapWipe = upcomingWipes.find((upcoming)=> upcoming.type === 'map')
if (nextMapWipe){
firstWipe = { name: Client.client.intlGet(guildId, 'nextMapWipe'), value: `<t:${nextMapWipe.discordTimestamp}:R>`, inline: true}
}
const nextFullWipe = upcomingWipes.find((upcoming)=> upcoming.type === 'full')
if (nextFullWipe){
secondWipe = { name: Client.client.intlGet(guildId, 'nextFullWipe'), value: `<t:${nextFullWipe.discordTimestamp}:R> `, inline: true }
}
} else {
/*if closest wipe is not matched type*/
firstWipe = {
name: Client.client.intlGet(guildId, 'nextWipe'),
value: `<t:${closestWipe.discordTimestamp}:R> `,
inline: true
}
}
}
}

const time = rustplus.getCommandTime(true);
const timeLeftTitle = Client.client.intlGet(rustplus.guildId, 'timeTill', {
event: rustplus.time.isDay() ? Constants.NIGHT_EMOJI : Constants.DAY_EMOJI
Expand All @@ -719,18 +753,20 @@ module.exports = {
fields: [
{ name: playersFieldName, value: `\`${rustplus.getCommandPop(true)}\``, inline: true },
{ name: timeFieldName, value: `\`${time[0]}\``, inline: true },
{ name: wipeFieldName, value: `\`${rustplus.getCommandWipe(true)}\``, inline: true }],
time[1] ? { name: timeLeftTitle, value: `\`${time[1]}\``, inline: true } : emptyFieldValue,
],
timestamp: true
});

if (time[1] !== null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this dependent on time?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched field Time till with Wipe field so wipe info is on second row and Players, Time and Time till are on first row. So this row you pointed is field Time till (if you compare actual bot with images i add to pull request you will see).

If you want it in different order i can fix it however you like.

Copy link
Owner

@alexemanuelol alexemanuelol Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I dont understand is why you write out wipe stuff only if time[1] is present...

This part:

if (time[1] !== null) { embed.addFields( { name: wipeFieldName, value: "${rustplus.getCommandWipe(true)}", inline: true }, firstWipe, secondWipe, ); } else { embed.addFields(emptyFieldValue); }

embed.addFields(
{ name: timeLeftTitle, value: `\`${time[1]}\``, inline: true },
{ name: '\u200B', value: '\u200B', inline: true },
{ name: '\u200B', value: '\u200B', inline: true });
{ name: wipeFieldName, value: `\`${rustplus.getCommandWipe(true)}\``, inline: true },
firstWipe,
secondWipe,
);
}
else {
embed.addFields({ name: '\u200B', value: '\u200B', inline: false });
embed.addFields(emptyFieldValue);
}

embed.addFields(
Expand Down
18 changes: 18 additions & 0 deletions src/handlers/buttonHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,24 @@ module.exports = async (client, interaction) => {
instance.generalSettings.displayInformationBattlemetricsAllOnlinePlayers)]
});
}
else if (interaction.customId === 'DisplayInformationBattlemetricsUpcomingWipes') {
instance.generalSettings.displayInformationBattlemetricsUpcomingWipes =
!instance.generalSettings.displayInformationBattlemetricsUpcomingWipes;
client.setInstance(guildId, instance);

if (rustplus) rustplus.generalSettings.displayInformationBattlemetricsUpcomingWipes =
instance.generalSettings.displayInformationBattlemetricsUpcomingWipes;

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'buttonValueChange', {
id: `${verifyId}`,
value: `${instance.generalSettings.displayInformationBattlemetricsUpcomingWipes}`
}));

await client.interactionUpdate(interaction, {
components: [DiscordButtons.getDisplayInformationBattlemetricsUpcomingWipesButton(guildId,
instance.generalSettings.displayInformationBattlemetricsUpcomingWipes)]
});
}
else if (interaction.customId === 'BattlemetricsServerNameChanges') {
instance.generalSettings.battlemetricsServerNameChanges =
!instance.generalSettings.battlemetricsServerNameChanges;
Expand Down
4 changes: 4 additions & 0 deletions src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@
"discordCap": "DISCORD",
"discordUsers": "Discord Users",
"displayInformationBattlemetricsAllOnlinePlayers": "Should all online players from Battlemetrics be displayed in the information channel?",
"displayInformationBattlemetricsUpcomingWipes": "Should be closest wipe from Battlemetrics be displayed in the information channel?",
"displayingMap": "Displaying {mapName} map.",
"displayingOnlinePlayers": "Displaying online players.",
"distanceDirectionGrid": "{distance}m in direction {direction}° [{grid}].",
Expand Down Expand Up @@ -484,6 +485,9 @@
"new": "New",
"newVendingMachine": "New Vending Machine located at {location}.",
"newsCap": "NEWS",
"nextFullWipe": "Next full wipe",
"nextMapWipe": "Next map wipe",
"nextWipe": "Next wipe",
"noActiveTimers": "No active timers.",
"noCommandDelay": "No command delay.",
"noCommunicationSmartSwitch": "Could not communicate with Smart Switch: {name}",
Expand Down
20 changes: 20 additions & 0 deletions src/structures/Battlemetrics.js
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should consider adding server_rust_wipes to evaluation function. But from testing it looks like a few more elements have been added to the API. I could fix it.

Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Battlemetrics {
this.server_rust_last_wipe = null;
this.server_rust_last_wipe_ent = null;
this.server_serverSteamId = null;
this.server_rust_wipes = null;
this.map_url = null;
this.map_thumbnailUrl = null;
this.map_monuments = null;
Expand Down Expand Up @@ -153,6 +154,7 @@ class Battlemetrics {
set offlinePlayers(offlinePlayers) { this._offlinePlayers = offlinePlayers; }
get serverEvaluation() { return this._serverEvaluation; }
set serverEvaluation(serverEvaluation) { this._serverEvaluation = serverEvaluation; }
get rustWipes() { return this.server_rust_wipes; }

/**
* Construct the Battlemetrics API call for searching servers by name.
Expand Down Expand Up @@ -667,6 +669,11 @@ class Battlemetrics {
if (content['status'] === false) this.offlinePlayers.push(playerId);
}

const rustWipes = details.rust_wipes;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place this closer to the other evaluation checks, below the rustmap evaluation check instead of down here

if (rustWipes) {
this.#evaluateServerParameter('server_rust_wipes', this.server_rust_wipes, rustWipes, firstTime);
}

this.update(data);
return true;
}
Expand Down Expand Up @@ -725,6 +732,7 @@ class Battlemetrics {
this.server_rust_last_wipe = details.rust_last_wipe;
this.server_rust_last_wipe_ent = details.rust_last_wipe_ent;
this.server_serverSteamId = details.serverSteamId;
this.server_rust_wipes = details.rust_wipes

const rustMaps = details.rust_maps;
if (rustMaps) {
Expand Down Expand Up @@ -801,6 +809,18 @@ class Battlemetrics {
return ordered.map(e => e[1]);
}

getUpcomingWipesOrderedByTime() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this run even if there is a server that does not give any details about upcoming wipes? Or is rust_wipes always available in the battlemetrics API call?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a comment header for the function

const unordered = [];
for (const wipe of this.rustWipes) {
const timestampDate = new Date(wipe.timestamp)
wipe.discordTimestamp = timestampDate.getTime() / 1000;
const timestampSplit = timestampDate.toISOString().split('T')
wipe.readableTimestamp = timestampSplit.length === 2 ?`${timestampSplit[0]} T ${timestampSplit[1]}`: timestampDate.toISOString()
unordered.push(wipe);
}
return unordered.sort(function (a, b) { return a.discordTimestamp - b.discordTimestamp })
}

/**
* Get the offline time from a player.
* @param {string} playerId The id of the player to get offline time from.
Expand Down
1 change: 1 addition & 0 deletions src/templates/generalSettingsTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"mapWipeNotifyEveryone": false,
"itemAvailableInVendingMachineNotifyInGame": true,
"displayInformationBattlemetricsAllOnlinePlayers": false,
"displayInformationBattlemetricsUpcomingWipes": false,
"battlemetricsServerNameChanges": true,
"battlemetricsTrackerNameChanges": true,
"battlemetricsGlobalNameChanges": false,
Expand Down