@@ -658,8 +658,8 @@ API.functions["shop"] = function(args)
658658 return
659659 end
660660
661- -- Get card buy button from zero based index
662- local card_pos = args .index + 1 -- Lua is 1-based
661+ -- Get card index (1-based) and shop area
662+ local card_pos = args .index + 1
663663 local area = G .shop_jokers
664664
665665 -- Validate card index is in range
@@ -672,30 +672,54 @@ API.functions["shop"] = function(args)
672672 return
673673 end
674674
675- -- Get card buy button from zero based index
676- local card = area .cards [card_pos ]
675+ -- Evaluate card
676+ local card = area .cards [card_pos ]
677677
678- -- Click the card
679- card :click ()
680-
681- -- Wait for the shop to update
682- love .timer .sleep (0.5 )
683-
684- -- Get the buy button
685- local buy_btn = card .children and card .children .buy_button
686-
687- -- activate the buy button
688- G .FUNCS [buy_btn .config .button ](buy_btn )
678+ -- Check if the card can be afforded
679+ if card .cost > G .GAME .dollars then
680+ API .send_error_response (
681+ " Card is not affordable" ,
682+ ERROR_CODES .INVALID_ACTION ,
683+ { index = args .index , cost = card .cost , dollars = G .GAME .dollars }
684+ )
685+ return
686+ end
689687
688+ -- Ensure card has an ability set (should be redundant)
689+ if not card .ability or not card .ability .set then
690+ API .send_error_response (
691+ " Card has no ability set, can't check consumable area" ,
692+ ERROR_CODES .INVALID_ACTION ,
693+ { index = args .index }
694+ )
695+ return
696+ end
690697
691- sendDebugMessage (" Card: " .. tostring (card ))
692- sendDebugMessage (" Card Label: " .. tostring (card .label ))
693- sendDebugMessage (" Buy button: " .. tostring (buy_btn ))
698+ -- Ensure card area is not full
699+ if card .ability .set == " Joker" then
700+ -- Check for free joker slots
701+ if G .jokers and G .jokers .cards and G .jokers .card_limit and # G .jokers .cards >= G .jokers .card_limit then
702+ API .send_error_response (
703+ " Can't purchase joker card, joker slots are full" ,
704+ ERROR_CODES .INVALID_ACTION ,
705+ { index = args .index }
706+ )
707+ end
708+ elseif card .ability .set == " Planet" or card .ability .set == " Tarot" or card .ability .set == " Spectral" then
709+ -- Check for free consumable slots (typo is intentional, present in source)
710+ 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
711+ API .send_error_response (
712+ " Can't purchase consumable card, consumable slots are full" ,
713+ ERROR_CODES .INVALID_ACTION ,
714+ { index = args .index }
715+ )
716+ end
717+ end
694718
695- -- Wait for the shop to update
696- love .timer .sleep (0.5 )
697719
698- if not buy_btn then
720+ -- Validate that some purchase button exists (should be a redundant check)
721+ local card_buy_button = card .children .buy_button and card .children .buy_button .definition
722+ if not card_buy_button then
699723 API .send_error_response (
700724 " Card has no buy button" ,
701725 ERROR_CODES .INVALID_ACTION ,
@@ -704,24 +728,14 @@ API.functions["shop"] = function(args)
704728 return
705729 end
706730
707- -- -- Validate card is purchasable
708- -- This is wrong, and would be a redundant check anyways.
709- -- Can_buy is used to create the buy button.
710- -- if not G.FUNCS.can_buy(buy_btn) then
711- -- API.send_error_response(
712- -- "Card is not purchasable",
713- -- ERROR_CODES.INVALID_ACTION,
714- -- { index = args.index }
715- -- )
716- -- return
717- -- end
718-
719- -- Execute buy_from_shop action in G.FUNCS
720- -- G.FUNCS.buy_from_shop(buy_btn)
731+ -- activate the buy button using the UI element handler
732+ G .FUNCS .buy_from_shop (card_buy_button )
733+
721734
722735 -- send response once shop is updated
723736 --- @type PendingRequest
724737 API .pending_requests [" shop" ] = {
738+ -- TODO: This sends the update before the dollars have been updated.
725739 condition = function ()
726740 return G .STATE == G .STATES .SHOP
727741 and # G .E_MANAGER .queues .base < EVENT_QUEUE_THRESHOLD
@@ -732,7 +746,8 @@ API.functions["shop"] = function(args)
732746 API .send_response (game_state )
733747 end ,
734748 }
735- -- TODO: add other shop actions
749+
750+ -- TODO: add other shop actions [buy_and_use | reroll | open_pack | redeem_voucher]
736751 else
737752 API .send_error_response (
738753 " Invalid action for shop" ,
0 commit comments