Skip to content

Commit 6c866b8

Browse files
stirbyS1M0N38
authored andcommitted
added prototype buy_card action with types
1 parent 6cd1746 commit 6c866b8

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
@@ -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(

src/lua/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
---@field cards number[] Array of card indices for every card in hand (0-based)
5454

5555
---@class ShopActionArgs
56-
---@field action "next_round" The action to perform
56+
---@field action "next_round" | "buy_card" The action to perform
57+
---@field index? number The index of the card to act on (buy, buy_and_use, redeem, open) (0-based)
5758

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

0 commit comments

Comments
 (0)