Skip to content

Commit

Permalink
basic support for sowing grain/vegetable (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Sep 16, 2024
1 parent e7a671d commit 77eb69b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/component-farmyard.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class FarmYard extends LitElement {
return html`
<agricola-space
id=${id}
type=${space?.type ?? nothing}>
type=${space?.type ?? nothing}
grain=${space?.grain ?? nothing}>
${map(selections ?? [], (sel) => this._cta(sel))}
</agricola-space>
`;
Expand Down
4 changes: 4 additions & 0 deletions src/component-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Space extends LitElement {
background-color: green;
}
:host([type="field"][grain]) {
background-color: yellow;
}
:host([type="wooden-hut"]) {
background-color: brown;
}
Expand Down
2 changes: 2 additions & 0 deletions src/messages_en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ select.stable: Stable
select.wooden-hut: Wooden Hut
select.clay-hut: Clay Hut
select.stone-house: Stone House
select.sow-grain: Sow Grain
select.sow-vegetable: Sow Vegetable

task-not-avail: >
Not available until turn {turn}
Expand Down
77 changes: 69 additions & 8 deletions src/task-113.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@
*/

import {and, or} from 'xstate';
import {and, or, enqueueActions} from 'xstate';
import {base} from './task-lib.js';

function sow_grain({params: {space_id}}, game_context) {
game_context.supply.grain -= 1;
game_context.farmyard[space_id].grain = 3;
return game_context;
}

function sow_vegetable({params: {space_id}}, game_context) {
game_context.supply.vegetable -= 1;
game_context.farmyard[space_id].vegetable = 2;
return game_context;
}

const machine = base.createMachine({
initial: 'idle',
states: {
idle: {
on: {
'task.selected': [
{
target: 'work',
guard: and(['has-empty-fields?', or(['has-grain?',
'has-vegetable?'])]),
target: 'select-space',
guard: and(['has-empty-fields?', or(['has-grain?', 'has-vegetable?'])]),
actions: {
type: 'abort',
type: 'display-selection',
params: {
task_id: 113,
err: 'TODO'
opts: ['select.sow']
}
}

},
{
actions: {
Expand All @@ -38,10 +48,61 @@ const machine = base.createMachine({
]
}
},
'select-space': {
entry: 'compute-&-display-selection',
on: {
'select.*': {
target: 'work',
},
'task.exit': {
target: 'idle',
actions: ['early-exit-stop', 'task-complete']
}
},
exit: 'clear-selection'
},
work: {
entry: {
type: 'game-update',
params: ({event: {type, space_id}}) => ({
space_id,
reply_to: 113,
updater: ( type === 'select.sow-grain' ? sow_grain
: type === 'select.sow-vegetable' ? sow_vegetable
: null /* throw? */)
})
},
on: {
'game.updated': [
{
target: 'select-space',
guard: and(['has-empty-fields?', or(['has-grain?', 'has-vegetable?'])]),
actions: {
type: 'early-exit-init',
params: {
task_id: 113
}
}
},
{
target: 'idle',
actions: ['early-exit-stop', 'task-complete']
}
]
}
}
}
});

export default machine;
export default machine.provide({
actions: {
'compute-&-display-selection':
enqueueActions(({enqueue, check}) => {
const opts = [];
if (check('has-grain?')) opts.push('select.sow-grain');
if (check('has-vegetable?')) opts.push('select.sow-vegetable');
enqueue({type: 'display-selection', params: {task_id: 113, opts}});
})
}
});

22 changes: 16 additions & 6 deletions src/task-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import {
produce
} from 'immer';


function is_empty_field(space) {
if (space?.type !== 'field') return false;
const {grain = 0, vegetable = 0} = space;
return !grain && !vegetable;
}

/*
TODO:
If things of the same kind already exist
Expand All @@ -19,7 +26,14 @@ function selection({params}, draft) {
const spaces = Object.entries(draft.farmyard);

draft.selection = opts.flatMap(opt => {
const avail = spaces.filter(([, sp]) => sp == null);
let avail;

if (opt === 'select.sow-grain' || opt === 'select.sow-vegetable') {
avail = spaces.filter(([, sp]) => is_empty_field(sp));
} else {
avail = spaces.filter(([, sp]) => sp == null);
}

return avail.map(([space_id]) => ({type: opt, task_id, space_id}));
});

Expand Down Expand Up @@ -122,11 +136,7 @@ export const base = setup({
({event: {game_context}}) => {
const {farmyard} = game_context;
const spaces = Object.values(farmyard);
return spaces.some(space => {
if (space?.type !== 'field') return false;
const {grain = 0, vegetable = 0} = space;
return !grain && !vegetable;
});
return spaces.some(space => is_empty_field(space));
}
}
});
Expand Down

0 comments on commit 77eb69b

Please sign in to comment.