Skip to content

Commit cddd451

Browse files
S1M0N38claude
andcommitted
refactor(api): improve code quality and documentation
- Add detailed documentation for main API functions - Remove placeholder TODO functions to reduce code bloat - Replace string-based G.FUNCS calls with direct function calls - Enhance type annotations and inline comments - Clean up blind selection logic with better variable naming Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b2acefe commit cddd451

File tree

1 file changed

+22
-55
lines changed

1 file changed

+22
-55
lines changed

src/lua/api.lua

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ API.functions["get_game_state"] = function(_)
120120
API.send_response(game_state)
121121
end
122122

123-
---Navigates to the main menu
123+
---Navigates to the main menu.
124+
---Call G.FUNCS.go_to_menu() to navigate to the main menu.
124125
---@param _ table Arguments (not used)
125126
API.functions["go_to_menu"] = function(_)
126127
if G.STATE == G.STATES.MENU and G.MAIN_MENU_UI then
@@ -143,12 +144,12 @@ API.functions["go_to_menu"] = function(_)
143144
end
144145

145146
---Starts a new game run with specified parameters
147+
---Call G.FUNCS.start_run() to start a new game run with specified parameters.
146148
---@param args StartRunArgs The run configuration
147149
API.functions["start_run"] = function(args)
148150
-- Reset the game
149-
local play_button = G.MAIN_MENU_UI:get_UIE_by_ID("main_menu_play")
150-
G.FUNCS[play_button.config.button]({ config = {} })
151-
G.FUNCS.exit_overlay_menu({})
151+
G.FUNCS.setup_run({ config = {} })
152+
G.FUNCS.exit_overlay_menu()
152153

153154
-- Set the deck
154155
local deck_found = false
@@ -194,6 +195,7 @@ API.functions["start_run"] = function(args)
194195
end
195196

196197
---Skips or selects the current blind
198+
---Call G.FUNCS.select_blind(button) or G.FUNCS.skip_blind(button)
197199
---@param args BlindActionArgs The blind action to perform
198200
API.functions["skip_or_select_blind"] = function(args)
199201
-- Validate current game state is appropriate for blind selection
@@ -202,11 +204,14 @@ API.functions["skip_or_select_blind"] = function(args)
202204
return
203205
end
204206

