jillo-bot/src/lib/rpg/craftingStations.ts

66 lines
2.2 KiB
TypeScript

import { pickRandom } from '../util';
import { DefaultItems, Item, Items, formatItems, formatItemsArray, getDefaultItem, getItemQuantity } from './items';
export interface CraftingStation {
key: string,
name: string,
description: string,
emoji: string,
requires?: Item,
// in seconds
cooldown?: number,
formatRecipe?: (inputs: Items[], requirements: Items[], outputs: Items[], disableBold?: boolean) => string,
manipulateResults?: (outputs: Items[]) => Items[]
}
export function getStation(key: string) {
return craftingStations.find(station => station.key === key);
}
export const craftingStations: CraftingStation[] = [
{
key: 'forage',
name: 'Forage',
description: 'Pick up various sticks and stones from the forest',
emoji: '🌲',
cooldown: 60 * 5,
formatRecipe: (_inputs, requirements, outputs, disableBold = false) =>
`${requirements.length > 0 ? `w/ ${formatItemsArray(requirements, disableBold)}: ` : ''}${outputs.map(i => formatItems(i.item, i.quantity, disableBold) + '?').join(' ')}`,
manipulateResults: (outputs) => {
const totalItems = outputs.reduce((a, b) => a + b.quantity, 0);
// grab from 1/3 to the entire pool, ensure it never goes below 1
const rolledItems = Math.max(Math.round(totalItems/3 + Math.random() * totalItems*2/3), 1);
const res: Items[] = [];
for (let i = 0; i < rolledItems; i++) {
const rolled = pickRandom(outputs);
const r = res.find(r => r.item.id === rolled.item.id);
if (r) {
r.quantity = r.quantity + 1;
} else {
res.push({ item: rolled.item, quantity: 1 });
}
}
return res;
}
},
{
key: 'hand',
name: 'Hand',
description: 'You can use your hands to make a small assortment of things',
emoji: '✋'
},
{
key: 'workbench',
name: 'Workbench',
description: 'A place for you to work with tools, for simple things',
emoji: '🛠️',
requires: getDefaultItem(DefaultItems.WORKBENCH)
}
];
export async function canUseStation(user: string, station: CraftingStation) {
if (!station.requires) return true;
const inv = await getItemQuantity(user, station.requires.id);
return inv.quantity > 0;
}