Skip to content

Commit 8b3e478

Browse files
committed
Basic API functionality
1 parent 0d90cb9 commit 8b3e478

File tree

3 files changed

+125
-29
lines changed

3 files changed

+125
-29
lines changed

api.lua

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,59 @@ local running = true
1111

1212
BalatrobotAPI = { }
1313

14+
function BalatrobotAPI.respond(str)
15+
udp:sendto(string.format("%s\n", str), msg_or_ip, port_or_nil)
16+
end
17+
18+
function BalatrobotAPI.queueaction(action)
19+
local _params = Bot.ACTIONPARAMS[action[1]]
20+
List.pushleft(Botlogger['q_'.._params.func], { 0, action })
21+
end
22+
1423
function BalatrobotAPI.update(dt)
1524
data, msg_or_ip, port_or_nil = udp:receivefrom()
1625
if data then
1726

18-
-- Protocol is be ACTION|arg1|arg2
19-
action, params = data:match("^([%a%u_]*)|(.*)")
27+
-- Protocol is ACTION|arg1|arg2
28+
action = data:match("^([%a%u_]*)")
29+
params = data:match("|(.*)")
2030

2131
if action then
2232
local _action = Bot.ACTIONS[action]
2333
sendDebugMessage("Action is: " .. tostring(_action))
2434

25-
sendDebugMessage(params)
26-
for _arg in params:gmatch("[%d,]+") do
27-
sendDebugMessage(tostring(_arg))
35+
if not _action then
36+
BalatrobotAPI.respond("Error: Invalid action "..action..". See Bot.ACTIONS for valid actions.")
37+
socket.sleep(0.01)
38+
return
39+
end
40+
41+
local _actiontable = { }
42+
_actiontable[1] = _action
43+
44+
if params then
45+
local _i = 2
46+
for _arg in params:gmatch("[%d,]+") do
47+
local _splitstring = { }
48+
local _j = 1
49+
for _str in _arg:gmatch('([^,]+)') do
50+
_splitstring[_j] = tonumber(_str)
51+
_j = _j + 1
52+
end
53+
_actiontable[_i] = _splitstring
54+
_i = _i + 1
55+
end
2856
end
57+
58+
if #_actiontable > Bot.ACTIONPARAMS[_action].num_args then
59+
BalatrobotAPI.respond("Error: Incorrect number of params for action " .. action)
60+
socket.sleep(0.01)
61+
return
62+
end
63+
64+
BalatrobotAPI.queueaction(_actiontable)
2965
else
30-
udp:sendto(string.format("%s", "Error: Incorrect message format. Should be ACTION|arg1|arg2"), msg_or_ip, port_or_nil)
66+
BalatrobotAPI.respond("Error: Incorrect message format. Should be ACTION|arg1|arg2")
3167
end
3268

3369
elseif msg_or_ip ~= 'timeout' then

bot.lua

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,78 @@ Bot.ACTIONS = {
2424
}
2525

