Skip to content
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

Battlemetrics Upcoming Wipe v2 #305

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions src/discordTools/SetupSettingsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ async function setupGeneralSettings(client, guildId, channel) {
files: [new Discord.AttachmentBuilder(
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({
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.

To make me happy, use /**/

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
5 changes: 4 additions & 1 deletion src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -766,5 +766,8 @@
"wipeDetected": "Wipe detected!",
"yield": "Yield",
"youAreAlreadyLeader": "You are already leader.",
"youAreNotPairedWithServer": "Leader command does not work because you're not paired with the server."
Copy link
Owner

Choose a reason for hiding this comment

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

phrases should be in order (alphabetical)

Copy link
Owner

Choose a reason for hiding this comment

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

Dont remove "youAreNotPairedWithServer"

"nextFullWipe": "Next full wipe",
"nextMapWipe": "Next map wipe",
"nextWipe": "Next wipe",
"displayInformationBattlemetricsUpcomingWipes": "Should be closest wipe from Battlemetrics be displayed in the information channel?"
}
15 changes: 15 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 @@ -725,6 +727,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 +804,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
6 changes: 4 additions & 2 deletions src/util/CreateInstanceFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module.exports = (client, guild) => {
server: null,
event: null,
team: null,
battlemetricsPlayers: null
battlemetricsPlayers: null,
battlemetricsUpcomingWipes:null
Copy link
Owner

Choose a reason for hiding this comment

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

Remove this

},
activeServer: null,
serverList: {},
Expand Down Expand Up @@ -153,7 +154,8 @@ module.exports = (client, guild) => {
server: null,
event: null,
team: null,
battlemetricsPlayers: null
battlemetricsPlayers: null,
battlemetricsUpcomingWipes:null
Copy link
Owner

Choose a reason for hiding this comment

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

Remove this

}
}
else {
Expand Down