UNPKG

hathora-et-labora-game

Version:

Game logic reducer for Uwe Rosenberg's Ora et Labora

43 lines 1.73 kB
import { always, curry, identity, pipe, view } from 'ramda'; import { P, match } from 'ts-pattern'; import { activeLens, getCost, withActivePlayer } from '../board/player'; import { BuildingEnum } from '../types'; import { forestLocations, forestLocationsForCol } from '../board/landscape'; // TODO: refactor this with carpentry const checkSpotIsForest = (col, row) => (player) => { if (player === undefined) return undefined; if (player.landscape[row + player.landscapeOffset][col + 2][1] !== BuildingEnum.Forest) return undefined; return player; }; const removeForestAt = (col, row) => (player) => player && { ...player, landscape: [ ...player.landscape.slice(0, row + player.landscapeOffset), [ ...player.landscape[row + player.landscapeOffset].slice(0, col + 2), [player.landscape[row + player.landscapeOffset][col + 2][0]], ...player.landscape[row + player.landscapeOffset].slice(col + 2 + 1), ], ...player.landscape.slice(row + player.landscapeOffset + 1), ], }; export const forestHut = (col, row) => { if (col === undefined || row === undefined) return identity; return withActivePlayer(pipe( // checkSpotIsForest(col, row), removeForestAt(col, row), getCost({ sheep: 2, wood: 2, stone: 1 }))); }; export const complete = curry((partial, state) => { const player = view(activeLens(state), state); return match(partial) .with([], () => [...forestLocations(player), '']) .with([P._], ([c]) => { return [...forestLocationsForCol(c, player)]; }) .with([P._, P._], always([''])) .otherwise(always([])); }); //# sourceMappingURL=forestHut.js.map