@@ -555,6 +555,84 @@ API.functions["shop"] = function(args)
555555 API .send_response (game_state )
556556 end ,
557557 }
558+ elseif action == " buy_card" then
559+
560+ -- TODO: Implement buy card.
561+ -- Get card to buy from zero-based index
562+ -- Execute buy_from_shop action in G.FUNCS
563+ -- Defer sending response until the shop has updated
564+
565+ -- Validate index argument
566+ if args .index == nil then
567+ API .send_error_response (
568+ " Missing required field: index" ,
569+ ERROR_CODES .INVALID_PARAMETER ,
570+ { field = " index" }
571+ )
572+ return
573+ end
574+ if type (args .index ) ~= " number" or args .index < 0 then
575+ API .send_error_response (
576+ " Index must be a non-negative number" ,
577+ ERROR_CODES .INVALID_PARAMETER ,
578+ { index = args .index }
579+ )
580+ return
581+ end
582+
583+ -- Get card buy button from zero based index
584+ local card_pos = args .index + 1 -- Lua is 1-based
585+ local area = G .shop_jokers
586+
587+ -- Validate card index is in range
588+ if not area or not area .cards or not area .cards [card_pos ] then
589+ API .send_error_response (
590+ " Card index out of range" ,
591+ ERROR_CODES .PARAMETER_OUT_OF_RANGE ,
592+ { index = args .index , max_index = # area .cards - 1 }
593+ )
594+ return
595+ end
596+
597+ -- Get card buy button from zero based index
598+ local card = area .cards [card_pos ]
599+ local buy_btn = card .children and card .children .buy_button
600+
601+ if not buy_btn then
602+ API .send_error_response (
603+ " Card has no buy button" ,
604+ ERROR_CODES .INVALID_ACTION ,
605+ { index = args .index }
606+ )
607+ return
608+ end
609+
610+ -- Validate card is purchasable
611+ if not buy_btn :can_buy () then
612+ API .send_error_response (
613+ " Card is not purchasable" ,
614+ ERROR_CODES .INVALID_ACTION ,
615+ { index = args .index }
616+ )
617+ return
618+ end
619+
620+ -- Execute buy_from_shop action in G.FUNCS
621+ G .FUNCS .buy_from_shop (buy_btn )
622+
623+ -- send response once shop is updated
624+ --- @type PendingRequest
625+ API .pending_requests [" shop" ] = {
626+ condition = function ()
627+ return G .STATE == G .STATES .SHOP
628+ and # G .E_MANAGER .queues .base < EVENT_QUEUE_THRESHOLD
629+ and G .STATE_COMPLETE
630+ end ,
631+ action = function ()
632+ local game_state = utils .get_game_state ()
633+ API .send_response (game_state )
634+ end ,
635+ }
558636 -- TODO: add other shop actions
559637 else
560638 API .send_error_response (
0 commit comments