|
| 1 | +-- src/lua/endpoints/buy_and_use.lua |
| 2 | + |
| 3 | +-- ========================================================================== |
| 4 | +-- Buy and Use Endpoint Params |
| 5 | +-- ========================================================================== |
| 6 | + |
| 7 | +---@class Request.Endpoint.BuyAndUse.Params |
| 8 | +---@field card integer? 0-based index of shop consumable to buy and use |
| 9 | + |
| 10 | +-- ========================================================================== |
| 11 | +-- Buy and Use Endpoint |
| 12 | +-- ========================================================================== |
| 13 | + |
| 14 | +---@type Endpoint |
| 15 | +return { |
| 16 | + |
| 17 | + name = "buy_and_use", |
| 18 | + |
| 19 | + description = "Buy and use a consumable card from the shop in one step", |
| 20 | + |
| 21 | + schema = { |
| 22 | + card = { |
| 23 | + type = "integer", |
| 24 | + required = false, |
| 25 | + description = "0-based index of shop consumable to buy and use", |
| 26 | + }, |
| 27 | + }, |
| 28 | + |
| 29 | + requires_state = { G.STATES.SHOP }, |
| 30 | + |
| 31 | + ---@param args Request.Endpoint.BuyAndUse.Params |
| 32 | + ---@param send_response fun(response: Response.Endpoint) |
| 33 | + execute = function(args, send_response) |
| 34 | + sendDebugMessage("buy_and_use()", "BB.ENDPOINTS") |
| 35 | + local gamestate = BB_GAMESTATE.get_gamestate() |
| 36 | + |
| 37 | + -- 1. Validate card param is provided |
| 38 | + if not args.card then |
| 39 | + send_response({ |
| 40 | + message = "Invalid arguments. You must provide: card", |
| 41 | + name = BB_ERROR_NAMES.BAD_REQUEST, |
| 42 | + }) |
| 43 | + return |
| 44 | + end |
| 45 | + |
| 46 | + local area = gamestate.shop |
| 47 | + local pos = args.card + 1 |
| 48 | + |
| 49 | + -- 2. Validate shop has cards |
| 50 | + if #area.cards == 0 then |
| 51 | + send_response({ |
| 52 | + message = "No consumables in the shop. Use `reroll` to restock the shop.", |
| 53 | + name = BB_ERROR_NAMES.BAD_REQUEST, |
| 54 | + }) |
| 55 | + return |
| 56 | + end |
| 57 | + |
| 58 | + -- 3. Validate card index is in range |
| 59 | + if not area.cards[pos] then |
| 60 | + send_response({ |
| 61 | + message = "Card index out of range. Index: " .. args.card .. ", Available cards: " .. area.count, |
| 62 | + name = BB_ERROR_NAMES.BAD_REQUEST, |
| 63 | + }) |
| 64 | + return |
| 65 | + end |
| 66 | + |
| 67 | + local card = area.cards[pos] |
| 68 | + local live_card = G.shop_jokers.cards[pos] |
| 69 | + |
| 70 | + -- 4. Affordability check (mirrors can_buy_and_use; handles Credit Card joker) |
| 71 | + local available_money = G.GAME.dollars - G.GAME.bankrupt_at |
| 72 | + if card.cost.buy > 0 and card.cost.buy > available_money then |
| 73 | + send_response({ |
| 74 | + message = "Card is not affordable. Cost: " .. card.cost.buy .. ", Available money: " .. available_money, |
| 75 | + name = BB_ERROR_NAMES.BAD_REQUEST, |
| 76 | + }) |
| 77 | + return |
| 78 | + end |
| 79 | + |
| 80 | + -- 5. Feasibility gate. Mirrors G.FUNCS.can_buy_and_use, which the game only |
| 81 | + -- ever attaches to consumable shop cards. Card:can_use_consumeable() |
| 82 | + -- indexes ability.consumeable, so guard non-consumables (jokers/playing |
| 83 | + -- cards) for which the Buy-and-Use button never exists. (Per §2.3 we do |
| 84 | + -- NOT pre-call check_use here — the game calls it inside use_card.) |
| 85 | + local is_consumable = live_card.ability and live_card.ability.consumeable ~= nil |
| 86 | + if not is_consumable or not live_card:can_use_consumeable() then |
| 87 | + send_response({ |
| 88 | + message = "Consumable '" |
| 89 | + .. (live_card.ability.name or card.label or "Unknown") |
| 90 | + .. "' cannot be buy-and-used at this time.", |
| 91 | + name = BB_ERROR_NAMES.NOT_ALLOWED, |
| 92 | + }) |
| 93 | + return |
| 94 | + end |
| 95 | + |
| 96 | + -- Capture initial state for completion detection |
| 97 | + local initial_shop_count = (G.shop_jokers and G.shop_jokers.config and G.shop_jokers.config.card_count or 0) |
| 98 | + local initial_money = gamestate.money |
| 99 | + |
| 100 | + -- Log what we're buy-and-using |
| 101 | + local item_name = live_card.ability.name or card.label or "Unknown" |
| 102 | + sendInfoMessage(string.format("Buy-and-using '%s' for $%d", item_name, card.cost.buy), "BB.ENDPOINTS") |
| 103 | + |
| 104 | + -- Programmatic mock-button invocation (ephemeral inline table; routes to |
| 105 | + -- the buy_and_use branch inside G.FUNCS.buy_from_shop via config.id). |
| 106 | + G.FUNCS.buy_from_shop({ |
| 107 | + config = { |
| 108 | + id = "buy_and_use", |
| 109 | + ref_table = live_card, |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + -- Completion detection: union of the buy-phase and use-phase terminal |
| 114 | + -- conditions. Race-free: during use the state is PLAY_TAROT (predicate |
| 115 | + -- waits); the Ankh-noop never leaves SHOP and never acquires the lock. |
| 116 | + G.E_MANAGER:add_event(Event({ |
| 117 | + trigger = "condition", |
| 118 | + blocking = false, |
| 119 | + func = function() |
| 120 | + local shop_count = (G.shop_jokers and G.shop_jokers.config and G.shop_jokers.config.card_count or 0) |
| 121 | + local shop_decreased = (shop_count == initial_shop_count - 1) |
| 122 | + local money_deducted = (G.GAME.dollars == initial_money - card.cost.buy) |
| 123 | + if shop_decreased and money_deducted and G.STATE == G.STATES.SHOP and not G.CONTROLLER.locks.use then |
| 124 | + sendDebugMessage("buy_and_use() → ok", "BB.ENDPOINTS") |
| 125 | + send_response(BB_GAMESTATE.get_gamestate()) |
| 126 | + return true |
| 127 | + end |
| 128 | + return false |
| 129 | + end, |
| 130 | + })) |
| 131 | + end, |
| 132 | +} |
0 commit comments