Skip to content

Commit

Permalink
Merge pull request #21 from idinium96/revert-18-revert-17-construct-o…
Browse files Browse the repository at this point in the history
…ffer-craftweapons

Revert "Revert "Improved feature: Construct offer with craft/uncraft weapons""
  • Loading branch information
idinium96 authored Jun 26, 2020
2 parents d22ac7a + bc742f8 commit ce70745
Show file tree
Hide file tree
Showing 5 changed files with 2,278 additions and 57 deletions.
121 changes: 121 additions & 0 deletions src/classes/AdminCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,125 @@ class AdminCart extends Cart {
});
});
}

constructOfferWithWeapons(): Promise<string> {
return new Promise((resolve, reject) => {
if (this.isEmpty()) {
return reject('cart is empty');
}

const offer = this.bot.manager.createOffer(this.partner);

const alteredMessages: string[] = [];

// Add our items
const ourInventory = this.bot.inventoryManager.getInventory();

for (const sku in this.our) {
if (!Object.prototype.hasOwnProperty.call(this.our, sku)) {
continue;
}

let amount = this.getOurCount(sku);
const ourAssetids = ourInventory.findBySKU(sku, true);

if (amount > ourAssetids.length) {
amount = ourAssetids.length;
// Remove the item from the cart
this.removeOurItem(sku);

if (ourAssetids.length === 0) {
alteredMessages.push(
"I don't have any " + pluralize(this.bot.schema.getName(SKU.fromString(sku), false))
);
} else {
alteredMessages.push(
'I only have ' +
pluralize(this.bot.schema.getName(SKU.fromString(sku), false), ourAssetids.length, true)
);

// Add the max amount to the offer
this.addOurItem(sku, ourAssetids.length);
}
}

for (let i = 0; i < amount; i++) {
offer.addMyItem({
appid: 440,
contextid: '2',
assetid: ourAssetids[i]
});
}
}

if (Object.keys(this.their).length === 0) {
// Done constructing offer

offer.data('dict', { our: this.our, their: this.their });

this.offer = offer;

return resolve(alteredMessages.length === 0 ? undefined : alteredMessages.join(', '));
}

// Load their inventory

const theirInventory = new Inventory(this.partner, this.bot.manager, this.bot.schema);

theirInventory.fetch().asCallback(err => {
if (err) {
return reject('Failed to load inventories (Steam might be down)');
}

// Add their items

for (const sku in this.their) {
if (!Object.prototype.hasOwnProperty.call(this.their, sku)) {
continue;
}

let amount = this.getTheirCount(sku);
const theirAssetids = theirInventory.findBySKU(sku, true);

if (amount > theirAssetids.length) {
amount = theirAssetids.length;
// Remove the item from the cart
this.removeTheirItem(sku);

if (theirAssetids.length === 0) {
alteredMessages.push(
"you don't have any " + pluralize(this.bot.schema.getName(SKU.fromString(sku), false))
);
} else {
alteredMessages.push(
'you only have ' +
pluralize(
this.bot.schema.getName(SKU.fromString(sku), false),
theirAssetids.length,
true
)
);

// Add the max amount to the offer
this.addTheirItem(sku, theirAssetids.length);
}
}

for (let i = 0; i < amount; i++) {
offer.addTheirItem({
appid: 440,
contextid: '2',
assetid: theirAssetids[i]
});
}
}

offer.data('dict', { our: this.our, their: this.their });

this.offer = offer;

return resolve(alteredMessages.length === 0 ? undefined : alteredMessages.join(', '));
});
});
}
}
127 changes: 126 additions & 1 deletion src/classes/Cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,100 @@ abstract class Cart {
return summary;
}

summarizeWithWeapons(): string {
const ourSummary = this.summarizeOurWithWeapons();

let ourSummaryString: string;

if (ourSummary.length > 1) {
ourSummaryString =
ourSummary.slice(0, ourSummary.length - 1).join(', ') + ' and ' + ourSummary[ourSummary.length - 1];
} else if (ourSummary.length === 0) {
ourSummaryString = 'nothing';
} else {
ourSummaryString = ourSummary.join(', ');
}

const theirSummary = this.summarizeTheirWithWeapons();

let theirSummaryString: string;

if (theirSummary.length > 1) {
theirSummaryString =
theirSummary.slice(0, theirSummary.length - 1).join(', ') +
' and ' +
theirSummary[theirSummary.length - 1];
} else if (theirSummary.length === 0) {
theirSummaryString = 'nothing';
} else {
theirSummaryString = theirSummary.join(', ');
}

return `You will be offered ${ourSummaryString} for ${theirSummaryString}`;
}

summarizeOurWithWeapons(): string[] {
const items: { name: string; amount: number }[] = [];

for (const sku in this.our) {
if (!Object.prototype.hasOwnProperty.call(this.our, sku)) {
continue;
}

items.push({ name: this.bot.schema.getName(SKU.fromString(sku), false), amount: this.our[sku] });
}

let summary: string[];

if (items.length <= 1) {
summary = items.map(v => {
if (v.amount === 1) {
return 'a ' + v.name;
} else {
return pluralize(v.name, v.amount, true);
}
});
} else {
summary = items.map(v => pluralize(v.name, v.amount, true));
}

return summary;
}

summarizeTheirWithWeapons(): string[] {
const items: { name: string; amount: number }[] = [];

for (const sku in this.their) {
if (!Object.prototype.hasOwnProperty.call(this.their, sku)) {
continue;
}

items.push({ name: this.bot.schema.getName(SKU.fromString(sku), false), amount: this.their[sku] });
}

let summary: string[];

if (items.length <= 1) {
summary = items.map(v => {
if (v.amount === 1) {
return 'a ' + v.name;
} else {
return pluralize(v.name, v.amount, true);
}
});
} else {
summary = items.map(v => pluralize(v.name, v.amount, true));
}

return summary;
}

protected abstract preSendOffer(): Promise<void>;

abstract constructOffer(): Promise<string>;

abstract constructOfferWithWeapons(): Promise<string>;

sendOffer(): Promise<string | void> {
if (this.isEmpty()) {
return Promise.reject("❌ I don't or you don't have enough items for this trade");
Expand Down Expand Up @@ -383,6 +473,37 @@ abstract class Cart {
return str;
}

toStringWithWeapons(): string {
if (this.isEmpty()) {
return '❌ Your cart is empty.';
}

let str = '🛒== YOUR CART ==🛒';

str += '\n\nMy side (items you will receive):';
for (const sku in this.our) {
if (!Object.prototype.hasOwnProperty.call(this.our, sku)) {
continue;
}

const name = this.bot.schema.getName(SKU.fromString(sku), false);
str += `\n- ${this.our[sku]}x ${name}`;
}

str += '\n\nYour side (items you will lose):';
for (const sku in this.their) {
if (!Object.prototype.hasOwnProperty.call(this.their, sku)) {
continue;
}

const name = this.bot.schema.getName(SKU.fromString(sku), false);
str += `\n- ${this.their[sku]}x ${name}`;
}
str += '\n\nType !checkout to checkout and proceed trade, or !clearcart to cancel.';

return str;
}

static hasCart(steamID: SteamID): boolean {
return this.carts[steamID.getSteamID64()] !== undefined;
}
Expand Down Expand Up @@ -410,6 +531,10 @@ abstract class Cart {
return '❌ Your cart is empty.';
}

return cart.toString();
if (process.env.DISABLE_CRAFTWEAPON_AS_CURRENCY !== 'true') {
return cart.toStringWithWeapons();
} else {
return cart.toString();
}
}
}
Loading

0 comments on commit ce70745

Please sign in to comment.