-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a45fc46
commit 2fa3919
Showing
2 changed files
with
57 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,66 @@ | ||
/* | ||
Sow and/or Bake bread | ||
Sow and/or Bake bread | ||
*/ | ||
|
||
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: 'select-space', | ||
guard: and(['has-empty-fields?', or(['has-grain?', 'has-vegetable?'])]) | ||
}, | ||
{ | ||
actions: { | ||
type: 'abort', | ||
params: { | ||
task_id: 113, | ||
err: 'NOT_ENOUGH_RESOURCES' | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
'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'] | ||
} | ||
] | ||
} | ||
import task from './lib-task.js'; | ||
|
||
export default task({ | ||
id: '113', | ||
|
||
repeat: true, | ||
|
||
check: (_, game) => { | ||
const {supply: {grain, vegetable}, farmyard} = game; | ||
|
||
// TODO: specific error code | ||
if (!grain && !vegetable) return false; | ||
|
||
const spaces = Object.values(farmyard); | ||
|
||
const has_empty_fields = spaces.some(space => { | ||
const {type, grain, vegetable} = space ?? {}; | ||
return type == 'field' && !grain && !vegetable | ||
}); | ||
|
||
// TODO: specific error code | ||
if (!has_empty_fields) return false; | ||
|
||
return true; | ||
}, | ||
|
||
selection: (_, game) => { | ||
const task_id = '113'; | ||
const spaces = Object.entries(game.farmyard); | ||
const opts = []; | ||
|
||
if (game.supply.grain) opts.push('select.sow-grain'); | ||
if (game.supply.vegetable) opts.push('select.sow-vegetable'); | ||
|
||
game.selection = spaces.flatMap(([space_id, space]) => { | ||
const {type, grain, vegetable} = space ?? {}; | ||
const exit = type != 'field' || (grain || vegetable); | ||
return exit ? [] : opts.map(type => ({type, task_id, space_id})); | ||
}); | ||
|
||
return game; | ||
}, | ||
|
||
execute: ({event}, game) => { | ||
const {type, space_id} = event; | ||
|
||
if (type == 'select.sow-grain') { | ||
game.supply.grain -= 1; | ||
game.farmyard[space_id].grain = 3; | ||
} | ||
|
||
if (type == 'select.sow-vegetable') { | ||
game.supply.vegetable -= 1; | ||
game.farmyard[space_id].vegetable = 2; | ||
} | ||
} | ||
}); | ||
|
||
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}}); | ||
}) | ||
return game; | ||
} | ||
}); | ||
|