Skip to content

Commit

Permalink
placeholder for feeding phase (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Oct 13, 2024
1 parent d9ca0bf commit c4508b4
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 46 deletions.
3 changes: 3 additions & 0 deletions src/component-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
early_exit$,
error$,
farmyard$,
feed_phase$,
selection$,
supply$,
tasks$,
Expand Down Expand Up @@ -72,6 +73,7 @@ class App extends LitElement {
this._provide('early_exit');
this._provide('error');
this._provide('farmyard');
this._provide('feed_phase');
this._provide('selection');
this._provide('supply');
this._provide('tasks');
Expand Down Expand Up @@ -219,6 +221,7 @@ class App extends LitElement {
this._observe(early_exit$, 'early_exit');
this._observe(error$ , 'error' );
this._observe(farmyard$ , 'farmyard' );
this._observe(feed_phase$, 'feed_phase');
this._observe(selection$ , 'selection' );
this._observe(supply$ , 'supply' );
this._observe(tasks$ , 'tasks' );
Expand Down
77 changes: 46 additions & 31 deletions src/component-infobar.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
import {LitElement, css, html} from 'lit';
import {
LitElement,
css,
html,
nothing,
} from 'lit';

import {ContextConsumer} from '@lit/context';

class InfoBar extends LitElement {
#early_exit;
#messages;
#turn;

constructor() {
super();
this._consume('messages', {subscribe: false});
this._consume('early_exit');
this._consume('feed_phase');
this._consume('turn');
}

this.#messages = new ContextConsumer(this, {
context: 'messages'
});

this.#turn = new ContextConsumer(this, {
context: 'turn',
subscribe: true
});

this.#early_exit = new ContextConsumer(this, {
context: 'early_exit',
subscribe: true
});
_consume(context, opts = {}) {
const {subscribe = true} = opts;
this[`_${context}`] = new ContextConsumer(this, {context, subscribe});
}

