saveAs('omega_ftr_market_purchase', async function({ state, intent }) {
const mutatedState = {};
const events = [];
const { playerId, item: targetItemId } = intent;
const coreKey = `players.${playerId}.core`;
const invKey = `players.${playerId}.inventory`;
const marketKey = 'world.market';
// Retrieve required state slices
const market = state[marketKey];
const playerCore = state[coreKey];
const playerInventory = state[invKey];
// Basic structural validation
if (!market || !playerCore || !playerInventory) {
events.push({ type: 'PURCHASE_FAILED', reason: 'MISSING_STATE_DATA'
});
return { mutatedState, events };
}
// Locate the item in the market listings
const marketListing = market.listings.find(listing => listing.id ===
targetItemId);
if (!marketListing) {
events.push({ type: 'PURCHASE_FAILED', reason: 'ITEM_NOT_FOUND' });
return { mutatedState, events };
}
// Validate budget
if (playerCore.budget < marketListing.cost) {
events.push({ type: 'PURCHASE_FAILED', reason: 'INSUFFICIENT_FUNDS'
});
return { mutatedState, events };
}
// Validate inventory capacity
if (playerInventory.items.length >= playerInventory.slots) {
events.push({ type: 'PURCHASE_FAILED', reason: 'INVENTORY_FULL' });
return { mutatedState, events };
}
// Apply mutations immutably
mutatedState[coreKey] = {
...playerCore,
budget: playerCore.budget - marketListing.cost
};
mutatedState[invKey] = {
...playerInventory,
items: [...playerInventory.items, targetItemId]
};
// Log successful transaction
events.push({
type: 'PURCHASE_SUCCESS',
payload: {
playerId,
item: targetItemId,
cost: marketListing.cost,
remainingBudget: mutatedState[coreKey].budget
}
});
return { mutatedState, events };
}, {
requires: ['world.market', 'players.[id].core', 'players.[id].inventory'],
mutates: ['players.[id].core', 'players.[id].inventory']
});
Logic Explanation:
This routine acts as a pure, transactional state resolver. It first extracts
the necessary state representations (market data, player core, and player
inventory) based on the intent.playerId. It implements safety guards to ensure
the market item exists, the player has sufficient budget, and the player has
open inventory slots. If the validation passes, it immutably clones and
updates the core and inventory objects to deduct the item's cost and append
the item ID to the inventory array. The routine returns only the mutated
player states per the contract rules, leaving world.market unaltered since
market purchases in this context don't modify global listings. A system event
is dispatched to notify the registry of the transaction outcome.
Publishing [omega_ftr_market_purchase] to registry...
Promise {
<pending>,
Symbol(async_id_symbol): 336,
Symbol(trigger_async_id_symbol): 11
}
mesh-edge>
[Edge] Hot-reloaded [omega_ftr_market_purchase] directly into memory.