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

🎯 Simplify craftToken command #1723

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/classes/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export default class Commands {
this.buyBPTFPremiumCommand(steamID, message);
} else if (command === 'refreshschema' && isAdmin) {
this.manager.refreshSchema(steamID);
} else if (command === 'crafttoken' && isAdmin) {
} else if (['crafttoken', 'ct'].includes(command) && isAdmin) {
this.crafting.craftTokenCommand(steamID, message);
} else {
const custom = this.bot.options.customMessage.commandNotFound;
Expand Down
62 changes: 29 additions & 33 deletions src/classes/Commands/sub-classes/Crafting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {

const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);

const classes = ['scout', 'soldier', 'pyro', 'demoman', 'heavy', 'engineer', 'medic', 'sniper', 'spy'];
const slotType = ['primary', 'secondary', 'melee', 'pda2'];
const combineToken = classes.concat(slotType);

export default class CraftingCommands {
private craftWeaponsBySlot: CraftWeaponsBySlot;

Expand Down Expand Up @@ -47,56 +51,47 @@ export default class CraftingCommands {
return this.getCraftTokenInfo(steamID);
}

if (parts.length < 3) {
if (parts.length < 2) {
return this.bot.sendMessage(
steamID,
'❌ Wrong syntax. Correct syntax: !craftToken <tokenType> <subTokenType> <amount>' +
'\n - tokenType: "class" or "slot"' +
'\n - subTokenType: one of the 9 TF2 class characters if TokenType is class, or "primary"/"secondary"/"melee"/"pda2" if TokenType is slot' +
'❌ Wrong syntax. Correct syntax: !craftToken <tokenName> <amount>' +
'\n - TokenName: one of the 9 TF2 class characters, or "primary"/"secondary"/"melee"/"pda2" slot' +
'\n - amount: Must be an integer, or "max"'
);
}

const tokenType = parts[0];
const subTokenType = parts[1];
const amount: number | 'max' = parts[2] === 'max' ? 'max' : parseInt(parts[2]);
const tokenName = parts[0];
const amount: number | 'max' = parts[1] === 'max' ? 'max' : parseInt(parts[1]);

if (amount !== 'max') {
if (isNaN(amount)) {
return this.bot.sendMessage(steamID, '❌ Amount must be type integer!');
}
}

if (!['class', 'slot'].includes(tokenType)) {
return this.bot.sendMessage(steamID, '❌ tokenType must only be either "class" or "slot"!');
}

const classes = ['scout', 'soldier', 'pyro', 'demoman', 'heavy', 'engineer', 'medic', 'sniper', 'spy'];
const slotType = ['primary', 'secondary', 'melee', 'pda2'];

if (tokenType === 'class' && !classes.includes(subTokenType)) {
if (!combineToken.includes(tokenName)) {
return this.bot.sendMessage(
steamID,
'❌ subTokenType must be one of 9 TF2 class character since your tokenType is "class"!'
);
} else if (tokenType === 'slot' && !slotType.includes(subTokenType)) {
return this.bot.sendMessage(
steamID,
'❌ subTokenType must only be either "primary", "secondary", "melee", or "pda2" since your tokenType is "slot"!'
'❌ Invalid token name!' +
'\n• Slot: primary/secondary/melee/pda2' +
'\n• Classes: scout/soldier/pyro/demoman/heavy/engineer/medic/sniper/spy'
);
}

if (tokenType === 'slot') {
let isSlotToken = false;

if (slotType.includes(tokenName)) {
// only load on demand
isSlotToken = true;
this.defineCraftWeaponsBySlots();
}

const assetids: string[] = [];

const craftableItems = this.bot.inventoryManager.getInventory.getCurrencies(
tokenType === 'class'
? this.bot.craftWeaponsByClass[subTokenType as ClassesForCraftableWeapons]
: this.craftWeaponsBySlot[subTokenType as SlotsForCraftableWeapons],
!isSlotToken
? this.bot.craftWeaponsByClass[tokenName as ClassesForCraftableWeapons]
: this.craftWeaponsBySlot[tokenName as SlotsForCraftableWeapons],
false
);

Expand All @@ -115,21 +110,22 @@ export default class CraftingCommands {

const availableAmount = assetids.length;
const amountCanCraft = Math.floor(availableAmount / 3);
const capTokenName = tokenName === 'pda2' ? 'PDA2' : capitalize(tokenName);
const tokenType = isSlotToken ? 'slot' : 'class';
const capTokenType = capitalize(tokenType);
const capSubTokenType = subTokenType === 'pda2' ? 'PDA2' : capitalize(subTokenType);

if (amount === 'max' && amountCanCraft === 0) {
return this.bot.sendMessage(
steamID,
`❌ Unable to craft ${capTokenType} Token - ${capSubTokenType} since I only have of ${capSubTokenType} ${tokenType} items.`
`❌ Unable to craft ${capTokenType} Token - ${capTokenName} since I only have ${availableAmount} of ${capTokenName} ${capTokenType} items.`
);
}

if (amount !== 'max' && amount > amountCanCraft) {
return this.bot.sendMessage(
steamID,
`❌ I can only craft ${amountCanCraft} ${capTokenType} Token - ${capSubTokenType} at the moment, since I only ` +
`have ${availableAmount} of ${capSubTokenType} ${tokenType} items.`
`❌ I can only craft ${amountCanCraft} ${capTokenType} Token - ${capTokenName} at the moment, since I only ` +
`have ${availableAmount} of ${capTokenName} ${capTokenType} items.`
);
}

Expand All @@ -141,10 +137,10 @@ export default class CraftingCommands {
const amountToCraft = amount === 'max' ? amountCanCraft : amount;
for (let i = 0; i < amountToCraft; i++) {
const assetidsToCraft = assetids.splice(0, 3);
this.bot.tf2gc.craftToken(assetidsToCraft, tokenType as TokenType, subTokenType as SubTokenType, err => {
this.bot.tf2gc.craftToken(assetidsToCraft, tokenType as TokenType, tokenName as SubTokenType, err => {
if (err) {
log.debug(
`Error crafting ${assetidsToCraft.join(', ')} for ${capTokenType} Token - ${capSubTokenType}`
`Error crafting ${assetidsToCraft.join(', ')} for ${capTokenType} Token - ${capTokenName}`
);
crafted--;
}
Expand All @@ -163,13 +159,13 @@ export default class CraftingCommands {
if (crafted < amountToCraft) {
return this.bot.sendMessage(
steamID,
`✅ Successfully crafted ${crafted} ${capTokenType} Token - ${capSubTokenType} (there were some error while crafting).`
`✅ Successfully crafted ${crafted} ${capTokenType} Token - ${capTokenName} (there were some error while crafting).`
);
}

return this.bot.sendMessage(
steamID,
`✅ Successfully crafted ${crafted} ${capTokenType} Token - ${capSubTokenType}!`
`✅ Successfully crafted ${crafted} ${capTokenType} Token - ${capTokenName}!`
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Commands/sub-classes/Help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class HelpCommands {
'.\n✨=== Crafting ===✨\n- ' +
[
`${prefix}craftToken <info|check> - Check the availability to craft tokens ℹ️🔨`,
`${prefix}craftToken <tokenType> <subTokenType> <amount> - Craft Class or Slot Tokens 🔨`
`${prefix}craftToken <tokenName> <amount> - Craft Class or Slot Tokens 🔨`
].join('\n- ')
);

Expand Down
Loading