hathora-et-labora-game
Version:
Plays Uwe Rosenberg's Ora et Labora for the Hathora engine. It reduces a list of moves into a board game state.
54 lines • 2.42 kB
JavaScript
import { always, curry, identity, map, pipe, unnest, view } from 'ramda';
import { P, match } from 'ts-pattern';
import { activeLens, getWonder, payCost, withActivePlayer } from '../board/player';
import { coinCostOptions, costPoints, parseResourceParam, pointCostOptions } from '../board/resource';
import { removeWonder } from '../board/state';
import { ResourceEnum } from '../types';
const removeWhiskey = (cost) => {
if (!cost)
return undefined;
if (!cost.whiskey)
return undefined;
return Object.assign(Object.assign({}, cost), { whiskey: cost.whiskey - 1 });
};
const removeFiveCoins = (cost) => {
var _a, _b, _c, _d;
if (!cost)
return undefined;
if (((_a = cost.penny) !== null && _a !== void 0 ? _a : 0) >= 5)
return Object.assign(Object.assign({}, cost), { penny: ((_b = cost.penny) !== null && _b !== void 0 ? _b : 0) - 5 });
if (((_c = cost.nickel) !== null && _c !== void 0 ? _c : 0) >= 1)
return Object.assign(Object.assign({}, cost), { nickel: ((_d = cost.nickel) !== null && _d !== void 0 ? _d : 0) - 1 });
return undefined;
};
export const roundTower = (param = '') => {
const rawInput = parseResourceParam(param);
const input = pipe(
//
removeWhiskey, removeFiveCoins)(rawInput);
if (!input || costPoints(input) < 14)
return identity;
return pipe(withActivePlayer(pipe(
//
payCost(rawInput), getWonder())), removeWonder);
};
export const complete = curry((partial, state) => match(partial)
.with([], () => {
const player = view(activeLens(state), state);
if (!player.whiskey)
return [''];
// 1st: just remove the whiskey
const paidWhiskey = payCost({ whiskey: 1 })(player);
// 2nd: find all the ways, without that whiskey, to pay 5 coins
const waysToPayCoins = coinCostOptions(5)(paidWhiskey);
// 3rd: with each of those ways, find all the ways to make 14 points
const waysToMakePoints = map((wayToPayCoin) => {
const paidCoins = payCost(parseResourceParam(wayToPayCoin))(paidWhiskey);
const waysToPayPoints = pointCostOptions(14)(paidCoins);
return map((wayToPayPoints) => `${wayToPayCoin}${ResourceEnum.Whiskey}${wayToPayPoints}`, waysToPayPoints);
}, waysToPayCoins);
return [...unnest(waysToMakePoints), ''];
})
.with([P._], always(['']))
.otherwise(always([])));
//# sourceMappingURL=roundTower.js.map