Skip to content

Commit b205aae

Browse files
committed
refactor(api): move debug functionality to utils module
Relocate debugplus integration from main mod file to utils.lua for better organization and add section headers for clarity.
1 parent b554b44 commit b205aae

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

balatrobot.lua

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,10 @@ BALATRO_BOT_CONFIG = {
1313
-- max_fps = nil,
1414
}
1515

16-
-- Load debug
17-
local success, dpAPI = pcall(require, "debugplus-api")
18-
1916
-- Load minimal required files
2017
assert(SMODS.load_file("src/lua/utils.lua"))()
2118
assert(SMODS.load_file("src/lua/api.lua"))()
2219

23-
if success and dpAPI.isVersionCompatible(1) then
24-
local debugplus = dpAPI.registerID("balatrobot")
25-
debugplus.addCommand({
26-
name = "env",
27-
shortDesc = "Get game state",
28-
desc = "Get the current game state, useful for debugging",
29-
exec = function(args, _, _)
30-
debugplus.logger.log('{"name": "' .. args[1] .. '", "G": ' .. utils.table_to_json(G.GAME, 2) .. "}")
31-
end,
32-
})
33-
end
3420

3521
-- Initialize API
3622
API.init()

src/lua/utils.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
utils = {}
33
local json = require("json")
44

5+
-- ==========================================================================
6+
-- Game State Extraction
7+
-- ==========================================================================
8+
59
---Extracts the current game state including game info, hand, and jokers
610
---@return GameStateResponse The complete game state
711
function utils.get_game_state()
@@ -65,6 +69,10 @@ function utils.get_game_state()
6569
}
6670
end
6771

72+
-- ==========================================================================
73+
-- Debugging Utilities
74+
-- ==========================================================================
75+
6876
---Converts a Lua table to JSON string with depth limiting to prevent infinite recursion
6977
---@param obj any The object to convert to JSON
7078
---@param depth? number Maximum depth to traverse (default: 3)
@@ -103,4 +111,19 @@ function utils.table_to_json(obj, depth)
103111
return json.encode(sanitized)
104112
end
105113

114+
-- Load debug
115+
local success, dpAPI = pcall(require, "debugplus-api")
116+
117+
if success and dpAPI.isVersionCompatible(1) then
118+
local debugplus = dpAPI.registerID("balatrobot")
119+
debugplus.addCommand({
120+
name = "env",
121+
shortDesc = "Get game state",
122+
desc = "Get the current game state, useful for debugging",
123+
exec = function(args, _, _)
124+
debugplus.logger.log('{"name": "' .. args[1] .. '", "G": ' .. utils.table_to_json(G.GAME, 2) .. "}")
125+
end,
126+
})
127+
end
128+
106129
return utils

0 commit comments

Comments
 (0)