@@ -190,6 +190,70 @@ function LOG.hook_toggle_shop()
190190 sendDebugMessage (" Hooked into G.FUNCS.toggle_shop for logging" , " LOG" )
191191end
192192
193+ -- -----------------------------------------------------------------------------
194+ -- hand_rearrange Hook
195+ -- -----------------------------------------------------------------------------
196+
197+ --- Hooks into CardArea:align_cards for hand reordering detection
198+ function LOG .hook_hand_rearrange ()
199+ local original_function = CardArea .align_cards
200+ local previous_order = {}
201+ CardArea .align_cards = function (self , ...)
202+ -- Only monitor hand cards
203+ --- @diagnostic disable-next-line : undefined-field
204+ if self .config and self .config .type == " hand" and self .cards then
205+ -- Call the original function with all arguments
206+ local result = original_function (self , ... )
207+
208+ --- @diagnostic disable-next-line : undefined-field
209+ if self .config .card_count ~= # self .cards then
210+ -- We're drawing cards from the deck
211+ return result
212+ end
213+
214+ -- Capture current card order after alignment
215+ local current_order = {}
216+ --- @diagnostic disable-next-line : undefined-field
217+ for i , card in ipairs (self .cards ) do
218+ current_order [i ] = card .sort_id
219+ end
220+
221+ if utils .sets_equal (previous_order , current_order ) then
222+ local order_changed = false
223+ for i = 1 , # current_order do
224+ if previous_order [i ] ~= current_order [i ] then
225+ order_changed = true
226+ break
227+ end
228+ end
229+
230+ if order_changed then
231+ -- TODO: compute the rearrangement from previous_order and current_order
232+ -- and use as the arguments to the rearrange_hand API call
233+ -- So remove previous_order and current_order and use cards
234+ -- Then remove sendInfoMessage calls
235+ local arguments = {
236+ previous_order = previous_order ,
237+ current_order = current_order ,
238+ }
239+ local name = " rearrange_hand"
240+ sendInfoMessage (" Hand rearranged - cards reordered" , " LOG" )
241+ sendInfoMessage (" Before: " .. json .encode (previous_order ), " LOG" )
242+ sendInfoMessage (" After: " .. json .encode (current_order ), " LOG" )
243+ LOG .write (name , arguments )
244+ end
245+ end
246+
247+ previous_order = current_order
248+ return result
249+ else
250+ -- For non-hand card areas, just call the original function
251+ return original_function (self , ... )
252+ end
253+ end
254+ sendInfoMessage (" Hooked into CardArea:align_cards for hand rearrange logging" , " LOG" )
255+ end
256+
193257-- TODO: add hooks for other shop functions
194258
195259-- =============================================================================
@@ -216,6 +280,7 @@ function LOG.init()
216280 LOG .hook_discard_cards_from_highlighted ()
217281 LOG .hook_cash_out ()
218282 LOG .hook_toggle_shop ()
283+ LOG .hook_hand_rearrange ()
219284
220285 sendInfoMessage (" Logger initialized" , " LOG" )
221286end
0 commit comments