Skip to content

Commit

Permalink
Fix a basic functionality for recycle stuff again
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Jul 19, 2024
1 parent 0d2a720 commit f9e9a5f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 52 deletions.
16 changes: 13 additions & 3 deletions src/commands/recycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ module.exports = {
.addIntegerOption(option => option
.setName('quantity')
.setDescription(client.intlGet(guildId, 'commandsRecycleQuantityDesc'))
.setRequired(false));
.setRequired(false))
.addStringOption(option => option
.setName('recycler-type')
.setDescription(client.intlGet(guildId, 'commandsRecycleRecyclerTypeDesc'))
.setRequired(false)
.addChoices(
{ name: client.intlGet(guildId, 'recycler'), value: 'recycler' },
{ name: client.intlGet(guildId, 'shredder'), value: 'shredder' },
{ name: client.intlGet(guildId, 'safe-zone-recycler'), value: 'safe-zone-recycler' }));
},

async execute(client, interaction) {
Expand All @@ -56,6 +64,7 @@ module.exports = {
const recycleItemName = interaction.options.getString('name');
const recycleItemId = interaction.options.getString('id');
const recycleItemQuantity = interaction.options.getInteger('quantity');
const recycleItemRecyclerType = interaction.options.getString('recycler-type');

let itemId = null;
if (recycleItemName !== null) {
Expand Down Expand Up @@ -104,13 +113,14 @@ module.exports = {
}

const quantity = recycleItemQuantity === null ? 1 : recycleItemQuantity;
const recyclerType = recycleItemRecyclerType === null ? 'recycler' : recycleItemRecyclerType;

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `${recycleItemName} ${recycleItemId} ${recycleItemQuantity}`
value: `${recycleItemName} ${recycleItemId} ${recycleItemQuantity} ${recycleItemRecyclerType}`
}));

await DiscordMessages.sendRecycleMessage(interaction, recycleDetails, quantity);
await DiscordMessages.sendRecycleMessage(interaction, recycleDetails, quantity, recyclerType);
client.log(client.intlGet(null, 'infoCap'), client.intlGet(guildId, 'commandsRecycleDesc'));
},
};
13 changes: 7 additions & 6 deletions src/discordTools/discordEmbeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ module.exports = {
const grid = entity.location !== null ? ` (${entity.location})` : '';

let itemName = '', itemQuantity = '';
for (const item of items) {
for (const item of items['recycler']) {
itemName += `\`${Client.client.items.getName(item.itemId)}\`\n`;
itemQuantity += `\`${item.quantity}\`\n`;
}
Expand Down Expand Up @@ -1141,21 +1141,22 @@ module.exports = {
});
},

