Skip to content

Commit c9fe966

Browse files
committed
feat: add hook for joker rearrangement
1 parent b63c500 commit c9fe966

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

src/lua/log.lua

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,33 +253,49 @@ function hook_reroll_shop()
253253
end
254254

255255
-- -----------------------------------------------------------------------------
256-
-- hand_rearrange Hook
256+
-- hand_rearrange Hook (also handles joker rearrange)
257257
-- -----------------------------------------------------------------------------
258258

259-
---Hooks into CardArea:align_cards for hand reordering detection
259+
---Hooks into CardArea:align_cards for hand and joker reordering detection
260260
function hook_hand_rearrange()
261261
local original_function = CardArea.align_cards
262-
local previous_order = {}
262+
local previous_orders = {
263+
hand = {},
264+
joker = {},
265+
}
266+
-- local previous_hand_order = {}
267+
-- local previous_joker_order = {}
263268
CardArea.align_cards = function(self, ...)
264-
-- Only monitor hand cards
265-
---@diagnostic disable-next-line: undefined-field
266-
if self.config and self.config.type == "hand" and self.cards then
269+
-- Monitor both hand and joker cards
270+
if
271+
---@diagnostic disable-next-line: undefined-field
272+
self.config
273+
---@diagnostic disable-next-line: undefined-field
274+
and (self.config.type == "hand" or self.config.type == "joker")
275+
---@diagnostic disable-next-line: undefined-field
276+
and self.cards
277+
---@diagnostic disable-next-line: undefined-field
278+
and #self.cards > 0
279+
then
267280
-- Call the original function with all arguments
268281
local result = original_function(self, ...)
269282

270283
---@diagnostic disable-next-line: undefined-field
271284
if self.config.card_count ~= #self.cards then
272-
-- We're drawing cards from the deck
285+
-- We're adding/removing cards
273286
return result
274287
end
275288

276-
-- Capture current card order after alignment
277289
local current_order = {}
290+
-- Capture current card order after alignment
278291
---@diagnostic disable-next-line: undefined-field
279292
for i, card in ipairs(self.cards) do
280293
current_order[i] = card.sort_id
281294
end
282295

296+
---@diagnostic disable-next-line: undefined-field
297+
previous_order = previous_orders[self.config.type]
298+
283299
if utils.sets_equal(previous_order, current_order) then
284300
local order_changed = false
285301
for i = 1, #current_order do
@@ -303,10 +319,26 @@ function hook_hand_rearrange()
303319
cards[pos] = lookup[card_id]
304320
end
305321

306-
local function_call = {
307-
name = "rearrange_hand",
308-
arguments = { cards = cards },
309-
}
322+
local function_call
323+
324+
if self.config.type == "hand" then ---@diagnostic disable-line: undefined-field
325+
function_call = {
326+
name = "rearrange_hand",
327+
arguments = { cards = cards },
328+
}
329+
elseif self.config.type == "joker" then ---@diagnostic disable-line: undefined-field
330+
sendInfoMessage("Logging joker rearrangement", "LOG")
331+
function_call = {
332+
name = "rearrange_jokers",
333+
arguments = { jokers = cards },
334+
}
335+
else
336+
function_call = {
337+
name = "unknown_rearrange",
338+
arguments = {},
339+
}
340+
sendErrorMessage("Unknown card type for rearrange", "LOG")
341+
end
310342

311343
-- NOTE: We cannot schedule a log write at this point because we do not have
312344
-- access to the game state before the function call. The game state is only
@@ -335,14 +367,16 @@ function hook_hand_rearrange()
335367
end
336368
end
337369

338-
previous_order = current_order
370+
---@diagnostic disable-next-line: undefined-field
371+
previous_orders[self.config.type] = current_order
372+
339373
return result
340374
else
341-
-- For non-hand card areas, just call the original function
375+
-- For non-hand/joker card areas, just call the original function
342376
return original_function(self, ...)
343377
end
344378
end
345-
sendInfoMessage("Hooked into CardArea:align_cards for hand rearrange logging", "LOG")
379+
sendInfoMessage("Hooked into CardArea:align_cards for card rearrange logging", "LOG")
346380
end
347381

348382
-- TODO: add hooks for other shop functions

0 commit comments

Comments
 (0)