Skip to content

Commit 08185da

Browse files
committed
project reorg
1 parent ae94b0c commit 08185da

File tree

11 files changed

+135
-100
lines changed

11 files changed

+135
-100
lines changed

api.lua

Lines changed: 0 additions & 84 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

main.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
--- STEAMODDED HEADER
22
--- MOD_NAME: Balatrobot
3-
--- MOD_ID: Balatrobot-v0.2
3+
--- MOD_ID: Balatrobot-v0.3
44
--- MOD_AUTHOR: [Besteon]
55
--- MOD_DESCRIPTION: A botting API for Balatro
66

77
function SMODS.INIT.BALATROBOT()
8-
mw = SMODS.findModByID("Balatrobot-v0.2")
8+
mw = SMODS.findModByID("Balatrobot-v0.3")
99

10-
assert(load(NFS.read(mw.path .. "list.lua")))()
11-
assert(load(NFS.read(mw.path .. "hook.lua")))()
12-
assert(load(NFS.read(mw.path .. "bitser.lua")))()
13-
assert(load(NFS.read(mw.path .. "sock.lua")))()
14-
assert(load(NFS.read(mw.path .. "api.lua")))()
15-
assert(load(NFS.read(mw.path .. "bot.lua")))()
16-
assert(load(NFS.read(mw.path .. "middleware.lua")))()
17-
assert(load(NFS.read(mw.path .. "botlogger.lua")))()
10+
-- External libraries
11+
assert(load(NFS.read(mw.path .. "lib/list.lua")))()
12+
assert(load(NFS.read(mw.path .. "lib/hook.lua")))()
13+
assert(load(NFS.read(mw.path .. "lib/bitser.lua")))()
14+
assert(load(NFS.read(mw.path .. "lib/sock.lua")))()
1815

19-
sendDebugMessage("Balatrobot v0.2 loaded")
16+
-- Mod specific files
17+
assert(load(NFS.read(mw.path .. "src/utils.lua")))()
18+
assert(load(NFS.read(mw.path .. "src/bot.lua")))()
19+
assert(load(NFS.read(mw.path .. "src/middleware.lua")))()
20+
assert(load(NFS.read(mw.path .. "src/botlogger.lua")))()
21+
assert(load(NFS.read(mw.path .. "src/api.lua")))()
22+
23+
sendDebugMessage("Balatrobot v0.3 loaded")
2024

2125
Middleware.hookbalatro()
2226

2327
Botlogger.path = mw.path
24-
Botlogger.inithooks()
25-
28+
Botlogger.init()
2629
BalatrobotAPI.init()
2730
end

src/api.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
local socket = require "socket"
3+
4+
local data, msg_or_ip, port_or_nil
5+
6+
BalatrobotAPI = { }
7+
BalatrobotAPI.socket = nil
8+
9+
function BalatrobotAPI.notifyapiclient(...)
10+
-- TODO Generate gamestate json object
11+
local _gamestate = Utils.getgamestate()
12+
13+
14+
str = "json gamestate"
15+
16+
if BalatrobotAPI.socket then
17+
BalatrobotAPI.socket:sendto(string.format("%s\n", str), msg_or_ip, port_or_nil)
18+
end
19+
end
20+
21+
function BalatrobotAPI.respond(str)
22+
if BalatrobotAPI.socket then
23+
BalatrobotAPI.socket:sendto(string.format("%s\n", str), msg_or_ip, port_or_nil)
24+
end
25+
end
26+
27+
function BalatrobotAPI.queueaction(action)
28+
local _params = Bot.ACTIONPARAMS[action[1]]
29+
List.pushleft(Botlogger['q_'.._params.func], { 0, action })
30+
end
31+
32+
function BalatrobotAPI.update(dt)
33+
if not BalatrobotAPI.socket then
34+
BalatrobotAPI.socket = socket.udp()
35+
BalatrobotAPI.socket:settimeout(0)
36+
BalatrobotAPI.socket:setsockname('*', 12345)
37+
end
38+
39+
data, msg_or_ip, port_or_nil = BalatrobotAPI.socket:receivefrom()
40+
if data then
41+
42+
local _action = Utils.parseaction(data)
43+
44+
if _action and #_action > 1 and #_action > Bot.ACTIONPARAMS[_action[1]].num_args then
45+
BalatrobotAPI.respond("Error: Incorrect number of params for action " .. action)
46+
elseif not _action then
47+
BalatrobotAPI.respond("Error: Incorrect message format. Should be ACTION|arg1|arg2")
48+
else
49+
BalatrobotAPI.queueaction(_action)
50+
end
51+
52+
elseif msg_or_ip ~= 'timeout' then
53+
sendDebugMessage("Unknown network error: "..tostring(msg))
54+
end
55+
56+
socket.sleep(0.01)
57+
end
58+
59+
function BalatrobotAPI.init()
60+
61+
love.update = Hook.addcallback(love.update, BalatrobotAPI.update)
62+
63+
if Bot.SETTINGS.api == true then
64+
for k,v in pairs(Bot) do
65+
if type(Bot[k]) == 'function' then
66+
Bot[k] = Hook.addbreakpoint(Bot[k], BalatrobotAPI.notifyapiclient)
67+
end
68+
end
69+
end
70+
end
71+
72+
return BalatrobotAPI
File renamed without changes.

botlogger.lua renamed to src/botlogger.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function Botlogger.start_run()
8484
end
8585
end
8686

87-
function Botlogger.inithooks()
87+
function Botlogger.init()
8888

8989
Botlogger.q_skip_or_select_blind = List.new()
9090
Botlogger.q_select_cards_from_hand = List.new()
@@ -142,7 +142,7 @@ function Botlogger.inithooks()
142142
for line in io.lines(_replayfile) do
143143
_num_action = _num_action + 1
144144

145-
local _action = BalatrobotAPI.parseaction(line)
145+
local _action = Utils.parseaction(line)
146146
sendDebugMessage(line)
147147
local _params = Bot.ACTIONPARAMS[_action[1]]
148148
for i = 2, #_action do

middleware.lua renamed to src/middleware.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ local function c_initgamehooks()
529529
firewhenready(function()
530530
return G.buttons and G.STATE_COMPLETE and G.STATE == G.STATES.SELECTING_HAND
531531
end, function()
532-
sendDebugMessage("drawn to hand")
533532
c_sell_jokers()
534533
c_rearrange_jokers()
535534
c_use_or_sell_consumables()

0 commit comments

Comments
 (0)