Skip to content

Commit 680fd45

Browse files
committed
feat(lua): improve logging for endpoints
1 parent ace84b5 commit 680fd45

11 files changed

Lines changed: 196 additions & 10 deletions

File tree

src/lua/core/dispatcher.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
---@type Validator
88
local Validator = assert(SMODS.load_file("src/lua/core/validator.lua"))()
9+
---@type BB_LOGGER
10+
local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))()
11+
local socket = require("socket")
912

1013
---@type table<integer, string>?
1114
local STATE_NAME_CACHE = nil
@@ -162,11 +165,15 @@ function BB_DISPATCHER.dispatch(request)
162165
BB_DISPATCHER.send_error("Unknown method: " .. request.method, BB_ERROR_NAMES.BAD_REQUEST)
163166
return
164167
end
165-
sendDebugMessage("Dispatching: " .. request.method, "BB.DISPATCHER")
168+
169+
-- Log incoming request with params
170+
local start_time = socket.gettime()
171+
sendDebugMessage(request.method .. BB_LOGGER.serialize_params(params), "BB.REQUEST")
166172

167173
-- TIER 2: Schema Validation
168174
local valid, err_msg, err_code = Validator.validate(params, endpoint.schema)
169175
if not valid then
176+
sendWarnMessage(request.method .. ": " .. (err_msg or "Validation failed"), "BB.VALIDATION")
170177
BB_DISPATCHER.send_error(err_msg or "Validation failed", err_code or BB_ERROR_NAMES.BAD_REQUEST)
171178
return
172179
end
@@ -186,6 +193,11 @@ function BB_DISPATCHER.dispatch(request)
186193
for _, state in ipairs(endpoint.requires_state) do
187194
table.insert(state_names, get_state_name(state))
188195
end
196+
local current_state_name = get_state_name(current_state)
197+
sendWarnMessage(
198+
string.format("%s: requires %s, current=%s", request.method, table.concat(state_names, "|"), current_state_name),
199+
"BB.STATE"
200+
)
189201
BB_DISPATCHER.send_error(
190202
"Method '" .. request.method .. "' requires one of these states: " .. table.concat(state_names, ", "),
191203
BB_ERROR_NAMES.INVALID_STATE
@@ -197,6 +209,14 @@ function BB_DISPATCHER.dispatch(request)
197209
-- TIER 4: Execute Endpoint
198210
local function send_response(response)
199211
if BB_DISPATCHER.Server then
212+
-- Log response with timing
213+
local duration_ms = (socket.gettime() - start_time) * 1000
214+
local is_error = response.message ~= nil
215+
if is_error then
216+
sendDebugMessage(string.format("%s ERR (%.0fms)", request.method, duration_ms), "BB.RESPONSE")
217+
else
218+
sendDebugMessage(string.format("%s OK (%.0fms)", request.method, duration_ms), "BB.RESPONSE")
219+
end
200220
BB_DISPATCHER.Server.send_response(response)
201221
else
202222
sendDebugMessage("Cannot send response - Server not initialized", "BB.DISPATCHER")
@@ -206,6 +226,7 @@ function BB_DISPATCHER.dispatch(request)
206226
endpoint.execute(params, send_response)
207227
end)
208228
if not exec_success then
229+
sendErrorMessage(request.method .. ": " .. tostring(exec_error), "BB.EXEC")
209230
BB_DISPATCHER.send_error(tostring(exec_error), BB_ERROR_NAMES.INTERNAL_ERROR)
210231
end
211232
end

