Skip to content

Mostra quale azione è in attesa ​

Prima di eseguire un'azione chiesta dalla persona le regole possono aspettare gli altri giocatori o il turno della persona. Segna un'azione del genere come in attesa e disattiva il suo pulsante finché le regole non vanno avanti. Un'azione è in attesa quando qualcuno l'ha chiesta (isRequested) o quando questa persona l'ha appena chiesta (act si è risolta con 'requested'). Premi una delle azioni dell'esempio per vederla in attesa.

jsx
import { useEffect, useState } from 'react';
import { useWorld } from './useWorld.js';

// A fake kitchen to build with. Its rules offer two actions.
const kitchen = {
	name: 'Kitchen',
	now: {
		text: 'Dinner is being planned.',
		actions: [
			{ name: 'cook', text: 'Start cooking' },
			{ name: 'pizza', text: 'Order pizza' },
		],
	},
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: kitchen } });
	const [asked, setAsked] = useState([]); // the actions this person asked for, here and now
	const nowText = world?.now?.text;

	// When the rules move on, what was asked before is done. Start the list again.
	useEffect(() => setAsked([]), [nowText]);

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	async function choose(name) {
		// The rules answer 'requested' when they have the request and will do it when they can.
		if ((await world.act(name)) === 'requested') setAsked((names) => [...names, name]);
	}

	return (
		<>
			<p>{world.now?.text}</p>
			<ul>
				{(world.now?.actions ?? []).map((action) => {
					// An action waits when somebody asked for it (`isRequested`), or when this person just did.
					const isWaiting = action.isRequested || asked.includes(action.name);
					return (
						<li key={action.name}>
							<button onClick={() => choose(action.name)} disabled={isWaiting}>{action.text ?? action.name}</button>
							{isWaiting && <i> (waiting for the rules)</i>}
						</li>
					);
				})}
			</ul>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake kitchen to build with. Its rules offer two actions.
const kitchen = {
	name: 'Kitchen',
	now: {
		text: 'Dinner is being planned.',
		actions: [
			{ name: 'cook', text: 'Start cooking' },
			{ name: 'pizza', text: 'Order pizza' },
		],
	},
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: kitchen } });

const now = document.createElement('p');
const list = document.createElement('ul');
document.body.append(now, list);

let asked = []; // the actions this person asked for, here and now
let shownNow = null;

function draw(node) {
	// When the rules move on, what was asked before is done. Start the list again.
	if (node.now?.text !== shownNow) asked = [];
	shownNow = node.now?.text;
	now.textContent = node.now?.text ?? '';
	list.replaceChildren(...(node.now?.actions ?? []).map((action) => {
		// An action waits when somebody asked for it (`isRequested`), or when this person just did.
		const isWaiting = action.isRequested || asked.includes(action.name);
		const item = document.createElement('li');
		const button = document.createElement('button');
		button.textContent = action.text ?? action.name;
		button.disabled = isWaiting;
		button.onclick = async () => {
			// The rules answer 'requested' when they have the request and will do it when they can.
			if ((await world.act(action.name)) === 'requested') asked = [...asked, action.name];
			draw(world.get());
		};
		item.append(button);
		if (isWaiting) {
			const waiting = document.createElement('i');
			waiting.textContent = ' (waiting for the rules)';
			item.append(waiting);
		}
		return item;
	}));
}

world.subscribe(draw);
Live, on a fake world
Open

Come funziona ​

action.isRequested è true quando qualcuno (questa persona o un'altra) ha già chiesto quell'azione. Dal momento in cui act si risolve con 'requested' la vista tiene il nome dell'azione in asked. Il mondo finto dell'esempio non registra richieste (non ha mai isRequested: true) e nell'esempio solo questa lista segna l'azione come in attesa. La vista svuota asked quando un nuovo now.text mostra che le regole sono andate avanti e che le azioni possono essere cambiate. Fino ad allora il pulsante resta disattivato perché una seconda richiesta non aggiungerebbe niente.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.