Skip to content

Commit

Permalink
fix resources check and mark task 113 as todo
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Sep 11, 2024
1 parent 73289f1 commit 513b8b8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/task-113.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

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

const machine = base.createMachine({
Expand All @@ -14,12 +15,16 @@ const machine = base.createMachine({
'task.selected': [
{
target: 'work',
guard: {
type: 'enough-supply?',
guard: and(['has-empty-fields?', or(['has-grain?',
'has-vegetable?'])]),
actions: {
type: 'abort',
params: {
grain: 1
task_id: 113,
err: 'TODO'
}
}

},
{
actions: {
Expand Down
26 changes: 26 additions & 0 deletions src/task-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ export const base = setup({
const {supply} = game_context;
const checks = Object.entries(params);
return checks.every(([k, min]) => supply[k] >= min);
},

// True if there is at least one grain available in the supply.
'has-grain?':
({event: {game_context}}) => {
const {supply} = game_context;
return supply.grain > 0;
},

// True if there is at least one vegetable available in the supply.
'has-vegetable?':
({event: {game_context}}) => {
const {supply} = game_context;
return supply.vegetable > 0;
},

// True if there is at least one empty field.
'has-empty-fields?':
({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;
});
}
}
});
Expand Down

0 comments on commit 513b8b8

Please sign in to comment.