@@ -639,6 +639,84 @@ API.functions["shop"] = function(args)
639639 API .send_response (game_state )
640640 end ,
641641 }
642+ elseif action == " buy_card" then
643+
644+ -- TODO: Implement buy card.
645+ -- Get card to buy from zero-based index
646+ -- Execute buy_from_shop action in G.FUNCS
647+ -- Defer sending response until the shop has updated
648+
649+ -- Validate index argument
650+ if args .index == nil then
651+ API .send_error_response (
652+ " Missing required field: index" ,
653+ ERROR_CODES .INVALID_PARAMETER ,
654+ { field = " index" }
655+ )
656+ return
657+ end
658+ if type (args .index ) ~= " number" or args .index < 0 then
659+ API .send_error_response (
660+ " Index must be a non-negative number" ,
661+ ERROR_CODES .INVALID_PARAMETER ,
662+ { index = args .index }
663+ )
664+ return
665+ end
666+
667+ -- Get card buy button from zero based index
668+ local card_pos = args .index + 1 -- Lua is 1-based
669+ local area = G .shop_jokers
670+
671+ -- Validate card index is in range
672+ if not area or not area .cards or not area .cards [card_pos ] then
673+ API .send_error_response (
674+ " Card index out of range" ,
675+ ERROR_CODES .PARAMETER_OUT_OF_RANGE ,
676+ { index = args .index , max_index = # area .cards - 1 }
677+ )
678+ return
679+ end
680+
681+ -- Get card buy button from zero based index
682+ local card = area .cards [card_pos ]
683+ local buy_btn = card .children and card .children .buy_button
684+
685+ if not buy_btn then
686+ API .send_error_response (
687+ " Card has no buy button" ,
688+ ERROR_CODES .INVALID_ACTION ,
689+ { index = args .index }
690+ )
691+ return
692+ end
693+
694+ -- Validate card is purchasable
695+ if not buy_btn :can_buy () then
696+ API .send_error_response (
697+ " Card is not purchasable" ,
698+ ERROR_CODES .INVALID_ACTION ,
699+ { index = args .index }
700+ )
701+ return
702+ end
703+
704+ -- Execute buy_from_shop action in G.FUNCS
705+ G .FUNCS .buy_from_shop (buy_btn )
706+
707+ -- send response once shop is updated
708+ --- @type PendingRequest
709+ API .pending_requests [" shop" ] = {
710+ condition = function ()
711+ return G .STATE == G .STATES .SHOP
712+ and # G .E_MANAGER .queues .base < EVENT_QUEUE_THRESHOLD
713+ and G .STATE_COMPLETE
714+ end ,
715+ action = function ()
716+ local game_state = utils .get_game_state ()
717+ API .send_response (game_state )
718+ end ,
719+ }
642720 -- TODO: add other shop actions
643721 else
644722 API .send_error_response (
0 commit comments