mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-28 12:05:21 +09:00
Merge pull request #140 from Cooldude2606/feature/async
Added Async Core Module
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
--- This file defines the different triggers for the chat bot
|
||||
-- @config Chat-Reply
|
||||
|
||||
local Async = require 'expcore.async'
|
||||
local format_time = ext_require('expcore.common','format_time') --- @dep expcore.common
|
||||
|
||||
local async_message = Async.register(function(player, message)
|
||||
player.print(message)
|
||||
end)
|
||||
|
||||
return {
|
||||
allow_command_prefix_for_messages = true, --- @setting allow_command_prefix_for_messages when true any message trigger will print to all player when prefixed
|
||||
messages = { --- @setting messages will trigger when ever the word is said
|
||||
@@ -65,5 +70,48 @@ return {
|
||||
['evolution']=function()
|
||||
return {'chat-bot.current-evolution',string.format('%.2f',game.forces['enemy'].evolution_factor)}
|
||||
end,
|
||||
['makepopcorn']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.get-popcorn-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.get-popcorn-2',player.name}})
|
||||
end,
|
||||
['passsomesnaps']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.get-snaps-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.get-snaps-2',player.name}})
|
||||
Async.wait(timeout*(math.random()+0.5),async_message,player,{'chat-bot.reply',{'chat-bot.get-snaps-3',player.name}})
|
||||
end,
|
||||
['makecocktail']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.get-cocktail-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.get-cocktail-2',player.name}})
|
||||
Async.wait(timeout*(math.random()+0.5),async_message,player,{'chat-bot.reply',{'chat-bot.get-cocktail-3',player.name}})
|
||||
end,
|
||||
['makecoffee']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.make-coffee-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.make-coffee-2',player.name}})
|
||||
end,
|
||||
['orderpizza']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.order-pizza-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.order-pizza-2',player.name}})
|
||||
Async.wait(timeout*(math.random()+0.5),async_message,player,{'chat-bot.reply',{'chat-bot.order-pizza-3',player.name}})
|
||||
end,
|
||||
['maketea']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.make-tea-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.make-tea-2',player.name}})
|
||||
end,
|
||||
['meadplease']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.get-mead-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.get-mead-2',player.name}})
|
||||
end,
|
||||
['passabeer']=function(player)
|
||||
local timeout = math.floor(180*(math.random()+0.5))
|
||||
Async(async_message,player,{'chat-bot.reply',{'chat-bot.get-beer-1'}})
|
||||
Async.wait(timeout,async_message,player,{'chat-bot.reply',{'chat-bot.get-beer-2',player.name}})
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
@@ -57,8 +57,25 @@ riot=(admins) ┬┴┬┴┤ᵒ_ᵒ)├┬┴┬┴ ‹ ‹\(´ω` )/››‹
|
||||
loops=NO LOOPS; LOOPS ARE BAD; JUST NO LOOPS!!!!!; IF YOU MAKE A LOOP.... IT WILL NOT END WELL!!!!!!!
|
||||
lenny=( ͡° ͜ʖ ͡°)
|
||||
hodor=Hodor
|
||||
|
||||
|
||||
get-popcorn-1=Heating the oil and waiting for the popping sound...
|
||||
get-popcorn-2=__1__ your popcorn is finished. Lean backwards and watch the drama unfold.
|
||||
get-snaps-1=Pouring the glasses and finding the correct song book...
|
||||
get-snaps-2=Singing a song...🎤🎶
|
||||
get-snaps-3=schkål, my friends!
|
||||
get-cocktail-1= 🍸 Inintiating mind reading unit... 🍸
|
||||
get-cocktail-2= 🍸 Mixing favourite ingredients of __1__ 🍸
|
||||
get-cocktail-3=🍸 __1__ your cocktail is done.🍸
|
||||
make-coffee-1= ☕ Boiling the water and grinding the coffee beans... ☕
|
||||
make-coffee-2= ☕ __1__ we ran out of coffe beans! Have some tea instead. ☕
|
||||
order-pizza-1= 🍕 Finding nearest pizza supplier... 🍕
|
||||
order-pizza-2= 🍕 Figuring out the favourite pizza of __1__ 🍕
|
||||
order-pizza-3= 🍕 __1__ your pizza is here! 🍕
|
||||
make-tea-1= ☕ Boiling the water... ☕
|
||||
make-tea-2= ☕ __1__ your tea is done! ☕
|
||||
get-mead-1= Filling the drinking horn
|
||||
get-mead-2= Skål!
|
||||
get-beer-1= 🍺 Pouring A Glass 🍺
|
||||
get-beer-2= 🍻 Chears Mate 🍻
|
||||
|
||||
[greetings]
|
||||
greet=[color=0,1,0] Welcome to explosive gaming community server! If you like the server join our discord: __1__ [/color]
|
||||
|
||||
@@ -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