Ekonomi
Toko & Dompet Koin
Mengurangi item inventori dan menambahkan hasil penjualan ke saldo koin.
shop-wallet.js
function sellItem(progress, itemUuid, quantity, price) {
const owned = progress.inventory[itemUuid];
if (!owned || quantity > owned.quantity) return progress;
return {
...progress,
coins: progress.coins + quantity * price,
inventory: {
...progress.inventory,
[itemUuid]: { ...owned, quantity: owned.quantity - quantity },
},
};
}
Potongan kode disederhanakan untuk pembelajaran. Implementasi production dapat memiliki validasi, penyimpanan progres, dan penanganan error tambahan.