207+
-- Get the current blind pane
205208
local current_blind = G.GAME.blind_on_deck
206-
local blind_obj = G.blind_select_opts[string.lower(current_blind)]
209+
assert(current_blind, "current_blind is nil")
210+
local blind_pane = G.blind_select_opts[string.lower(current_blind)]
211+
207212
if args.action == "select" then
208-
button = blind_obj:get_UIE_by_ID("select_blind_button")
209-
G.FUNCS[button.config.button](button)
213+
local button = blind_pane:get_UIE_by_ID("select_blind_button")
214+
G.FUNCS.select_blind(button)
210215
API.pending_requests["skip_or_select_blind"] = {
211216
condition = function()
212217
return G.GAME and G.GAME.facing_blind and G.STATE == G.STATES.SELECTING_HAND
@@ -218,13 +223,14 @@ API.functions["skip_or_select_blind"] = function(args)
218223
args = args,
219224
}
220225
elseif args.action == "skip" then
221-
button = blind_obj:get_UIE_by_ID("tag_" .. current_blind).children[2]
222-
G.FUNCS[button.config.button](button)
226+
local tag_element = blind_pane:get_UIE_by_ID("tag_" .. current_blind)
227+
local button = tag_element.children[2]
228+
G.FUNCS.skip_blind(button)
223229
API.pending_requests["skip_or_select_blind"] = {
224230
condition = function()
225231
local prev_state = {
226232
["Small"] = G.prev_small_state,
227-
["Large"] = G.prev_large_state,
233+
["Large"] = G.prev_large_state, -- this is Large not Big
228234
["Boss"] = G.prev_boss_state,
229235
}
230236
return prev_state[current_blind] == "Skipped"
@@ -241,6 +247,8 @@ API.functions["skip_or_select_blind"] = function(args)
241247
end
242248

243249
---Plays selected cards or discards them
250+
---Call G.FUNCS.play_cards_from_highlighted(play_button)
251+
---or G.FUNCS.discard_cards_from_highlighted(discard_button)
244252
---@param args HandActionArgs The hand action to perform
245253
API.functions["play_hand_or_discard"] = function(args)
246254
-- Validate current game state is appropriate for playing hand or discarding
@@ -278,11 +286,11 @@ API.functions["play_hand_or_discard"] = function(args)
278286
if args.action == "play_hand" then
279287
---@diagnostic disable-next-line: undefined-field
280288
local play_button = UIBox:get_UIE_by_ID("play_button", G.buttons.UIRoot)
281-
G.FUNCS["play_cards_from_highlighted"](play_button)
289+
G.FUNCS.play_cards_from_highlighted(play_button)
282290
elseif args.action == "discard" then
283291
---@diagnostic disable-next-line: undefined-field
284292
local discard_button = UIBox:get_UIE_by_ID("discard_button", G.buttons.UIRoot)
285-
G.FUNCS["discard_cards_from_highlighted"](discard_button)
293+
G.FUNCS.discard_cards_from_highlighted(discard_button)
286294
else
287295
API.send_error_response("Invalid action arg for play_hand_or_discard", { action = args.action })
288296
return
@@ -291,7 +299,6 @@ API.functions["play_hand_or_discard"] = function(args)
291299
-- Defer sending response until the run has started
292300
API.pending_requests["play_hand_or_discard"] = {
293301
condition = function()
294-
-- TODO: maybe remove brittle G.E_MANAGER check
295302
if #G.E_MANAGER.queues.base < EVENT_QUEUE_THRESHOLD and G.STATE_COMPLETE then
296303
-- round still going
297304
if G.buttons and G.STATE == G.STATES.SELECTING_HAND then
@@ -314,6 +321,7 @@ API.functions["play_hand_or_discard"] = function(args)
314321
end
315322

316323
---Cashes out from the current round to enter the shop
324+
---Call G.FUNCS.cash_out() to cash out from the current round to enter the shop.
317325
---@param _ table Arguments (not used)
318326
API.functions["cash_out"] = function(_)
319327
-- Validate current game state is appropriate for cash out
@@ -335,6 +343,7 @@ API.functions["cash_out"] = function(_)
335343
end
336344

337345
---Selects an action for shop
346+
---Call G.FUNCS.toggle_shop() to select an action for shop.
338347
---@param args ShopActionArgs The shop action to perform
339348
API.functions["shop"] = function(args)
340349
-- Validate current game state is appropriate for shop
@@ -363,46 +372,4 @@ API.functions["shop"] = function(args)
363372
end
364373
end
365374

366-
---Selects an action for booster packs (TODO implement)
367-
---@param _ table Arguments
368-
API.functions["select_booster_action"] = function(_)
369-
-- TODO: implement
370-
end
371-
372-
---Selects an action in the shop (TODO implement)
373-
---@param _ table Arguments
374-
API.functions["select_shop_action"] = function(_)
375-
-- TODO: implement
376-
end
377-
378-
---Rearranges cards in hand (TODO implement)
379-
---@param _ table Arguments
380-
API.functions["rearrange_hand"] = function(_)
381-
-- TODO: implement
382-
end
383-
384-
---Rearranges consumable cards (TODO implement)
385-
---@param _ table Arguments
386-
API.functions["rearrange_consumables"] = function(_)
387-
-- TODO: implement
388-
end
389-
390-
---Rearranges joker cards (TODO implement)
391-
---@param _ table Arguments
392-
API.functions["rearrange_jokers"] = function(_)
393-
-- TODO: implement
394-
end
395-
396-
---Uses or sells consumable cards (TODO implement)
397-
---@param _ table Arguments
398-
API.functions["use_or_sell_consumables"] = function(_)
399-
-- TODO: implement
400-
end
401-
402-
---Sells joker cards (TODO implement)
403-
---@param _ table Arguments
404-
API.functions["sell_jokers"] = function(_)
405-
-- TODO: implement
406-
end
407-
408375
return API

0 commit comments

Comments
 (0)