Skip to content

Commit

Permalink
Use clamp to clamp numbers; Fix usage of baseAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
pstalcup committed Dec 17, 2024
1 parent bf6c7fa commit 454e10c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {
itemAmount,
mallPrice,
print,
putCloset,
putDisplay,
putShop,
shopAmount,
use,
wellStocked,
Expand All @@ -21,6 +18,7 @@ import {
bulkPutCloset,
bulkPutDisplay,
bulkPutShop,
clamp,
Kmail,
sumNumbers,
} from "libram";
Expand All @@ -31,7 +29,7 @@ import { warn } from "./lib";

function amount(item: Item, options: Options) {
if (options.keep) {
return Math.max(0, itemAmount(item) - options.keep);
return clamp(itemAmount(item) - options.keep, 0, itemAmount(item));
} else {
return itemAmount(item);
}
Expand All @@ -58,7 +56,7 @@ function makeBulk(
items.set(
item,
baseAmount && options.stock
? Math.min(Math.max(0, (options.stock ?? 0) - shopAmount(item)), amount(item, options))
? clamp((options.stock ?? 0) - baseAmount(item), 0, amount(item, options))
: amount(item, options)
),
finalize: () => {
Expand All @@ -72,8 +70,7 @@ function makeMall(mallOptions?: { price?: (item: Item) => number; stock?: boolea
const mallItems = new Map<Item, { quantity?: number; limit?: number; price: number }>();
const quantity =
options.stock && mallOptions?.stock
? (item: Item) =>
Math.min(Math.max(0, (options.stock ?? 0) - shopAmount(item)), amount(item, options))
? (item: Item) => clamp((options.stock ?? 0) - shopAmount(item), 0, amount(item, options))
: (item: Item) => amount(item, options);

return {
Expand Down Expand Up @@ -102,7 +99,11 @@ export const actions: {
return {
action: (item: Item) => {
if (
wellStocked(`${item}`, 1000, Math.max(100, autosellPrice(item) * 2)) ||
wellStocked(
`${item}`,
1000,
clamp(autosellPrice(item) * 2, 100, autosellPrice(item) * 2)
) ||
!item.tradeable
) {
autosellAction.action(item);
Expand Down

0 comments on commit 454e10c

Please sign in to comment.