mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 20:41:41 +09:00
Server api added
This commit is contained in:
@@ -15,13 +15,13 @@ This file will return a function which can be used to access only the part you w
|
|||||||
Pass a table with the names of the objects you want and it will be return in that order
|
Pass a table with the names of the objects you want and it will be return in that order
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local ExpCore = {}
|
local StdExpCoreLib = {}
|
||||||
|
|
||||||
ExpCore.Server = require '/server'
|
StdExpCoreLib.Server = require '/server'
|
||||||
require '/commands'
|
require '/commands'
|
||||||
--[[ExpCore.Ranking = require '/ranking'
|
--[[StdExpCoreLib.Ranking = require '/ranking'
|
||||||
require '/ranks'
|
require '/ranks'
|
||||||
ExpCore.Gui = require '/gui'
|
StdExpCoreLib.Gui = require '/gui'
|
||||||
require '/GuiParts/inputs'
|
require '/GuiParts/inputs'
|
||||||
require '/GuiParts/toolbar'
|
require '/GuiParts/toolbar'
|
||||||
require '/GuiParts/center'
|
require '/GuiParts/center'
|
||||||
@@ -31,7 +31,7 @@ require '/GuiParts/popup']]
|
|||||||
return function(rtn)
|
return function(rtn)
|
||||||
local to_return = {}
|
local to_return = {}
|
||||||
for _,name in pairs(rtn) do
|
for _,name in pairs(rtn) do
|
||||||
if ExpCore[name] then
|
if StdExpCoreLib[name] then
|
||||||
table.insert(to_return,StdExpCoreLib[name])
|
table.insert(to_return,StdExpCoreLib[name])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,49 +8,123 @@ Discord: https://discord.gg/r6dC2uK
|
|||||||
]]
|
]]
|
||||||
--Please Only Edit Below This Line-----------------------------------------------------------
|
--Please Only Edit Below This Line-----------------------------------------------------------
|
||||||
-- server allows control over threads and other features the devs missed out
|
-- server allows control over threads and other features the devs missed out
|
||||||
local server = {}
|
local Server = {}
|
||||||
|
|
||||||
--- Returns a un-used uuid (better system needed)
|
--- Returns a un-used uuid (better system needed)
|
||||||
-- @usage obj.uuid = server.new_uuid()
|
-- @usage obj.uuid = Server.new_uuid()
|
||||||
-- @treturn string the new uuid
|
-- @treturn string the new uuid
|
||||||
function server.new_uuid()
|
function Server.new_uuid()
|
||||||
uuid = tostring(global.exp_core.uuids.operator())
|
uuid = tostring(Server._uuid().operator())
|
||||||
uuid = string.tohex('uuid'..uuid)
|
uuid = string.tohex('uuid'..uuid)
|
||||||
return uuid
|
return uuid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- use this to change the location of the server uuids
|
||||||
|
function Server._uuids(reset)
|
||||||
|
global.exp_core = not reset and global.exp_core or {}
|
||||||
|
global.exp_core.uuids = not reset and global.exp_core.uuids or game.create_random_generator()
|
||||||
|
return global.exp_core.uuids
|
||||||
|
end
|
||||||
|
|
||||||
--- Returns either the number of threads or a able of threads
|
--- Returns either the number of threads or a able of threads
|
||||||
-- @usage server.threads() -- {...}
|
-- @usage Server.threads() -- return {...}
|
||||||
-- server.threads(true) -- int
|
-- Server.threads(true) -- return int
|
||||||
-- @tparam[opt=nil] bolean count true to return the number of threads
|
-- @tparam[opt=nil] bolean count true to return the number of threads
|
||||||
-- @return either a list of threads or a number
|
-- @return either a list of threads or a number
|
||||||
function server.threads(count)
|
function Server.threads(count)
|
||||||
return count and #global.exp_core.threads.all or global.exp_core.threads.all
|
return count and #Server._threads().all or Server._threads().all
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.queue_thread(thread)
|
-- use this to change the location of the server threads
|
||||||
-- adds a thread the the queue to be resolved
|
-- all stores the threads indexed uuid, the other three only store the uuid's to index in the all table
|
||||||
|
function Server._threads(reset)
|
||||||
|
global.exp_core = not reset and global.exp_core or {}
|
||||||
|
global.exp_core.threads = not reset and global.exp_core.threads or {queue={},on_tick={},timeout={},all={}}
|
||||||
|
return global.exp_core.threads
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.close_all_threads(with_force)
|
--- Adds a thread into the resolve queue, can be used to lower lag
|
||||||
-- closes all threads, if with_force then it sets all threads to nil (no on_close event)
|
-- @usage Server.queue_thread(thread) -- return true/false
|
||||||
|
-- @tparam table the thread to add to the queue must have a resolve function (must be open)
|
||||||
|
-- @treturn bolean was the thread added
|
||||||
|
function Server.queue_thread(thread)
|
||||||
|
if not thread and not thread.valid and not thread:valid() then return false end
|
||||||
|
if not thread._resolve then return false end
|
||||||
|
table.insert(Server._threads().queue,thread.uuid)
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.run_on_tick_threads()
|
--- Closes all active threads, can use force if it causes errors
|
||||||
-- runs the on tick function for all threads
|
-- @usage Server.close_all_threads()
|
||||||
|
-- Server.close_all_threads(true) -- use if no force makes errors
|
||||||
|
-- @tparam bolean with_force use force when closing
|
||||||
|
function Server.close_all_threads(with_force)
|
||||||
|
if not with_force then
|
||||||
|
for uuid,thread in pairs(Server.threads()) do
|
||||||
|
thread:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Server._threads(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.check_timeouts()
|
--- Runs all the theads which have opened with an on_tick event
|
||||||
-- checks the timeout status on all threads with timeout
|
-- @ussage Server.run_on_tick_threads()
|
||||||
|
function Server.run_on_tick_threads()
|
||||||
|
table.each(Server._threads().on_tick,function(uuid)
|
||||||
|
local thread = Server._threads().all[uuid]
|
||||||
|
if thread and thread:valid() and thread._on_tick then
|
||||||
|
local success, err = pcall(thread._on_tick,thread)
|
||||||
|
if not success then error(err) end
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function server.interface(callback)
|
--- Checks the timeout on all active timeout threads
|
||||||
-- runs a function from a string and returns any errors or values
|
-- @ussage Server.check_timeouts()
|
||||||
|
function Server.check_timeouts()
|
||||||
|
table.each(Server._threads().timeout,function(uuid)
|
||||||
|
local thread = Server._threads().all[uuid]
|
||||||
|
if thread and thread:valid() then
|
||||||
|
thread:check_timeout()
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Given a string or function it will run that function and return any values
|
||||||
|
-- @usage Server.interface('local x = 1+1 print(x) return x') -- return 2
|
||||||
|
-- Server.interface('local x = 1+1 print(x)',thread) -- no return
|
||||||
|
-- @param callback either a function or string which will be ran via pcall
|
||||||
|
-- @param[opt] thread give a thread for the interface to run on (does not need to be open, but cant use on_resolve)
|
||||||
|
-- @param[opt] ... any args you want to pass to the function
|
||||||
|
function Server.interface(callback,thread,...)
|
||||||
|
if thread then
|
||||||
|
thread:on_resolve(function(callback,...)
|
||||||
|
if is_type(callback,'function') then
|
||||||
|
pcall(callback,...)
|
||||||
|
else
|
||||||
|
pcall(loadstring(callback),...)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
thread:open()
|
||||||
|
Server.queue_thread(thread)
|
||||||
|
else
|
||||||
|
if is_type(callback,'function') then
|
||||||
|
local success, err = pcall(callback,...)
|
||||||
|
return success, err
|
||||||
|
else
|
||||||
|
local success, err = pcall(loadstring(callback),...)
|
||||||
|
return success, err
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- thread allows you to run fuinction async to the main game
|
-- thread allows you to run fuinction async to the main game
|
||||||
local thread = {}
|
local thread = {}
|
||||||
thread.__index = thread
|
thread.__index = thread
|
||||||
|
thread.uuid = Server.new_uuid
|
||||||
--- Returns a new thread object
|
--- Returns a new thread object
|
||||||
-- @usage new_thread = thread:create()
|
-- @usage new_thread = thread:create()
|
||||||
-- @tparam[opt={}] table obj all are opt {timeout=int,name=str,data=any} advanced users can prefix with _function to avoid the on_function functions
|
-- @tparam[opt={}] table obj all are opt {timeout=int,name=str,data=any} advanced users can prefix with _function to avoid the on_function functions
|
||||||
@@ -58,12 +132,28 @@ thread.__index = thread
|
|||||||
function thread:create(obj)
|
function thread:create(obj)
|
||||||
local obj = obj or {}
|
local obj = obj or {}
|
||||||
setmetatable(obj,self)
|
setmetatable(obj,self)
|
||||||
obj.uuid = server.new_uuid()
|
obj.uuid = Server.new_uuid()
|
||||||
return obj
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Test if the thread has all requied parts
|
||||||
|
-- @usage if thread:valid() then end
|
||||||
|
-- @treturn bolean is the thread valid
|
||||||
function thread:valid()
|
function thread:valid()
|
||||||
-- tests if the thread is valid and stored
|
if is_type(self.uuid,'string') and
|
||||||
|
is_type(self.opened,'number') and
|
||||||
|
is_type(global.exp_core.threads[self.uuid],'table') and
|
||||||
|
is_type(self.timeout,'nil') or is_type(self.timeout,'number') and
|
||||||
|
is_type(self.name,'nil') or is_type(self.name,'string') and
|
||||||
|
is_type(self._close,'nil') or is_type(self._close,'function') and
|
||||||
|
is_type(self._timeout,'nil') or is_type(self._timeout,'function') and
|
||||||
|
is_type(self._on_tick,'nil') or is_type(self._on_tick,'function') and
|
||||||
|
is_type(self._resolve,'nil') or is_type(self._resolve,'function') and
|
||||||
|
is_type(self._success,'nil') or is_type(self._success,'function') and
|
||||||
|
is_type(self._error,'nil') or is_type(self._error,'function') then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function thread:open()
|
function thread:open()
|
||||||
@@ -83,7 +173,7 @@ function thread:check_timeout()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function thread:on_close(callback)
|
function thread:on_close(callback)
|
||||||
-- set the function to run when closed (does not include timeout)
|
-- set the function to run when closed (also called on_timeout)
|
||||||
end
|
end
|
||||||
|
|
||||||
function thread:on_timeout(callback)
|
function thread:on_timeout(callback)
|
||||||
@@ -105,9 +195,3 @@ end
|
|||||||
function thread:on_error(callback)
|
function thread:on_error(callback)
|
||||||
-- set the function to run if if resolve gives an error
|
-- set the function to run if if resolve gives an error
|
||||||
end
|
end
|
||||||
|
|
||||||
Event.register(Event.soft_init,function()
|
|
||||||
-- all stores the threads by uuid, the other three only store the uuid's to index in the all table
|
|
||||||
global.exp_core.threads = {queue={},on_tick={},timeout={},all={}}
|
|
||||||
global.exp_core.uuids = game.create_random_generator()
|
|
||||||
end)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user