Skip to content

Commit

Permalink
Add price abuse check to packaged buyables. (#5745)
Browse files Browse the repository at this point in the history
* Add price abuse check to packaged buyables.
  • Loading branch information
themrrobert authored Mar 4, 2024
1 parent eceed16 commit f64ffb2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/lib/data/buyables/buyables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,8 @@ const Buyables: Buyable[] = [
})),
{
name: 'Menaphite purple outfit',
gpCost: 5000,
ironmanPrice: 600,
gpCost: 25_000,
ironmanPrice: 10_000,
outputItems: new Bank({
'Menaphite purple hat': 1,
'Menaphite purple top': 1,
Expand All @@ -1005,8 +1005,8 @@ const Buyables: Buyable[] = [
},
{
name: 'Menaphite red outfit',
gpCost: 5000,
ironmanPrice: 600,
gpCost: 25_000,
ironmanPrice: 10_000,
outputItems: new Bank({
'Menaphite red hat': 1,
'Menaphite red top': 1,
Expand Down
59 changes: 58 additions & 1 deletion tests/unit/priceAbuses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,63 @@ import { sacrificePriceOfItem } from '../../src/mahoji/commands/sacrifice';
import { sellPriceOfItem, sellStorePriceOfItem } from '../../src/mahoji/commands/sell';

describe('Price Abusing', () => {
const gpPackageBuyables = Buyables.filter(
i =>
i.gpCost !== undefined &&
i.itemCost === undefined &&
i.outputItems !== undefined &&
!isFunction(i.outputItems) &&
i.outputItems.length > 1
);

test('Package buyables', () => {
for (const b of gpPackageBuyables) {
// Check for packaged items
if (b.outputItems) {
if (isFunction(b.outputItems)) continue;
const outputItems = b.outputItems.items();

let totalPriceSoldFor = 0;
for (const [item, qty] of outputItems) {
totalPriceSoldFor += sellPriceOfItem(item, 0).price * qty;
}
const priceBoughtFor = b.gpCost;

if (totalPriceSoldFor > priceBoughtFor!) {
throw new Error(
`(Package) ${b.name} has an abusable price: buys for ${priceBoughtFor}, sells for ${totalPriceSoldFor}.`
);
}
if (b.ironmanPrice) {
let storePrice = 0;
for (const [item, qty] of outputItems) {
storePrice += sellStorePriceOfItem(item, qty).price;
}
if (storePrice > b.ironmanPrice) {
throw new Error(
`(Package) ${b.name} has an abusable price: buys for ${b.ironmanPrice}, sells for ${storePrice}.`
);
}
}

let sacPrice = 0;
for (const [item, qty] of outputItems) {
sacPrice += sacrificePriceOfItem(item, qty);
}
if (sacPrice > priceBoughtFor!) {
throw new Error(
`${b.name} has an abusable sacrifice price: buys for ${priceBoughtFor}, sacrifices for ${sacPrice}.`
);
}
if (b.ironmanPrice && sacPrice > b.ironmanPrice) {
throw new Error(
`${b.name} has an abusable ironman sacrifice price: buys for ${b.ironmanPrice}, sacrifices for ${sacPrice}.`
);
}
}
}
});

const gpBuyables = Buyables.filter(
i =>
i.gpCost !== undefined &&
Expand All @@ -31,7 +88,7 @@ describe('Price Abusing', () => {
const storePrice = sellStorePriceOfItem(item, 1);
if (storePrice.price > b.ironmanPrice) {
throw new Error(
`${item.name} has an abusable price: buys for ${storePrice.price}, sells for ${b.ironmanPrice}.`
`${item.name} has an abusable price: buys for ${b.ironmanPrice}, sells for ${storePrice.price}.`
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/snapshots/banksnapshots.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ exports[`OSB Buyables 1`] = `
"qpRequired": 10,
},
{
"gpCost": 5000,
"ironmanPrice": 600,
"gpCost": 25000,
"ironmanPrice": 10000,
"itemCost": Bank {
"bank": {},
"frozen": false,
Expand All @@ -779,8 +779,8 @@ exports[`OSB Buyables 1`] = `
},
},
{
"gpCost": 5000,
"ironmanPrice": 600,
"gpCost": 25000,
"ironmanPrice": 10000,
"itemCost": Bank {
"bank": {},
"frozen": false,
Expand Down

0 comments on commit f64ffb2

Please sign in to comment.