Skip to content

Commit 5254cd1

Browse files
committed
added prototype buy_card action with types
1 parent 11a0b0e commit 5254cd1

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/lua/api.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

src/lua/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
---@field cards number[] Array of card indices (0-based)
4949

5050
---@class ShopActionArgs
51-
---@field action "next_round" The action to perform
51+
---@field action "next_round" | "buy_card" The action to perform
52+
---@field index? number The index of the card to act on (buy, buy_and_use, redeem, open) (0-based)
5253

5354
-- TODO: add the other actions "reroll" | "buy" | "buy_and_use" | "redeem" | "open"
5455
--@field item number? The item to buy/buy_and_use/redeem/open (0-based)

0 commit comments

Comments
 (0)