Added Temp vars

This commit is contained in:
Cooldude2606
2017-08-20 18:22:20 +01:00
parent 6d88e58e12
commit c0668dac43
2 changed files with 68 additions and 12 deletions

View File

@@ -73,6 +73,12 @@ See code for more details.
* table.to_string(tbl)
* We stole this but we don't know from where, just google it
* output -> table as a string
## Get Commands
* get_temp_var_data(name)
* name = value retured by sudo
* returens a list if the data returend by thefunction if any
* get_sudo_info(string)
* return either a list or string based on the string boliean
## Other
* define_command(name,help,inputs,restriction,event)
* Add game commands in a way it does not cause crashes
@@ -81,10 +87,16 @@ See code for more details.
* inputs = {'input name',...,true/nil} last value being true means no cap on the length
* restriction = 'rank name'
* event = on command -> function(player,event,args)
* sudo(command,args)
* Ask server to run a script function for a user i.e. give_rank
* command = function(...)
* args = {...}
* sudo(command,args,custom_return_name)
* Ask server to run a script function at a diffrent time
* command = function or function name
* args (as function needs) = {...}
* custom_return_name (opt) = name of the value temp varible returned
* returns the name of its temp varible
* refresh_temp_var(name,value,offset)
* name = value retured by sudo
* value (opt) = new value if making a new temp varible
* offset (opt) = if the base time is too short, for very big commands
* command: /server-interface
* Run loadstring on lua code given like /c but does not break achievements
* restriction = 'Admin'

View File

@@ -33,21 +33,65 @@ end)
--this is used when changing permission groups when the person does not have permsion to, can also be used to split a large event accross multiple ticks
local commands_per_iteration = 50 --number of sudo commands ran every sudo iteration
local ticks_per_iteration = 1 --the number of ticks break before the next sudo iteration
function sudo(command,args) table.insert(global.sudo,{fun=command,var=args}) end
--runs at most five sudo commands every five ticks if one is present
local temp_var_time = 1000/commands_per_iteration*ticks_per_iteration --temp vars allow sudo funnctions to share data
function sudo(command,args,custom_return_name)
if type(command) == 'function' then
local args = args or {}
local return_name = custom_return_name or tostring(game.tick)..tostring(command)..tostring(#global.sudo.commands)
table.insert(global.sudo.commands,{fun=command,args=args,return_name=return_name})
return {'sudo-temp-var',return_name}
end
end
--update the time on a temp var or add it as a new one
function refresh_temp_var(name,value,offset)
local offset = offset or temp_var_time
if global.sudo.temp_varibles[name] then
global.sudo.temp_varibles[name][2] = game.tick+offset
else
global.sudo.temp_varibles[name] = {data=value,remove_time=game.tick+offset}
end
end
-- gets the data stored in a temp varible
function get_temp_var_data(name)
local to_return = nil
if global.sudo.temp_varibles[name] then to_return = global.sudo.temp_varibles[name].data
elseif name[2] and global.sudo.temp_varibles[name[2]] then to_return = global.sudo.temp_varibles[name[2]].data end
return to_return
end
-- returns the lenth of the temp varible list and command queue, is string is true then it is retured as a string
function get_sudo_info(string)
local lenth = 0
for _,v in pairs(global.sudo.temp_varibles) do lenth = lenth + 1 end
if string then return 'At game tick: '..game.tick..' Queue Lenth: '..#global.sudo.commands..' Number of temp vars: '..lenth
else return {tick=game.tick,commands=#global.sudo.commands,temp_varibles=#global.sudo.temp_varibles} end
end
--sudo main loop
Event.register(defines.events.on_tick, function(event)
if game.tick % ticks_per_iteration == 0 and global.sudo and #global.sudo > 0 then
-- runs the commands in sudo
if game.tick % ticks_per_iteration == 0 and global.sudo.commands and #global.sudo.commands > 0 then
local length = nil
if #global.sudo > commands_per_iteration then length = commands_per_iteration else length = #global.sudo end
if #global.sudo.commands > commands_per_iteration then length = commands_per_iteration else length = #global.sudo.commands end
-- runs the right number of commands as set
for i = 1,length do
command=table.remove(global.sudo)
local command=table.remove(global.sudo.commands,1)
if command and command.fun and type(command.fun) == 'function' then
local args = command.var or {}
command.fun(unpack(args))
local args = {}
-- retrives and temp varibles
for n,value in pairs(command.args) do
if type(value) == 'list' and value[1] == 'sudo-temp-var' then args[n] = global.sudo.temp_varibles[value[2]][1]
else args[n] = value end
end
-- makes new temp value and runs command
local returns = {command.fun(unpack(args))} or {}
refresh_temp_var(command.return_name,returns)
end
end
end
-- removes old temp varibles
for name,data in pairs(global.sudo.temp_varibles) do
if data.remove_time >= game.tick then global.sudo.temp_varibles[name] = nil end
end
end)
Event.register(-1,function() global.sudo = {} end)
Event.register(-1,function() global.sudo = {commands={},temp_varibles={}} end)
--Please Only Edit Above This Line-----------------------------------------------------------
return credits