getRecycleEmbed: function (guildId, recycleDetails, quantity) {
const title = quantity === 1 ? `${recycleDetails[1].name}` : `${recycleDetails[1].name} x${quantity}`;
getRecycleEmbed: function (guildId, recycleDetails, quantity, recyclerType) {
let title = quantity === 1 ? `${recycleDetails[1].name}` : `${recycleDetails[1].name} x${quantity}`;
title += ` (${Client.client.intlGet(guildId, recyclerType)})`;

const recycleData = Client.client.rustlabs.getRecycleDataFromArray([
{ itemId: recycleDetails[0], quantity: quantity, itemIsBlueprint: false }
]);

let items0 = '', quantities0 = '';
for (const item of recycleDetails[2]) {
for (const item of recycleDetails[2][recyclerType]['yield']) {
items0 += `${Client.client.items.getName(item.id)}\n`;
quantities0 += (item.probability !== 1) ? `${parseInt(item.probability * 100)}%\n` : `${item.quantity}\n`;
}

let items1 = '', quantities1 = '';
for (const item of recycleData) {
for (const item of recycleData[recyclerType]) {
items1 += `${Client.client.items.getName(item.itemId)}\n`;
quantities1 += `${item.quantity}\n`;
}
Expand Down Expand Up @@ -1335,7 +1336,7 @@ module.exports = {

const recycleDetails = type === 'items' ? Client.client.rustlabs.getRecycleDetailsById(itemId) : null;
if (recycleDetails !== null) {
const details = recycleDetails[2];
const details = recycleDetails[2]['recycler']['yield'];

let recycleString = '';
for (const recycleItem of details) {
Expand Down
4 changes: 2 additions & 2 deletions src/discordTools/discordMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ module.exports = {
await Client.client.interactionEditReply(interaction, content);
},

sendRecycleMessage: async function (interaction, recycleDetails, quantity) {
sendRecycleMessage: async function (interaction, recycleDetails, quantity, recyclerType) {
const content = {
embeds: [DiscordEmbeds.getRecycleEmbed(interaction.guildId, recycleDetails, quantity)],
embeds: [DiscordEmbeds.getRecycleEmbed(interaction.guildId, recycleDetails, quantity, recyclerType)],
ephemeral: true
}

Expand Down
4 changes: 4 additions & 0 deletions src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"commandsPlayersStatusDesc": "Search for players that are online/offline/any.",
"commandsRecycleDesc": "Display the output of recycling an item.",
"commandsRecycleQuantityDesc": "The quantity of items to recycle.",
"commandsRecycleRecyclerTypeDesc": "The recycler type (recycler, shredder, safe-zone-recycler).",
"commandsResearchDesc": "Display the cost to research an item.",
"commandsResetAlarmsDesc": "Reset alarms channel.",
"commandsResetDesc": "Reset Discord channels.",
Expand Down Expand Up @@ -581,6 +582,7 @@
"reconnectingToServer": "RECONNECTING TO SERVER...",
"recycle": "Recycle",
"recycleCap": "RECYCLE",
"recycler": "Recycler",
"remain": "left",
"removePlayerCap": "REMOVE PLAYER",
"removeSwitchCap": "REMOVE SWITCH",
Expand All @@ -597,6 +599,7 @@
"roleSet": "rustplusplus role has been set to {name}.",
"rustMonument": "Rust Monument",
"rustplusOperational": "RUSTPLUS OPERATIONAL.",
"safe-zone-recycler": "Safe Zone Recycler",
"samsite": "SAM site",
"satelliteDish": "Satellite Dish",
"scrap": "Scrap",
Expand Down Expand Up @@ -633,6 +636,7 @@
"shouldSmartSwitchNotifyInGameWhenChangedFromDiscord": "Should Smart Switches and Smart Switch Groups notify In-Game when they are changed from discord?",
"showingBlacklist": "Showing the blacklist.",
"showingSubscriptionList": "Showing the subscription list.",
"shredder": "Shredder",
"sirenLight": "Siren Light",
"six": "Six",
"slash": "Slash",
Expand Down
89 changes: 49 additions & 40 deletions src/structures/RustLabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ class RustLabs {
/**
* Get recycle data from an array of items.
* @param {array} items The array of items (every item include itemId, quantity, itemIsBlueprint).
* @return {array} An array of the output of recycling the items (every item include itemId, quantity,
* itemIsBlueprint).
* @return {Object} An object with recycler, shredder and safe-zone-recycler recycle data where
* the all recycled items have (itemId, quantity, itemIsBlueprint).
*/
getRecycleDataFromArray(items) {
/* Remove element duplicates */
Expand All @@ -360,54 +360,63 @@ class RustLabs {
}
items = mergedItems.slice();

let recycleData = items.slice();
while (true) {
let noMoreIterations = true;
const recycleData = new Object();
recycleData['recycler'] = [];
recycleData['shredder'] = [];
recycleData['safe-zone-recycler'] = [];

const expandedItems = [];
for (const item of recycleData) {
if (!this.hasRecycleDetails(item.itemId)) {
expandedItems.push(item);
continue;
}
for (const recyclerType in recycleData) {
let recycledItems = items.slice();
while (true) {
let noMoreIterations = true;

/* Can the item be recycled further? */
if (this.recycleData[item.itemId].length > 0 && !item.itemIsBlueprint &&
!IGNORED_RECYCLE_ITEMS.includes(item.itemId)) {
noMoreIterations = false;
for (const recycleItem of this.recycleData[item.itemId]) {
for (let i = 0; i < item.quantity; i++) {
if (recycleItem.probability < 1 && Math.random() * 1 > recycleItem.probability) continue;

const found = expandedItems.find(e => e.itemId === recycleItem.id);
if (found === undefined) {
expandedItems.push({
itemId: recycleItem.id,
quantity: recycleItem.quantity,
itemIsBlueprint: false
});
}
else {
found.quantity += recycleItem.quantity;
const expandedItems = [];
for (const item of recycledItems) {
if (!this.hasRecycleDetails(item.itemId)) {
expandedItems.push(item);
continue;
}

/* Can the item be recycled further? */
if (this.recycleData[item.itemId][recyclerType]['yield'].length > 0 && !item.itemIsBlueprint &&
!IGNORED_RECYCLE_ITEMS.includes(item.itemId)) {
noMoreIterations = false;
for (const recycleItem of this.recycleData[item.itemId][recyclerType]['yield']) {
for (let i = 0; i < item.quantity; i++) {
if (recycleItem.probability < 1 && Math.random() * 1 > recycleItem.probability) continue;

const found = expandedItems.find(e => e.itemId === recycleItem.id);
if (found === undefined) {
expandedItems.push({
itemId: recycleItem.id,
quantity: recycleItem.quantity,
itemIsBlueprint: false
});
}
else {
found.quantity += recycleItem.quantity;
}
}
}
}
}
else {
const found = expandedItems.find(e => e.itemId === item.itemId &&
e.itemIsBlueprint === item.itemIsBlueprint);
if (found === undefined) {
expandedItems.push(item);
}
else {
found.quantity += item.quantity;
const found = expandedItems.find(e => e.itemId === item.itemId &&
e.itemIsBlueprint === item.itemIsBlueprint);
if (found === undefined) {
expandedItems.push(item);
}
else {
found.quantity += item.quantity;
}
}
}
}

recycleData = expandedItems.slice();
recycledItems = expandedItems.slice();

if (noMoreIterations) break;
}

if (noMoreIterations) break;
recycleData[recyclerType] = recycledItems;
}

return recycleData;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/RustPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ class RustPlus extends RustPlusLib {
]);

let str = `${itemName}: `;
for (const item of recycleData) {
for (const item of recycleData['recycler']) {
str += `${Client.client.items.getName(item.itemId)} x${item.quantity}, `;
}
str = str.slice(0, -2);
Expand Down

0 comments on commit f9e9a5f

Please sign in to comment.