From e2728ebad7f742fcfe36160643e02e58a2646845 Mon Sep 17 00:00:00 2001 From: customcommander Date: Sun, 6 Oct 2024 15:50:26 +0100 Subject: [PATCH] invoke dispatcher instead --- src/dispatcher.js | 38 ++++++++++------------------ src/game.js | 64 ++++++++++++++++++++++++----------------------- src/task-001.js | 2 +- 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/dispatcher.js b/src/dispatcher.js index 5583ebc..9b9149f 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -12,51 +12,39 @@ import { } from 'xstate'; const src = setup({ - context: { - jobs: [] - }, actions: { - start: - assign(({event: {jobs}}) => ({jobs})), - - dispatch: - enqueueActions(({enqueue, context, system}) => { + dispatch: enqueueActions(({enqueue, context, system}) => { const [{ev: type, task_id}, ...jobs] = context.jobs; enqueue.sendTo(system.get(`task-${task_id}`), {type}); enqueue.assign({jobs}); - }), - - quit: - sendTo(({system}) => system.get('gamesys'), { - type: 'dispatch.done' }) }, guards: { - 'repeat?': - ({context: {jobs}}) => jobs.length > 0 + 'repeat?': ({context}) => { + const {jobs} = context; + return jobs.length > 0 + } } }); export default src.createMachine({ - initial: 'idle', + context: ({input}) => ({ + jobs: input.jobs + }), + initial: 'loop', states: { - idle: { - on: { - dispatch: { - target: 'loop', - actions: 'start' - } - } - }, loop: { entry: 'dispatch', on: { 'task.ack': [ {target: 'loop', guard: 'repeat?', reenter: true}, - {target: 'idle', actions: 'quit'} + {target: 'done'} ] } + }, + done: { + type: 'final' } } }); diff --git a/src/game.js b/src/game.js index e6511da..a16c155 100644 --- a/src/game.js +++ b/src/game.js @@ -26,9 +26,6 @@ const src = setup({ }, actions: { - 'init-dispatcher': - spawnChild('dispatcher', {systemId: 'dispatcher'}), - 'setup-new-turn': assign(({context}) => produce(context, draft => { draft.turn += 1; @@ -135,13 +132,6 @@ const src = setup({ } }); -const dispatcher_params = - (channel) => ({context}) => [channel].flatMap(ch => { - const task_not_hidden = id => context.tasks[id].hidden !== true; - const ids = context.__dispatch[ch].filter(task_not_hidden); - return ids.map((id) => ({ev: `task.${ch}`, task_id: id})); - }); - const machine = src.createMachine({ context: ({input}) => { const turn = input?.turn ?? 1; @@ -223,18 +213,15 @@ const machine = src.createMachine({ error: null, early_exit: null, - __dispatch: { - replenish: ['107','108','109','110','114'], - 'harvest-fields': ['001'] - } + on_replenish: ['107','108','109','110','114'], + on_fields: ['001'] }; }, "initial": "init", "states": { "init": { - "entry": "init-dispatcher", - "invoke": { - "src": "task-loader", + invoke: { + src: 'task-loader', input: ({context}) => Object.keys(context.tasks), onDone: { target: 'work', @@ -252,15 +239,27 @@ const machine = src.createMachine({ } }, replenish: { - entry: { - type: 'dispatch', - params: dispatcher_params('replenish') - }, - on: { - 'dispatch.done': { + invoke: { + src: 'dispatcher', + systemId: 'dispatcher', + input: ({context}) => { + const {on_replenish: notify, tasks} = context; + + const jobs = notify.reduce((acc, task_id) => { + const available = tasks[task_id].hidden !== true; + // Ignore tasks not yet available + if (available) { + acc.push({task_id, ev: 'task.replenish'}); + } + return acc; + }, []); + + return {jobs}; + }, + onDone: { target: 'done' } - }, + } }, done: { type: 'final' @@ -320,15 +319,18 @@ const machine = src.createMachine({ "initial": "fields", "states": { "fields": { - entry: { - type: 'dispatch', - params: dispatcher_params('harvest-fields') - }, - on: { - 'dispatch.done': { + invoke: { + src: 'dispatcher', + systemId: 'dispatcher', + input: ({context}) => { + const {on_fields: notify} = context; + const jobs = notify.map(task_id => ({task_id, ev: 'task.fields'})); + return {jobs}; + }, + onDone: { target: 'feed' } - } + }, }, "feed": { "after": { diff --git a/src/task-001.js b/src/task-001.js index d1e1b86..0f65fdd 100644 --- a/src/task-001.js +++ b/src/task-001.js @@ -31,7 +31,7 @@ function harvest_fields(_, game_context) { export default base.createMachine({ context: {}, on: { - 'task.harvest-fields': { + 'task.fields': { actions: { type: 'game-update', params: {