@@ -20,50 +20,52 @@ function BalatrobotAPI.queueaction(action)
2020 List .pushleft (Botlogger [' q_' .. _params .func ], { 0 , action })
2121end
2222
23- function BalatrobotAPI .update (dt )
24- data , msg_or_ip , port_or_nil = udp :receivefrom ()
25- if data then
26-
27- -- Protocol is ACTION|arg1|arg2
28- action = data :match (" ^([%a%u_]*)" )
29- params = data :match (" |(.*)" )
23+ function BalatrobotAPI .parseaction (data )
24+ -- Protocol is ACTION|arg1|arg2
25+ action = data :match (" ^([%a%u_]*)" )
26+ params = data :match (" |(.*)" )
3027
31- if action then
32- local _action = Bot .ACTIONS [action ]
33- sendDebugMessage (" Action is: " .. tostring (_action ))
28+ if action then
29+ local _action = Bot .ACTIONS [action ]
30+ sendDebugMessage (" Action is: " .. tostring (_action ))
3431
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
32+ if not _action then
33+ return nil
34+ end
4035
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 ) or _str
51- _j = _j + 1
52- end
53- _actiontable [_i ] = _splitstring
54- _i = _i + 1
36+ local _actiontable = { }
37+ _actiontable [1 ] = _action
38+
39+ if params then
40+ local _i = 2
41+ for _arg in params :gmatch (" [%d,]+" ) do
42+ local _splitstring = { }
43+ local _j = 1
44+ for _str in _arg :gmatch (' ([^,]+)' ) do
45+ _splitstring [_j ] = tonumber (_str ) or _str
46+ _j = _j + 1
5547 end
48+ _actiontable [_i ] = _splitstring
49+ _i = _i + 1
5650 end
51+ end
5752
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
53+ return _actiontable
54+ end
55+ end
6356
64- BalatrobotAPI .queueaction (_actiontable )
65- else
57+ function BalatrobotAPI .update (dt )
58+ data , msg_or_ip , port_or_nil = udp :receivefrom ()
59+ if data then
60+
61+ local _action = BalatrobotAPI .parseaction (data )
62+
63+ if _action and # _action > 1 and # _action > Bot .ACTIONPARAMS [_action [1 ]].num_args then
64+ BalatrobotAPI .respond (" Error: Incorrect number of params for action " .. action )
65+ elseif not _action then
6666 BalatrobotAPI .respond (" Error: Incorrect message format. Should be ACTION|arg1|arg2" )
67+ else
68+ BalatrobotAPI .queueaction (_action )
6769 end
6870
6971 elseif msg_or_ip ~= ' timeout' then
0 commit comments