mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Fixed Existing Lua Check Errors
This commit is contained in:
@@ -16,11 +16,11 @@ Async.register(function(player)
|
||||
end)
|
||||
|
||||
-- This will allow us to bypass the error by running one tick later outside of any player scope
|
||||
Async(promote_player,game.player)
|
||||
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)
|
||||
Async.register(function(player, message)
|
||||
player.print(message)
|
||||
end)
|
||||
|
||||
@@ -71,7 +71,7 @@ Async.register = Token.register
|
||||
Async.run(set_admin, player, true)
|
||||
|
||||
]]
|
||||
function Async.run(token,...)
|
||||
function Async.run(token, ...)
|
||||
Task.queue_task(internal_run, {
|
||||
token = token,
|
||||
params = {...}
|
||||
@@ -87,15 +87,15 @@ end
|
||||
Async.wait(300, print_to_player, 'Hello, World!')
|
||||
|
||||
]]
|
||||
function Async.wait(ticks,token,...)
|
||||
function Async.wait(ticks, token, ...)
|
||||
Task.set_timeout_in_ticks(ticks, internal_run, {
|
||||
token = token,
|
||||
params = {...}
|
||||
})
|
||||
end
|
||||
|
||||
return setmetatable(Async,{
|
||||
__call = function(self,...)
|
||||
return setmetatable(Async, {
|
||||
__call = function(self, ...)
|
||||
self.run(...)
|
||||
end
|
||||
})
|
||||
@@ -25,7 +25,7 @@ end)
|
||||
msg = ':'..msg
|
||||
end
|
||||
|
||||
for 1 = 1,repeat_count do
|
||||
for 1 = 1, repeat_count do
|
||||
Command.print(1..msg)
|
||||
end
|
||||
end)
|
||||
@@ -91,7 +91,7 @@ end)
|
||||
-- this is where that smiley param is used
|
||||
msg = ':'..msg
|
||||
end
|
||||
for 1 = 1,repeat_count do
|
||||
for 1 = 1, repeat_count do
|
||||
-- this print function will return ANY value to the user in a desync safe manor, this includes if the command was used through rcon
|
||||
Command.print(1..msg)
|
||||
end
|
||||
@@ -99,7 +99,7 @@ end)
|
||||
end)
|
||||
|
||||
-- Other values that can be returned from register
|
||||
Commands.print(any,colour[opt]) -- this will return any value value to the user including if it is ran through rcon console
|
||||
Commands.print(any, colour[opt]) -- this will return any value value to the user including if it is ran through rcon console
|
||||
Commands.error(message[opt]) -- this returns a warning to the user, aka an error that does not prevent execution of the command
|
||||
return Commands.error(message[opt]) -- this returns an error to the user, and will halt the command execution, ie no success message is returned
|
||||
Commands.success(message[opt]) -- used to return a success message however don't use this method see below
|
||||
@@ -178,7 +178,7 @@ input = Commands.parse('number-int', input, player, reject)
|
||||
if not input then return end -- nil check
|
||||
|
||||
-- Example Code:
|
||||
Commands.add_parse('number-range-int',function(input, player, reject, range_min, range_max)
|
||||
Commands.add_parse('number-range-int', function(input, player, reject, range_min, range_max)
|
||||
local rtn = tonumber(input) and math.floor(tonumber(input)) or nil -- converts input to number
|
||||
if not rtn or rtn < range_min or rtn > range_max then
|
||||
-- the input is either not a number or is outside the range
|
||||
@@ -192,7 +192,7 @@ end)
|
||||
]]
|
||||
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local player_return,write_json = _C.player_return, _C.write_json --- @dep expcore.common
|
||||
local player_return, write_json = _C.player_return, _C.write_json --- @dep expcore.common
|
||||
|
||||
local Commands = {
|
||||
--- Values returned by the signal functions to cause the command system to react
|
||||
@@ -235,7 +235,7 @@ end)
|
||||
|
||||
]]
|
||||
function Commands.add_authenticator(callback)
|
||||
table.insert(Commands.authorization,callback)
|
||||
table.insert(Commands.authorization, callback)
|
||||
return #Commands.authorization
|
||||
end
|
||||
|
||||
@@ -251,13 +251,13 @@ function Commands.remove_authenticator(callback)
|
||||
if type(callback) == 'number' then
|
||||
-- if a number is passed then it is assumed to be the index
|
||||
if Commands.authorization[callback] then
|
||||
table.remove(Commands.authorization,callback)
|
||||
table.remove(Commands.authorization, callback)
|
||||
return true
|
||||
end
|
||||
else
|
||||
-- will search the array and remove the key
|
||||
local index
|
||||
for key,value in pairs(Commands.authorization) do
|
||||
for key, value in pairs(Commands.authorization) do
|
||||
if value == callback then
|
||||
index = key
|
||||
break
|
||||
@@ -265,7 +265,7 @@ function Commands.remove_authenticator(callback)
|
||||
end
|
||||
-- if the function was found it is removed
|
||||
if index then
|
||||
table.remove(Commands.authorization,index)
|
||||
table.remove(Commands.authorization, index)
|
||||
return true
|
||||
end
|
||||
end
|
||||
@@ -284,7 +284,7 @@ end
|
||||
local authorized, status = Commands.authorize(game.player, 'repeat-name')
|
||||
|
||||
]]
|
||||
function Commands.authorize(player,command_name)
|
||||
function Commands.authorize(player, command_name)
|
||||
local failed
|
||||
if not player then return true end
|
||||
local command_data = Commands.commands[command_name]
|
||||
@@ -297,9 +297,9 @@ function Commands.authorize(player,command_name)
|
||||
end
|
||||
|
||||
-- loops over each authorization callback if any return false or unauthorized command will fail
|
||||
for _,callback in pairs(Commands.authorization) do
|
||||
for _, callback in pairs(Commands.authorization) do
|
||||
-- callback(player: LuaPlayer, command: string, flags: table, reject: function(error_message?: string))
|
||||
local success, rtn = pcall(callback,player,command_name,command_data.flags,auth_fail)
|
||||
local success, rtn = pcall(callback, player, command_name, command_data.flags, auth_fail)
|
||||
-- error handler
|
||||
if not success then
|
||||
-- the callback failed to run
|
||||
@@ -341,8 +341,8 @@ function Commands.get(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
if not player then return Commands.commands end
|
||||
local allowed = {}
|
||||
for name,command_data in pairs(Commands.commands) do
|
||||
if Commands.authorize(player,name) then
|
||||
for name, command_data in pairs(Commands.commands) do
|
||||
if Commands.authorize(player, name) then
|
||||
allowed[name]=command_data
|
||||
end
|
||||
end
|
||||
@@ -361,20 +361,20 @@ local commands = Commands.search('repeat')
|
||||
local commands = Commands.search('repeat', game.player)
|
||||
|
||||
]]
|
||||
function Commands.search(keyword,player)
|
||||
function Commands.search(keyword, player)
|
||||
local custom_commands = Commands.get(player)
|
||||
local matches = {}
|
||||
keyword = keyword:lower()
|
||||
-- loops over custom commands
|
||||
for name,command_data in pairs(custom_commands) do
|
||||
for name, command_data in pairs(custom_commands) do
|
||||
-- combines name help and aliases into one message to be searched
|
||||
local search = string.format('%s %s %s',name,command_data.help,table.concat(command_data.aliases,' '))
|
||||
local search = string.format('%s %s %s', name, command_data.help, table.concat(command_data.aliases, ' '))
|
||||
if search:lower():match(keyword) then
|
||||
matches[name] = command_data
|
||||
end
|
||||
end
|
||||
-- loops over the names of game commands
|
||||
for name,description in pairs(commands.game_commands) do
|
||||
for name, description in pairs(commands.game_commands) do
|
||||
if name:lower():match(keyword) then
|
||||
-- because game commands lack some stuff that the custom ones have they are formated
|
||||
matches[name] = {
|
||||
@@ -411,7 +411,7 @@ Commands.add_parse('number-range-int', function(input, player, reject, range_min
|
||||
end)
|
||||
|
||||
]]
|
||||
function Commands.add_parse(name,callback)
|
||||
function Commands.add_parse(name, callback)
|
||||
if Commands.parse_functions[name] then
|
||||
return false
|
||||
else
|
||||
@@ -442,10 +442,10 @@ end
|
||||
local parsed_input = Commands.parse('number-range-int', '7', player, reject, 1, 10) -- valid range 1 to 10
|
||||
|
||||
]]
|
||||
function Commands.parse(name,input,player,reject,...)
|
||||
function Commands.parse(name, input, player, reject, ...)
|
||||
if not Commands.parse_functions[name] then return end
|
||||
local success,rtn = pcall(Commands.parse_functions[name],input,player,reject,...)
|
||||
if not success then error(rtn,2) return end
|
||||
local success, rtn = pcall(Commands.parse_functions[name], input, player, reject, ...)
|
||||
if not success then error(rtn, 2) return end
|
||||
if not rtn then return end
|
||||
if rtn == Commands.defines.error then return end
|
||||
return rtn
|
||||
@@ -465,11 +465,11 @@ local command =
|
||||
Commands.new_command('repeat-name', 'Will repeat you name a number of times in chat.')
|
||||
|
||||
]]
|
||||
function Commands.new_command(name,help)
|
||||
function Commands.new_command(name, help)
|
||||
local command = setmetatable({
|
||||
name=name,
|
||||
help=help,
|
||||
callback=function() Commands.internal_error(false,name,'No callback registered') end,
|
||||
callback=function() Commands.internal_error(false, name, 'No callback registered') end,
|
||||
auto_concat=false,
|
||||
min_param_count=0,
|
||||
max_param_count=0,
|
||||
@@ -500,10 +500,10 @@ command:add_param('smiley', true, function(input, player, reject)
|
||||
end)
|
||||
|
||||
]]
|
||||
function Commands._prototype:add_param(name,optional,parse,...)
|
||||
function Commands._prototype:add_param(name, optional, parse, ...)
|
||||
local parse_args = {...}
|
||||
if type(optional) ~= 'boolean' then
|
||||
parse_args = {parse,...}
|
||||
parse_args = {parse, ...}
|
||||
parse = optional
|
||||
optional = false
|
||||
end
|
||||
@@ -535,7 +535,7 @@ command:set_defaults{
|
||||
|
||||
]]
|
||||
function Commands._prototype:set_defaults(defaults)
|
||||
for name,value in pairs(defaults) do
|
||||
for name, value in pairs(defaults) do
|
||||
if self.params[name] then
|
||||
self.params[name].default = value
|
||||
end
|
||||
@@ -555,7 +555,7 @@ command:set_flag('admin_only', true)
|
||||
command:set_flag('admin_only')
|
||||
|
||||
]]
|
||||
function Commands._prototype:set_flag(name,value)
|
||||
function Commands._prototype:set_flag(name, value)
|
||||
value = value or true
|
||||
self.flags[name] = value
|
||||
return self
|
||||
@@ -570,8 +570,8 @@ command:add_alias('name', 'rname')
|
||||
|
||||
]]
|
||||
function Commands._prototype:add_alias(...)
|
||||
for _,alias in pairs({...}) do
|
||||
table.insert(self.aliases,alias)
|
||||
for _, alias in pairs({...}) do
|
||||
table.insert(self.aliases, alias)
|
||||
--Commands.alias_map[alias] = self.name
|
||||
end
|
||||
return self
|
||||
@@ -600,7 +600,7 @@ command:register(function(player, repeat_count, smiley, _)
|
||||
local msg = ') '..player.name
|
||||
if smiley then msg = ':'..msg end
|
||||
|
||||
for 1 = 1,repeat_count do
|
||||
for 1 = 1, repeat_count do
|
||||
Command.print(1..msg)
|
||||
end
|
||||
end)
|
||||
@@ -610,26 +610,26 @@ function Commands._prototype:register(callback)
|
||||
-- generates a description to be used
|
||||
self.callback = callback
|
||||
local description = ''
|
||||
for param_name,param_details in pairs(self.params) do
|
||||
for param_name, param_details in pairs(self.params) do
|
||||
if param_details.optional then
|
||||
description = string.format('%s [%s]',description,param_name)
|
||||
description = string.format('%s [%s]', description, param_name)
|
||||
else
|
||||
description = string.format('%s <%s>',description,param_name)
|
||||
description = string.format('%s <%s>', description, param_name)
|
||||
end
|
||||
end
|
||||
self.description = description
|
||||
-- registers the command under its own name
|
||||
commands.add_command(self.name,{'expcore-commands.command-help',description,self.help},function(command_event)
|
||||
local success, err = pcall(Commands.run_command,command_event)
|
||||
commands.add_command(self.name, {'expcore-commands.command-help', description, self.help}, function(command_event)
|
||||
local success, err = pcall(Commands.run_command, command_event)
|
||||
if not success then log('[ERROR] command/'..self.name..' :: '..err) end
|
||||
end)
|
||||
-- adds any aliases that it has
|
||||
for _,alias in pairs(self.aliases) do
|
||||
for _, alias in pairs(self.aliases) do
|
||||
if not commands.commands[alias] and not commands.game_commands[alias] then
|
||||
commands.add_command(alias,{'expcore-commands.command-help',description,self.help},function(command_event)
|
||||
commands.add_command(alias, {'expcore-commands.command-help', description, self.help}, function(command_event)
|
||||
command_event.name = self.name
|
||||
local success, err = pcall(Commands.run_command,command_event)
|
||||
Commands.internal_error(success,self.name,err)
|
||||
local success, err = pcall(Commands.run_command, command_event)
|
||||
Commands.internal_error(success, self.name, err)
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -649,9 +649,9 @@ nb: this is for non fatal errors meaning there is no log of this event, use duri
|
||||
return Commands.error('The player you selected is offline')
|
||||
|
||||
]]
|
||||
function Commands.error(error_message,play_sound)
|
||||
function Commands.error(error_message, play_sound)
|
||||
error_message = error_message or ''
|
||||
player_return({'expcore-commands.command-fail',error_message},'orange_red')
|
||||
player_return({'expcore-commands.command-fail', error_message}, 'orange_red')
|
||||
if play_sound ~= false then
|
||||
play_sound = play_sound or 'utility/wire_pickup'
|
||||
if game.player then game.player.play_sound{path=play_sound} end
|
||||
@@ -673,10 +673,10 @@ if Commands.internal_error(success, command_data.name, err) then
|
||||
end
|
||||
|
||||
]]
|
||||
function Commands.internal_error(success,command_name,error_message)
|
||||
function Commands.internal_error(success, command_name, error_message)
|
||||
if not success then
|
||||
Commands.error('Internal Error, Please contact an admin','utility/cannot_build')
|
||||
log{'expcore-commands.command-error-log-format',command_name,error_message}
|
||||
Commands.error('Internal Error, Please contact an admin', 'utility/cannot_build')
|
||||
log{'expcore-commands.command-error-log-format', command_name, error_message}
|
||||
end
|
||||
return not success
|
||||
end
|
||||
@@ -695,7 +695,7 @@ return 'Your message has been printed'
|
||||
]]
|
||||
function Commands.success(value)
|
||||
if value ~= nil then player_return(value) end
|
||||
player_return({'expcore-commands.command-ran'},'cyan')
|
||||
player_return({'expcore-commands.command-ran'}, 'cyan')
|
||||
return Commands.defines.success
|
||||
end
|
||||
|
||||
@@ -710,9 +710,9 @@ Commands.print('Your command is in progress')
|
||||
]]
|
||||
|
||||
-- logs command usage to file
|
||||
local function command_log(player,command,comment,params,raw,details)
|
||||
local function command_log(player, command, comment, params, raw, details)
|
||||
local player_name = player and player.name or '<Server>'
|
||||
write_json('log/commands.log',{
|
||||
write_json('log/commands.log', {
|
||||
player_name=player_name,
|
||||
command_name=command.name,
|
||||
comment=comment,
|
||||
@@ -734,27 +734,27 @@ function Commands.run_command(command_event)
|
||||
end
|
||||
|
||||
-- checks if player is allowed to use the command
|
||||
local authorized, auth_fail = Commands.authorize(player,command_data.name)
|
||||
local authorized, auth_fail = Commands.authorize(player, command_data.name)
|
||||
if not authorized then
|
||||
command_log(player,command_data,'Failed Auth',{},command_event.parameter)
|
||||
Commands.error(auth_fail,'utility/cannot_build')
|
||||
command_log(player, command_data, 'Failed Auth', {}, command_event.parameter)
|
||||
Commands.error(auth_fail, 'utility/cannot_build')
|
||||
return
|
||||
end
|
||||
|
||||
-- null param check
|
||||
if command_data.min_param_count > 0 and not command_event.parameter then
|
||||
command_log(player,command_data,'No Params Given',{},command_event.parameter)
|
||||
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
|
||||
command_log(player, command_data, 'No Params Given', {}, command_event.parameter)
|
||||
Commands.error({'expcore-commands.invalid-inputs', command_data.name, command_data.description})
|
||||
return
|
||||
end
|
||||
|
||||
-- splits the arguments
|
||||
local input_string = command_event.parameter or ''
|
||||
local quote_params = {} -- stores any " " params
|
||||
input_string = input_string:gsub(' "[^"]-"',function(w)
|
||||
input_string = input_string:gsub(' "[^"]-"', function(w)
|
||||
-- finds all " " params are removes spaces for the next part
|
||||
local no_spaces = w:gsub('%s','_')
|
||||
local no_quotes = w:sub(2,-2)
|
||||
local no_spaces = w:gsub('%s', '_')
|
||||
local no_quotes = w:sub(2, -2)
|
||||
quote_params[no_spaces]=no_quotes
|
||||
if command_data.auto_concat then
|
||||
-- if auto concat then don't remove quotes as it should be included later
|
||||
@@ -772,8 +772,8 @@ function Commands.run_command(command_event)
|
||||
-- there are too many params given to the command
|
||||
if not command_data.auto_concat then
|
||||
-- error as they should not be more
|
||||
command_log(player,command_data,'Invalid Input: Too Many Params',raw_params,input_string)
|
||||
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
|
||||
command_log(player, command_data, 'Invalid Input: Too Many Params', raw_params, input_string)
|
||||
Commands.error({'expcore-commands.invalid-inputs', command_data.name, command_data.description})
|
||||
return
|
||||
else
|
||||
-- concat to the last param
|
||||
@@ -789,10 +789,10 @@ function Commands.run_command(command_event)
|
||||
-- all words are added to an array
|
||||
if quote_params[word] then
|
||||
-- if it was a " " param then the spaces are re added now
|
||||
table.insert(raw_params,quote_params[word])
|
||||
table.insert(raw_params, quote_params[word])
|
||||
last_index = last_index + 1
|
||||
else
|
||||
table.insert(raw_params,word)
|
||||
table.insert(raw_params, word)
|
||||
last_index = last_index + 1
|
||||
end
|
||||
end
|
||||
@@ -801,8 +801,8 @@ function Commands.run_command(command_event)
|
||||
-- checks param count
|
||||
local param_count = #raw_params
|
||||
if param_count < command_data.min_param_count then
|
||||
command_log(player,command_data,'Invalid Input: Not Enough Params',raw_params,input_string)
|
||||
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
|
||||
command_log(player, command_data, 'Invalid Input: Not Enough Params', raw_params, input_string)
|
||||
Commands.error({'expcore-commands.invalid-inputs', command_data.name, command_data.description})
|
||||
return
|
||||
end
|
||||
|
||||
@@ -817,58 +817,58 @@ function Commands.run_command(command_event)
|
||||
end
|
||||
if not type(parse_callback) == 'function' then
|
||||
-- if its not a function throw and error
|
||||
Commands.internal_error(false,command_data.name,'Invalid param parse '..tostring(param_data.parse))
|
||||
command_log(player,command_data,'Internal Error: Invalid Param Parse',params,command_event.parameter,tostring(param_data.parse))
|
||||
Commands.internal_error(false, command_data.name, 'Invalid param parse '..tostring(param_data.parse))
|
||||
command_log(player, command_data, 'Internal Error: Invalid Param Parse', params, command_event.parameter, tostring(param_data.parse))
|
||||
return
|
||||
end
|
||||
-- used below as the reject function
|
||||
local parse_fail = function(error_message)
|
||||
error_message = error_message or ''
|
||||
command_log(player,command_data,'Invalid Param Given',raw_params,input_string)
|
||||
return Commands.error{'expcore-commands.invalid-param',param_name,error_message}
|
||||
command_log(player, command_data, 'Invalid Param Given', raw_params, input_string)
|
||||
return Commands.error{'expcore-commands.invalid-param', param_name, error_message}
|
||||
end
|
||||
-- input: string, player: LuaPlayer, reject: function, ... extra args
|
||||
local success,param_parsed = pcall(parse_callback,raw_params[index],player,parse_fail,unpack(param_data.parse_args))
|
||||
if Commands.internal_error(success,command_data.name,param_parsed) then
|
||||
return command_log(player,command_data,'Internal Error: Param Parse Fail',params,command_event.parameter,param_parsed)
|
||||
local success, param_parsed = pcall(parse_callback, raw_params[index], player, parse_fail, unpack(param_data.parse_args))
|
||||
if Commands.internal_error(success, command_data.name, param_parsed) then
|
||||
return command_log(player, command_data, 'Internal Error: Param Parse Fail', params, command_event.parameter, param_parsed)
|
||||
end
|
||||
if param_data.optional == true and raw_params[index] == nil then
|
||||
-- if it is optional and param is nil then it is set to default
|
||||
param_parsed = param_data.default
|
||||
if type(param_parsed) == 'function' then
|
||||
-- player: LuaPlayer
|
||||
success,param_parsed = pcall(param_parsed,player)
|
||||
if Commands.internal_error(success,command_data.name,param_parsed) then
|
||||
return command_log(player,command_data,'Internal Error: Default Value Fail',params,command_event.parameter,param_parsed)
|
||||
success, param_parsed = pcall(param_parsed, player)
|
||||
if Commands.internal_error(success, command_data.name, param_parsed) then
|
||||
return command_log(player, command_data, 'Internal Error: Default Value Fail', params, command_event.parameter, param_parsed)
|
||||
end
|
||||
end
|
||||
elseif param_parsed == nil or param_parsed == Commands.defines.error or param_parsed == parse_fail then
|
||||
-- no value was returned or error was returned, if nil then give generic error
|
||||
if not param_parsed == Commands.defines.error then
|
||||
command_log(player,command_data,'Invalid Param Given',raw_params,input_string,param_name)
|
||||
Commands.error{'expcore-commands.command-error-param-format',param_name,'please make sure it is the correct type'}
|
||||
command_log(player, command_data, 'Invalid Param Given', raw_params, input_string, param_name)
|
||||
Commands.error{'expcore-commands.command-error-param-format', param_name, 'please make sure it is the correct type'}
|
||||
end
|
||||
return
|
||||
end
|
||||
-- adds the param to the table to be passed to the command callback
|
||||
table.insert(params,param_parsed)
|
||||
table.insert(params, param_parsed)
|
||||
index=index+1
|
||||
end
|
||||
|
||||
-- runs the command
|
||||
-- player: LuaPlayer, ... command params, raw: string
|
||||
table.insert(params,command_data.max_param_count+1,input_string)
|
||||
local success, err = pcall(command_data.callback,player,unpack(params))
|
||||
if Commands.internal_error(success,command_data.name,err) then
|
||||
return command_log(player,command_data,'Internal Error: Command Callback Fail',raw_params,command_event.parameter,err)
|
||||
table.insert(params, command_data.max_param_count+1, input_string)
|
||||
local success, err = pcall(command_data.callback, player, unpack(params))
|
||||
if Commands.internal_error(success, command_data.name, err) then
|
||||
return command_log(player, command_data, 'Internal Error: Command Callback Fail', raw_params, command_event.parameter, err)
|
||||
end
|
||||
if err == Commands.defines.error or err == Commands.error then
|
||||
return command_log(player,command_data,'Custom Error',raw_params,input_string)
|
||||
return command_log(player, command_data, 'Custom Error', raw_params, input_string)
|
||||
elseif err ~= Commands.defines.success and err ~= Commands.success then
|
||||
-- in this case the user has not received any output
|
||||
Commands.success(err)
|
||||
end
|
||||
command_log(player,command_data,'Success',raw_params,input_string)
|
||||
command_log(player, command_data, 'Success', raw_params, input_string)
|
||||
end
|
||||
|
||||
return Commands
|
||||
@@ -42,7 +42,7 @@ type_error(value, 'number', 'Value must be a number')
|
||||
]]
|
||||
function Common.type_error(value, test_type, error_message, level)
|
||||
level = level and level+1 or 2
|
||||
return Common.type_check(value,test_type) or error(error_message,level)
|
||||
return Common.type_check(value, test_type) or error(error_message, level)
|
||||
end
|
||||
|
||||
--[[-- Asserts the argument is one of type test_types
|
||||
@@ -51,7 +51,7 @@ end
|
||||
@treturn boolean true if value is one of test_types
|
||||
|
||||
@usage-- Check for a string or table
|
||||
local is_string_or_table = multi_type_check(value, {'string','table'})
|
||||
local is_string_or_table = multi_type_check(value, {'string', 'table'})
|
||||
|
||||
]]
|
||||
function Common.multi_type_check(value, test_types)
|
||||
@@ -72,12 +72,12 @@ end
|
||||
@treturn boolean true if no error was called
|
||||
|
||||
@usage-- Raise error if value is not a string or table
|
||||
multi_type_error('foo', {'string','table'}, 'Value must be a string or table')
|
||||
multi_type_error('foo', {'string', 'table'}, 'Value must be a string or table')
|
||||
|
||||
]]
|
||||
function Common.multi_type_error(value, test_types, error_message, level)
|
||||
level = level and level+1 or 2
|
||||
return Common.mult_type_check(value, test_types) or error(error_message,level)
|
||||
return Common.mult_type_check(value, test_types) or error(error_message, level)
|
||||
end
|
||||
|
||||
--[[-- Raises an error when the value is the incorrect type, uses a consistent error message format
|
||||
@@ -95,15 +95,15 @@ validate_argument_type(value, 'number', 2, 'repeat_count')
|
||||
|
||||
]]
|
||||
function Common.validate_argument_type(value, test_type, param_number, param_name)
|
||||
if not Common.test_type(value,test_type) then
|
||||
local function_name = debug.getinfo(2,'n').name or '<anon>'
|
||||
if not Common.test_type(value, test_type) then
|
||||
local function_name = debug.getinfo(2, 'n').name or '<anon>'
|
||||
local error_message
|
||||
if param_name then
|
||||
error_message = string.format('Bad argument #%d to %q; %q is of type %s expected %s', param_number, function_name, param_name, type(value), test_type)
|
||||
else
|
||||
error_message = string.format('Bad argument #%d to %q; argument is of type %s expected %s', param_number, function_name, type(value), test_type)
|
||||
end
|
||||
return error(error_message,3)
|
||||
return error(error_message, 3)
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -116,22 +116,22 @@ end
|
||||
@treturn boolean true if no error was raised
|
||||
|
||||
@usage-- Output: "Bad argument #2 to "<anon>"; argument is of type number expected string or table"
|
||||
validate_argument_type(value, {'string','table'}, 2)
|
||||
validate_argument_type(value, {'string', 'table'}, 2)
|
||||
|
||||
@usage-- Output: "Bad argument #2 to "<anon>"; "player" is of type number expected string or table"
|
||||
validate_argument_type(value, {'string','table'}, 2, 'player')
|
||||
validate_argument_type(value, {'string', 'table'}, 2, 'player')
|
||||
|
||||
]]
|
||||
function Common.validate_argument_multi_type(value, test_types, param_number, param_name)
|
||||
if not Common.multi_type_check(value,test_types) then
|
||||
local function_name = debug.getinfo(2,'n').name or '<anon>'
|
||||
if not Common.multi_type_check(value, test_types) then
|
||||
local function_name = debug.getinfo(2, 'n').name or '<anon>'
|
||||
local error_message
|
||||
if param_name then
|
||||
error_message = string.format('Bad argument #%2d to %q; %q is of type %s expected %s', param_number, function_name, param_name, type(value), table.concat(test_types,' or '))
|
||||
error_message = string.format('Bad argument #%2d to %q; %q is of type %s expected %s', param_number, function_name, param_name, type(value), table.concat(test_types, ' or '))
|
||||
else
|
||||
error_message = string.format('Bad argument #%2d to %q; argument is of type %s expected %s', param_number, function_name, type(value), table.concat(test_types,' or '))
|
||||
error_message = string.format('Bad argument #%2d to %q; argument is of type %s expected %s', param_number, function_name, type(value), table.concat(test_types, ' or '))
|
||||
end
|
||||
return error(error_message,3)
|
||||
return error(error_message, 3)
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -140,8 +140,8 @@ end
|
||||
-- @usage error_if_runtime()
|
||||
function Common.error_if_runtime()
|
||||
if _LIFECYCLE == 8 then
|
||||
local function_name = debug.getinfo(2,'n').name or '<anon>'
|
||||
error(function_name..' can not be called during runtime',3)
|
||||
local function_name = debug.getinfo(2, 'n').name or '<anon>'
|
||||
error(function_name..' can not be called during runtime', 3)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -149,8 +149,8 @@ end
|
||||
-- @usage error_if_runetime_closure(func)
|
||||
function Common.error_if_runetime_closure(func)
|
||||
if _LIFECYCLE == 8 and Debug.is_closure(func) then
|
||||
local function_name = debug.getinfo(2,'n').name or '<anon>'
|
||||
error(function_name..' can not be called during runtime with a closure',3)
|
||||
local function_name = debug.getinfo(2, 'n').name or '<anon>'
|
||||
error(function_name..' can not be called during runtime with a closure', 3)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -180,7 +180,7 @@ end
|
||||
local value = Common.resolve_value(self.defaut_value, self)
|
||||
|
||||
]]
|
||||
function Common.resolve_value(value,...)
|
||||
function Common.resolve_value(value, ...)
|
||||
return value and type(value) == 'function' and value(...) or value
|
||||
end
|
||||
|
||||
@@ -201,7 +201,7 @@ end
|
||||
-- @usage comma_value(input_number)
|
||||
function Common.comma_value(n) -- credit http://richard.warburton.it
|
||||
local left, num, right = string.match(n, '^([^%d]*%d)(%d*)(.-)$')
|
||||
return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right
|
||||
return left .. (num:reverse():gsub('(%d%d%d)', '%1, '):reverse()) .. right
|
||||
end
|
||||
|
||||
--[[-- Sets a table element to value while also returning value.
|
||||
@@ -227,8 +227,8 @@ end
|
||||
write_json('dump', tbl)
|
||||
|
||||
]]
|
||||
function Common.write_json(path,tbl)
|
||||
game.write_file(path,game.table_to_json(tbl)..'\n',true,0)
|
||||
function Common.write_json(path, tbl)
|
||||
game.write_file(path, game.table_to_json(tbl)..'\n', true, 0)
|
||||
end
|
||||
|
||||
--[[-- Calls a require that will not error if the file is not found
|
||||
@@ -241,9 +241,9 @@ local Module = opt_require 'expcore.common'
|
||||
|
||||
]]
|
||||
function Common.opt_require(path)
|
||||
local success, rtn = pcall(require,path)
|
||||
local success, rtn = pcall(require, path)
|
||||
if success then return rtn
|
||||
else return nil,rtn end
|
||||
else return nil, rtn end
|
||||
end
|
||||
|
||||
--[[-- Returns a desync safe file path for the current file
|
||||
@@ -273,17 +273,17 @@ local colors = enum{
|
||||
]]
|
||||
function Common.enum(tbl)
|
||||
local rtn = {}
|
||||
for k,v in pairs(tbl) do
|
||||
for k, v in pairs(tbl) do
|
||||
if type(k) ~= 'number' then
|
||||
rtn[v]=k
|
||||
end
|
||||
end
|
||||
for k,v in pairs(tbl) do
|
||||
for k, v in pairs(tbl) do
|
||||
if type(k) == 'number' then
|
||||
table.insert(rtn,v)
|
||||
table.insert(rtn, v)
|
||||
end
|
||||
end
|
||||
for k,v in pairs(rtn) do
|
||||
for k, v in pairs(rtn) do
|
||||
rtn[v]=k
|
||||
end
|
||||
return rtn
|
||||
@@ -306,20 +306,19 @@ local value = auto_complete(tbl, "foo", true)
|
||||
local key = auto_complete(tbl, "foo", true, true)
|
||||
|
||||
]]
|
||||
function Common.auto_complete(options,input,use_key,rtn_key)
|
||||
local rtn = {}
|
||||
function Common.auto_complete(options, input, use_key, rtn_key)
|
||||
if type(input) ~= 'string' then return end
|
||||
input = input:lower()
|
||||
for key,value in pairs(options) do
|
||||
for key, value in pairs(options) do
|
||||
local check = use_key and key or value
|
||||
if Common.string_contains(string.lower(check),input) then
|
||||
if Common.string_contains(string.lower(check), input) then
|
||||
return rtn_key and key or value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Formating.
|
||||
-- @section formating
|
||||
--- Formatting.
|
||||
-- @section formatting
|
||||
|
||||
--[[-- Returns a valid string with the name of the actor of a command.
|
||||
@tparam string player_name the name of the player to use rather than server, used only if game.player is nil
|
||||
@@ -335,32 +334,32 @@ end
|
||||
|
||||
--[[-- Returns a message with valid chat tags to change its colour
|
||||
@tparam string message the message that will be in the output
|
||||
@tparam table color a color which contains r,g,b as its keys
|
||||
@tparam table color a color which contains r, g, b as its keys
|
||||
@treturn string the message with the color tags included
|
||||
|
||||
@usage-- Use factorio tags to color a chat message
|
||||
local message = format_chat_colour('Hello, World!', { r=355, g=100, b=100 })
|
||||
|
||||
]]
|
||||
function Common.format_chat_colour(message,color)
|
||||
function Common.format_chat_colour(message, color)
|
||||
color = color or Colours.white
|
||||
local color_tag = '[color='..math.round(color.r,3)..','..math.round(color.g,3)..','..math.round(color.b,3)..']'
|
||||
return string.format('%s%s[/color]',color_tag,message)
|
||||
local color_tag = '[color='..math.round(color.r, 3)..', '..math.round(color.g, 3)..', '..math.round(color.b, 3)..']'
|
||||
return string.format('%s%s[/color]', color_tag, message)
|
||||
end
|
||||
|
||||
--[[-- Returns a message with valid chat tags to change its colour, using localization
|
||||
@tparam ?string|table message the message that will be in the output
|
||||
@tparam table color a color which contains r,g,b as its keys
|
||||
@tparam table color a color which contains r, g, b as its keys
|
||||
@treturn table the message with the color tags included
|
||||
|
||||
@usage-- Use factorio tags and locale strings to color a chat message
|
||||
local message = format_chat_colour_localized('Hello, World!', { r=355, g=100, b=100 })
|
||||
|
||||
]]
|
||||
function Common.format_chat_colour_localized(message,color)
|
||||
function Common.format_chat_colour_localized(message, color)
|
||||
color = color or Colours.white
|
||||
color = math.round(color.r,3)..','..math.round(color.g,3)..','..math.round(color.b,3)
|
||||
return {'color-tag',color,message}
|
||||
color = math.round(color.r, 3)..', '..math.round(color.g, 3)..', '..math.round(color.b, 3)
|
||||
return {'color-tag', color, message}
|
||||
end
|
||||
|
||||
--[[-- Returns the players name in the players color
|
||||
@@ -372,14 +371,14 @@ end
|
||||
local message = format_chat_player_name(game.player, true)
|
||||
|
||||
]]
|
||||
function Common.format_chat_player_name(player,raw_string)
|
||||
function Common.format_chat_player_name(player, raw_string)
|
||||
player = Game.get_player_from_any(player)
|
||||
local player_name = player and player.name or '<Server>'
|
||||
local player_chat_colour = player and player.chat_color or Colours.white
|
||||
if raw_string then
|
||||
return Common.format_chat_colour(player_name,player_chat_colour)
|
||||
return Common.format_chat_colour(player_name, player_chat_colour)
|
||||
else
|
||||
return Common.format_chat_colour_localized(player_name,player_chat_colour)
|
||||
return Common.format_chat_colour_localized(player_name, player_chat_colour)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -398,16 +397,16 @@ player_return('Hello, World!', 'green')
|
||||
player_return('Hello, World!', nil, player)
|
||||
|
||||
]]
|
||||
function Common.player_return(value,colour,player)
|
||||
colour = Common.type_check(colour,'table') and colour or Colours[colour] ~= Colours.white and Colours[colour] or Colours.white
|
||||
function Common.player_return(value, colour, player)
|
||||
colour = Common.type_check(colour, 'table') and colour or Colours[colour] ~= Colours.white and Colours[colour] or Colours.white
|
||||
player = player or game.player
|
||||
-- converts the value to a string
|
||||
local returnAsString
|
||||
if Common.type_check(value,'table') or type(value) == 'userdata' then
|
||||
if Common.type_check(value.__self,'userdata') or type(value) == 'userdata' then
|
||||
if Common.type_check(value, 'table') or type(value) == 'userdata' then
|
||||
if Common.type_check(value.__self, 'userdata') or type(value) == 'userdata' then
|
||||
-- value is userdata
|
||||
returnAsString = 'Cant Display Userdata'
|
||||
elseif Common.type_check(value[1],'string') and string.find(value[1],'.+[.].+') and not string.find(value[1],'%s') then
|
||||
elseif Common.type_check(value[1], 'string') and string.find(value[1], '.+[.].+') and not string.find(value[1], '%s') then
|
||||
-- value is a locale string
|
||||
returnAsString = value
|
||||
elseif getmetatable(value) ~= nil and not tostring(value):find('table: 0x') then
|
||||
@@ -415,9 +414,9 @@ function Common.player_return(value,colour,player)
|
||||
returnAsString = tostring(value)
|
||||
else
|
||||
-- value is a table
|
||||
returnAsString = table.inspect(value,{depth=5,indent=' ',newline='\n'})
|
||||
returnAsString = table.inspect(value, {depth=5, indent=' ', newline='\n'})
|
||||
end
|
||||
elseif Common.type_check(value,'function') then
|
||||
elseif Common.type_check(value, 'function') then
|
||||
-- value is a function
|
||||
returnAsString = 'Cant Display Functions'
|
||||
else returnAsString = tostring(value) end
|
||||
@@ -425,10 +424,10 @@ function Common.player_return(value,colour,player)
|
||||
if player then
|
||||
-- allows any valid player identifier to be used
|
||||
player = Game.get_player_from_any(player)
|
||||
if not player then error('Invalid Player given to player_return',2) end
|
||||
if not player then error('Invalid Player given to player_return', 2) end
|
||||
-- plays a nice sound that is different to normal message sound
|
||||
player.play_sound{path='utility/scenario_message'}
|
||||
player.print(returnAsString,colour)
|
||||
player.print(returnAsString, colour)
|
||||
else rcon.print(returnAsString) end
|
||||
end
|
||||
|
||||
@@ -452,7 +451,7 @@ local time = format_time(18000, { hours=true, minutes=true, seconds=true, string
|
||||
local time = format_time(18000, { hours=true, minutes=true, seconds=true, string=true, null=true })
|
||||
|
||||
]]
|
||||
function Common.format_time(ticks,options)
|
||||
function Common.format_time(ticks, options)
|
||||
-- Sets up the options
|
||||
options = options or {
|
||||
days=false,
|
||||
@@ -508,31 +507,31 @@ function Common.format_time(ticks,options)
|
||||
rtn_minutes = long and rtn_minutes..' minutes' or rtn_minutes..'m'
|
||||
rtn_seconds = long and rtn_seconds..' seconds' or rtn_seconds..'s'
|
||||
else
|
||||
rtn_days = {suffix..'days'..suffix_2,rtn_days}
|
||||
rtn_hours = {suffix..'hours'..suffix_2,rtn_hours}
|
||||
rtn_minutes = {suffix..'minutes'..suffix_2,rtn_minutes}
|
||||
rtn_seconds = {suffix..'seconds'..suffix_2,rtn_seconds}
|
||||
rtn_days = {suffix..'days'..suffix_2, rtn_days}
|
||||
rtn_hours = {suffix..'hours'..suffix_2, rtn_hours}
|
||||
rtn_minutes = {suffix..'minutes'..suffix_2, rtn_minutes}
|
||||
rtn_seconds = {suffix..'seconds'..suffix_2, rtn_seconds}
|
||||
end
|
||||
elseif not options.null then
|
||||
-- weather string or not it has same format
|
||||
rtn_days = string.format('%02d',rtn_days)
|
||||
rtn_hours = string.format('%02d',rtn_hours)
|
||||
rtn_minutes = string.format('%02d',rtn_minutes)
|
||||
rtn_seconds = string.format('%02d',rtn_seconds)
|
||||
rtn_days = string.format('%02d', rtn_days)
|
||||
rtn_hours = string.format('%02d', rtn_hours)
|
||||
rtn_minutes = string.format('%02d', rtn_minutes)
|
||||
rtn_seconds = string.format('%02d', rtn_seconds)
|
||||
end
|
||||
-- The final return is construed
|
||||
local rtn
|
||||
local append = function(dom,value)
|
||||
local append = function(dom, value)
|
||||
if dom and options.string then
|
||||
rtn = rtn and rtn..div..value or value
|
||||
elseif dom then
|
||||
rtn = rtn and {div,rtn,value} or value
|
||||
rtn = rtn and {div, rtn, value} or value
|
||||
end
|
||||
end
|
||||
append(options.days,rtn_days)
|
||||
append(options.hours,rtn_hours)
|
||||
append(options.minutes,rtn_minutes)
|
||||
append(options.seconds,rtn_seconds)
|
||||
append(options.days, rtn_days)
|
||||
append(options.hours, rtn_hours)
|
||||
append(options.minutes, rtn_minutes)
|
||||
append(options.seconds, rtn_seconds)
|
||||
return rtn
|
||||
end
|
||||
|
||||
@@ -542,31 +541,31 @@ end
|
||||
--[[-- Moves items to the position and stores them in the closest entity of the type given
|
||||
@tparam table items items which are to be added to the chests, ['name']=count
|
||||
@tparam[opt=navies] LuaSurface surface the surface that the items will be moved to
|
||||
@tparam[opt={0,0}] table position the position that the items will be moved to {x=100,y=100}
|
||||
@tparam[opt={0, 0}] table position the position that the items will be moved to {x=100, y=100}
|
||||
@tparam[opt=32] number radius the radius in which the items are allowed to be placed
|
||||
@tparam[opt=iron-chest] string chest_type the chest type that the items should be moved into
|
||||
@treturn LuaEntity the last chest that had items inserted into it
|
||||
|
||||
@usage-- Copy all the items in a players inventory and place them in chests at {0,0}
|
||||
@usage-- Copy all the items in a players inventory and place them in chests at {0, 0}
|
||||
move_items(game.player.get_main_inventory().get_contents())
|
||||
|
||||
]]
|
||||
function Common.move_items(items,surface,position,radius,chest_type)
|
||||
function Common.move_items(items, surface, position, radius, chest_type)
|
||||
chest_type = chest_type or 'iron-chest'
|
||||
surface = surface or game.surfaces[1]
|
||||
if position and type(position) ~= 'table' then return end
|
||||
if type(items) ~= 'table' then return end
|
||||
-- Finds all entities of the given type
|
||||
local p = position or {x=0,y=0}
|
||||
local p = position or {x=0, y=0}
|
||||
local r = radius or 32
|
||||
local entities = surface.find_entities_filtered{area={{p.x-r,p.y-r},{p.x+r,p.y+r}},name=chest_type} or {}
|
||||
local entities = surface.find_entities_filtered{area={{p.x-r, p.y-r}, {p.x+r, p.y+r}}, name=chest_type} or {}
|
||||
local count = #entities
|
||||
local current = 1
|
||||
-- Makes a new empty chest when it is needed
|
||||
local function make_new_chest()
|
||||
local pos = surface.find_non_colliding_position(chest_type,position,32,1)
|
||||
local chest = surface.create_entity{name=chest_type,position=pos,force='neutral'}
|
||||
table.insert(entities,chest)
|
||||
local pos = surface.find_non_colliding_position(chest_type, position, 32, 1)
|
||||
local chest = surface.create_entity{name=chest_type, position=pos, force='neutral'}
|
||||
table.insert(entities, chest)
|
||||
count = count + 1
|
||||
return chest
|
||||
end
|
||||
@@ -581,16 +580,16 @@ function Common.move_items(items,surface,position,radius,chest_type)
|
||||
return chest
|
||||
else
|
||||
-- Other wise it is removed from the list
|
||||
table.remove(entities,current)
|
||||
table.remove(entities, current)
|
||||
count = count - 1
|
||||
end
|
||||
end
|
||||
-- Inserts the items into the chests
|
||||
local last_chest
|
||||
for item_name,item_count in pairs(items) do
|
||||
local chest = next_chest{name=item_name,count=item_count}
|
||||
if not chest then return error(string.format('Cant move item %s to %s{%s, %s} no valid chest in radius',item.name,surface.name,p.x,p.y)) end
|
||||
Util.insert_safe(chest,{[item_name]=item_count})
|
||||
for item_name, item_count in pairs(items) do
|
||||
local chest = next_chest{name=item_name, count=item_count}
|
||||
if not chest then return error(string.format('Cant move item %s to %s{%s, %s} no valid chest in radius', item_name, surface.name, p.x, p.y)) end
|
||||
Util.insert_safe(chest, {[item_name]=item_count})
|
||||
last_chest = chest
|
||||
end
|
||||
return last_chest
|
||||
@@ -606,7 +605,7 @@ https://github.com/Refactorio/RedMew/blob/9184b2940f311d8c9c891e83429fc57ec7e0c4
|
||||
@tparam[opt=0] number offset the offset in the +x +y direction
|
||||
@tparam[opt=false] boolean immutable if immutable, only set, never do a surface lookup, values never change
|
||||
|
||||
@usage-- Place a 0 at {0,0}
|
||||
@usage-- Place a 0 at {0, 0}
|
||||
print_grid_value(0, game.player.surface, { x=0, y=0 })
|
||||
|
||||
]]
|
||||
@@ -664,7 +663,7 @@ clear_flying_text(game.player.surface)
|
||||
]]
|
||||
function Common.clear_flying_text(surface)
|
||||
local entities = surface.find_entities_filtered{name ='flying-text'}
|
||||
for _,entity in pairs(entities) do
|
||||
for _, entity in pairs(entities) do
|
||||
if entity and entity.valid then
|
||||
entity.destroy()
|
||||
end
|
||||
|
||||
@@ -18,7 +18,7 @@ Gui.element{
|
||||
@usage-- Making a factory function for a button which is contained within a flow
|
||||
-- This method is for when you still want to register event handlers but cant use the table method
|
||||
local example_flow_with_button =
|
||||
Gui.element(function(event_trigger,parent,...)
|
||||
Gui.element(function(event_trigger, parent, ...)
|
||||
-- ... shows that all other arguments from the factory call are passed to this function
|
||||
-- Here we are adding a flow which we will then later add a button to
|
||||
local flow =
|
||||
@@ -60,7 +60,7 @@ Gui.element{
|
||||
caption = 'Example Button',
|
||||
style = 'forward_button' -- factorio styles can be applied here
|
||||
}
|
||||
:style(function(style,element,...)
|
||||
:style(function(style, element, ...)
|
||||
-- style is the current style object for the elemenent
|
||||
-- element is the element that is being changed
|
||||
-- ... shows that all other arguments from the factory call are passed to this function
|
||||
@@ -76,7 +76,7 @@ Gui.element{
|
||||
type = 'button',
|
||||
caption = 'Example Button'
|
||||
}
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player, element, event)
|
||||
-- player is the player who interacted with the element to cause the event
|
||||
-- element is a refrence to the element which caused the event
|
||||
-- event is a raw refrence to the event data if player and element are not enough
|
||||
@@ -98,21 +98,21 @@ Gui.element{
|
||||
width = 18,
|
||||
height = 20
|
||||
}
|
||||
:on_click(function(player,_,_)
|
||||
:on_click(function(player, _,_)
|
||||
Gui.hide_left_flow(player)
|
||||
end)
|
||||
|
||||
@usage-- Eample from defines, Gui.alignment, called like: Gui.alignment(parent, name, horizontal_align, vertical_align)
|
||||
-- Notice how _ are used to blank arguments that are not needed in that context and how they line up with above
|
||||
Gui.alignment =
|
||||
Gui.element(function(_,parent,name,_,_)
|
||||
Gui.element(function(_, parent, name, _,_)
|
||||
return parent.add{
|
||||
name = name or 'alignment',
|
||||
type = 'flow',
|
||||
}
|
||||
end)
|
||||
:style(function(style,_,_,horizontal_align,vertical_align)
|
||||
style.padding = {1,2}
|
||||
:style(function(style, _,_, horizontal_align, vertical_align)
|
||||
style.padding = {1, 2}
|
||||
style.vertical_align = vertical_align or 'center'
|
||||
style.horizontal_align = horizontal_align or 'right'
|
||||
style.vertically_stretchable = style.vertical_align ~= 'center'
|
||||
|
||||
@@ -23,7 +23,7 @@ Gui.element{
|
||||
width = 18,
|
||||
height = 36
|
||||
}
|
||||
:on_click(function(player,_,_)
|
||||
:on_click(function(player, _,_)
|
||||
Gui.toggle_top_flow(player)
|
||||
end)
|
||||
Gui.core_defines.hide_top_flow = hide_top_flow
|
||||
@@ -42,7 +42,7 @@ Gui.element{
|
||||
width = 18,
|
||||
height = 20
|
||||
}
|
||||
:on_click(function(player,_,_)
|
||||
:on_click(function(player, _,_)
|
||||
Gui.toggle_top_flow(player)
|
||||
end)
|
||||
Gui.core_defines.show_top_flow = show_top_flow
|
||||
@@ -61,13 +61,13 @@ Gui.element{
|
||||
width = 18,
|
||||
height = 20
|
||||
}
|
||||
:on_click(function(player,_,_)
|
||||
:on_click(function(player, _,_)
|
||||
Gui.hide_left_flow(player)
|
||||
end)
|
||||
Gui.core_defines.hide_left_flow = hide_left_flow
|
||||
|
||||
--- Draw the core elements when a player joins the game
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
Event.add(defines.events.on_player_created, function(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
||||
-- Draw the top flow
|
||||
|
||||
@@ -17,21 +17,21 @@ local Gui = require 'expcore.gui.prototype'
|
||||
@treturn LuaGuiElement the alignment flow that was created
|
||||
|
||||
@usage-- Adding a right align flow
|
||||
local alignment = Gui.alignment(element,'example_right_alignment')
|
||||
local alignment = Gui.alignment(element, 'example_right_alignment')
|
||||
|
||||
@usage-- Adding a horizontal center and top align flow
|
||||
local alignment = Gui.alignment(element,'example_center_top_alignment','center','top')
|
||||
local alignment = Gui.alignment(element, 'example_center_top_alignment', 'center', 'top')
|
||||
|
||||
]]
|
||||
Gui.alignment =
|
||||
Gui.element(function(_,parent,name,_,_)
|
||||
Gui.element(function(_, parent, name, _,_)
|
||||
return parent.add{
|
||||
name = name or 'alignment',
|
||||
type = 'flow',
|
||||
}
|
||||
end)
|
||||
:style(function(style,_,_,horizontal_align,vertical_align)
|
||||
style.padding = {1,2}
|
||||
:style(function(style, _,_, horizontal_align, vertical_align)
|
||||
style.padding = {1, 2}
|
||||
style.vertical_align = vertical_align or 'center'
|
||||
style.horizontal_align = horizontal_align or 'right'
|
||||
style.vertically_stretchable = style.vertical_align ~= 'center'
|
||||
@@ -47,11 +47,11 @@ end)
|
||||
@treturn LuaGuiElement the table that was created
|
||||
|
||||
@usage-- Adding a scroll table with max height of 200 and column count of 3
|
||||
local scroll_table = Gui.scroll_table(element,200,3)
|
||||
local scroll_table = Gui.scroll_table(element, 200, 3)
|
||||
|
||||
]]
|
||||
Gui.scroll_table =
|
||||
Gui.element(function(_,parent,height,column_count,name)
|
||||
Gui.element(function(_, parent, height, column_count, name)
|
||||
-- Draw the scroll
|
||||
local scroll_pane =
|
||||
parent.add{
|
||||
@@ -65,7 +65,7 @@ Gui.element(function(_,parent,height,column_count,name)
|
||||
|
||||
-- Set the style of the scroll pane
|
||||
local scroll_style = scroll_pane.style
|
||||
scroll_style.padding = {1,3}
|
||||
scroll_style.padding = {1, 3}
|
||||
scroll_style.maximal_height = height
|
||||
scroll_style.horizontally_stretchable = true
|
||||
|
||||
@@ -105,7 +105,7 @@ local header = Gui.header(
|
||||
|
||||
]]
|
||||
Gui.header =
|
||||
Gui.element(function(_,parent,caption,tooltip,add_alignment,name)
|
||||
Gui.element(function(_, parent, caption, tooltip, add_alignment, name)
|
||||
-- Draw the header
|
||||
local header =
|
||||
parent.add{
|
||||
@@ -116,7 +116,7 @@ Gui.element(function(_,parent,caption,tooltip,add_alignment,name)
|
||||
|
||||
-- Change the style of the header
|
||||
local style = header.style
|
||||
style.padding = {2,4}
|
||||
style.padding = {2, 4}
|
||||
style.use_header_filler = false
|
||||
style.horizontally_stretchable = true
|
||||
|
||||
@@ -153,7 +153,7 @@ local footer = Gui.footer(
|
||||
|
||||
]]
|
||||
Gui.footer =
|
||||
Gui.element(function(_,parent,caption,tooltip,add_alignment,name)
|
||||
Gui.element(function(_, parent, caption, tooltip, add_alignment, name)
|
||||
-- Draw the header
|
||||
local footer =
|
||||
parent.add{
|
||||
@@ -164,7 +164,7 @@ Gui.element(function(_,parent,caption,tooltip,add_alignment,name)
|
||||
|
||||
-- Change the style of the footer
|
||||
local style = footer.style
|
||||
style.padding = {2,4}
|
||||
style.padding = {2, 4}
|
||||
style.use_header_filler = false
|
||||
style.horizontally_stretchable = true
|
||||
|
||||
@@ -190,11 +190,11 @@ end)
|
||||
@tparam number width the minimal width that the frame will have
|
||||
|
||||
@usage-- Adding a container as a base
|
||||
local container = Gui.container(parent,'my_container',200)
|
||||
local container = Gui.container(parent, 'my_container', 200)
|
||||
|
||||
]]
|
||||
Gui.container =
|
||||
Gui.element(function(_,parent,name,_)
|
||||
Gui.element(function(_, parent, name, _)
|
||||
-- Draw the external container
|
||||
local frame =
|
||||
parent.add{
|
||||
@@ -210,7 +210,7 @@ Gui.element(function(_,parent,name,_)
|
||||
style = 'window_content_frame_packed'
|
||||
}
|
||||
end)
|
||||
:style(function(style,element,_,width)
|
||||
:style(function(style, element, _,width)
|
||||
style.vertically_stretchable = false
|
||||
local frame_style = element.parent.style
|
||||
frame_style.padding = 2
|
||||
@@ -227,16 +227,16 @@ local bar = Gui.bar(parent, 100)
|
||||
|
||||
]]
|
||||
Gui.bar =
|
||||
Gui.element(function(_,parent)
|
||||
Gui.element(function(_, parent)
|
||||
return parent.add{
|
||||
type = 'progressbar',
|
||||
size = 1,
|
||||
value = 1
|
||||
}
|
||||
end)
|
||||
:style(function(style,_,width)
|
||||
:style(function(style, _,width)
|
||||
style.height = 3
|
||||
style.color = {r=255,g=255,b=255}
|
||||
style.color = {r=255, g=255, b=255}
|
||||
if width then style.width = width
|
||||
else style.horizontally_stretchable = true end
|
||||
end)
|
||||
@@ -253,7 +253,7 @@ local label = Gui.centered_label(parent, 100, 'This is centered')
|
||||
|
||||
]]
|
||||
Gui.centered_label =
|
||||
Gui.element(function(_,parent,width,caption,tooltip)
|
||||
Gui.element(function(_, parent, width, caption, tooltip)
|
||||
local label = parent.add{
|
||||
type = 'label',
|
||||
caption = caption,
|
||||
@@ -281,11 +281,11 @@ local label = Gui.centered_label(parent, 100, 'This is centered')
|
||||
|
||||
]]
|
||||
Gui.title_label =
|
||||
Gui.element(function(_,parent,width,caption,tooltip)
|
||||
Gui.element(function(_, parent, width, caption, tooltip)
|
||||
local title_flow = parent.add{ type='flow' }
|
||||
title_flow.style.vertical_align = 'center'
|
||||
|
||||
Gui.bar(title_flow,width)
|
||||
Gui.bar(title_flow, width)
|
||||
local title_label = title_flow.add{
|
||||
type = 'label',
|
||||
caption = caption,
|
||||
|
||||
@@ -30,7 +30,7 @@ end
|
||||
local new_enabled_state = Gui.toggle_enabled_state(element)
|
||||
|
||||
]]
|
||||
function Gui.toggle_enabled_state(element,state)
|
||||
function Gui.toggle_enabled_state(element, state)
|
||||
if not element or not element.valid then return end
|
||||
if state == nil then state = not element.enabled end
|
||||
element.enabled = state
|
||||
@@ -46,7 +46,7 @@ end
|
||||
local new_visible_state = Gui.toggle_visible_state(element)
|
||||
|
||||
]]
|
||||
function Gui.toggle_visible_state(element,state)
|
||||
function Gui.toggle_visible_state(element, state)
|
||||
if not element or not element.valid then return end
|
||||
if state == nil then state = not element.visible end
|
||||
element.visible = state
|
||||
@@ -82,7 +82,7 @@ Gui.element{
|
||||
:style(Gui.sprite_style(20))
|
||||
|
||||
]]
|
||||
function Gui.sprite_style(size,padding,style)
|
||||
function Gui.sprite_style(size, padding, style)
|
||||
style = style or {}
|
||||
style.padding = padding or -2
|
||||
style.height = size
|
||||
|
||||
@@ -55,11 +55,11 @@ Gui.left_toolbar_button('entity/inserter', 'Nothing to see here', example_flow_w
|
||||
end)
|
||||
|
||||
]]
|
||||
function Gui.left_toolbar_button(sprite,tooltip,element_define,authenticator)
|
||||
local button = Gui.toolbar_button(sprite,tooltip,authenticator)
|
||||
function Gui.left_toolbar_button(sprite, tooltip, element_define, authenticator)
|
||||
local button = Gui.toolbar_button(sprite, tooltip, authenticator)
|
||||
|
||||
-- Add on_click handler to handle click events comming from the player
|
||||
button:on_click(function(player,_,_)
|
||||
button:on_click(function(player, _,_)
|
||||
local top_flow = Gui.get_top_flow(player)
|
||||
local element = top_flow[button.name]
|
||||
local visibility_state = Gui.toggle_left_element(player, element_define)
|
||||
@@ -169,7 +169,7 @@ function Gui.hide_left_flow(player)
|
||||
|
||||
-- Set the visible state of all elements in the flow
|
||||
hide_button.visible = false
|
||||
for name,_ in pairs(Gui.left_elements) do
|
||||
for name, _ in pairs(Gui.left_elements) do
|
||||
left_flow[name].visible = false
|
||||
|
||||
-- Check if the the element has a toobar button attached
|
||||
@@ -202,7 +202,7 @@ end
|
||||
local frame = Gui.get_left_element(game.player, example_flow_with_button)
|
||||
|
||||
]]
|
||||
function Gui.get_left_element(player,element_define)
|
||||
function Gui.get_left_element(player, element_define)
|
||||
local left_flow = Gui.get_left_flow(player)
|
||||
return left_flow[element_define.name]
|
||||
end
|
||||
@@ -220,7 +220,7 @@ Gui.toggle_top_flow(game.player, example_flow_with_button)
|
||||
Gui.toggle_top_flow(game.player, example_flow_with_button, true)
|
||||
|
||||
]]
|
||||
function Gui.toggle_left_element(player,element_define,state)
|
||||
function Gui.toggle_left_element(player, element_define, state)
|
||||
local left_flow = Gui.get_left_flow(player)
|
||||
local top_flow = Gui.get_top_flow(player)
|
||||
|
||||
|
||||
@@ -22,9 +22,9 @@ local Gui = {
|
||||
_prototype_element = {},
|
||||
--- The prototype metatable applied to new element defines
|
||||
_mt_element = {
|
||||
__call = function(self,parent,...)
|
||||
local element = self._draw(self.name,parent,...)
|
||||
if self._style then self._style(element.style,element,...) end
|
||||
__call = function(self, parent, ...)
|
||||
local element = self._draw(self.name, parent, ...)
|
||||
if self._style then self._style(element.style, element, ...) end
|
||||
return element
|
||||
end
|
||||
}
|
||||
@@ -50,7 +50,7 @@ Gui.element{
|
||||
@usage-- Using element defines with a custom factory function
|
||||
-- This method can be used if you still want to be able register event handlers but it is too complex to be compatible with LuaGuiElement.add
|
||||
local example_flow_with_button =
|
||||
Gui.element(function(event_trigger,parent,...)
|
||||
Gui.element(function(event_trigger, parent, ...)
|
||||
-- ... shows that all other arguments from the factory call are passed to this function
|
||||
-- parent is the element which was passed to the factory function where you should add your new element
|
||||
-- here we are adding a flow which we will then later add a button to
|
||||
@@ -90,7 +90,7 @@ function Gui.element(element_define)
|
||||
if type(element_define) == 'table' then
|
||||
Gui.debug_info[name].draw = element_define
|
||||
element_define.name = name
|
||||
element._draw = function(_,parent)
|
||||
element._draw = function(_, parent)
|
||||
return parent.add(element_define)
|
||||
end
|
||||
else
|
||||
@@ -131,7 +131,7 @@ Gui.element{
|
||||
caption = 'Example Button',
|
||||
style = 'forward_button' -- factorio styles can be applied here
|
||||
}
|
||||
:style(function(style,element,...)
|
||||
:style(function(style, element, ...)
|
||||
-- style is the current style object for the elemenent
|
||||
-- element is the element that is being changed
|
||||
-- ... shows that all other arguments from the factory call are passed to this function
|
||||
@@ -147,7 +147,7 @@ function Gui._prototype_element:style(style_define)
|
||||
if type(style_define) == 'table' then
|
||||
Gui.debug_info[self.name].style = style_define
|
||||
self._style = function(style)
|
||||
for key,value in pairs(style_define) do
|
||||
for key, value in pairs(style_define) do
|
||||
style[key] = value
|
||||
end
|
||||
end
|
||||
@@ -171,8 +171,8 @@ element_deinfe:on_custom_event('my_custom_event', function(event)
|
||||
end)
|
||||
|
||||
]]
|
||||
function Gui._prototype_element:on_custom_event(event_name,handler)
|
||||
table.insert(Gui.debug_info[self.name].events,event_name)
|
||||
function Gui._prototype_element:on_custom_event(event_name, handler)
|
||||
table.insert(Gui.debug_info[self.name].events, event_name)
|
||||
Gui.events[event_name] = event_name
|
||||
self[event_name] = handler
|
||||
return self
|
||||
@@ -210,7 +210,7 @@ function Gui._prototype_element:raise_custom_event(event)
|
||||
end
|
||||
event.player = player
|
||||
|
||||
local success, err = pcall(handler,player,element,event)
|
||||
local success, err = pcall(handler, player, element, event)
|
||||
if not success then
|
||||
error('There as been an error with an event handler for a gui element:\n\t'..err)
|
||||
end
|
||||
@@ -227,8 +227,8 @@ local function event_handler_factory(event_name)
|
||||
element_define:raise_custom_event(event)
|
||||
end)
|
||||
|
||||
return function(self,handler)
|
||||
table.insert(Gui.debug_info[self.name].events,debug.getinfo(1, "n").name)
|
||||
return function(self, handler)
|
||||
table.insert(Gui.debug_info[self.name].events, debug.getinfo(1, "n").name)
|
||||
self[event_name] = handler
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -87,10 +87,10 @@ end
|
||||
Gui.toggle_top_flow(game.player)
|
||||
|
||||
@usage-- Open your top flow
|
||||
Gui.toggle_top_flow(game.player,true)
|
||||
Gui.toggle_top_flow(game.player, true)
|
||||
|
||||
]]
|
||||
function Gui.toggle_top_flow(player,state)
|
||||
function Gui.toggle_top_flow(player, state)
|
||||
-- Get the top flow and hide button
|
||||
local top_flow = Gui.get_top_flow(player)
|
||||
if state == nil then state = not top_flow.visible end
|
||||
@@ -130,7 +130,7 @@ Gui.left_toolbar_button('entity/inserter', 'Nothing to see here', function(playe
|
||||
end)
|
||||
|
||||
]]
|
||||
function Gui.toolbar_button(sprite,tooltip,authenticator)
|
||||
function Gui.toolbar_button(sprite, tooltip, authenticator)
|
||||
return Gui.element{
|
||||
type = 'sprite-button',
|
||||
sprite = sprite,
|
||||
|
||||
@@ -35,14 +35,14 @@ local Permissions_Groups = {
|
||||
|
||||
-- Async function to add players to permission groups
|
||||
local add_to_permission_group =
|
||||
Async.register(function(permission_group,player)
|
||||
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)
|
||||
Async.register(function(permission_group, player)
|
||||
permission_group.remove_player(player)
|
||||
end)
|
||||
Permissions_Groups.async_token_remove_from_permission_group = remove_from_permission_group
|
||||
@@ -64,7 +64,7 @@ function Permissions_Groups.new_group(name)
|
||||
name=name,
|
||||
actions={},
|
||||
allow_all_actions=true
|
||||
},{
|
||||
}, {
|
||||
__index= Permissions_Groups._prototype
|
||||
})
|
||||
Permissions_Groups.groups[name] = group
|
||||
@@ -111,7 +111,7 @@ Groups.reload_permissions()
|
||||
|
||||
]]
|
||||
function Permissions_Groups.reload_permissions()
|
||||
for _,group in pairs(Permissions_Groups.groups) do
|
||||
for _, group in pairs(Permissions_Groups.groups) do
|
||||
group:create()
|
||||
end
|
||||
end
|
||||
@@ -125,7 +125,7 @@ end
|
||||
Groups.set_player_group(game.player, 'Admin')
|
||||
|
||||
]]
|
||||
function Permissions_Groups.set_player_group(player,group)
|
||||
function Permissions_Groups.set_player_group(player, group)
|
||||
player = Game.get_player_from_any(player)
|
||||
group = Permissions_Groups.get_group_by_name(group)
|
||||
if not group or not player then return false end
|
||||
@@ -146,7 +146,7 @@ end
|
||||
group:set_action('toggle_map_editor', false)
|
||||
|
||||
]]
|
||||
function Permissions_Groups._prototype:set_action(action,state)
|
||||
function Permissions_Groups._prototype:set_action(action, state)
|
||||
if type(action) == 'string' then
|
||||
action = defines.input_action[action]
|
||||
end
|
||||
@@ -168,8 +168,8 @@ function Permissions_Groups._prototype:allow(actions)
|
||||
if type(actions) ~= 'table' then
|
||||
actions = {actions}
|
||||
end
|
||||
for _,action in pairs(actions) do
|
||||
self:set_action(action,true)
|
||||
for _, action in pairs(actions) do
|
||||
self:set_action(action, true)
|
||||
end
|
||||
return self
|
||||
end
|
||||
@@ -192,8 +192,8 @@ function Permissions_Groups._prototype:disallow(actions)
|
||||
if type(actions) ~= 'table' then
|
||||
actions = {actions}
|
||||
end
|
||||
for _,action in pairs(actions) do
|
||||
self:set_action(action,false)
|
||||
for _, action in pairs(actions) do
|
||||
self:set_action(action, false)
|
||||
end
|
||||
return self
|
||||
end
|
||||
@@ -257,8 +257,8 @@ function Permissions_Groups._prototype:create()
|
||||
if not group then
|
||||
group = game.permissions.create_group(self.name)
|
||||
end
|
||||
for _,action in pairs(defines.input_action) do
|
||||
group.set_allows_action(action,self:is_allowed(action))
|
||||
for _, action in pairs(defines.input_action) do
|
||||
group.set_allows_action(action, self:is_allowed(action))
|
||||
end
|
||||
return group
|
||||
end
|
||||
@@ -324,9 +324,9 @@ function Permissions_Groups._prototype:get_players(online)
|
||||
if online == nil then
|
||||
return group.players
|
||||
else
|
||||
for _,player in pairs(group.players) do
|
||||
for _, player in pairs(group.players) do
|
||||
if player.connected == online then
|
||||
table.insert(player,player)
|
||||
table.insert(player, player)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -344,7 +344,7 @@ group:print('Hello, World!')
|
||||
]]
|
||||
function Permissions_Groups._prototype:print(message)
|
||||
local players = self:get_players(true)
|
||||
for _,player in pairs(players) do
|
||||
for _, player in pairs(players) do
|
||||
player.print(message)
|
||||
end
|
||||
return #players
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
@usage--- Using Role System (assignment):
|
||||
--When a map first starts you will want to define on mass all the players you expect to join and the roles to give them:
|
||||
Roles.override_player_roles{
|
||||
Cooldude2606 = {'Owner','Admin','Member'},
|
||||
Cooldude2606 = {'Owner', 'Admin', 'Member'},
|
||||
NotCooldude2606 = {'Member'}
|
||||
}
|
||||
|
||||
--Once the game is running you still want to be able to give role and remove them which is when you would use:
|
||||
Roles.assign_player(player,'Admin',by_player_name) -- this will give the "Admin" role to the player
|
||||
Roles.unassign_player(player,{'Admin','Moderator'},by_player_name) -- this will remove "Admin" and "Moderator" role in one go
|
||||
Roles.assign_player(player, 'Admin', by_player_name) -- this will give the "Admin" role to the player
|
||||
Roles.unassign_player(player, {'Admin', 'Moderator'}, by_player_name) -- this will remove "Admin" and "Moderator" role in one go
|
||||
|
||||
@usage--- Using Role System (role testing):
|
||||
--To comparer two players you can comparer the index of they highest roles, can be used when you want to allow a "write" down type system:
|
||||
@@ -22,9 +22,9 @@ Roles.get_player_highest_role(playerOne).index < Roles.get_player_highest_role(p
|
||||
Roles.get_player_roles(player) -- the return is an array that can be looped over however this is not in particular order
|
||||
|
||||
--Finally you may want to test if a player has a certain role, flag or action allowed which is when you would use:
|
||||
Roles.player_has_role(player,'Admin') -- you can provide a role name if you only want a name based system
|
||||
Roles.player_has_flag(player,'is_donator') -- your roles can be grouped together with flags such as is_donator
|
||||
Roles.player_allowed(player,'game modifiers') -- or you can have an action based system where each action is something the player can do
|
||||
Roles.player_has_role(player, 'Admin') -- you can provide a role name if you only want a name based system
|
||||
Roles.player_has_flag(player, 'is_donator') -- your roles can be grouped together with flags such as is_donator
|
||||
Roles.player_allowed(player, 'game modifiers') -- or you can have an action based system where each action is something the player can do
|
||||
|
||||
@usage--- Example Flag Define:
|
||||
--Flags can be used to group multiple roles and actions under one catch all, for example if you want a piece of code to only
|
||||
@@ -32,7 +32,7 @@ Roles.player_allowed(player,'game modifiers') -- or you can have an action based
|
||||
--a player has that tag present:
|
||||
|
||||
-- give you donators a speed boost when they join; these functions aren't required but can be useful
|
||||
Roles.define_flag_trigger('is_donator',function(player,state)
|
||||
Roles.define_flag_trigger('is_donator', function(player, state)
|
||||
if state then
|
||||
player.character_running_speed_modifier = 1.5
|
||||
else
|
||||
@@ -45,30 +45,30 @@ Roles.new_role('Donator')
|
||||
:set_flag('is_donator')
|
||||
|
||||
-- and in your code you would test for
|
||||
if Roles.player_has_flag(player,'is_donator') then
|
||||
if Roles.player_has_flag(player, 'is_donator') then
|
||||
-- some donator only code
|
||||
end
|
||||
|
||||
@usage--- Example Role Define:
|
||||
--You can't use a role system without any roles so first you must define your roles; each role has a minimum of a name with
|
||||
--the option for a shorthand:
|
||||
Roles.new_role('Administrator','Admin')
|
||||
Roles.new_role('Administrator', 'Admin')
|
||||
|
||||
--Next you will want to add any extras you want to have, such as a tag, colour, permission group or any custom flags:
|
||||
Roles.new_role('Administrator','Admin')
|
||||
Roles.new_role('Administrator', 'Admin')
|
||||
:set_custom_tag('[Admin]')
|
||||
:set_custom_color('red') -- this can be {r=0,g=0,b=0} or a predefined value
|
||||
:set_custom_color('red') -- this can be {r=0, g=0, b=0} or a predefined value
|
||||
:set_permission_group('Staff') -- a second argument can be added if you have not used the custom permission group config
|
||||
:set_flag('is_admin')
|
||||
|
||||
--You will then want to decide if you want to allow all actions, this should of course be used sparely:
|
||||
Roles.new_role('Administrator','Admin')
|
||||
Roles.new_role('Administrator', 'Admin')
|
||||
...extras...
|
||||
:set_allow_all()
|
||||
|
||||
--If you don't do this want this as i would advise you do then you will want to define what the role can do; this comes with
|
||||
--an optional inheritance system if you like those sort of things in which case disallow may also be of some use to you:
|
||||
Roles.new_role('Administrator','Admin')
|
||||
Roles.new_role('Administrator', 'Admin')
|
||||
...extras...
|
||||
:set_parent('Moderator') -- the admin can do anything that a moderator can do
|
||||
:allow{ -- these actions can be anything just try to keep them without conflicts
|
||||
@@ -77,7 +77,7 @@ Roles.new_role('Administrator','Admin')
|
||||
}
|
||||
|
||||
--Here is what the finished admin role would look like:
|
||||
Roles.new_role('Administrator','Admin')
|
||||
Roles.new_role('Administrator', 'Admin')
|
||||
:set_custom_tag('[Admin]')
|
||||
:set_custom_color('red')
|
||||
:set_permission_group('Staff')
|
||||
@@ -132,10 +132,10 @@ local Roles = {
|
||||
}
|
||||
|
||||
--- When global is loaded it will have the metatable re-assigned to the roles
|
||||
Global.register(Roles.config,function(tbl)
|
||||
Global.register(Roles.config, function(tbl)
|
||||
Roles.config = tbl
|
||||
for _,role in pairs(Roles.config.roles) do
|
||||
setmetatable(role,{__index=Roles._prototype})
|
||||
for _, role in pairs(Roles.config.roles) do
|
||||
setmetatable(role, {__index=Roles._prototype})
|
||||
local parent = Roles.config.roles[role.parent]
|
||||
if parent then
|
||||
setmetatable(role.allowed_actions, {__index=parent.allowed_actions})
|
||||
@@ -150,7 +150,7 @@ end)
|
||||
--- Internal function used to trigger a few different things when roles are changed
|
||||
-- this is the raw internal trigger as the other function is called at other times
|
||||
-- there is a second half called role_update which triggers after the event call, it also is called when a player joins
|
||||
local function emit_player_roles_updated(player,type,roles,by_player_name,skip_game_print)
|
||||
local function emit_player_roles_updated(player, type, roles, by_player_name, skip_game_print)
|
||||
by_player_name = game.player and game.player.name or by_player_name or '<server>'
|
||||
local by_player = Game.get_player_from_any(by_player_name)
|
||||
local by_player_index = by_player and by_player.index or 0
|
||||
@@ -161,30 +161,30 @@ local function emit_player_roles_updated(player,type,roles,by_player_name,skip_g
|
||||
end
|
||||
-- convert the roles to objects and get the names of the roles
|
||||
local role_names = {}
|
||||
for index,role in pairs(roles) do
|
||||
for index, role in pairs(roles) do
|
||||
role = Roles.get_role_from_any(role)
|
||||
if role then
|
||||
roles[index] = role
|
||||
table.insert(role_names,role.name)
|
||||
table.insert(role_names, role.name)
|
||||
end
|
||||
end
|
||||
-- output to all the different locations: game print, player sound, event trigger and role log
|
||||
if not skip_game_print then
|
||||
game.print({'expcore-roles.game-message-'..type,player.name,table.concat(role_names,', '),by_player_name},Colours.cyan)
|
||||
game.print({'expcore-roles.game-message-'..type, player.name, table.concat(role_names, ', '), by_player_name}, Colours.cyan)
|
||||
end
|
||||
if type == 'assign' then
|
||||
player.play_sound{path='utility/achievement_unlocked'}
|
||||
else
|
||||
player.play_sound{path='utility/game_lost'}
|
||||
end
|
||||
script.raise_event(event,{
|
||||
script.raise_event(event, {
|
||||
name=event,
|
||||
tick=game.tick,
|
||||
player_index=player.index,
|
||||
by_player_index=by_player_index,
|
||||
roles=roles
|
||||
})
|
||||
write_json('log/roles.log',{
|
||||
write_json('log/roles.log', {
|
||||
player_name=player.name,
|
||||
by_player_name=by_player_name,
|
||||
type=type,
|
||||
@@ -201,11 +201,11 @@ game.player.print(Roles.debug())
|
||||
]]
|
||||
function Roles.debug()
|
||||
local output = ''
|
||||
for index,role_name in pairs(Roles.config.order) do
|
||||
for index, role_name in pairs(Roles.config.order) do
|
||||
local role = Roles.config.roles[role_name]
|
||||
local color = role.custom_color or Colours.white
|
||||
color = string.format('[color=%d,%d,%d]',color.r,color.g,color.b)
|
||||
output = output..string.format('\n%s %s) %s[/color]',color,index,serpent.line(role))
|
||||
color = string.format('[color=%d, %d, %d]', color.r, color.g, color.b)
|
||||
output = output..string.format('\n%s %s) %s[/color]', color, index, serpent.line(role))
|
||||
end
|
||||
return output
|
||||
end
|
||||
@@ -215,11 +215,11 @@ end
|
||||
@tparam string message the message to send to the players
|
||||
|
||||
@usage-- Print a message to the given roles
|
||||
Roles.print_to_roles({'Administrator','Moderator'}, 'Hello, World!')
|
||||
Roles.print_to_roles({'Administrator', 'Moderator'}, 'Hello, World!')
|
||||
|
||||
]]
|
||||
function Roles.print_to_roles(roles,message)
|
||||
for _,role in pairs(roles) do
|
||||
function Roles.print_to_roles(roles, message)
|
||||
for _, role in pairs(roles) do
|
||||
role = Roles.get_role_from_any(role)
|
||||
if role then role:print(message) end
|
||||
end
|
||||
@@ -233,16 +233,16 @@ end
|
||||
Roles.print_to_roles_higher('Moderator', 'Hello, World!')
|
||||
|
||||
]]
|
||||
function Roles.print_to_roles_higher(role,message)
|
||||
function Roles.print_to_roles_higher(role, message)
|
||||
role = Roles.get_role_from_any(role)
|
||||
if not role then return end
|
||||
local roles = {}
|
||||
for index,role_name in pairs(Roles.config.order) do
|
||||
for index, role_name in pairs(Roles.config.order) do
|
||||
if index <= role.index and role_name ~= Roles.config.internal.default then
|
||||
table.insert(roles,role_name)
|
||||
table.insert(roles, role_name)
|
||||
end
|
||||
end
|
||||
Roles.print_to_roles(roles,message)
|
||||
Roles.print_to_roles(roles, message)
|
||||
end
|
||||
|
||||
--[[-- Prints a message to all players who have the given role or one which is lower (excluding default)
|
||||
@@ -253,16 +253,16 @@ end
|
||||
Roles.print_to_roles_higher('Moderator', 'Hello, World!')
|
||||
|
||||
]]
|
||||
function Roles.print_to_roles_lower(role,message)
|
||||
function Roles.print_to_roles_lower(role, message)
|
||||
role = Roles.get_role_from_any(role)
|
||||
if not role then return end
|
||||
local roles = {}
|
||||
for index,role_name in pairs(Roles.config.order) do
|
||||
for index, role_name in pairs(Roles.config.order) do
|
||||
if index >= role.index and role_name ~= Roles.config.internal.default then
|
||||
table.insert(roles,role_name)
|
||||
table.insert(roles, role_name)
|
||||
end
|
||||
end
|
||||
Roles.print_to_roles(roles,message)
|
||||
Roles.print_to_roles(roles, message)
|
||||
end
|
||||
|
||||
--[[-- Get a role for the given name
|
||||
@@ -290,7 +290,7 @@ function Roles.get_role_by_order(index)
|
||||
return Roles.config.roles[name]
|
||||
end
|
||||
|
||||
--[[-- Gets a role from a name,index or role object (where it is just returned)
|
||||
--[[-- Gets a role from a name, index or role object (where it is just returned)
|
||||
nb: this function is used for the input for most outward facing functions
|
||||
@tparam ?number|string|table any the value used to find the role
|
||||
@treturn Roles._prototype the role that was found or nil see above
|
||||
@@ -325,8 +325,8 @@ function Roles.get_player_roles(player)
|
||||
local roles = Roles.config.players[player.name] or {}
|
||||
local default = Roles.config.roles[Roles.config.internal.default]
|
||||
local rtn = {default}
|
||||
for _,role_name in pairs(roles) do
|
||||
table.insert(rtn,Roles.config.roles[role_name])
|
||||
for _, role_name in pairs(roles) do
|
||||
table.insert(rtn, Roles.config.roles[role_name])
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
@@ -343,7 +343,7 @@ function Roles.get_player_highest_role(player)
|
||||
local roles = Roles.get_player_roles(player)
|
||||
if not roles then return end
|
||||
local highest
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
if not highest or role.index < highest.index then
|
||||
highest = role
|
||||
end
|
||||
@@ -369,13 +369,13 @@ Roles.assign_player(game.player, 'Moderator')
|
||||
Roles.assign_player('Cooldude2606', 'Moderator', nil, true)
|
||||
|
||||
]]
|
||||
function Roles.assign_player(player,roles,by_player_name,skip_checks,silent)
|
||||
function Roles.assign_player(player, roles, by_player_name, skip_checks, silent)
|
||||
local valid_player = Game.get_player_from_any(player)
|
||||
if not skip_checks and not valid_player then return end
|
||||
if type(roles) ~= 'table' or roles.name then
|
||||
roles = {roles}
|
||||
end
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
role = Roles.get_role_from_any(role)
|
||||
if role then
|
||||
role:add_player(valid_player or player, valid_player == nil, true)
|
||||
@@ -400,14 +400,14 @@ Roles.unassign_player(game.player, 'Moderator')
|
||||
Roles.unassign_player('Cooldude2606', 'Moderator', nil, true)
|
||||
|
||||
]]
|
||||
function Roles.unassign_player(player,roles,by_player_name,skip_checks,silent)
|
||||
function Roles.unassign_player(player, roles, by_player_name, skip_checks, silent)
|
||||
local valid_player = Game.get_player_from_any(player)
|
||||
if not skip_checks and not valid_player then return end
|
||||
if not player then return end
|
||||
if type(roles) ~= 'table' or roles.name then
|
||||
roles = {roles}
|
||||
end
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
role = Roles.get_role_from_any(role)
|
||||
if role then
|
||||
role:remove_player(valid_player or player, valid_player == nil, true)
|
||||
@@ -427,12 +427,12 @@ Roles.override_player_roles('Cooldude2606', {'Moderator'})
|
||||
|
||||
@usage-- Override all existing roles, effects all users not just ones listed
|
||||
Roles.override_player_roles{
|
||||
['Cooldude2606'] = {'Administrator','Moderator'},
|
||||
['arty714'] = {'Administrator','Moderator'},
|
||||
['Cooldude2606'] = {'Administrator', 'Moderator'},
|
||||
['arty714'] = {'Administrator', 'Moderator'},
|
||||
}
|
||||
|
||||
]]
|
||||
function Roles.override_player_roles(player_name,roles)
|
||||
function Roles.override_player_roles(player_name, roles)
|
||||
if not roles then
|
||||
Roles.config.players = player_name
|
||||
else
|
||||
@@ -453,12 +453,12 @@ end
|
||||
local has_role = Roles.player_has_role(game.player, 'Moderator')
|
||||
|
||||
]]
|
||||
function Roles.player_has_role(player,search_role)
|
||||
function Roles.player_has_role(player, search_role)
|
||||
local roles = Roles.get_player_roles(player)
|
||||
if not roles then return end
|
||||
search_role = Roles.get_role_from_any(search_role)
|
||||
if not search_role then return end
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
if role.name == search_role.name then return true end
|
||||
end
|
||||
return false
|
||||
@@ -473,10 +473,10 @@ end
|
||||
local has_flag = Roles.player_has_flag(game.player, 'is_donator')
|
||||
|
||||
]]
|
||||
function Roles.player_has_flag(player,flag_name)
|
||||
function Roles.player_has_flag(player, flag_name)
|
||||
local roles = Roles.get_player_roles(player)
|
||||
if not roles then return end
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
if role:has_flag(flag_name) then
|
||||
return true
|
||||
end
|
||||
@@ -493,10 +493,10 @@ end
|
||||
local has_flag = Roles.player_has_flag(game.player, 'is_donator')
|
||||
|
||||
]]
|
||||
function Roles.player_allowed(player,action)
|
||||
function Roles.player_allowed(player, action)
|
||||
local roles = Roles.get_player_roles(player)
|
||||
if not roles then return end
|
||||
for _,role in pairs(roles) do
|
||||
for _, role in pairs(roles) do
|
||||
if role:is_allowed(action) then
|
||||
return true
|
||||
end
|
||||
@@ -527,31 +527,31 @@ function Roles.define_role_order(order)
|
||||
_C.error_if_runtime()
|
||||
Roles.config.order = {}
|
||||
local done = {}
|
||||
for _,role in ipairs(order) do
|
||||
for _, role in ipairs(order) do
|
||||
if type(role) == 'table' and role.name then
|
||||
done[role.name] = true
|
||||
table.insert(Roles.config.order,role.name)
|
||||
table.insert(Roles.config.order, role.name)
|
||||
else
|
||||
done[role] = true
|
||||
table.insert(Roles.config.order,role)
|
||||
table.insert(Roles.config.order, role)
|
||||
end
|
||||
end
|
||||
-- Check no roles were missed
|
||||
for role_name,_ in pairs(Roles.config.roles) do
|
||||
for role_name, _ in pairs(Roles.config.roles) do
|
||||
if not done[role_name] then
|
||||
error('Role missing '..role_name..' from role order, all defined roles must be included.',2)
|
||||
error('Role missing '..role_name..' from role order, all defined roles must be included.', 2)
|
||||
end
|
||||
end
|
||||
-- Re-links roles to they parents as this is called at the end of the config
|
||||
for index,role_name in pairs(Roles.config.order) do
|
||||
for index, role_name in pairs(Roles.config.order) do
|
||||
local role = Roles.config.roles[role_name]
|
||||
if not role then
|
||||
error('Role with name '..role_name..' has not beed defined, either define it or remove it from the order list.',2)
|
||||
error('Role with name '..role_name..' has not beed defined, either define it or remove it from the order list.', 2)
|
||||
end
|
||||
role.index = index
|
||||
local parent = Roles.config.roles[role.parent]
|
||||
if parent then
|
||||
setmetatable(role.allowed_actions,{__index=parent.allowed_actions})
|
||||
setmetatable(role.allowed_actions, {__index=parent.allowed_actions})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -566,7 +566,7 @@ Roles.define_flag_trigger('is_donator', function(player, state)
|
||||
end)
|
||||
|
||||
]]
|
||||
function Roles.define_flag_trigger(name,callback)
|
||||
function Roles.define_flag_trigger(name, callback)
|
||||
_C.error_if_runtime()
|
||||
Roles.config.flags[name] = Async.register(callback)
|
||||
end
|
||||
@@ -607,7 +607,7 @@ end
|
||||
local role = Roles.new_role('Moderator', 'Mod')
|
||||
|
||||
]]
|
||||
function Roles.new_role(name,short_hand)
|
||||
function Roles.new_role(name, short_hand)
|
||||
_C.error_if_runtime()
|
||||
if Roles.config.roles[name] then return error('Role name is non unique') end
|
||||
local role = setmetatable({
|
||||
@@ -616,7 +616,7 @@ function Roles.new_role(name,short_hand)
|
||||
allowed_actions={},
|
||||
allow_all_actions=false,
|
||||
flags={}
|
||||
},{__index=Roles._prototype})
|
||||
}, {__index=Roles._prototype})
|
||||
Roles.config.roles[name] = role
|
||||
return role
|
||||
end
|
||||
@@ -654,7 +654,7 @@ function Roles._prototype:allow(actions)
|
||||
if type(actions) ~= 'table' then
|
||||
actions = {actions}
|
||||
end
|
||||
for _,action in pairs(actions) do
|
||||
for _, action in pairs(actions) do
|
||||
self.allowed_actions[action]=true
|
||||
end
|
||||
return self
|
||||
@@ -675,7 +675,7 @@ function Roles._prototype:disallow(actions)
|
||||
if type(actions) ~= 'table' then
|
||||
actions = {actions}
|
||||
end
|
||||
for _,action in pairs(actions) do
|
||||
for _, action in pairs(actions) do
|
||||
self.allowed_actions[action]=false
|
||||
end
|
||||
return self
|
||||
@@ -707,13 +707,13 @@ end
|
||||
role:set_flag('is_admin')
|
||||
|
||||
]]
|
||||
function Roles._prototype:set_flag(name,value)
|
||||
function Roles._prototype:set_flag(name, value)
|
||||
if value == nil then value = true end
|
||||
self.flags[name] = not not value -- not not forces a boolean value
|
||||
return self
|
||||
end
|
||||
|
||||
--[[-- Clears all flags from this role, individual flags can be removed with set_flag(name,false)
|
||||
--[[-- Clears all flags from this role, individual flags can be removed with set_flag(name, false)
|
||||
@treturn Roles._prototype allows chaining
|
||||
|
||||
@usage-- Remove all flags from a role
|
||||
@@ -779,10 +779,10 @@ end
|
||||
role:set_permission_group('Admin')
|
||||
|
||||
]]
|
||||
function Roles._prototype:set_permission_group(name,use_factorio_api)
|
||||
function Roles._prototype:set_permission_group(name, use_factorio_api)
|
||||
_C.error_if_runtime()
|
||||
if use_factorio_api then
|
||||
self.permission_group = {true,name}
|
||||
self.permission_group = {true, name}
|
||||
else
|
||||
local group = Groups.get_group_by_name(name)
|
||||
if not group then return end
|
||||
@@ -854,7 +854,7 @@ end
|
||||
role:add_player(game.player)
|
||||
|
||||
]]
|
||||
function Roles._prototype:add_player(player,skip_check,skip_event)
|
||||
function Roles._prototype:add_player(player, skip_check, skip_event)
|
||||
player = Game.get_player_from_any(player)
|
||||
-- Default role cant have players added or removed
|
||||
if self.name == Roles.config.internal.default then return end
|
||||
@@ -869,16 +869,16 @@ function Roles._prototype:add_player(player,skip_check,skip_event)
|
||||
-- Add the role name to the player's roles
|
||||
local player_roles = Roles.config.players[player.name]
|
||||
if player_roles then
|
||||
for _,role_name in pairs(player_roles) do
|
||||
for _, role_name in pairs(player_roles) do
|
||||
if role_name == self.name then return false end
|
||||
end
|
||||
table.insert(player_roles,self.name)
|
||||
table.insert(player_roles, self.name)
|
||||
else
|
||||
Roles.config.players[player.name] = {self.name}
|
||||
end
|
||||
-- Emits event if required
|
||||
if not skip_event then
|
||||
emit_player_roles_updated(player,'assign',{self})
|
||||
emit_player_roles_updated(player, 'assign', {self})
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -893,7 +893,7 @@ end
|
||||
role:remove_player(game.player)
|
||||
|
||||
]]
|
||||
function Roles._prototype:remove_player(player,skip_check,skip_event)
|
||||
function Roles._prototype:remove_player(player, skip_check, skip_event)
|
||||
player = Game.get_player_from_any(player)
|
||||
-- Default role cant have players added or removed
|
||||
if self.name == Roles.config.internal.default then return end
|
||||
@@ -909,9 +909,9 @@ function Roles._prototype:remove_player(player,skip_check,skip_event)
|
||||
local player_roles = Roles.config.players[player.name]
|
||||
local rtn = false
|
||||
if player_roles then
|
||||
for index,role_name in pairs(player_roles) do
|
||||
for index, role_name in pairs(player_roles) do
|
||||
if role_name == self.name then
|
||||
table.remove(player_roles,index)
|
||||
table.remove(player_roles, index)
|
||||
rtn = true
|
||||
break
|
||||
end
|
||||
@@ -922,7 +922,7 @@ function Roles._prototype:remove_player(player,skip_check,skip_event)
|
||||
end
|
||||
-- Emits event if required
|
||||
if not skip_event then
|
||||
emit_player_roles_updated(player,'unassign',{self})
|
||||
emit_player_roles_updated(player, 'unassign', {self})
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
@@ -941,15 +941,15 @@ local players = role:get_players(true)
|
||||
function Roles._prototype:get_players(online)
|
||||
local players = {}
|
||||
-- Gets all players that have this role
|
||||
for player_name,player_roles in pairs(Roles.config.players) do
|
||||
for _,role_name in pairs(player_roles) do
|
||||
for player_name, player_roles in pairs(Roles.config.players) do
|
||||
for _, role_name in pairs(player_roles) do
|
||||
if role_name == self.name then
|
||||
table.insert(players,player_name)
|
||||
table.insert(players, player_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Convert the player names to LuaPlayer
|
||||
for index,player_name in pairs(players) do
|
||||
for index, player_name in pairs(players) do
|
||||
players[index] = Game.get_player_from_any(player_name)
|
||||
end
|
||||
-- Filter by online if param is defined
|
||||
@@ -957,9 +957,9 @@ function Roles._prototype:get_players(online)
|
||||
return players
|
||||
else
|
||||
local filtered = {}
|
||||
for _,player in pairs(players) do
|
||||
for _, player in pairs(players) do
|
||||
if player.connected == online then
|
||||
table.insert(filtered,player)
|
||||
table.insert(filtered, player)
|
||||
end
|
||||
end
|
||||
return filtered
|
||||
@@ -976,7 +976,7 @@ role:print('Hello, World!')
|
||||
]]
|
||||
function Roles._prototype:print(message)
|
||||
local players = self:get_players(true)
|
||||
for _,player in pairs(players) do
|
||||
for _, player in pairs(players) do
|
||||
player.print(message)
|
||||
end
|
||||
return #players
|
||||
@@ -987,7 +987,7 @@ local function role_update(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
-- Updates flags given to the player
|
||||
for flag, async_token in pairs(Roles.config.flags) do
|
||||
local state = Roles.player_has_flag(player,flag)
|
||||
local state = Roles.player_has_flag(player, flag)
|
||||
Async(async_token, player, state)
|
||||
end
|
||||
-- Updates the players permission group
|
||||
@@ -1005,22 +1005,22 @@ local function role_update(event)
|
||||
end
|
||||
|
||||
--- When a player joined or has a role change then the update is triggered
|
||||
Event.add(Roles.events.on_role_assigned,role_update)
|
||||
Event.add(Roles.events.on_role_unassigned,role_update)
|
||||
Event.add(defines.events.on_player_joined_game,role_update)
|
||||
Event.add(Roles.events.on_role_assigned, role_update)
|
||||
Event.add(Roles.events.on_role_unassigned, role_update)
|
||||
Event.add(defines.events.on_player_joined_game, role_update)
|
||||
-- Every 60 seconds the auto promote check is preformed
|
||||
Event.on_nth_tick(3600,function()
|
||||
Event.on_nth_tick(3600, function()
|
||||
local promotes = {}
|
||||
for _,player in pairs(game.connected_players) do
|
||||
for _,role in pairs(Roles.config.roles) do
|
||||
for _, player in pairs(game.connected_players) do
|
||||
for _, role in pairs(Roles.config.roles) do
|
||||
if role.auto_promote_condition then
|
||||
local success,err = pcall(role.auto_promote_condition,player)
|
||||
local success, err = pcall(role.auto_promote_condition, player)
|
||||
if not success then
|
||||
log{'expcore-roles.error-log-format-promote',role.name,err}
|
||||
log{'expcore-roles.error-log-format-promote', role.name, err}
|
||||
else
|
||||
if err == true and not Roles.player_has_role(player,role) then
|
||||
if err == true and not Roles.player_has_role(player, role) then
|
||||
if promotes[player.name] then
|
||||
table.insert(promotes[player.name],role.name)
|
||||
table.insert(promotes[player.name], role.name)
|
||||
else
|
||||
promotes[player.name] = {role.name}
|
||||
end
|
||||
@@ -1029,8 +1029,8 @@ Event.on_nth_tick(3600,function()
|
||||
end
|
||||
end
|
||||
end
|
||||
for player_name,roles in pairs(promotes) do
|
||||
Roles.assign_player(player_name,roles)
|
||||
for player_name, roles in pairs(promotes) do
|
||||
Roles.assign_player(player_name, roles)
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
@@ -9,13 +9,13 @@ local Store = require 'expcore.store' --- @dep expcore.store
|
||||
local scenario_diffculty = Store.register()
|
||||
|
||||
-- When the store is changed this function will trigger
|
||||
Store.watch(scenario_diffculty,function(value)
|
||||
Store.watch(scenario_diffculty, function(value)
|
||||
game.print('The scenario diffculty has been set to '..value)
|
||||
end)
|
||||
|
||||
Store.set(scenario_diffculty,'hard') -- Set the value stored to 'hard'
|
||||
Store.set(scenario_diffculty, 'hard') -- Set the value stored to 'hard'
|
||||
Store.get(scenario_diffculty) -- Returns 'hard'
|
||||
Store.update(scenario_diffculty,function(value) -- Will set value to 'normal' if no value is present
|
||||
Store.update(scenario_diffculty, function(value) -- Will set value to 'normal' if no value is present
|
||||
return not value and 'normal'
|
||||
end)
|
||||
|
||||
@@ -27,13 +27,13 @@ local player_scores = Store.register(function(player) -- Use player name as the
|
||||
end)
|
||||
|
||||
-- When any key in the store is changed this function will trigger
|
||||
Store.watch(player_scores,function(value,key,old_value)
|
||||
Store.watch(player_scores, function(value, key, old_value)
|
||||
game.print(key..' now has a score of '..value)
|
||||
end)
|
||||
|
||||
Store.set(player_scores,game.player,10) -- Set your score to 10
|
||||
Store.get(scenario_diffculty,game.player) -- Returns 10
|
||||
Store.update(scenario_diffculty,game.player,function(value) -- Add 1 to your score
|
||||
Store.set(player_scores, game.player, 10) -- Set your score to 10
|
||||
Store.get(scenario_diffculty, game.player) -- Returns 10
|
||||
Store.update(scenario_diffculty, game.player, function(value) -- Add 1 to your score
|
||||
return value + 1
|
||||
end)
|
||||
|
||||
@@ -79,33 +79,33 @@ local player_scores = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- player_scores is a valid store and key will be your player name
|
||||
local key = Store.validate(player_scores,game.player)
|
||||
local key = Store.validate(player_scores, game.player)
|
||||
|
||||
]]
|
||||
function Store.validate(store,key,error_stack)
|
||||
function Store.validate(store, key, error_stack)
|
||||
error_stack = error_stack or 1
|
||||
|
||||
if type(store) ~= 'number' then
|
||||
-- Store is not a number and so if not valid
|
||||
error('Store uid given is not a number; recived type '..type(store),error_stack+1)
|
||||
error('Store uid given is not a number; recived type '..type(store), error_stack+1)
|
||||
elseif store > Store.uid then
|
||||
-- Store is a number but it is out of range, ie larger than the current highest uid
|
||||
error('Store uid is out of range; recived '..tostring(store),error_stack+1)
|
||||
error('Store uid is out of range; recived '..tostring(store), error_stack+1)
|
||||
elseif key ~= nil and type(key) ~= 'string' and Store.serializers[store] == nil then
|
||||
-- Key is present but is not a string and there is no serializer registered
|
||||
error('Store key is not a string and no serializer has been registered; recived '..type(key),error_stack+1)
|
||||
error('Store key is not a string and no serializer has been registered; recived '..type(key), error_stack+1)
|
||||
elseif key ~= nil then
|
||||
-- Key is present and so it is serialized and returned
|
||||
local serializer = Store.serializers[store]
|
||||
if type(key) ~= 'string' then
|
||||
local success, serialized_key = pcall(serializer,key)
|
||||
local success, serialized_key = pcall(serializer, key)
|
||||
|
||||
if not success then
|
||||
-- Serializer casued an error while serializing the key
|
||||
error('Store watcher casued an error:\n\t'..key,error_stack+1)
|
||||
error('Store watcher casued an error:\n\t'..key, error_stack+1)
|
||||
elseif type(serialized_key) ~= 'string' then
|
||||
-- Serializer was successful but failed to return a string value
|
||||
error('Store key serializer did not return a string; recived type '..type(key),error_stack+1)
|
||||
error('Store key serializer did not return a string; recived type '..type(key), error_stack+1)
|
||||
end
|
||||
|
||||
return serialized_key
|
||||
@@ -161,12 +161,12 @@ end
|
||||
local scenario_diffculty = Store.register()
|
||||
|
||||
-- Register the watcher so that when we change the value the message is printed
|
||||
Store.watch(scenario_diffculty,function(value)
|
||||
Store.watch(scenario_diffculty, function(value)
|
||||
game.print('The scenario diffculty has been set to '..value)
|
||||
end)
|
||||
|
||||
-- Set a new value for the diffculty and see that it has printed to the game
|
||||
Store.set(scenario_diffculty,'hard')
|
||||
Store.set(scenario_diffculty, 'hard')
|
||||
|
||||
@usage-- Printing the changed value to all players, with keys
|
||||
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
|
||||
@@ -175,21 +175,21 @@ local player_scores = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Register the watcher so that when we change the value the message is printed
|
||||
Store.watch(player_scores,function(value,key,old_value)
|
||||
Store.watch(player_scores, function(value, key, old_value)
|
||||
game.print(key..' now has a score of '..value)
|
||||
end)
|
||||
|
||||
-- Set a new value for your score and see that it has printed to the game
|
||||
Store.set(player_scores,game.player,10)
|
||||
Store.set(player_scores, game.player, 10)
|
||||
|
||||
]]
|
||||
function Store.watch(store,watcher)
|
||||
function Store.watch(store, watcher)
|
||||
if _LIFECYCLE ~= _STAGE.control then
|
||||
-- Only allow this function to be called during the control stage
|
||||
error('Store watcher can not be registered durring runtime', 2)
|
||||
end
|
||||
|
||||
Store.validate(store,nil,2)
|
||||
Store.validate(store, nil, 2)
|
||||
|
||||
-- Add the watchers table if it does not exist
|
||||
local watchers = Store.watchers[store]
|
||||
@@ -224,14 +224,14 @@ local player_scores = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Get your current score
|
||||
local my_score = Store.get(player_scores,game.player)
|
||||
local my_score = Store.get(player_scores, game.player)
|
||||
|
||||
-- Get all scores
|
||||
lcoal scores = Store.get(player_scores)
|
||||
|
||||
]]
|
||||
function Store.get(store,key)
|
||||
key = Store.validate(store,key,2)
|
||||
function Store.get(store, key)
|
||||
key = Store.validate(store, key, 2)
|
||||
|
||||
-- Get the data from the data store
|
||||
local data = data_store[store]
|
||||
@@ -266,14 +266,14 @@ local player_scores = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Clear your score
|
||||
Store.clear(player_scores,game.player)
|
||||
Store.clear(player_scores, game.player)
|
||||
|
||||
-- Clear all scores
|
||||
Store.clear(player_scores)
|
||||
|
||||
]]
|
||||
function Store.clear(store,key)
|
||||
key = Store.validate(store,key,2)
|
||||
function Store.clear(store, key)
|
||||
key = Store.validate(store, key, 2)
|
||||
local old_value
|
||||
|
||||
-- Check if there is a key being used
|
||||
@@ -288,7 +288,7 @@ function Store.clear(store,key)
|
||||
end
|
||||
|
||||
-- Trigger any watch functions
|
||||
Store.raw_trigger(store,key,nil,old_value)
|
||||
Store.raw_trigger(store, key, nil, old_value)
|
||||
end
|
||||
|
||||
--[[-- Used to set the data in a store, will trigger any watchers, key is optional depending on if you are using them
|
||||
@@ -301,7 +301,7 @@ end
|
||||
local scenario_diffculty = Store.register()
|
||||
|
||||
-- Set the new scenario diffculty
|
||||
Store.set(scenario_diffculty,'hard')
|
||||
Store.set(scenario_diffculty, 'hard')
|
||||
|
||||
@usage-- Set data in a store with keys
|
||||
-- Register the new store, we are not using player names as the keys so it would be useful to accept LuaPlayer objects
|
||||
@@ -310,16 +310,16 @@ local player_scores = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Set your current score
|
||||
Store.set(player_scores,game.player,10)
|
||||
Store.set(player_scores, game.player, 10)
|
||||
|
||||
-- Set all scores, note this might not have much use
|
||||
Store.set(player_scores,{
|
||||
Store.set(player_scores, {
|
||||
[game.player.name] = 10,
|
||||
['SomeOtherPlayer'] = 0
|
||||
})
|
||||
|
||||
]]
|
||||
function Store.set(store,key,value)
|
||||
function Store.set(store, key, value)
|
||||
-- Allow for key to be optional
|
||||
if value == nil then
|
||||
value = key
|
||||
@@ -327,7 +327,7 @@ function Store.set(store,key,value)
|
||||
end
|
||||
|
||||
-- Check the store is valid
|
||||
key = Store.validate(store,key,2)
|
||||
key = Store.validate(store, key, 2)
|
||||
local old_value
|
||||
|
||||
-- If there is a key being used then the store must be a able
|
||||
@@ -343,7 +343,7 @@ function Store.set(store,key,value)
|
||||
end
|
||||
|
||||
-- Trigger any watchers
|
||||
Store.raw_trigger(store,key,value,old_value)
|
||||
Store.raw_trigger(store, key, value, old_value)
|
||||
end
|
||||
|
||||
--[[-- Used to update the data in a store, use this with tables, will trigger any watchers, key is optional depending on if you are using them
|
||||
@@ -356,10 +356,10 @@ end
|
||||
local game_score = Store.register()
|
||||
|
||||
-- Setting a default value
|
||||
Store.set(game_score,0)
|
||||
Store.set(game_score, 0)
|
||||
|
||||
-- We now will update the game score by one, we return the value so that it is set as the new value in the store
|
||||
Store.update(game_score,function(value)
|
||||
Store.update(game_score, function(value)
|
||||
return value + 1
|
||||
end)
|
||||
|
||||
@@ -370,7 +370,7 @@ local player_data = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Setting a default value for your player, used to show the table structure
|
||||
Store.set(player_data,game.player,{
|
||||
Store.set(player_data, game.player, {
|
||||
group = 'Admin',
|
||||
role = 'Owner',
|
||||
show_group_config = false
|
||||
@@ -378,12 +378,12 @@ Store.set(player_data,game.player,{
|
||||
|
||||
-- Updating the show_group_config key in your player data, note that it would be harder to call set every time
|
||||
-- We do not need to return anything in this case as we are not replacing all the data
|
||||
Store.update(player_data,game.player,function(data)
|
||||
Store.update(player_data, game.player, function(data)
|
||||
data.show_group_config = not data.show_group_config
|
||||
end)
|
||||
|
||||
]]
|
||||
function Store.update(store,key,updater)
|
||||
function Store.update(store, key, updater)
|
||||
-- Allow for key to be nil
|
||||
if updater == nil then
|
||||
updater = key
|
||||
@@ -391,7 +391,7 @@ function Store.update(store,key,updater)
|
||||
end
|
||||
|
||||
-- Check the store is valid
|
||||
key = Store.validate(store,key,2)
|
||||
key = Store.validate(store, key, 2)
|
||||
local value, old_value
|
||||
|
||||
-- If a key is used then the store must be a table
|
||||
@@ -420,7 +420,7 @@ function Store.update(store,key,updater)
|
||||
end
|
||||
|
||||
-- Trigger any watchers
|
||||
Store.raw_trigger(store,key,value,old_value)
|
||||
Store.raw_trigger(store, key, value, old_value)
|
||||
end
|
||||
|
||||
--[[-- Used to update all values that are in a store, similar to Store.update but acts on all keys at once, will trigger watchers for every key present
|
||||
@@ -434,7 +434,7 @@ local player_data = Store.register(function(player)
|
||||
end)
|
||||
|
||||
-- Setting a default value for your player, used to show the table structure
|
||||
Store.set(player_data,game.player,{
|
||||
Store.set(player_data, game.player, {
|
||||
group = 'Admin',
|
||||
role = 'Owner',
|
||||
show_group_config = false
|
||||
@@ -443,13 +443,13 @@ Store.set(player_data,game.player,{
|
||||
-- Updating the show_group_config key for all players, note that it would be harder to call set every time
|
||||
-- We do not need to return anything in this case as we are not replacing all the data
|
||||
-- We also have access to the current key being updated if needed
|
||||
Store.map(player_data,function(data,key)
|
||||
Store.map(player_data, function(data, key)
|
||||
data.show_group_config = not data.show_group_config
|
||||
end)
|
||||
|
||||
]]
|
||||
function Store.map(store,updater)
|
||||
Store.validate(store,nil,2)
|
||||
function Store.map(store, updater)
|
||||
Store.validate(store, nil, 2)
|
||||
|
||||
-- Get all that data in the store and check its a table
|
||||
local data = data_store[store]
|
||||
@@ -458,12 +458,12 @@ function Store.map(store,updater)
|
||||
end
|
||||
|
||||
-- Loop over all the keys and call the updater, setting value if returned, and calling watcher functions
|
||||
for key,value in pairs(data) do
|
||||
local rtn = updater(value,key)
|
||||
for key, value in pairs(data) do
|
||||
local rtn = updater(value, key)
|
||||
if rtn then
|
||||
data[key] = rtn
|
||||
end
|
||||
Store.raw_trigger(store,key,data[key],value)
|
||||
Store.raw_trigger(store, key, data[key], value)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -478,16 +478,16 @@ local scenario_diffculty = Store.register()
|
||||
Store.trigger(scenario_diffculty)
|
||||
|
||||
]]
|
||||
function Store.trigger(store,key)
|
||||
key = Store.validate(store,key,2)
|
||||
function Store.trigger(store, key)
|
||||
key = Store.validate(store, key, 2)
|
||||
|
||||
-- Get the data from the data store
|
||||
local data = data_store[store]
|
||||
if key then
|
||||
data = data[key]
|
||||
Store.raw_trigger(store,key,data,data)
|
||||
Store.raw_trigger(store, key, data, data)
|
||||
else
|
||||
Store.raw_trigger(store,key,data,data)
|
||||
Store.raw_trigger(store, key, data, data)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -503,16 +503,16 @@ local scenario_diffculty = Store.register()
|
||||
|
||||
-- Trigger the watchers with a fake change of diffculty
|
||||
-- This is mostly used internally but it can be useful in other cases
|
||||
Store.raw_trigger(scenario_diffculty,nil,'normal','normal')
|
||||
Store.raw_trigger(scenario_diffculty, nil, 'normal', 'normal')
|
||||
|
||||
]]
|
||||
function Store.raw_trigger(store,key,value,old_value)
|
||||
key = Store.validate(store,key,2)
|
||||
function Store.raw_trigger(store, key, value, old_value)
|
||||
key = Store.validate(store, key, 2)
|
||||
|
||||
-- Get the watchers and then loop over them
|
||||
local watchers = Store.watchers[store] or {}
|
||||
for _,watcher in pairs(watchers) do
|
||||
local success, err = pcall(watcher,value,key,old_value)
|
||||
for _, watcher in pairs(watchers) do
|
||||
local success, err = pcall(watcher, value, key, old_value)
|
||||
if not success then
|
||||
error('Store watcher casued an error:\n\t'..err)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user