mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added Async
This commit is contained in:
71
expcore/async.lua
Normal file
71
expcore/async.lua
Normal file
@@ -0,0 +1,71 @@
|
||||
--[[-- Core Module - Async
|
||||
- An extention of task and token to allow a single require to register and run async functions.
|
||||
@core Async
|
||||
@alias Async
|
||||
|
||||
@usage
|
||||
-- To use Async you must register the allowed functions when the files are loaded, often this will just be giving access to
|
||||
-- some functions within a module if you expect that some parts may be blocked by in game permissions or a custom system you have made
|
||||
-- you may also want to register functions that you want to have a delayed call, such as waiting 2 seconds before printing a message
|
||||
|
||||
-- This player.admin is called (command or gui element event) by a player who isnt admin then it will error
|
||||
-- here we register the function to promote the player so that it will run async
|
||||
local promote_player =
|
||||
Async.register(function(player)
|
||||
player.admin = true
|
||||
end)
|
||||
|
||||
-- This will allow us to bypass this by running one tick later outside of any player scope
|
||||
Async(promote_player,game.player)
|
||||
|
||||
-- Here we make an sync function that we want to have a delay, note the delay is not defined here
|
||||
local print_message =
|
||||
Async.register(function(player,message)
|
||||
player.print(message)
|
||||
end)
|
||||
|
||||
-- We can then call the async function with a delay using the wait function
|
||||
Async.wait(60,print_message,game.player,'One second has passed!')
|
||||
|
||||
]]
|
||||
local Task = require 'utils.task' --- @dep utils.task
|
||||
local Token = require 'utils.token' --- @dep utils.token
|
||||
|
||||
local Async = {}
|
||||
|
||||
local internal_run =
|
||||
Token.register(function(params)
|
||||
local func = Token.get(params.token)
|
||||
func(unpack(params.params))
|
||||
end)
|
||||
|
||||
--- Register a new async function, must called when the file is loaded
|
||||
-- @tparam function callback the function that will become an async function
|
||||
Async.register = Token.register
|
||||
|
||||
--- Runs the async function linked to this token, you may supply any number of params as needed
|
||||
-- @tparam string token the token of the async function you want to run
|
||||
-- @tparam[opt] any ... the other params that you want to pass to your function
|
||||
function Async.run(token,...)
|
||||
Task.queue_task(internal_run, {
|
||||
token = token,
|
||||
params = {...}
|
||||
})
|
||||
end
|
||||
|
||||
--- Runs the async function linked to this token after the given number of ticks, you may supply any number of params as needed
|
||||
-- @tparam number ticks the number of ticks that you want the function to run after
|
||||
-- @tparam string token the token of the async function you want to run
|
||||
-- @tparam[opt] any ... the other params that you want to pass to your function
|
||||
function Async.wait(ticks,token,...)
|
||||
Task.set_timeout_in_ticks(ticks, internal_run, {
|
||||
token = token,
|
||||
params = {...}
|
||||
})
|
||||
end
|
||||
|
||||
return setmetatable(Async,{
|
||||
__call = function(self,...)
|
||||
self.run(...)
|
||||
end
|
||||
})
|
||||
@@ -30,13 +30,27 @@
|
||||
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Sudo = require 'expcore.sudo' --- @dep expcore.sudo
|
||||
local Async = require 'expcore.async' --- @dep expcore.async
|
||||
|
||||
local Permissions_Groups = {
|
||||
groups={}, -- store for the different groups that are created
|
||||
_prototype={} -- stores functions that are used on group instances
|
||||
}
|
||||
|
||||
-- Async function to add players to permission groups
|
||||
local add_to_permission_group =
|
||||
Async.register(function(permission_group,player)
|
||||
permission_group.add_player(player)
|
||||
end)
|
||||
Permissions_Groups.async_token_add_to_permission_group = add_to_permission_group
|
||||
|
||||
-- Async function to remove players from permission groups
|
||||
local remove_from_permission_group =
|
||||
Async.register(function(permission_group,player)
|
||||
permission_group.remove_player(player)
|
||||
end)
|
||||
Permissions_Groups.async_token_remove_from_permission_group = remove_from_permission_group
|
||||
|
||||
--- Getters.
|
||||
-- Functions that get permission groups
|
||||
-- @section getters
|
||||
@@ -224,7 +238,7 @@ function Permissions_Groups._prototype:add_player(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local group = self:get_raw()
|
||||
if not group or not player then return false end
|
||||
Sudo('add-player-to-permission-group',group,player)
|
||||
Async(add_to_permission_group, group, player)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -235,7 +249,7 @@ function Permissions_Groups._prototype:remove_player(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local group = self:get_raw()
|
||||
if not group or not player then return false end
|
||||
Sudo('remove-player-from-permission-group',group,player)
|
||||
Async(remove_from_permission_group, group, player)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -275,11 +289,4 @@ Event.on_init(function()
|
||||
Permissions_Groups.reload_permissions()
|
||||
end)
|
||||
|
||||
Sudo.register('add-player-to-permission-group',function(permission_group,player)
|
||||
permission_group.add_player(player)
|
||||
end)
|
||||
Sudo.register('remove-player-from-permission-group',function(permission_group,player)
|
||||
permission_group.remove_player(player)
|
||||
end)
|
||||
|
||||
return Permissions_Groups
|
||||
@@ -116,7 +116,7 @@ local Game = require 'utils.game' --- @dep utils.game
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Groups = require 'expcore.permission_groups' --- @dep expcore.permission_groups
|
||||
local Sudo = require 'expcore.sudo' --- @dep expcore.sudo
|
||||
local Async = require 'expcore.async' --- @dep expcore.async
|
||||
local Colours = require 'resources.color_presets' --- @dep resources.color_presets
|
||||
local write_json = ext_require('expcore.common','write_json') --- @dep expcore.common
|
||||
|
||||
@@ -443,9 +443,7 @@ end
|
||||
-- flag param - player - the player that has had they roles changed
|
||||
-- flag param - state - the state of the flag, aka if the flag is present
|
||||
function Roles.define_flag_trigger(name,callback)
|
||||
local sudo_name = 'role-flag-'..name
|
||||
Roles.config.flags[name] = sudo_name
|
||||
Sudo.register(sudo_name,callback)
|
||||
Roles.config.flags[name] = Async.register(callback)
|
||||
end
|
||||
|
||||
--- Sets the default role which every player will have, this needs to be called at least once
|
||||
@@ -748,9 +746,9 @@ end
|
||||
local function role_update(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
-- Updates flags given to the player
|
||||
for flag,sudo_name in pairs(Roles.config.flags) do
|
||||
for flag, async_token in pairs(Roles.config.flags) do
|
||||
local state = Roles.player_has_flag(player,flag)
|
||||
Sudo(sudo_name,player,state)
|
||||
Async(async_token, player, state)
|
||||
end
|
||||
-- Updates the players permission group
|
||||
local highest = Roles.get_player_highest_role(player)
|
||||
@@ -758,10 +756,10 @@ local function role_update(event)
|
||||
if highest.permission_group[1] then
|
||||
local group = game.permissions.get_group(highest.permission_group[2])
|
||||
if group then
|
||||
Sudo('add-player-to-permission-group',group,player)
|
||||
Async(Groups.async_token_add_to_permission_group, group, player)
|
||||
end
|
||||
else
|
||||
Groups.set_player_group(player,highest.permission_group)
|
||||
Groups.set_player_group(player, highest.permission_group)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
--[[-- Core Module - Sudo
|
||||
- An extention of task and token to allow a single require to register and run functions bypassing all permissions.
|
||||
@core Sudo
|
||||
@alias Sudo
|
||||
|
||||
@usage
|
||||
-- To use sudo you must register the allowed functions when the files are loaded, often this will just be giving access to
|
||||
-- some functions within a module if you expect that some parts may be blocked by in game permissions or a custom system you have made
|
||||
|
||||
-- This will be blocked if the current player (from a command or gui) is not admin
|
||||
local function make_admin(player)
|
||||
player.admin = true
|
||||
end
|
||||
|
||||
-- Here we give sudo access to the function under the name "make-admin"
|
||||
Sudo.register('make-admin',make_admin)
|
||||
|
||||
-- This will allow us to bypass this by running one tick later outside of any player scope
|
||||
Sudo.run('make-admin',game.player)
|
||||
]]
|
||||
local Task = require 'utils.task' --- @dep utils.task
|
||||
local Token = require 'utils.token' --- @dep utils.token
|
||||
|
||||
local Sudo = {
|
||||
tokens={}
|
||||
}
|
||||
|
||||
local internal_run =
|
||||
Token.register(function(params)
|
||||
local func = Token.get(params.token)
|
||||
func(unpack(params.params))
|
||||
end)
|
||||
|
||||
--- Registers a new callback under the given name, used to avoid desyncs
|
||||
-- @tparam string name the name that will be used to call this function
|
||||
-- @tparam function callback the function that will be called by this name
|
||||
function Sudo.register(name,callback)
|
||||
if _LIFECYCLE == 8 then
|
||||
error('Calling Sudo.register after on_init() or on_load() has run is a desync risk.', 2)
|
||||
end
|
||||
|
||||
if Sudo.tokens[name] then
|
||||
error(name..' is already registered',2)
|
||||
end
|
||||
|
||||
Sudo.tokens[name] = Token.register(callback)
|
||||
end
|
||||
|
||||
--- Gets the function that is registered under the given name
|
||||
-- @tparam string name the name of the function you want to get
|
||||
function Sudo.get(name)
|
||||
local token = Sudo.tokens[name]
|
||||
return token and Token.get(token)
|
||||
end
|
||||
|
||||
--- Runs the function that is registered under the given name, you may supply any number of params as needed
|
||||
-- @tparam string name the name of the function you want to run
|
||||
-- @tparam[opt] any ... the other params that you want to pass to your function
|
||||
function Sudo.run(name,...)
|
||||
local token = Sudo.tokens[name]
|
||||
|
||||
if not token then
|
||||
error('Sudo does not have access to run "'..tostring(name)..'" please make sure it is registered to sudo',2)
|
||||
end
|
||||
|
||||
Task.set_timeout_in_ticks(1, internal_run, {
|
||||
token = token,
|
||||
params = {...}
|
||||
})
|
||||
end
|
||||
|
||||
return setmetatable(Sudo,{
|
||||
__call = function(self,...)
|
||||
self.run(...)
|
||||
end
|
||||
})
|
||||
@@ -17,7 +17,7 @@ local interface_modules = {
|
||||
['Roles']='expcore.roles',
|
||||
['Store']='expcore.store',
|
||||
['Gui']='expcore.gui',
|
||||
['Sudo']='expcore.sudo'
|
||||
['Async']='expcore.async'
|
||||
}
|
||||
|
||||
-- loads all the modules given in the above table
|
||||
|
||||
Reference in New Issue
Block a user