fairly give foraging results

This commit is contained in:
Jill 2023-11-15 18:33:08 +03:00
parent 41af7fa2e2
commit 169b81ea06
Signed by: oat
GPG Key ID: 33489AA58A955108
2 changed files with 21 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import { pickRandom } from '../util';
import { DefaultItems, Item, Items, formatItems, getDefaultItem, getItemQuantity } from './items';
export interface CraftingStation {
@ -24,8 +25,22 @@ export const craftingStations: CraftingStation[] = [
emoji: '🌲',
cooldown: 60 * 5,
formatRecipe: (_inputs, _requirements, outputs, disableBold = false) => `${outputs.map(i => formatItems(i.item, i.quantity, disableBold) + '?').join(' ')}`,
manipulateResults: (outputs) =>
outputs.map(o => ({item: o.item, quantity: Math.floor(o.quantity * Math.random())})).filter(o => o.quantity !== 0)
manipulateResults: (outputs) => {
const totalItems = outputs.reduce((a, b) => a + b.quantity, 0);
// grab from 1/2 to the entire pool, ensure it never goes below 1
const rolledItems = Math.max(Math.round(totalItems/2 + Math.random() * totalItems/2), 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: rolled.quantity });
}
}
return res;
}
},
{
key: 'hand',

View File

@ -23,4 +23,8 @@ export async function writeTmpFile(data: string | Buffer, filename?: string, ext
const path = join(tmpdir(), file);
await fsp.writeFile(path, data);
return path;
}
export function pickRandom<T>(list: T[]): T {
return list[Math.floor(Math.random() * list.length)];
}