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

use same code for equipping 1 or many items #6249

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 14 additions & 66 deletions src/mahoji/lib/abstracted_commands/gearCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ChatInputCommandInteraction } from 'discord.js';
import { objectValues } from 'e';
import { Bank } from 'oldschooljs';

import { MAX_INT_JAVA, PATRON_ONLY_GEAR_SETUP } from '../../../lib/constants';
import { PATRON_ONLY_GEAR_SETUP } from '../../../lib/constants';
import { generateAllGearImage, generateGearImage } from '../../../lib/gear/functions/generateGearImage';
import type { GearSetup, GearSetupType } from '../../../lib/gear/types';
import { GearStat } from '../../../lib/gear/types';
Expand All @@ -19,7 +19,6 @@ import { gearEquipMultiImpl } from '../../../lib/util/equipMulti';
import { getItem } from '../../../lib/util/getOSItem';
import { handleMahojiConfirmation } from '../../../lib/util/handleMahojiConfirmation';
import { minionIsBusy } from '../../../lib/util/minionIsBusy';
import { mahojiParseNumber } from '../../mahojiSettings';

import { getSimilarItems } from '../../../lib/data/similarItems';

Expand Down Expand Up @@ -119,19 +118,8 @@ async function gearPresetEquipCommand(user: MUser, gearSetup: string, presetName
};
}

async function gearEquipMultiCommand(
user: MUser,
interaction: ChatInputCommandInteraction,
setup: string,
items: string
) {
async function gearEquipMultiCommand(user: MUser, setup: string, items: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The interaction parameter was removed from gearEquipMultiCommand, but the confirmation logic for wildy setup was not replaced. Consider re-adding user confirmation for equipping items in the wildy setup.

if (!isValidGearSetup(setup)) return 'Invalid gear setup.';
if (setup === 'wildy') {
await handleMahojiConfirmation(
interaction,
"You're trying to equip items into your *wildy* setup. ANY item in this setup can potentially be lost if doing Wilderness activities. Please confirm you understand this."
);
}

// We must update the user after any confirmation because the bank/gear could change from something else.
await user.sync();
Expand Down Expand Up @@ -161,6 +149,12 @@ async function gearEquipMultiCommand(
if (skillFailBank!.length > 0) {
content += `\nThese items failed to be equipped as you don't have the requirements: ${skillFailBank}.`;
}

if (setup === 'wildy') {
content +=
"\n\nYou're trying to equip items into your *wildy* setup. ANY item in this setup can potentially be lost if doing Wilderness activities. Please confirm you understand this.";
}

return {
content,
files: [{ name: 'gear.jpg', attachment: image }]
Expand All @@ -178,15 +172,15 @@ export async function gearEquipCommand(args: {
unEquippedItem: Bank | undefined;
auto: string | undefined;
}): CommandResponse {
const { interaction, userID, setup, item, items, preset, quantity: _quantity, auto } = args;
const { userID, setup, item, items, preset, quantity: _quantity, auto } = args;
if (!isValidGearSetup(setup)) return 'Invalid gear setup.';
const user = await mUserFetch(userID);
if (minionIsBusy(user.id)) {
return `${user.minionName} is currently out on a trip, so you can't change their gear!`;
}

if (items) {
return gearEquipMultiCommand(user, interaction, setup, items);
return gearEquipMultiCommand(user, setup, items);
}
if (setup === 'other' && user.perkTier() < PerkTier.Four) {
return PATRON_ONLY_GEAR_SETUP;
Expand All @@ -197,59 +191,13 @@ export async function gearEquipCommand(args: {
if (auto) {
return autoEquipCommand(user, setup, auto);
}
const itemToEquip = getItem(item);
if (!itemToEquip) return "You didn't supply the name of an item or preset you want to equip.";
const quantity = mahojiParseNumber({ input: _quantity ?? 1, min: 1, max: MAX_INT_JAVA }) ?? 1;
if (!itemToEquip.equipable_by_player || !itemToEquip.equipment) return "This item isn't equipable.";

const bank = new Bank(user.bank);
const cost = new Bank().add(itemToEquip.id, quantity);
if (!bank.has(cost)) return `You don't own ${cost}.`;

const dbKey = `gear_${setup}` as const;
const allGear = user.gear;
const currentEquippedGear = allGear[setup];

if (!itemToEquip.stackable && quantity > 1) {
return "You can't equip more than 1 of this item at once, as it isn't stackable!";
}

if (itemToEquip.equipment.requirements) {
if (!user.hasSkillReqs(itemToEquip.equipment.requirements)) {
return `You can't equip a ${
itemToEquip.name
} because you don't have the required stats: ${formatSkillRequirements(
itemToEquip.equipment.requirements
)}.`;
}
if (!item) {
return 'You need to specify an item to equip.';
}

if (setup === 'wildy') {
await handleMahojiConfirmation(
interaction,
"You are equipping items to your **wilderness** setup. *Every* item in this setup can potentially be lost if you're doing activities in the wilderness. Are you sure you want to equip it?"
);
}

const result = currentEquippedGear.equip(itemToEquip, quantity);

await transactItems({
userID: user.id,
collectionLog: false,
dontAddToTempCL: true,
itemsToAdd: result.refundBank ?? undefined,
itemsToRemove: cost
});

const { newUser } = await user.update({
[dbKey]: currentEquippedGear.raw()
});
const image = await generateGearImage(user, newUser[dbKey] as GearSetup, setup, user.user.minion_equippedPet);

return {
content: `You equipped ${itemToEquip.name} in your ${toTitleCase(setup)} setup.`,
files: [{ attachment: image, name: 'osbot.png' }]
};
// They are trying to equip 1 item
return gearEquipMultiCommand(user, setup, item);
}

export async function gearUnequipCommand(
Expand Down
Loading