Skip to content

Commit b63c500

Browse files
committed
feat: add rearrange jokers endpoint
1 parent 4f4a9b8 commit b63c500

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

src/lua/api.lua

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ local json = require("json")
33

44
-- Constants
55
local SOCKET_TIMEOUT = 0
6-
-- The threshold for determining when game state transitions are complete.
7-
-- This value represents the maximum number of events allowed in the game's event queue
8-
-- to consider the game idle and waiting for user action. When the queue has fewer than
9-
-- 3 events, the game is considered stable enough to process API responses. This is a
10-
-- heuristic based on empirical testing to ensure smooth gameplay without delays.
11-
local EVENT_QUEUE_THRESHOLD = 3
126

137
-- Error codes for standardized error handling
148
local ERROR_CODES = {
@@ -536,6 +530,82 @@ API.functions["rearrange_hand"] = function(args)
536530
}
537531
end
538532

533+
---Rearranges the jokers based on the given card indices
534+
---Call G.FUNCS.rearrange_jokers(new_jokers)
535+
---@param args RearrangeJokersArgs The card indices to rearrange the jokers with
536+
API.functions["rearrange_jokers"] = function(args)
537+
-- Validate required parameters
538+
local success, error_message, error_code, context = validate_request(args, { "jokers" })
539+
540+
if not success then
541+
---@cast error_message string
542+
---@cast error_code string
543+
API.send_error_response(error_message, error_code, context)
544+
return
545+
end
546+
547+
-- Validate that jokers exist
548+
if not G.jokers or not G.jokers.cards or #G.jokers.cards == 0 then
549+
API.send_error_response(
550+
"No jokers available to rearrange",
551+
ERROR_CODES.MISSING_GAME_OBJECT,
552+
{ jokers_available = false }
553+
)
554+
return
555+
end
556+
557+
-- Validate number of jokers is equal to the number of jokers in the joker area
558+
if #args.jokers ~= #G.jokers.cards then
559+
API.send_error_response(
560+
"Invalid number of jokers to rearrange",
561+
ERROR_CODES.PARAMETER_OUT_OF_RANGE,
562+
{ jokers_count = #args.jokers, valid_range = tostring(#G.jokers.cards) }
563+
)
564+
return
565+
end
566+
567+
-- Convert incoming indices from 0-based to 1-based
568+
for i, joker_index in ipairs(args.jokers) do
569+
args.jokers[i] = joker_index + 1
570+
end
571+
572+
-- Create a new joker array to swap card indices
573+
local new_jokers = {}
574+
for _, old_index in ipairs(args.jokers) do
575+
local card = G.jokers.cards[old_index]
576+
if not card then
577+
API.send_error_response(
578+
"Joker index out of range",
579+
ERROR_CODES.PARAMETER_OUT_OF_RANGE,
580+
{ index = old_index, max_index = #G.jokers.cards }
581+
)
582+
return
583+
end
584+
table.insert(new_jokers, card)
585+
end
586+
587+
G.jokers.cards = new_jokers
588+
589+
-- Update each joker's order field so future sort('order') calls work correctly
590+
for i, card in ipairs(G.jokers.cards) do
591+
if card.ability then
592+
card.ability.order = i
593+
end
594+
if card.config and card.config.center then
595+
card.config.center.order = i
596+
end
597+
end
598+
599+
---@type PendingRequest
600+
API.pending_requests["rearrange_jokers"] = {
601+
condition = utils.COMPLETION_CONDITIONS.rearrange_jokers,
602+
action = function()
603+
local game_state = utils.get_game_state()
604+
API.send_response(game_state)
605+
end,
606+
}
607+
end
608+
539609
---Cashes out from the current round to enter the shop
540610
---Call G.FUNCS.cash_out() to cash out from the current round to enter the shop.
541611
---@param _ table Arguments (not used)

0 commit comments

Comments
 (0)