2626
Bot.ACTIONPARAMS = { }
27-
Bot.ACTIONPARAMS[Bot.ACTIONS.SELECT_BLIND] = 1
28-
Bot.ACTIONPARAMS[Bot.ACTIONS.SKIP_BLIND] = 1
29-
Bot.ACTIONPARAMS[Bot.ACTIONS.PLAY_HAND] = 2
30-
Bot.ACTIONPARAMS[Bot.ACTIONS.DISCARD_HAND] = 2
31-
Bot.ACTIONPARAMS[Bot.ACTIONS.END_SHOP] = 1
32-
Bot.ACTIONPARAMS[Bot.ACTIONS.REROLL_SHOP] = 1
33-
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_CARD] = 2
34-
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_VOUCHER] = 2
35-
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_BOOSTER] = 2
36-
Bot.ACTIONPARAMS[Bot.ACTIONS.SELECT_BOOSTER_CARD] = 3
37-
Bot.ACTIONPARAMS[Bot.ACTIONS.SKIP_BOOSTER_PACK] = 1
38-
Bot.ACTIONPARAMS[Bot.ACTIONS.SELL_JOKER] = 2
39-
Bot.ACTIONPARAMS[Bot.ACTIONS.USE_CONSUMABLE] = 2
40-
Bot.ACTIONPARAMS[Bot.ACTIONS.SELL_CONSUMABLE] = 2
41-
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_JOKERS] = 2
42-
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_CONSUMABLES] = 2
43-
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_HAND] = 2
44-
Bot.ACTIONPARAMS[Bot.ACTIONS.PASS] = 1
27+
Bot.ACTIONPARAMS[Bot.ACTIONS.SELECT_BLIND] = {
28+
num_args = 1,
29+
func = "skip_or_select_blind"
30+
}
31+
Bot.ACTIONPARAMS[Bot.ACTIONS.SKIP_BLIND] = {
32+
num_args = 1,
33+
func = "skip_or_select_blind"
34+
}
35+
Bot.ACTIONPARAMS[Bot.ACTIONS.PLAY_HAND] = {
36+
num_args = 2,
37+
func = "select_cards_from_hand"
38+
}
39+
Bot.ACTIONPARAMS[Bot.ACTIONS.DISCARD_HAND] = {
40+
num_args = 2,
41+
func = "select_cards_from_hand"
42+
}
43+
Bot.ACTIONPARAMS[Bot.ACTIONS.END_SHOP] = {
44+
num_args = 1,
45+
func = "select_shop_action"
46+
}
47+
Bot.ACTIONPARAMS[Bot.ACTIONS.REROLL_SHOP] = {
48+
num_args = 1,
49+
func = "select_shop_action"
50+
}
51+
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_CARD] = {
52+
num_args = 2,
53+
func = "select_shop_action"
54+
}
55+
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_VOUCHER] = {
56+
num_args = 2,
57+
func = "select_shop_action"
58+
}
59+
Bot.ACTIONPARAMS[Bot.ACTIONS.BUY_BOOSTER] = {
60+
num_args = 2,
61+
func = "select_shop_action"
62+
}
63+
Bot.ACTIONPARAMS[Bot.ACTIONS.SELECT_BOOSTER_CARD] = {
64+
num_args = 3,
65+
func = "select_booster_action"
66+
}
67+
Bot.ACTIONPARAMS[Bot.ACTIONS.SKIP_BOOSTER_PACK] = {
68+
num_args = 1,
69+
func = "select_booster_action"
70+
}
71+
Bot.ACTIONPARAMS[Bot.ACTIONS.SELL_JOKER] = {
72+
num_args = 2,
73+
func = "sell_jokers"
74+
}
75+
Bot.ACTIONPARAMS[Bot.ACTIONS.USE_CONSUMABLE] = {
76+
num_args = 2,
77+
func = "use_or_sell_consumables"
78+
}
79+
Bot.ACTIONPARAMS[Bot.ACTIONS.SELL_CONSUMABLE] = {
80+
num_args = 2,
81+
func = "use_or_sell_consumables"
82+
}
83+
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_JOKERS] = {
84+
num_args = 2,
85+
func = "rearrange_jokers"
86+
}
87+
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_CONSUMABLES] = {
88+
num_args = 2,
89+
func = "rearrange_consumables"
90+
}
91+
Bot.ACTIONPARAMS[Bot.ACTIONS.REARRANGE_HAND] = {
92+
num_args = 2,
93+
func = "rearrange_hand"
94+
}
95+
Bot.ACTIONPARAMS[Bot.ACTIONS.PASS] = {
96+
num_args = 1,
97+
func = ""
98+
}
4599

46100
-- CHANGE ME
47101
Bot.SETTINGS = {
@@ -57,10 +111,10 @@ Bot.SETTINGS = {
57111
action_delay = 0,
58112

59113
-- Replay actions from file?
60-
replay = true,
114+
replay = false,
61115

62116
-- Receive commands from the API?
63-
api = false,
117+
api = true,
64118
}
65119

66120
--- Skips or selects the current blind

botlogger.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ function Botlogger.inithooks()
116116
if not List.isempty(Botlogger['q_'..k]) then
117117
local _action = List.popright(Botlogger['q_'..k])
118118

119-
if _action[1] == Botlogger.nextaction then
119+
if Bot.SETTINGS.api == false and _action[1] == Botlogger.nextaction then
120120
Botlogger.nextaction = Botlogger.nextaction + 1
121121
return unpack(_action[2])
122-
else
122+
123+
elseif Bot.SETTINGS.api == false then
123124
List.pushright(Botlogger['q_'..k], _action)
124-
sendDebugMessage('q_'..k.." is not. Returning Bot.ACTIONS.PASS")
125+
sendDebugMessage('q_'..k.." is not empty. Returning Bot.ACTIONS.PASS")
125126
return Bot.ACTIONS.PASS
127+
128+
-- We don't care about action order for the API.
129+
-- When the queue is populated, return the choice.
130+
elseif Bot.SETTINGS.api == true then
131+
return unpack(_action[2])
126132
end
127133
else
128134
-- Return an action of "PASS" when the API is not enabled.

0 commit comments

Comments
 (0)