_dispatch(task_id) {
_dispatch(ev) {
this.dispatchEvent(
new CustomEvent('player.move', {
bubbles: true,
composed: true,
detail: {
type: 'task.exit',
task_id
}
detail: ev
})
);
}

_early_exit() {
const early_exit = this.#early_exit.value;
if (!early_exit) return;
_render_early_exit_button() {
const task_id = this._early_exit.value;

if (!task_id) {
return nothing;
}

const ev = {type: 'task.exit', task_id};

return html`
<button type="button" @click=${() => this._dispatch(ev)}>
${this._messages.value.complete()}
</button>
`;
}

_render_feed_button() {
if (!this._feed_phase.value) {
return nothing;
}

const ev = {type: 'task.selected', task_id: '002'};

return html`
<button type="button" @click=${() => this._dispatch(early_exit)}>
${this.#messages.value.complete()}
<button type="button" @click=${() => this._dispatch(ev)}>
FEED
</button>
`;
}

render() {
const msg = this.#messages.value;
const {turn, workers} = this.#turn.value;
const msg = this._messages.value;
const {turn, workers} = this._turn.value;
return html`
<span>${msg.turn({turn})}</span>
<span title=${msg.num_workers_description({num: workers})}>
${msg.num_workers({num:workers})}
</span>
${this._early_exit()}
${this._render_early_exit_button()}
${this._render_feed_button()}
`;
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ const src = setup({
const check = turn === 14;
console.log(`is last turn? ${turn} ${check}`);
return check;
},

'is-feeding-task?':
({context, event}) => {
const {task_id} = event;
return context.tasks[task_id].feeding === true;
},

'is-main-feeding-task?':
({context, event}) => {
const {task_id} = event;
return task_id === '002';
}
}
});
Expand Down Expand Up @@ -178,6 +190,7 @@ const machine = src.createMachine({
},
tasks: {
'001': {},
'002': {feeding: true},

101: {selected: false },
102: {selected: false },
Expand Down Expand Up @@ -257,7 +270,7 @@ const machine = src.createMachine({
*/
on_replenish: ['107','108','109','110','114', '116', '119'],
on_fields: ['001']
on_fields: ['001'],
};
},
"initial": "init",
Expand Down Expand Up @@ -375,9 +388,16 @@ const machine = src.createMachine({
}
},
},
"feed": {
"after": {
"50": "breed"
feed: {
on: {
'task.selected': {
guard: 'is-feeding-task?',
actions: ['task-forward', ({event}) => console.log('wwww', event)]
},
'task.completed': {
guard: 'is-main-feeding-task?',
target: 'breed'
}
}
},
"breed": {
Expand Down
53 changes: 44 additions & 9 deletions src/lib-task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
/*
Defines a blueprint for most tasks.
The purpose of this module is to generate
a state machine for a given task according
to a shared and unique blueprint.
The main export takes a task definition
and returns the corresponding state machine
for it.
A task definition is an object with
the following properties:
** id **
A unique task identifier.
** execute **
This should implement the purpose of the task.
*/

Expand Down Expand Up @@ -62,12 +79,13 @@ const lib = setup({
sendTo(dispatcher, {type: 'task.ack'}),

'task-complete':
enqueueActions(({enqueue, context}) => {
enqueueActions(({enqueue, context}, params) => {
const {task_id} = params;
if (context.exec > 0) {
enqueue.assign({exec: 0});
enqueue({type: 'game-update', params: {fn: early_exit_stop}});
}
enqueue.sendTo(gamesys, {type: 'task.completed'});
enqueue.sendTo(gamesys, {type: 'task.completed', task_id});
}),

'allow-early-exit':
Expand Down Expand Up @@ -130,7 +148,7 @@ export default function (definitions) {
}
});

const machine = {
let machine = {
context: {
/*
Expand Down Expand Up @@ -206,7 +224,12 @@ export default function (definitions) {
{
guard: 'silent-failure?',
target: 'idle',
actions: 'task-complete'
actions: {
type: 'task-complete',
params: {
task_id: id
}
}
},
{
target: 'idle',
Expand Down Expand Up @@ -237,7 +260,12 @@ export default function (definitions) {
},
'task.exit': {
target: 'idle',
actions: 'task-complete'
actions: {
type: 'task-complete',
params: {
task_id: id
}
}
}
},
exit: {
Expand Down Expand Up @@ -269,7 +297,12 @@ export default function (definitions) {
...(!repeat && {
'game.updated': {
target: 'idle',
actions: 'task-complete'
actions: {
type: 'task-complete',
params: {
task_id: id
}
}
}
})
}
Expand All @@ -278,8 +311,10 @@ export default function (definitions) {
}
};

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

machine = lib.createMachine(machine);

return lib.createMachine(machine);
return machine;
}

5 changes: 5 additions & 0 deletions src/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ export const early_exit$ = snapshot$ => snapshot$.pipe(
distinctUntilChanged()
);

export const feed_phase$ = snapshot$ => snapshot$.pipe(
map(snapshot => snapshot.matches({harvest: 'feed'})),
distinctUntilChanged(),
);

15 changes: 15 additions & 0 deletions src/task-002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Feeding phase
*/

import task from './lib-task.js';

export default task({
id: '002',
execute: (_, game) => {
return game;
}
});

2 changes: 2 additions & 0 deletions test/acceptance/001.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Scenario: Fields are automatically harvested
And I have 2 grain on A1
And I have 2 grain on A2

When I feed my family

# Turn 5
* I select "Take x Clay"
* I select "Take x Reed"
Expand Down
5 changes: 5 additions & 0 deletions test/acceptance/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ Then('I have the following stock on the board', async function (table) {
}));
});

When('I feed my family', async function () {
await this.wait(state => state.matches({harvest: 'feed'}));
await this.send({type: 'task.completed', task_id: '002'});
});

9 changes: 7 additions & 2 deletions test/acceptance/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ setWorldConstructor(class extends World {
this.game.send(ev);
}

async wait(ms) {
await new Promise(res => setTimeout(res, ms));
async wait(predicate) {
if (typeof predicate == 'number') {
await new Promise(res => setTimeout(res, predicate));
}
else {
await waitFor(this.game, predicate);
}
}

async assert(predicate) {
Expand Down

0 comments on commit c4508b4

Please sign in to comment.