src/lua/endpoints/buy.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ return {
183183
return
184184
end
185185

186+
-- Log what we're buying
187+
local item_name = card.name or (card.ability and card.ability.name) or card.label or "Unknown"
188+
local item_type = args.voucher and "Voucher" or args.pack and "Booster" or card.set or "item"
189+
sendDebugMessage(string.format("Buying %s '%s' for $%d", item_type, item_name, card.cost.buy), "BB.ENDPOINTS")
190+
186191
-- Use appropriate function: use_card for vouchers, buy_from_shop for others
187192
if args.voucher or args.pack then
188193
G.FUNCS.use_card(btn)

src/lua/endpoints/discard.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-- src/lua/endpoints/discard.lua
22

3+
---@type BB_LOGGER
4+
local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))()
5+
36
-- ==========================================================================
47
-- Discard Endpoint Params
58
-- ==========================================================================
@@ -76,6 +79,14 @@ return {
7679
G.hand.cards[card_index + 1]:click()
7780
end
7881

82+
-- Log the cards being discarded
83+
local card_str = BB_LOGGER.format_playing_cards(G.hand.cards, args.cards)
84+
local remaining = G.GAME.current_round.discards_left - 1
85+
sendDebugMessage(
86+
string.format("Discarding %d cards: %s (%d discards left)", #args.cards, card_str, remaining),
87+
"BB.ENDPOINTS"
88+
)
89+
7990
---@diagnostic disable-next-line: undefined-field
8091
local discard_button = UIBox:get_UIE_by_ID("discard_button", G.buttons.UIRoot)
8192
assert(discard_button ~= nil, "discard() discard button not found")

src/lua/endpoints/pack.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-- src/lua/endpoints/pack.lua
22

3+
---@type BB_LOGGER
4+
local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))()
5+
36
-- ==========================================================================
47
-- Pack Select Endpoint Params
58
-- ==========================================================================
@@ -215,6 +218,19 @@ return {
215218
end
216219
end
217220

221+
-- Log what we're selecting
222+
local card_name = card.ability and card.ability.name or "Unknown"
223+
local card_set = card.ability and card.ability.set or card.set or "card"
224+
if args.targets and #args.targets > 0 then
225+
local targets = BB_LOGGER.format_playing_cards(G.hand.cards, args.targets)
226+
sendDebugMessage(
227+
string.format("Pack: selecting %s '%s' targeting: %s", card_set, card_name, targets),
228+
"BB.ENDPOINTS"
229+
)
230+
else
231+
sendDebugMessage(string.format("Pack: selecting %s '%s'", card_set, card_name), "BB.ENDPOINTS")
232+
end
233+
218234
-- Select the card by calling use_card
219235
local btn = {
220236
config = {
@@ -264,6 +280,8 @@ return {
264280

265281
-- Handle skip
266282
if args.skip then
283+
local pack_count = G.pack_cards.config and G.pack_cards.config.card_count or 0
284+
sendDebugMessage(string.format("Pack: skipping (%d cards remaining)", pack_count), "BB.ENDPOINTS")
267285
G.FUNCS.skip_booster({})
268286

269287
-- Wait for pack to close and return to shop

src/lua/endpoints/play.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-- src/lua/endpoints/play.lua
22

3+
---@type BB_LOGGER
4+
local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))()
5+
36
-- ==========================================================================
47
-- Play Endpoint Params
58
-- ==========================================================================
@@ -68,6 +71,10 @@ return {
6871
G.hand.cards[card_index + 1]:click()
6972
end
7073

74+
-- Log the cards being played
75+
local card_str = BB_LOGGER.format_playing_cards(G.hand.cards, args.cards)
76+
sendDebugMessage(string.format("Playing %d cards: %s", #args.cards, card_str), "BB.ENDPOINTS")
77+
7178
---@diagnostic disable-next-line: undefined-field
7279
local play_button = UIBox:get_UIE_by_ID("play_button", G.buttons.UIRoot)
7380
assert(play_button ~= nil, "play() play button not found")

src/lua/endpoints/rearrange.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ return {
130130

131131
assert(type(indices) == "table", "indices must be a table")
132132

133+
-- Log what we're rearranging
134+
local order_str = "[" .. table.concat(indices, ",") .. "]"
135+
sendDebugMessage(
136+
string.format("Rearranging %s (%d cards): %s", type_name, #source_array, order_str),
137+
"BB.ENDPOINTS"
138+
)
139+
133140
-- Validate permutation: correct length, no duplicates, all indices present
134141
-- Check length matches
135142
if #indices ~= #source_array then

src/lua/endpoints/reroll.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ return {
3636
return
3737
end
3838

39-
sendDebugMessage("Init reroll()", "BB.ENDPOINTS")
39+
-- Log reroll with cost and money
40+
sendDebugMessage(string.format("Rerolling shop (cost=$%d, money=$%d)", reroll_cost, G.GAME.dollars), "BB.ENDPOINTS")
4041
G.FUNCS.reroll_shop(nil)
4142

4243
-- Wait for shop state to confirm reroll completed
@@ -46,7 +47,7 @@ return {
4647
func = function()
4748
local done = G.STATE == G.STATES.SHOP
4849
if done then
49-
sendDebugMessage("Return reroll() - shop rerolled", "BB.ENDPOINTS")
50+
sendDebugMessage(string.format("Return reroll() money=$%d", G.GAME.dollars), "BB.ENDPOINTS")
5051
send_response(BB_GAMESTATE.get_gamestate())
5152
end
5253
return done

src/lua/endpoints/select.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ return {
3333
local select_button = blind_pane:get_UIE_by_ID("select_blind_button")
3434
assert(select_button ~= nil, "select() select button not found: " .. current_blind)
3535

36+
-- Log which blind we're selecting
37+
local blind_info = BB_GAMESTATE.get_blinds_info()[string.lower(current_blind)]
38+
local blind_name = blind_info and blind_info.name or current_blind
39+
local chips = blind_info and blind_info.chips or "?"
40+
sendDebugMessage(
41+
string.format("Selecting %s (%s), chips required: %s", current_blind, blind_name, tostring(chips)),
42+
"BB.ENDPOINTS"
43+
)
44+
3645
-- Execute blind selection
3746
G.FUNCS.select_blind(select_button)
3847

src/lua/endpoints/sell.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ return {
102102
local expected_money = initial_money + card.sell_cost
103103
local card_id = card.sort_id
104104

105+
-- Log what we're selling
106+
local item_name = card.ability and card.ability.name or "Unknown"
107+
sendDebugMessage(string.format("Selling %s '%s' for $%d", sell_type, item_name, card.sell_cost), "BB.ENDPOINTS")
108+
105109
-- Create mock UI element for G.FUNCS.sell_card
106110
local mock_element = {
107111
config = {

src/lua/endpoints/use.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-- src/lua/endpoints/use.lua
22

3+
---@type BB_LOGGER
4+
local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))()
5+
36
-- ==========================================================================
47
-- Use Endpoint Params
58
-- ==========================================================================
@@ -150,11 +153,15 @@ return {
150153
local hand_card = G.hand.cards[card_idx + 1] -- Convert 0-based to 1-based
151154
G.hand:add_to_highlighted(hand_card, true) -- silent=true
152155
end
156+
end
153157

154-
sendDebugMessage(
155-
string.format("Selected %d cards for '%s'", #args.cards, consumable_card.ability.name),
156-
"BB.ENDPOINTS"
157-
)
158+
-- Log what we're using with target cards
159+
local cons_name = consumable_card.ability.name
160+
if args.cards and #args.cards > 0 then
161+
local targets = BB_LOGGER.format_playing_cards(G.hand.cards, args.cards)
162+
sendDebugMessage(string.format("Using '%s' on: %s", cons_name, targets), "BB.ENDPOINTS")
163+
else
164+
sendDebugMessage(string.format("Using '%s' (no targets)", cons_name), "BB.ENDPOINTS")
158165
end
159166

160167
-- Step 7: Game-Level Validation (e.g. try to use Familiar Spectral when G.hand is not available)
@@ -175,9 +182,6 @@ return {
175182
return
176183
end
177184

178-
-- Execution
179-
sendDebugMessage("Executing use() for consumable: " .. consumable_card.ability.name, "BB.ENDPOINTS")
180-
181185
-- Create mock UI element for game function
182186
local mock_element = {
183187
config = {

0 commit comments

Comments
 (0)