ludic/packages/ludic.wallet/wallet.ludic
Orkuncakilkaya 71735b10a2 feat(base): 0.R4 - a queue keeps its own count, so every package verb takes only its own state; ludic migrate state --prune
ludic.base's Queue<T> carries a QueueTag (its name and pending count): queue_new(name), q_push(q, v),
q_drain(q), q_clear(q) take no BaseState, and core_undrained(tags) names the given queues still holding
facts. A reducer on a package's state can now call that package's verbs (wallet_earn(wallet_st, n)).

ludic migrate state --prune (ludicc --migrate-prune) takes out each state parameter a function no
longer uses, nor anything it calls, and the argument that fills it - including an argument for a
parameter the callee has dropped, which is taken out before the call is checked, so a generic's T is
told by the argument that says it. A reducer keeps its state. --dry-run now counts the edits it would
make. Every package was moved with it: 608 base_st parameters and their arguments, 1889 edits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 04:19:56 +03:00

64 lines
2.1 KiB
Text

# wallet.ludic - the purse, its verbs, its facts and its save section
export property MoneyChanged {
before: int = 0
after: int = 0
}
export state WalletState {
wl_money: int = 0
wl_changes: Queue<MoneyChanged> = null
}
export function wallet_changes(wallet_st: mut WalletState) -> Queue<MoneyChanged> {
if wallet_st.wl_changes == null { wallet_st.wl_changes = queue_new("wallet.changes") }
return wallet_st.wl_changes
}
export function wallet_money(wallet_st: WalletState) -> int { return wallet_st.wl_money }
export function wallet_can(wallet_st: WalletState, n: int) -> bool { return wallet_st.wl_money >= n }
# the purse holds exactly n (a starting purse, a correction, the network's word)
export function wallet_set(wallet_st: mut WalletState, n: int) -> void {
if n == wallet_st.wl_money { return }
let c = new MoneyChanged
c.before = wallet_st.wl_money
c.after = n
wallet_st.wl_money = n
q_push(wallet_changes(wallet_st), c)
}
export function wallet_earn(wallet_st: mut WalletState, n: int) -> void { wallet_set(wallet_st, wallet_st.wl_money + n) }
# only what is there: false, and nothing spent, when it is not
export function wallet_spend(wallet_st: mut WalletState, n: int) -> bool {
if n > wallet_st.wl_money { return false }
wallet_set(wallet_st, wallet_st.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(wallet_st: mut WalletState, n: int) -> void { wallet_set(wallet_st, Math.max(wallet_st.wl_money - n, 0)) }
export function wallet_reset(wallet_st: mut WalletState) -> void {
wallet_st.wl_money = 0
q_clear(wallet_changes(wallet_st))
}
export function wallet_save(wallet_st: WalletState) -> Val {
let v = Value.object()
sv_put_int(v, "money", wallet_st.wl_money)
return v
}
export function wallet_load(wallet_st: mut WalletState, v: Val, version: int) -> void {
wallet_st.wl_money = sv_int(v, "money", 0)
q_clear(wallet_changes(wallet_st))
}
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
}