Skip to content

Commit 404de7d

Browse files
committed
feat: add rearrange consumeables endpoint
1 parent 02bf213 commit 404de7d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/lua/api.lua

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,82 @@ API.functions["rearrange_jokers"] = function(args)
606606
}
607607
end
608608

609+
---Rearranges the consumeables based on the given card indices
610+
---Call G.FUNCS.rearrange_consumeables(new_consumeables)
611+
---@param args RearrangeConsumeablesArgs The card indices to rearrange the consumeables with
612+
API.functions["rearrange_consumeables"] = function(args)
613+
-- Validate required parameters
614+
local success, error_message, error_code, context = validate_request(args, { "consumeables" })
615+
616+
if not success then
617+
---@cast error_message string
618+
---@cast error_code string
619+
API.send_error_response(error_message, error_code, context)
620+
return
621+
end
622+
623+
-- Validate that consumeables exist
624+
if not G.consumeables or not G.consumeables.cards or #G.consumeables.cards == 0 then
625+
API.send_error_response(
626+
"No consumeables available to rearrange",
627+
ERROR_CODES.MISSING_GAME_OBJECT,
628+
{ consumeables_available = false }
629+
)
630+
return
631+
end
632+
633+
-- Validate number of consumeables is equal to the number of consumeables in the consumeables area
634+
if #args.consumeables ~= #G.consumeables.cards then
635+
API.send_error_response(
636+
"Invalid number of consumeables to rearrange",
637+
ERROR_CODES.PARAMETER_OUT_OF_RANGE,
638+
{ consumeables_count = #args.consumeables, valid_range = tostring(#G.consumeables.cards) }
639+
)
640+
return
641+
end
642+
643+
-- Convert incoming indices from 0-based to 1-based
644+
for i, joker_index in ipairs(args.consumeables) do
645+
args.consumeables[i] = joker_index + 1
646+
end
647+
648+
-- Create a new consumeables array to swap card indices
649+
local new_consumables = {}
650+
for _, old_index in ipairs(args.consumeables) do
651+
local card = G.consumeables.cards[old_index]
652+
if not card then
653+
API.send_error_response(
654+
"Consumable index out of range",
655+
ERROR_CODES.PARAMETER_OUT_OF_RANGE,
656+
{ index = old_index, max_index = #G.consumeables.cards }
657+
)
658+
return
659+
end
660+
table.insert(new_consumables, card)
661+
end
662+
663+
G.consumeables.cards = new_consumables
664+
665+
-- Update each consumeable's order field so future sort('order') calls work correctly
666+
for i, card in ipairs(G.consumeables.cards) do
667+
if card.ability then
668+
card.ability.order = i
669+
end
670+
if card.config and card.config.center then
671+
card.config.center.order = i
672+
end
673+
end
674+
675+
---@type PendingRequest
676+
API.pending_requests["rearrange_consumeables"] = {
677+
condition = utils.COMPLETION_CONDITIONS["rearrange_consumeables"][""],
678+
action = function()
679+
local game_state = utils.get_game_state()
680+
API.send_response(game_state)
681+
end,
682+
}
683+
end
684+
609685
---Cashes out from the current round to enter the shop
610686
---Call G.FUNCS.cash_out() to cash out from the current round to enter the shop.
611687
---@param _ table Arguments (not used)

0 commit comments

Comments
 (0)