Tweak repeat.lua.
parent
ec8e58e5e3
commit
5404b69476
@ -1,97 +1,61 @@
|
|||||||
-- repeat.lua
|
-- scripts/repeat.lua
|
||||||
-- repeatedly calls a lua script, eg "repeat -time 1 months -command cleanowned"; to disable "repeat -cancel cleanowned"
|
-- repeatedly calls a lua script, eg "repeat -time 1 months -command cleanowned"; to disable "repeat -cancel cleanowned"
|
||||||
-- repeat -help for details
|
-- repeat -help for details
|
||||||
-- author expwnent
|
-- author expwnent
|
||||||
-- vaguely based on a script by Putnam
|
-- vaguely based on a script by Putnam
|
||||||
|
|
||||||
local repeatUtil = require 'repeatUtil'
|
local repeatUtil = require 'repeat-util'
|
||||||
|
local utils = require 'utils'
|
||||||
|
|
||||||
local args = {...}
|
validArgs = validArgs or utils.invert({
|
||||||
if args[1] == '-cancel' then
|
'help',
|
||||||
repeatUtil.cancel(args[2])
|
'cancel',
|
||||||
return
|
'name',
|
||||||
elseif args[1] == '-help' then
|
'time',
|
||||||
|
'timeUnits',
|
||||||
|
'command'
|
||||||
|
})
|
||||||
|
|
||||||
|
local args = utils.processArgs({...}, validArgs)
|
||||||
|
|
||||||
|
if args.help then
|
||||||
print([[repeat.lua
|
print([[repeat.lua
|
||||||
repeat -help
|
repeat -help
|
||||||
print this help message
|
print this help message
|
||||||
repeat -cancel bob
|
repeat -cancel bob
|
||||||
cancels the repetition with the name bob
|
cancels the repetition with the name bob
|
||||||
repeat -name jim -time delay timeUnits -printResult true -command printArgs 3 1 2
|
repeat -name jim -time delay -timeUnits units -printResult true -command printArgs 3 1 2
|
||||||
except for -command, arguments can go in any order
|
|
||||||
-name sets the name for the purposes of cancelling and making sure you don't schedule the same repeating event twice
|
-name sets the name for the purposes of cancelling and making sure you don't schedule the same repeating event twice
|
||||||
if not specified, it's set to the first argument after -command
|
if not specified, it's set to the first argument after -command
|
||||||
-time delay timeUnits
|
-time delay -timeUnits units
|
||||||
delay is some positive integer
|
delay is some positive integer
|
||||||
timeUnits is some valid time unit for dfhack.timeout(delay,timeUnits,function)
|
units is some valid time unit for dfhack.timeout(delay,timeUnits,function)
|
||||||
-printResult true
|
|
||||||
print the results of the command
|
|
||||||
-printResult false
|
|
||||||
suppress the results of the command
|
|
||||||
-later
|
|
||||||
make it happen later instead of now (every 5 ticks starting now vs every 5 ticks starting in 5 ticks)
|
|
||||||
-command ...
|
-command ...
|
||||||
specify the command to be run
|
specify the command to be run
|
||||||
]])
|
]])
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local name=nil
|
if args.cancel then
|
||||||
local time
|
repeatUtil.cancel(args.cancel)
|
||||||
local timeUnits
|
if args.name then
|
||||||
local i=1
|
repeatUtil.cancel(args.name)
|
||||||
local command={}
|
|
||||||
local printResult=true
|
|
||||||
local later = false
|
|
||||||
while i <= #args do
|
|
||||||
if args[i] == '-name' then
|
|
||||||
name = args[i+1]
|
|
||||||
i = i + 2
|
|
||||||
elseif args[i] == '-later' then
|
|
||||||
later = true
|
|
||||||
i = i+1
|
|
||||||
elseif args[i] == '-time' then
|
|
||||||
time = tonumber(args[i+1])
|
|
||||||
timeUnits = args[i+2]
|
|
||||||
i = i+3
|
|
||||||
elseif args[i] == '-command' then
|
|
||||||
name = name or args[i+1]
|
|
||||||
for j=i+1,#args,1 do
|
|
||||||
table.insert(command,args[j])
|
|
||||||
end
|
|
||||||
break
|
|
||||||
elseif args[i] == '-printResult' then
|
|
||||||
if args[i+1] == "true" then
|
|
||||||
printOutput = true
|
|
||||||
elseif args[i+1] == "false" then
|
|
||||||
printOutput = false
|
|
||||||
else
|
|
||||||
qerror("repeat -printResult " .. args[i+1] .. ": expected true or false")
|
|
||||||
end
|
|
||||||
i = i+2
|
|
||||||
else
|
|
||||||
qerror('Improper arguments to repeat.')
|
|
||||||
end
|
end
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local callCommand = function()
|
args.time = tonumber(args.time)
|
||||||
local result = dfhack.run_command(table.unpack(command))
|
if not args.name then
|
||||||
if printResult then
|
args.name = args.command[1]
|
||||||
print(result)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local func
|
if not args.timeUnits then
|
||||||
if later then
|
args.timeUnits = 'ticks'
|
||||||
local didOnce
|
end
|
||||||
func = function()
|
|
||||||
if didOnce then
|
local callCommand = function()
|
||||||
callCommand()
|
dfhack.run_command(table.unpack(args.command))
|
||||||
else
|
|
||||||
didOnce = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
func = callCommand
|
|
||||||
end
|
end
|
||||||
|
|
||||||
repeatUtil.scheduleEvery(name,time,timeUnits,func)
|
repeatUtil.scheduleEvery(args.name,args.time,args.timeUnits,callCommand)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue