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:
parent
6cfaaf0bf9
commit
8075892e0e
12 changed files with 445 additions and 0 deletions
60
packages/ludic.wallet/tests/wallet_test.ludic
Normal file
60
packages/ludic.wallet/tests/wallet_test.ludic
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue