Added Common Parse and /me

This commit is contained in:
Cooldude2606
2019-03-02 18:46:35 +00:00
parent 4e42451865
commit 89bd964498
97 changed files with 241 additions and 72 deletions

View File

@@ -29,7 +29,7 @@
-- adds a parse that will cover numbers within the given range
-- input, player and reject are common to all parse functions
-- range_min and range_max are passed to the function from add_param
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 -- check if it is nil or out of the range
-- invalid input for we will reject the input, they are a few ways to do this:
@@ -48,8 +48,8 @@
-- adds a command that will print the players name a given number of times
-- and can only be used by admin to show how auth works
Commands.add_command('repeat-name','Will repeat you name a number of times in chat.') -- creates the new command with the name "repeat-name" and a help message
:add_param('repeat-count',false,'number_range_int',1,5) -- adds a new param called "repeat-count" that is required and is type "number_range_int" the name can be used here as add_parse was used
Commands.new_command('repeat-name','Will repeat you name a number of times in chat.') -- creates the new command with the name "repeat-name" and a help message
:add_param('repeat-count',false,'number-range-int',1,5) -- adds a new param called "repeat-count" that is required and is type "number_range_int" the name can be used here as add_parse was used
:add_param('smiley',true,function(input,player,reject) -- this param is optional and has a custom parse function where add_parse was not used before hand
if not input then return end -- when they is an optional param input may be nil, you can return a default value here, but using nil will allow add_defaults to pick a default
if input:lower() == 'true' or input:lower() == 'yes' then
@@ -109,7 +109,7 @@
end
end)
Commands.add_command('repeat-name','Will repeat you name a number of times in chat.')
Commands.new_command('repeat-name','Will repeat you name a number of times in chat.')
:add_param('repeat-count',false,'number_range_int',1,5)
:add_param('smiley',true,function(input,player,reject)
if not input then return end
@@ -143,6 +143,7 @@
Commands.add_parse(name,callback) --- Adds a parse function which can be called by name rather than callback (used in add_param)
Commands.remove_parse(name) --- Removes a parse function, see add_parse for adding them
Commands.parse(name,input,player,reject,...) --- Intended to be used within other parse functions, runs a parse and returns success and new value
Commands.add_command(name,help) --- Creates a new command object to added details to, note this does not register the command to the game
Commands._prototype:add_param(name,optional,parse,...) --- Adds a new param to the command this will be displayed in the help and used to parse the input
@@ -233,7 +234,7 @@ function Commands.authorize(player,command_name)
-- function passed to authorization callback to make it simpler to use
local auth_fail = function(error_message)
failed = error_message or {'ExpGamingCore_Command.unauthorized'}
failed = error_message or {'expcore-commands.unauthorized'}
return Commands.defines.unauthorized
end
@@ -250,7 +251,7 @@ function Commands.authorize(player,command_name)
end
elseif rtn == false or rtn == Commands.defines.unauthorized or rtn == auth_fail or failed then
-- the callback returned unauthorized, failed be now be set if no value returned
failed = failed or {'ExpGamingCore_Command.unauthorized'}
failed = failed or {'expcore-commands.unauthorized'}
break
end
end
@@ -333,11 +334,24 @@ function Commands.remove_parse(name)
Commands.parse[name] = nil
end
--- Intended to be used within other parse functions, runs a parse and returns success and new value
-- @tparam name string the name of the parse to call, must be registered and cant be a function
-- @tparam input string the input to pass to the parse, will always be a string but might not be the orginal input
-- @treturn any the new value for the input, may be nil, if nil then either there was an error or input was nil
function Commands.parse(name,input,player,reject,...)
if not Commands.parse[name] then return end
local success,rtn = pcall(Commands.parse[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
end
--- Creates a new command object to added details to, note this does not register the command to the game
-- @tparam name string the name of the command to be created
-- @tparam help string the help message for the command
-- @treturn Commands._prototype this will be used with other functions to generate the command functions
function Commands.add_command(name,help)
function Commands.new_command(name,help)
local command = setmetatable({
name=name,
help=help,
@@ -474,7 +488,7 @@ end
-- @treturn Commands.defines.error return this to command handler to exit execution
function Commands.error(error_message,play_sound)
error_message = error_message or ''
player_return({'ExpGamingCore_Command.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
@@ -502,7 +516,7 @@ end
-- @treturn Commands.defines.success return this to the command handler to prevent two success messages
function Commands.success(value)
if value then player_return(value) end
player_return({'ExpGamingCore_Command.command-ran'},'cyan')
player_return({'expcore-commands.command-ran'},'cyan')
return Commands.defines.success
end
@@ -521,7 +535,7 @@ function Commands.run_command(command_event)
-- null param check
if command_data.min_param_count > 0 and not command_event.parameter then
Commands.error({'ExpGamingCore_Command.invalid-inputs',command_data.name,command_data.description})
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
return
end
@@ -549,7 +563,7 @@ 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
Commands.error({'ExpGamingCore_Command.invalid-inputs',command_data.name,command_data.description})
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
return
else
-- concat to the last param
@@ -575,7 +589,7 @@ function Commands.run_command(command_event)
-- checks param count
local param_count = #raw_params
if param_count < command_data.min_param_count then
Commands.error({'ExpGamingCore_Command.invalid-inputs',command_data.name,command_data.description})
Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description})
return
end
@@ -628,4 +642,5 @@ function Commands.run_command(command_event)
if err ~= Commands.defines.error and err ~= Commands.defines.success then Commands.success(err) end
end
require 'expcore.parse.command' -- loads some common parse types
return Commands

View File

@@ -1,9 +1,9 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Unbefugt: Zugang verweigert. Du hast keinen Zugriff auf diese Befehle!
reject-number-range=ungültige Reichweite, Min: __1__, Max: __2__
reject-string-max-length=ungültige Länge, Max: __1__
reject-player=ungültiger Spieler Name, __1__ , Versuche "Tab" zu benutzen, damit sich der Name automatisch vervollständigt.
reject-player-online=Der betroffene Spieler ist offline, Befehl konnte nicht ausgeführt werden.
reject-player-alive=Der betroffene Spieler ist Tod, Befehl konnte nicht ausgeführt werden.
invalid-inputs=ungültige Eingabe, /__1__ __2__
invalid-range=ungültige Reichweite, Min: __1__, Max: __2__
invalid-length=ungültige Länge, Max: __1__
invalid-player=ungültiger Spieler Name, __1__ , Versuche "Tab" zu benutzen, damit sich der Name automatisch vervollständigt.
offline-player=Der betroffene Spieler ist offline, Befehl konnte nicht ausgeführt werden.
dead-player=Der betroffene Spieler ist Tod, Befehl konnte nicht ausgeführt werden.
command-ran=Befehl ausgeführt.

View File

@@ -1,14 +1,14 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=Unauthorized, Access is denied due to invalid credentials
error-string-list=Invalid Option, Must be one of: __1__
error-string-len=Invalid Length, Max: __1__
error-number=Invalid Number
error-number-range=Invalid Range, Min (exclusive): __1__, Max (inclusive): __2__
error-player=Invaild Player Name, __1__ ,try using tab key to auto-complete the name
error-player-online=Player is offline.
error-player-alive=Player is dead.
error-player-rank=Player is of Higher Rank.
reject-string-options=Invalid Option, Must be one of: __1__
reject-string-max-length=Invalid Length, Max: __1__
reject-number=Invalid Number
reject-number-range=Invalid Range, Min (inclusive): __1__, Max (inclusive): __2__
reject-player=Invaild Player Name, __1__ ,try using tab key to auto-complete the name
reject-player-online=Player is offline.
reject-player-alive=Player is dead.
reject-force=Invaild Force Name
reject-surface=Invaild surface Name
invalid-inputs=Invalid Input, /__1__ __2__
invalid-parse=Invalid Input, There was a problem prasing the paramaters
command-ran=Command Complete
command-fail=Command failed to run: __1__

View File

@@ -1,9 +0,0 @@
[ExpGamingCore_Command]
unauthorized=401 - Unauthorized: Access is denied due to invalid credentials
invalid-inputs=Invalid Input, /__1__ __2__
invalid-range=Invalid Range, Min: __1__, Max: __2__
invalid-length=Invalid Length, Max: __1__
invalid-player=Invaild Player Name, __1__ ,try using tab key to auto-complete the name
offline-player=Player is offline, Command Failed To Run
dead-player=Player is dead, Command Failed To Run
command-ran=Command Complete

View File

@@ -1,9 +1,9 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Onbevoegd: toegang wordt geweigerd vanwege ongeldige inloggegevens
reject-number-range=Onjuiste radius, Min: __1__, Max: __2__
reject-string-max-length=Onjuiste lengte, Max: __1__
reject-player=Onjuiste naam, __1__ , probeer tab te gebruiken om de naam automatisch in te vullen
reject-player-online=Speler is offline.
reject-player-alive=Speler is dood.
invalid-inputs=Onjuiste invoer, /__1__ __2__
invalid-range=Onjuiste radius, Min: __1__, Max: __2__
invalid-length=Onjuiste lengte, Max: __1__
invalid-player=Onjuiste naam, __1__ , probeer tab te gebruiken om de naam automatisch in te vullen
offline-player=Speler is offline.
dead-player=Speler is dood.
command-ran=Commando uitgevoerd.

View File

@@ -1,9 +1,9 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Otillåten: Tillgång nekas på grund av otillräcklig säkerhetsprövning.
reject-number-range=Invalid räckvid, Min: __1__, Max: __2__
reject-string-max-length=ogiltig längd, Max: __1__
reject-player=Ogiltigt spelarnamn, __1__ , försök använda tab-tangenten för att auto-slutföra namn.
reject-player-online=Spelare är offline. Kommando misslyckades med att köras.
reject-player-alive=Spelare är död. Kommando misslyckades med att köras.
invalid-inputs=Igiltig inmatning, /__1__ __2__
invalid-range=Invalid räckvid, Min: __1__, Max: __2__
invalid-length=ogiltig längd, Max: __1__
invalid-player=Ogiltigt spelarnamn, __1__ , försök använda tab-tangenten för att auto-slutföra namn.
offline-player=Spelare är offline. Kommando misslyckades med att köras.
dead-player=Spelare är död. Kommando misslyckades med att köras.
command-ran=Kommandot slutfört

142
expcore/parse.command.lua Normal file
View File

@@ -0,0 +1,142 @@
local Commands = require 'expcore.commands'
local Game = require 'utils.game'
--[[
>>>>Adds parses:
boolean
string-options - options: array
string-max-length - max_length: number
number
integer
number-range - range_min: number, range_max: number
integer-range - range_min: number, range_max: number
player
player-online
player-alive
force
surface
]]
Commands.add_parse('boolean',function(input,player,reject)
if not input then return end -- nil check
input = input:lower()
if input == 'yes'
or input == 'y'
or input == 'true'
or input == '1' then
return true
else
return false
end
end)
Commands.add_parse('string-options',function(input,player,reject,options)
if not input then return end -- nil check
input = input:lower()
for option in options do
if input == option:lower() then
return true
end
end
return reject{'reject-string-options',options:concat(', ')}
end)
Commands.add_parse('string-max-length',function(input,player,reject,max_length)
if not input then return end -- nil check
local length = input:len()
if length > max_length then
return reject{'expcore-commands.reject-string-max-length',max_length}
else
return input
end
end)
Commands.add_parse('number',function(input,player,reject)
if not input then return end -- nil check
local number = tonumber(input)
if not number then
return reject{'expcore-commands.reject-number'}
else
return number
end
end)
Commands.add_parse('integer',function(input,player,reject)
if not input then return end -- nil check
local number = tonumber(input)
if not number then
return reject{'expcore-commands.reject-number'}
else
return number:floor()
end
end)
Commands.add_parse('number-range',function(input,player,reject,range_min,range_max)
local number = Commands.parse('number',input,player,reject)
if not number then return end -- nil check
if number < range_min or number > range_max then
return reject{'expcore-commands.reject-number-range',range_min,range_max}
else
return number
end
end)
Commands.add_parse('integer-range',function(input,player,reject,range_min,range_max)
local number = Commands.parse('integer',input,player,reject)
if not number then return end -- nil check
if number < range_min or number > range_max then
return reject{'expcore-commands.reject-number-range',range_min,range_max}
else
return number
end
end)
Commands.add_parse('player',function(input,player,reject)
if not input then return end -- nil check
local input_player = Game.get_player_from_any(input)
if not player then
return reject{'expcore-commands.reject-player',input}
else
return input_player
end
end)
Commands.add_parse('player-online',function(input,player,reject)
local input_player = Commands.parse('player',input,player,reject)
if not input_player then return end -- nil check
if not input_player.connected then
return reject{'expcore-commands.reject-player-online'}
else
return input_player
end
end)
Commands.add_parse('player-alive',function(input,player,reject)
local input_player = Commands.parse('player-online',input,player,reject)
if not input_player then return end -- nil check
if not input_player.character or not input_player.character.health > 0 then
return reject{'expcore-commands.reject-player-alive'}
else
return input_player
end
end)
Commands.add_parse('force',function(input,player,reject)
if not input then return end -- nil check
local force = game.forces[input]
if not force then
return reject{'expcore-commands.reject-force'}
else
return force
end
end)
Commands.add_parse('surface',function(input,player,reject)
if not input then return end
local surface = game.surfaces[input]
if not surface then
return reject{'expcore-commands.reject-surface'}
else
return surface
end
end)

14
locale/en/expcore.cfg Normal file
View File

@@ -0,0 +1,14 @@
[expcore-commands]
unauthorized=Unauthorized, Access is denied due to invalid credentials
reject-string-options=Invalid Option, Must be one of: __1__
reject-string-max-length=Invalid Length, Max: __1__
reject-number=Invalid Number
reject-number-range=Invalid Range, Min (inclusive): __1__, Max (inclusive): __2__
reject-player=Invaild Player Name, __1__ ,try using tab key to auto-complete the name
reject-player-online=Player is offline.
reject-player-alive=Player is dead.
reject-force=Invaild Force Name
reject-surface=Invaild surface Name
invalid-inputs=Invalid Input, /__1__ __2__
command-ran=Command Complete
command-fail=Command failed to run: __1__

7
modules/commands/me.lua Normal file
View File

@@ -0,0 +1,7 @@
local Commands = require 'expcore.commands'
Commands.new_command('me','Sends an action message in the chat')
:add_param('action',false)
:register(function(player,action,raw)
game.print(string.format('%s %s',player.name,action))
end)

View File

@@ -33,7 +33,7 @@ Commands.add_parse('number_range_int',function(input,player,reject,range_min,ran
end
end)
Commands.add_command('repeat-name','Will repeat you name a number of times in chat.')
Commands.new_command('repeat-name','Will repeat you name a number of times in chat.')
:add_param('repeat-count',false,'number_range_int',1,5)
:add_param('smiley',true,function(input,player,reject)
if not input then return end
@@ -54,5 +54,5 @@ end)
end
for i = 1,repeat_count do
Commands.print(i..msg)
end
end
end)

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=Unauthorized, Access is denied due to invalid credentials
error-string-list=Invalid Option, Must be one of: __1__
error-string-len=Invalid Length, Max: __1__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Unauthorized: Access is denied due to invalid credentials
invalid-inputs=Invalid Input, /__1__ __2__
invalid-range=Invalid Range, Min: __1__, Max: __2__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Unauthorized: Access is denied due to invalid credentials
invalid-inputs=Invalid Input, /__1__ __2__
invalid-range=Invalid Range, Min: __1__, Max: __2__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Onbevoegd: toegang wordt geweigerd vanwege ongeldige inloggegevens
invalid-inputs=Onjuiste invoer, /__1__ __2__
invalid-range=Onjuiste radius, Min: __1__, Max: __2__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Onbevoegd: toegang wordt geweigerd vanwege ongeldige inloggegevens
invalid-inputs=Onjuiste invoer, /__1__ __2__
invalid-range=Onjuiste radius, Min: __1__, Max: __2__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Otillåten: Tillgång nekas på grund av otillräcklig säkerhetsprövning.
invalid-inputs=Igiltig inmatning, /__1__ __2__
invalid-range=Invalid räckvid, Min: __1__, Max: __2__

View File

@@ -1,4 +1,4 @@
[ExpGamingCore_Command]
[expcore-commands]
unauthorized=401 - Otillåten: Tillgång nekas på grund av otillräcklig säkerhetsprövning.
invalid-inputs=Igiltig inmatning, /__1__ __2__
invalid-range=Invalid räckvid, Min: __1__, Max: __2__

View File

@@ -59,40 +59,40 @@ commands.validate = {
['string-inf']=function(value) return tostring(value) end,
['string-list']=function(value,event,list)
local rtn = tostring(value) and table.includes(list,tostring(value)) and tostring(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-list',table.concat(list,', ')} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-string-list',table.concat(list,', ')} end return rtn end,
['string-len']=function(value,event,max)
local rtn = tostring(value) and tostring(value):len() <= max and tostring(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-string-len',max} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-string-len',max} end return rtn end,
['number']=function(value)
local rtn = tonumber(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-number'} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-number'} end return rtn end,
['number-int']=function(value)
local rtn = tonumber(value) and math.floor(tonumber(value)) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-number'} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-number'} end return rtn end,
['number-range']=function(value,event,min,max)
local rtn = tonumber(value) and tonumber(value) > min and tonumber(value) <= max and tonumber(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-number-range',min,max} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-number-range',min,max} end return rtn end,
['number-range-int']=function(value,event,min,max)
local rtn = tonumber(value) and math.floor(tonumber(value)) > min and math.floor(tonumber(value)) <= max and math.floor(tonumber(value)) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-number-range',min,max} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-number-range',min,max} end return rtn end,
['player']=function(value)
local rtn = Game.get_player(value) or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-player',value} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-player',value} end return rtn end,
['player-online']=function(value)
local player,err = commands.validate['player'](value)
if err then return commands.error(err) end
local rtn = player.connected and player or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-online'} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-player-online'} end return rtn end,
['player-alive']=function(value)
local player,err = commands.validate['player-online'](value)
if err then return commands.error(err) end
local rtn = player.character and player.character.health > 0 and player or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-alive'} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-player-alive'} end return rtn end,
['player-rank']=function(value,event)
local player,err = commands.validate['player'](value)
if err then return commands.error(err) end
local rtn = player.admin and Game.get_player(event).admin and player or nil
if not rtn then return commands.error{'ExpGamingCore_Command.error-player-rank'} end return rtn end,
if not rtn then return commands.error{'expcore-commands.error-player-rank'} end return rtn end,
['player-rank-online']=function(value)
local player,err = commands.validate['player-online'](value)
if err then return commands.error(err) end
@@ -212,13 +212,13 @@ local function run_custom_command(command)
local success, err = pcall(callback,player,command.name,command)
if not success then error(err)
elseif not err then
player_return({'ExpGamingCore_Command.command-fail',{'ExpGamingCore_Command.unauthorized'}},defines.textcolor.crit)
player_return({'expcore-commands.command-fail',{'expcore-commands.unauthorized'}},defines.textcolor.crit)
logMessage(player.name,command,'Failed to use command (Unauthorized)',commands.validate_args(command))
game.player.play_sound{path='utility/cannot_build'}
return
end
end elseif data.default_admin_only == true and player and not player.admin then
player_return({'ExpGamingCore_Command.command-fail',{'ExpGamingCore_Command.unauthorized'}},defines.textcolor.crit)
player_return({'expcore-commands.command-fail',{'expcore-commands.unauthorized'}},defines.textcolor.crit)
logMessage(player.name,command,'Failed to use command (Unauthorized)',commands.validate_args(command))
game.player.play_sound{path='utility/cannot_build'}
return
@@ -227,7 +227,7 @@ local function run_custom_command(command)
local args, err = commands.validate_args(command)
if args == commands.error then
if is_type(err,'table') then table.insert(err,command.name) table.insert(err,commands.format_inputs(data))
player_return({'ExpGamingCore_Command.command-fail',err},defines.textcolor.high) else player_return({'ExpGamingCore_Command.command-fail',{'ExpGamingCore_Command.invalid-inputs',command.name,commands.format_inputs(data)}},defines.textcolor.high) end
player_return({'expcore-commands.command-fail',err},defines.textcolor.high) else player_return({'expcore-commands.command-fail',{'expcore-commands.invalid-inputs',command.name,commands.format_inputs(data)}},defines.textcolor.high) end
logMessage(player.name,command,'Failed to use command (Invalid Args)',args)
player.play_sound{path='utility/deconstruct_big'}
return
@@ -235,7 +235,7 @@ local function run_custom_command(command)
-- runs the command
local success, err = pcall(data.callback,command,args)
if not success then error(err) end
if err ~= commands.error then player_return({'ExpGamingCore_Command.command-ran'},defines.textcolor.info) end
if err ~= commands.error then player_return({'expcore-commands.command-ran'},defines.textcolor.info) end
logMessage(player.name,command,'Used command',args)
end