Skip to content

Commit 0845f3a

Browse files
committed
added extra checks to fixed buy-card function
1 parent 2729b3b commit 0845f3a

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

src/lua/api.lua

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ API.functions["shop"] = function(args)
662662
return
663663
end
664664

665-
-- Get card buy button from zero based index
666-
local card_pos = args.index + 1 -- Lua is 1-based
665+
-- Get card index (1-based) and shop area
666+
local card_pos = args.index + 1
667667
local area = G.shop_jokers
668668

669669
-- Validate card index is in range
@@ -676,30 +676,54 @@ API.functions["shop"] = function(args)
676676
return
677677
end
678678

679-
-- Get card buy button from zero based index
680-
local card = area.cards[card_pos]
679+
-- Evaluate card
680+
local card = area.cards[card_pos]
681681

682-
-- Click the card
683-
card:click()
684-
685-
-- Wait for the shop to update
686-
love.timer.sleep(0.5)
687-
688-
-- Get the buy button
689-
local buy_btn = card.children and card.children.buy_button
690-
691-
-- activate the buy button
692-
G.FUNCS[buy_btn.config.button](buy_btn)
682+
-- Check if the card can be afforded
683+
if card.cost > G.GAME.dollars then
684+
API.send_error_response(
685+
"Card is not affordable",
686+
ERROR_CODES.INVALID_ACTION,
687+
{ index = args.index, cost = card.cost, dollars = G.GAME.dollars }
688+
)
689+
return
690+
end
693691

692+
-- Ensure card has an ability set (should be redundant)
693+
if not card.ability or not card.ability.set then
694+
API.send_error_response(
695+
"Card has no ability set, can't check consumable area",
696+
ERROR_CODES.INVALID_ACTION,
697+
{ index = args.index }
698+
)
699+
return
700+
end
694701

695-
sendDebugMessage("Card: " .. tostring(card))
696-
sendDebugMessage("Card Label: " .. tostring(card.label))
697-
sendDebugMessage("Buy button: " .. tostring(buy_btn))
702+
-- Ensure card area is not full
703+
if card.ability.set == "Joker" then
704+
-- Check for free joker slots
705+
if G.jokers and G.jokers.cards and G.jokers.card_limit and #G.jokers.cards >= G.jokers.card_limit then
706+
API.send_error_response(
707+
"Can't purchase joker card, joker slots are full",
708+
ERROR_CODES.INVALID_ACTION,
709+
{ index = args.index }
710+
)
711+
end
712+
elseif card.ability.set == "Planet" or card.ability.set == "Tarot" or card.ability.set == "Spectral" then
713+
-- Check for free consumable slots (typo is intentional, present in source)
714+
if G.GAME.consumeables and G.GAME.consumeables.cards and G.GAME.consumeables.card_limit and #G.GAME.consumeables.cards >= G.GAME.consumeables.card_limit then
715+
API.send_error_response(
716+
"Can't purchase consumable card, consumable slots are full",
717+
ERROR_CODES.INVALID_ACTION,
718+
{ index = args.index }
719+
)
720+
end
721+
end
698722

699-
-- Wait for the shop to update
700-
love.timer.sleep(0.5)
701723

702-
if not buy_btn then
724+
-- Validate that some purchase button exists (should be a redundant check)
725+
local card_buy_button = card.children.buy_button and card.children.buy_button.definition
726+
if not card_buy_button then
703727
API.send_error_response(
704728
"Card has no buy button",
705729
ERROR_CODES.INVALID_ACTION,
@@ -708,24 +732,14 @@ API.functions["shop"] = function(args)
708732
return
709733
end
710734

711-
-- -- Validate card is purchasable
712-
-- This is wrong, and would be a redundant check anyways.
713-
-- Can_buy is used to create the buy button.
714-
-- if not G.FUNCS.can_buy(buy_btn) then
715-
-- API.send_error_response(
716-
-- "Card is not purchasable",
717-
-- ERROR_CODES.INVALID_ACTION,
718-
-- { index = args.index }
719-
-- )
720-
-- return
721-
-- end
722-
723-
-- Execute buy_from_shop action in G.FUNCS
724-
-- G.FUNCS.buy_from_shop(buy_btn)
735+
-- activate the buy button using the UI element handler
736+
G.FUNCS.buy_from_shop(card_buy_button)
737+
725738

726739
-- send response once shop is updated
727740
---@type PendingRequest
728741
API.pending_requests["shop"] = {
742+
-- TODO: This sends the update before the dollars have been updated.
729743
condition = function()
730744
return G.STATE == G.STATES.SHOP
731745
and #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD
@@ -736,7 +750,8 @@ API.functions["shop"] = function(args)
736750
API.send_response(game_state)
737751
end,
738752
}
739-
-- TODO: add other shop actions
753+
754+
-- TODO: add other shop actions [buy_and_use | reroll | open_pack | redeem_voucher]
740755
else
741756
API.send_error_response(
742757
"Invalid action for shop",

0 commit comments

Comments
 (0)