UNPKG

hathora-et-labora-game

Version:

Game logic reducer for Uwe Rosenberg's Ora et Labora

31 lines 1.77 kB
import { add, always, curry, map, min, pipe, range, reverse, unnest, view } from 'ramda'; import { P, match } from 'ts-pattern'; import { activeLens, getCost, payCost, withActivePlayer } from '../board/player'; import { costMoney, parseResourceParam, stringRepeater } from '../board/resource'; import { ResourceEnum } from '../types'; export const cloisterLibrary = (inputs = '') => { const input = parseResourceParam(inputs); const { book = 0, ...inputWithoutBook } = input; const newBooks = Math.min(costMoney(inputWithoutBook), 3); const soldBook = Math.min(book ?? 0, 1); return withActivePlayer(pipe( // payCost(inputWithoutBook), getCost({ book: newBooks }), payCost({ book: soldBook }), getCost({ meat: soldBook, wine: soldBook }))); }; export const complete = curry((partial, state) => match(partial) .with([], () => { const player = view(activeLens(state), state); const { penny = 0, book = 0 } = player; // step 1: figure out how many times we might send in penny, given max pennies const pennyAmounts = reverse(range(0, 1 + Math.min(3, penny))); // step 2: for each amount of penny, figure out how many books they would have const paymentAmounts = map((pennyAmount) => [pennyAmount + book, stringRepeater(ResourceEnum.Penny, pennyAmount)], pennyAmounts); // then for each of those, sell 0 or 1 book if there are any const consumeBreadAmounts = unnest(map(([sellableBooks, resourceString]) => pipe( // min(1), add(1), range(0), (reverse), map(stringRepeater(ResourceEnum.Book)), map((s) => resourceString + String(s)))(sellableBooks), paymentAmounts)); return consumeBreadAmounts; }) .with([P._], always([''])) .otherwise(always([]))); //# sourceMappingURL=cloisterLibrary.js.map