Skip to content

Commit

Permalink
poc lib-task
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Oct 9, 2024
1 parent 9223d66 commit b98869f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 46 deletions.
112 changes: 112 additions & 0 deletions src/lib-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Defines a blueprint for most tasks.
*/

import {
fromPromise,
sendTo,
setup,
} from 'xstate';

import {
produce
} from 'immer';

const gamesys = ({system}) => system.get('gamesys');
const dispatcher = ({system}) => system.get('dispatcher');

const lib = setup({
actions: {
'game-update':
sendTo(gamesys, ({event}, {fn, reply_to, ...params}) => ({
type: 'game.update',
updater: produce(fn.bind(null, {event, params})),
reply_to
})),

'task-ack':
sendTo(dispatcher, {type: 'task.ack'}),

'task-complete':
sendTo(gamesys, {type: 'task.completed'})
}
});

export default function (definitions) {
const {
execute,
id,
replenish,
} = definitions;

const m = {
initial: 'idle',

states: {

idle: {
on: {
'task.replenish': {
target: 'replenish'
},

'task.selected': {
target: 'execute'
}
}
},

replenish: (
typeof replenish == 'function' ?
{
entry: {
type: 'game-update',
params: {
fn: replenish,
reply_to: id
}
},
on: {
'game.updated': {
target: 'idle',
actions: 'task-ack'
}
}
}

: null

),

execute: (
{
entry: {
type: 'game-update',
params: {
fn: execute,
reply_to: id
}
},
on: {
'game.updated': {
target: 'idle',
actions: 'task-complete'
}
}
}
)
}
};

if (!replenish) {
delete m.states.idle.on['task.replenish'];
delete m.states.replenish;
}

// console.log(JSON.stringify(m, (k, v) => typeof v === 'function' ? `<${v.name}>` : v, 2));

return lib.createMachine(m);
}

20 changes: 6 additions & 14 deletions src/task-103.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import {base} from './task-lib.js';
import task from './lib-task.js';

function collect(_, game_context) {
game_context.supply.grain += 1;
return game_context;
}

export default base.createMachine({
on: {
'task.selected': {
actions: [
{type: 'game-update', params: {updater: collect}},
{type: 'task-complete'}
]
}
export default task({
id: '103',
execute: (_, game) => {
game.supply.grain += 1;
return game;
}
});

44 changes: 12 additions & 32 deletions src/task-107.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
import {base} from './task-lib.js';
import task from './lib-task.js';

function replenish(_, game_context) {
game_context.tasks[107].quantity += 2;
return game_context;
}

function collect(_, game_context) {
const {quantity} = game_context.tasks[107];
game_context.supply.wood += quantity;
game_context.tasks[107].quantity = 0;
return game_context;
}

export default base.createMachine({
initial: 'idle',
states: {
idle: {
on: {
'task.replenish': {
actions: [
{type: 'game-update', params: {updater: replenish}},
{type: 'ack'}
]
},
'task.selected': {
actions: [
{type: 'game-update', params: {updater: collect}},
{type: 'task-complete'}
]
}
}
}
export default task({
id: '107',
replenish: (_, game) => {
game.tasks['107'].quantity += 2;
return game;
},
execute: (_, game) => {
const {quantity} = game.tasks['107'];
game.supply.wood += quantity;
game.tasks['107'].quantity = 0;
return game;
}
});

0 comments on commit b98869f

Please sign in to comment.