From c0668dac4378caf035561dcefb20e6f768e710ef Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 20 Aug 2017 18:22:20 +0100 Subject: [PATCH] Added Temp vars --- locale/ExpGaming-Core/Core Help File.md | 20 +++++-- .../ExpGaming - Server Interface.lua | 60 ++++++++++++++++--- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/locale/ExpGaming-Core/Core Help File.md b/locale/ExpGaming-Core/Core Help File.md index 6cb9da5b..b139f6b5 100644 --- a/locale/ExpGaming-Core/Core Help File.md +++ b/locale/ExpGaming-Core/Core Help File.md @@ -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' \ No newline at end of file diff --git a/locale/ExpGaming-Core/ExpGaming - Server Interface.lua b/locale/ExpGaming-Core/ExpGaming - Server Interface.lua index 6e9f69ba..51791beb 100644 --- a/locale/ExpGaming-Core/ExpGaming - Server Interface.lua +++ b/locale/ExpGaming-Core/ExpGaming - Server Interface.lua @@ -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 \ No newline at end of file