Skip to content

Commit 0d90cb9

Browse files
committed
support api mode
1 parent 27ec49e commit 0d90cb9

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

bot.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Bot.SETTINGS = {
5858

5959
-- Replay actions from file?
6060
replay = true,
61+
62+
-- Receive commands from the API?
63+
api = false,
6164
}
6265

6366
--- Skips or selects the current blind

botlogger.lua

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ function Botlogger.logbotdecision(...)
5959
end
6060
end
6161

62-
local _f = io.open(Botlogger.filename, 'a')
63-
_f:write(_logstring, '\n')
64-
_f:close()
62+
if Botlogger.filename then
63+
local _f = io.open(Botlogger.filename, 'a')
64+
_f:write(_logstring, '\n')
65+
_f:close()
66+
end
6567
end
6668
end
6769

@@ -94,22 +96,55 @@ end
9496

9597
function Botlogger.inithooks()
9698

99+
Botlogger.q_skip_or_select_blind = List.new()
100+
Botlogger.q_select_cards_from_hand = List.new()
101+
Botlogger.q_select_shop_action = List.new()
102+
Botlogger.q_select_booster_action = List.new()
103+
Botlogger.q_sell_jokers = List.new()
104+
Botlogger.q_rearrange_jokers = List.new()
105+
Botlogger.q_use_or_sell_consumables = List.new()
106+
Botlogger.q_rearrange_consumables = List.new()
107+
Botlogger.q_rearrange_hand = List.new()
108+
97109
-- Hook bot functions
110+
if Bot.SETTINGS.replay == true or Bot.SETTINGS.api == true then
111+
-- Redefine Bot functions to just return the next action from their queue
112+
Botlogger.nextaction = 1
113+
for k,v in pairs(Bot) do
114+
if type(Bot[k]) == 'function' then
115+
Bot[k] = function()
116+
if not List.isempty(Botlogger['q_'..k]) then
117+
local _action = List.popright(Botlogger['q_'..k])
118+
119+
if _action[1] == Botlogger.nextaction then
120+
Botlogger.nextaction = Botlogger.nextaction + 1
121+
return unpack(_action[2])
122+
else
123+
List.pushright(Botlogger['q_'..k], _action)
124+
sendDebugMessage('q_'..k.." is not. Returning Bot.ACTIONS.PASS")
125+
return Bot.ACTIONS.PASS
126+
end
127+
else
128+
-- Return an action of "PASS" when the API is not enabled.
129+
-- When API is enabled, nothing is returned, and the system waits for the queue to be populated
130+
if Bot.SETTINGS.api == false then
131+
sendDebugMessage('q_'..k.." is empty. Returning Bot.ACTIONS.PASS")
132+
return Bot.ACTIONS.PASS
133+
else
134+
sendDebugMessage('q_'..k.." is empty. Waiting for API to populate queue...")
135+
end
136+
end
137+
end
138+
end
139+
end
140+
end
141+
142+
-- Read replay file and populate action queues
98143
if Bot.SETTINGS.replay == true then
99144
local _replayfile = Botlogger.getfilename(Bot.SETTINGS)
100145

101146
if Botlogger.fileexists(_replayfile) then
102147

103-
Botlogger.q_skip_or_select_blind = List.new()
104-
Botlogger.q_select_cards_from_hand = List.new()
105-
Botlogger.q_select_shop_action = List.new()
106-
Botlogger.q_select_booster_action = List.new()
107-
Botlogger.q_sell_jokers = List.new()
108-
Botlogger.q_rearrange_jokers = List.new()
109-
Botlogger.q_use_or_sell_consumables = List.new()
110-
Botlogger.q_rearrange_consumables = List.new()
111-
Botlogger.q_rearrange_hand = List.new()
112-
113148
local _num_action = 0
114149
for line in io.lines(_replayfile) do
115150
_num_action = _num_action + 1
@@ -184,31 +219,7 @@ function Botlogger.inithooks()
184219
_action[2] = _cards
185220
List.pushleft(Botlogger.q_rearrange_hand, { _num_action, _action })
186221
end
187-
end
188-
189-
-- Redefine Bot functions to just return the next action from their queue
190-
Botlogger.nextaction = 1
191-
for k,v in pairs(Bot) do
192-
if type(Bot[k]) == 'function' then
193-
Bot[k] = function()
194-
if not List.isempty(Botlogger['q_'..k]) then
195-
local _action = List.popright(Botlogger['q_'..k])
196-
197-
if _action[1] == Botlogger.nextaction then
198-
Botlogger.nextaction = Botlogger.nextaction + 1
199-
return unpack(_action[2])
200-
else
201-
List.pushright(Botlogger['q_'..k], _action)
202-
sendDebugMessage('q_'..k.." is not ready. Returning Bot.ACTIONS.PASS")
203-
return Bot.ACTIONS.PASS
204-
end
205-
else
206-
sendDebugMessage('q_'..k.." is empty. Returning Bot.ACTIONS.PASS")
207-
return Bot.ACTIONS.PASS
208-
end
209-
end
210-
end
211-
end
222+
end
212223
end
213224
elseif Bot.SETTINGS.replay == false then
214225
for k,v in pairs(Bot) do

0 commit comments

Comments
 (0)