Skip to content

Commit

Permalink
add stock option(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokoeka committed Jan 28, 2024
1 parent a3e3368 commit bcc45c5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ All options are supported in all tabs, unless specified. They are white space se

* `keepN`
* Keeps `N` copies of the item after running
* `stockN`
* (only supported by `mall`, `display`, and `closet`). Ensures `N` copies of the item are stocked in the relevant locations, keeps the rest in your inventory
* `<N`
* Only performs the given operation on items that have a `mallPrice` that is less than `N`
* `>N`
Expand Down
35 changes: 35 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {
autosell,
autosellPrice,
cliExecute,
closetAmount,
displayAmount,
Item,
itemAmount,
mallPrice,
print,
putCloset,
putDisplay,
putShop,
shopAmount,
use,
wellStocked,
} from "kolmafia";
Expand Down Expand Up @@ -41,6 +44,17 @@ export const actions: {
};
} = {
mall: (options: Options) => {
if (options.stock) {
return {
action: (item: Item) =>
putShop(
0,
0,
Math.min(Math.max(0, (options.stock ?? 0) - shopAmount(item)), amount(item, options)),
item
),
};
}
return { action: (item: Item) => putShop(0, 0, amount(item, options), item) };
},
sell: (options: Options) => {
Expand All @@ -55,6 +69,18 @@ export const actions: {
};
},
display: (options: Options) => {
if (options.stock) {
return {
action: (item: Item) =>
putDisplay(
Math.min(
Math.max(0, (options.stock ?? 0) - displayAmount(item)),
amount(item, options)
),
item
),
};
}
return { action: (item: Item) => putDisplay(amount(item, options), item) };
},
use: (options: Options) => {
Expand All @@ -79,6 +105,15 @@ export const actions: {
};
},
closet: (options: Options) => {
if (options.stock) {
return {
action: (item: Item) =>
putDisplay(
Math.min(Math.max(0, (options.stock ?? 0) - closetAmount(item)), amount(item, options)),
item
),
};
}
return {
action: (item: Item) => putCloset(amount(item, options), item),
};
Expand Down
9 changes: 9 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Item } from "kolmafia";

export class Options {
keep?: number;
stock?: number;
target?: string;
body?: string;
priceUpperThreshold?: number;
Expand All @@ -18,6 +19,11 @@ export class Options {
options.keep = parseInt(keep[1]);
continue;
}
const stock = optionStr.match(/stock(\d+)/);
if (stock && stock[1]) {
options.stock = parseInt(stock[1]);
continue;
}
const target = optionStr.match(/#(.*)/);
if (target && target[1]) {
options.target = target[1];
Expand Down Expand Up @@ -50,6 +56,9 @@ export class Options {
if (this.keep) {
optionsStr.push(`keep: ${this.keep}`);
}
if (this.stock) {
optionsStr.push(`stock: ${this.stock}`);
}
if (this.target) {
optionsStr.push(`target: ${this.target}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function isTabTitle(value: string): value is TabTitle {
return ALL_TAB_TITLES.includes(value as TabTitle);
}

const ALL_ACTION_OPTIONS = ["keep", "target"] as const;
const ALL_ACTION_OPTIONS = ["keep", "stock", "target"] as const;
export type ActionOption = typeof ALL_ACTION_OPTIONS[number];

export function isActionOption(value: string): value is ActionOption {
Expand Down

0 comments on commit bcc45c5

Please sign in to comment.