Offer the actions of the rules as buttons
The rules of a world can offer the person actions such as voting, passing or leaving. These actions are in world.now.actions and change when the rules move to a new state. Render a button for each action the person may choose and request that action with world.act(name). The example offers a vote.
jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// In this fake town hall, the rules offer three actions and the world answers each one.
const townHall = {
name: 'Town hall',
now: {
text: 'The meeting is open: vote on the new park.',
actions: [
{ name: 'vote_yes', text: 'Vote yes' },
{ name: 'vote_no', text: 'Vote no' },
{ name: 'leave', text: 'Leave the meeting' },
],
},
onAction: (name) => (name === 'leave' ? 'You left the meeting.' : `Your vote is in: ${name === 'vote_yes' ? 'yes' : 'no'}.`),
};
// What act() answered, in words.
const RESULTS = {
requested: 'Asked. The rules will do it.',
refused: 'The rules did not take it now.',
'not-allowed': 'This action is not offered now.',
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: townHall } });
const [result, setResult] = useState('');
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
async function choose(name) {
try {
// act() asks the rules to do one of `now.actions` for the person.
setResult(RESULTS[await world.act(name)]);
} catch (error) {
// The webapp could not even try it, for example 'failed' when something went wrong on its side.
setResult(`Not done (${error.code}).`);
}
}
// The actions the rules offer now. One whose `canChoose` is false runs on its own and gets no button.
const actions = (world.now?.actions ?? []).filter((action) => action.canChoose !== false);
const note = world.messages.filter((message) => message.isSystem).at(-1);
return (
<>
<p>{world.now?.text}</p>
{actions.map((action) => (
<button key={action.name} onClick={() => choose(action.name)}>{action.text ?? action.name}</button>
))}
<p>{result}</p>
<p>{note ? note.text : ''}</p>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// In this fake town hall, the rules offer three actions and the world answers each one.
const townHall = {
name: 'Town hall',
now: {
text: 'The meeting is open: vote on the new park.',
actions: [
{ name: 'vote_yes', text: 'Vote yes' },
{ name: 'vote_no', text: 'Vote no' },
{ name: 'leave', text: 'Leave the meeting' },
],
},
onAction: (name) => (name === 'leave' ? 'You left the meeting.' : `Your vote is in: ${name === 'vote_yes' ? 'yes' : 'no'}.`),
};
// What act() answered, in words.
const RESULTS = {
requested: 'Asked. The rules will do it.',
refused: 'The rules did not take it now.',
'not-allowed': 'This action is not offered now.',
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: townHall } });
const now = document.createElement('p');
const buttons = document.createElement('div');
const result = document.createElement('p');
const note = document.createElement('p');
document.body.append(now, buttons, result, note);
let shownActions = null; // the names of the buttons on the page
world.subscribe((node) => {
now.textContent = node.now?.text ?? '';
const last = node.messages.filter((message) => message.isSystem).at(-1);
note.textContent = last ? last.text : '';
// The actions the rules offer now. One whose `canChoose` is false runs on its own and gets no button.
const actions = (node.now?.actions ?? []).filter((action) => action.canChoose !== false);
const names = actions.map((action) => action.name).join(' ');
if (names === shownActions) return;
shownActions = names;
buttons.replaceChildren(...actions.map((action) => {
const button = document.createElement('button');
button.textContent = action.text ?? action.name;
button.onclick = async () => {
try {
// act() asks the rules to do one of `now.actions` for the person.
result.textContent = RESULTS[await world.act(action.name)];
} catch (error) {
// The webapp could not even try it, for example 'failed' when something went wrong on its side.
result.textContent = `Not done (${error.code}).`;
}
};
return button;
}));
});How it works
- Each action has a
namethatacttakes, atextthat describes it (ornull) andcanChoose. An action withcanChoose: falsehappens on its own when the rules decide and gets no button (the example leaves it out). world.act(name)resolves with'requested'when the rules accepted the request,'refused'when they turned it down or did not answer within 10 seconds and'not-allowed'when the action is not offered now.- The effect of the action arrives later as a change of the world. In the example it is a note of the rules (a message with
isSystem: true). - When the webapp cannot make the call at all,
actrejects with an error whosecodeis'failed'and thecatchof the example shows it. - A fake world needs
now.actionsand anonAction(name)callback that returns the answer of the world to the chosen action.
See also
- ActionButtons: ready buttons for the actions.
- Show which action is waiting.
actand The actions the rules offer.