feat(ludic.inventory, ludic.wallet): the pack and the purse as mechanic packages

ludic.inventory: counts per item kind, a PackRules port (stack_limit), add
what fits / take all or none / set outright, an ItemChanged queue, and a save
section keyed by item NAME so a game can reorder its items with no migration.
ludic.wallet: earn, spend only what is there, lose down to zero, set, a
MoneyChanged queue and its own save section. Six and five test blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 04:29:17 +03:00
parent 6cfaaf0bf9
commit 8075892e0e
12 changed files with 445 additions and 0 deletions

View file

@ -0,0 +1,29 @@
# ludic.wallet
Money carried, in whole units: earned, spent only when it is there, lost when a cost happens
anyway, and every change reported. Uses [`ludic.base`](../ludic.base/README.md) and nothing
else. Prices, reputation and what a thing costs are the game's; the wallet only holds the money.
```ludic
import "ludic.wallet"
```
No ports: the wallet asks the world nothing.
## API
| | |
| --- | --- |
| `wallet_money()`, `wallet_can(n)` | what is there, and whether `n` is |
| `wallet_earn(n)` | add |
| `wallet_spend(n) -> bool` | only what is there: false, and nothing spent, when it is not |
| `wallet_lose(n)` | a cost that happens whether or not it can be paid (a rescue); never below zero |
| `wallet_set(n)` | exactly `n` (a starting purse, a correction, the network's word) |
| `wallet_changes() -> Queue<MoneyChanged>` | the facts: `{ before, after }`, one per change |
| `wallet_system() -> System` | `"wallet"`, `PH_COMMIT`: reset, and its save section (`wallet_save`, `wallet_load`) |
## Tests
```bash
ludic build packages/ludic.wallet/tests/wallet_test.ludic --headless -o /tmp/wallet_test && /tmp/wallet_test
```

View file

@ -0,0 +1,6 @@
# ludic.wallet - one purse of whole money: verbs to earn, spend (only what is there) and lose (a
# cost that happens anyway), a MoneyChanged fact per change, and its own save section
module ludic_wallet
numbers float
import "ludic.base"
import "wallet.ludic"

View file

@ -0,0 +1,5 @@
# ludic.wallet - money carried: earned, spent, lost, and every change a fact. Uses ludic.base and
# nothing else. See README.md.
package "ludic.wallet"
version "0.1.0"
kind source

View file

@ -0,0 +1,60 @@
# wallet_test.ludic - earn, spend only what is there, lose down to zero, a fact per change, and
# its own save section
import "ludic.wallet"
import "ludic.base"
program WalletTest {
numbers float
function changes() -> []MoneyChanged { return q_drain(wallet_changes()) }
function waiting() -> int { return q_len(wallet_changes()) }
test "earning and spending move the purse, and each says so" {
wallet_reset()
wallet_earn(40)
expect(wallet_spend(15))
expect_eq(wallet_money(), 25)
let cs = changes()
expect_eq(len(cs), 2)
expect_eq(cs[0].after, 40)
expect_eq(cs[1].before, 40)
expect_eq(cs[1].after, 25)
}
test "a spend past what is there is refused and changes nothing" {
wallet_reset()
wallet_set(10)
changes()
expect(not wallet_spend(11))
expect_eq(wallet_money(), 10)
expect_eq(waiting(), 0)
expect(wallet_can(10))
expect(not wallet_can(11))
}
test "a loss happens anyway, but never below zero" {
wallet_reset()
wallet_set(7)
wallet_lose(20)
expect_eq(wallet_money(), 0)
}
test "setting what is already there is no change" {
wallet_reset()
wallet_set(5)
changes()
wallet_set(5)
expect_eq(waiting(), 0)
}
test "a save section reads back the same purse" {
wallet_reset()
wallet_set(123)
let root = save_tree()
save_section(root, "wallet", 1, wallet_save())
let text = save_encode(root)
wallet_reset()
let n = load_section(save_decode(text), "wallet")
wallet_load(n.data, n.version)
expect_eq(wallet_money(), 123)
}
}

View file

@ -0,0 +1,62 @@
# wallet.ludic - the purse, its verbs, its facts and its save section
export property MoneyChanged {
before: int = 0
after: int = 0
}
var wl_money: int = 0
var wl_changes: Queue<MoneyChanged> = null
export function wallet_changes() -> Queue<MoneyChanged> {
if wl_changes == null { wl_changes = queue_new("wallet.changes") }
return wl_changes
}
export function wallet_money() -> int { return wl_money }
export function wallet_can(n: int) -> bool { return wl_money >= n }
# the purse holds exactly n (a starting purse, a correction, the network's word)
export function wallet_set(n: int) -> void {
if n == wl_money { return }
let c = new MoneyChanged
c.before = wl_money
c.after = n
wl_money = n
q_push(wallet_changes(), c)
}
export function wallet_earn(n: int) -> void { wallet_set(wl_money + n) }
# only what is there: false, and nothing spent, when it is not
export function wallet_spend(n: int) -> bool {
if n > wl_money { return false }
wallet_set(wl_money - n)
return true
}
# a cost that happens whether or not it can be paid (a rescue, a fine); never below zero
export function wallet_lose(n: int) -> void { wallet_set(Math.max(wl_money - n, 0)) }
export function wallet_reset() -> void {
wl_money = 0
q_clear(wallet_changes())
}
export function wallet_save() -> Val {
let v = Value.object()
sv_put_int(v, "money", wl_money)
return v
}
export function wallet_load(v: Val, version: int) -> void {
wl_money = sv_int(v, "money", 0)
q_clear(wallet_changes())
}
export function wallet_system() -> System {
let s = system_new("wallet", PH_COMMIT)
s.reset = fn wallet_reset
s.save = fn wallet_save
s.load = fn wallet_load
return s
}