From 4ca1ba5c18f6273a62cf1c1e36b224cfe94adda8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 17:13:03 +0100 Subject: [PATCH 1/6] Added player as an option to list roles command --- locale/en/commands.cfg | 2 +- modules/commands/roles.lua | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index e1cff318..3e2a70c0 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -9,5 +9,5 @@ chelp-format=/__1__ __2__ - __3__ __4__ chelp-alias=Alias: __1__ chelp-out-of-range=__1__ is an invalid page number. roles-higher-role=The role you tried to assign is higher than your highest. -roles-list=All active roles are: +roles-list=All roles are: roles-list-element=__1__, [color=__2__]__3__ \ No newline at end of file diff --git a/modules/commands/roles.lua b/modules/commands/roles.lua index 64bdb74e..4501a822 100644 --- a/modules/commands/roles.lua +++ b/modules/commands/roles.lua @@ -31,9 +31,13 @@ Commands.new_command('unassign-role','Unassigns a role from a player') end) Commands.new_command('list-roles','Lists all roles in they correct order') +:add_param('player',true,'player') :add_alias('lroles','roles') -:register(function(player,raw) +:register(function(player,action_player,raw) local roles = Roles.config.order + if action_player then + roles = Roles.get_player_roles(action_player) + end local message = {'exp-commands.roles-list'} for _,role_name in pairs(roles) do local role = Roles.get_role_by_name(role_name) From 5fd5de65e0be0201e6159cfdbb51cb6300f7fec6 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 17:23:18 +0100 Subject: [PATCH 2/6] Fixed auto promote error --- config/roles.lua | 25 ++++++++++++++++++------- expcore/roles.lua | 4 ++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/config/roles.lua b/config/roles.lua index 81cf5b7d..f0dfa9f7 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -4,13 +4,16 @@ local Roles = require 'expcore.roles' -- Use these to adjust for ticks ie game.tick < 5*minutes local seconds, minutes, hours = 60, 3600, 216000 -local function playtime(time_required) - return function(player) - if player.online_time > time_required then - return true +--[[ + -- cant use a factory as it has upvalues ;-; + local function playtime(time_required) + return function(player) + if player.online_time > time_required then + return true + end end end -end +]] --- Role flags that will run when a player changes roles Roles.define_flag_trigger('is_admin',function(player,state) @@ -116,7 +119,11 @@ Roles.new_role('Veteran','Vet') :set_parent('Member') :allow{ } -:set_auto_promote_condition(playtime(10*hours)) +:set_auto_promote_condition(function(player) + if player.online_time > 3*hours then + return true + end +end) --- Standard User Roles Roles.new_role('Member','Mem') @@ -133,7 +140,11 @@ Roles.new_role('Regular','Reg') :allow{ 'command/kill' } -:set_auto_promote_condition(playtime(3*hours)) +:set_auto_promote_condition(function(player) + if player.online_time > 10*hours then + return true + end +end) --- Guest/Default role Roles.new_role('Guest','') diff --git a/expcore/roles.lua b/expcore/roles.lua index 6b24ed44..09fc11e0 100644 --- a/expcore/roles.lua +++ b/expcore/roles.lua @@ -715,8 +715,8 @@ end Event.add(Roles.player_role_assigned,role_update) Event.add(Roles.player_role_unassigned,role_update) Event.add(defines.events.on_player_joined_game,role_update) --- Every 5 seconds the auto promote check is preformed -Event.on_nth_tick(300,function() +-- Every 60 seconds the auto promote check is preformed +Event.on_nth_tick(3600,function() local promotes = {} for _,player in pairs(game.connected_players) do for _,role in pairs(Roles.config.roles) do From aa7c9b11941642dd2e114b09d95b7dc3a91147ea Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 17:27:30 +0100 Subject: [PATCH 3/6] Fixed assign and unassign --- config/roles.lua | 4 ++-- modules/commands/roles.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/roles.lua b/config/roles.lua index f0dfa9f7..29c0017b 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -120,7 +120,7 @@ Roles.new_role('Veteran','Vet') :allow{ } :set_auto_promote_condition(function(player) - if player.online_time > 3*hours then + if player.online_time > 10*hours then return true end end) @@ -141,7 +141,7 @@ Roles.new_role('Regular','Reg') 'command/kill' } :set_auto_promote_condition(function(player) - if player.online_time > 10*hours then + if player.online_time > 3*hours then return true end end) diff --git a/modules/commands/roles.lua b/modules/commands/roles.lua index 4501a822..278f6c10 100644 --- a/modules/commands/roles.lua +++ b/modules/commands/roles.lua @@ -8,7 +8,7 @@ Commands.new_command('assign-role','Assigns a role to a player') :set_flag('admin-only',true) :add_alias('rpromote','assign','role','add-role') :register(function(player,action_player,role,raw) - local player_highest = Roles.get_player_highest(player) + local player_highest = Roles.get_player_highest_role(player) if player_highest.index < role.index then Roles.assign_player(action_player,role,player.name) else @@ -22,7 +22,7 @@ Commands.new_command('unassign-role','Unassigns a role from a player') :set_flag('admin-only',true) :add_alias('rdemote','unassign','remove-role') :register(function(player,action_player,role,raw) - local player_highest = Roles.get_player_highest(player) + local player_highest = Roles.get_player_highest_role(player) if player_highest.index < role.index then Roles.unassign_player(action_player,role,player.name) else From 7b202a3c5fb03b2bbaddbaf2b9b133d9da6a5e91 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 17:36:31 +0100 Subject: [PATCH 4/6] help command now has [ ] rather than ( ) --- locale/en/commands.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 3e2a70c0..01a6e691 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -4,7 +4,7 @@ admin-chat-format=[Admin Chat] [color=__3__]__1__: __2__ tp-no-position-found=No position to teleport to was found, please try again later. tp-to-self=Player can not be teleported to themselves. chelp-title=Help results for "__1__": -chelp-footer=(__1__ results found; page __2__ of __3__) +chelp-footer=[__1__ results found; page __2__ of __3__] chelp-format=/__1__ __2__ - __3__ __4__ chelp-alias=Alias: __1__ chelp-out-of-range=__1__ is an invalid page number. From 5e8762c7bc150d5fa7da316a101956d0fcafd8f8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 17:53:53 +0100 Subject: [PATCH 5/6] Added role auto complete to commands --- config/command_parse_roles.lua | 4 +- expcore/common.lua | 75 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/config/command_parse_roles.lua b/config/command_parse_roles.lua index 82992a9c..8f91863f 100644 --- a/config/command_parse_roles.lua +++ b/config/command_parse_roles.lua @@ -1,11 +1,13 @@ --- Adds some parse functions that can be used with the role system local Commands = require 'expcore.commands' local Roles = require 'expcore.roles' +local auto_complete = ext_require('expcore.common','auto_complete') require 'config.command_parse_general' Commands.add_parse('role',function(input,player,reject) if not input then return end - local role = Roles.get_role_from_any(input) + local roles = Roles.config.roles + local role = auto_complete(roles,input,true) if not role then return reject{'expcore-role.reject-role'} else diff --git a/expcore/common.lua b/expcore/common.lua index 4b5aee81..145e19d8 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -420,4 +420,79 @@ function Public.clear_flying_text(surface) end end +--- Tests if a string contains a given substring. +-- @tparam string s the string to check for the substring +-- @tparam string contains the substring to test for +-- @treturn boolean true if the substring was found in the string +function Public.string_contains(s, contains) + return s and string.find(s, contains) ~= nil +end + +--- Returns the closest match to a key +-- @tparam options table a table of options for the auto complete +-- @tparam input string the input string that will be completed +-- @tparam[opt=false] use_key boolean when true the keys of options will be used as the options +-- @tparam[opt=false] rtn_key boolean when true the the key will be returned rather than the value +-- @return the list item found that matches the input +function Public.auto_complete(options,input,use_key,rtn_key) + local rtn = {} + if type(input)~= 'string' then return end + input = input:lower() + for key,value in pairs(options) do + local check = use_key and key or value + if Public.string_contains(string.lower(check),input) then + local result = rtn_key and key or value + table.insert(rtn,result) + end + end + return rtn[1] +end + +--- Returns all the keys of a table +-- @tparam tbl table the table to get the keys of +-- @treturn table an array of the table keys +function Public.table_keys(tbl) + local rtn = {} + for key,_ in pairs(tbl) do + table.insert(rtn,key) + end + return rtn +end + +--- Returns all the values of a table +-- @tparam tbl table the table to get the values of +-- @treturn table an array of the table values +function Public.table_values(tbl) + local rtn = {} + for _,value in pairs(tbl) do + table.insert(rtn,value) + end + return rtn +end + +--- Returns the list is a sorted way that would be expected by people (this is by key) +-- @tparam table tbl the table to be sorted +-- @treturn table the sorted table +function Public.table_alphanumsort(tbl) + local o = Public.table_keys(tbl) + local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)") + return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end + table.sort(o, function(a,b) + return tostring(a):gsub("%.?%d+",padnum)..("%3d"):format(#b) + < tostring(b):gsub("%.?%d+",padnum)..("%3d"):format(#a) end) + local _tbl = {} + for _,k in pairs(o) do _tbl[k] = tbl[k] end + return _tbl +end + +--- Returns the list is a sorted way that would be expected by people (this is by key) (faster alterative than above) +-- @tparam table tbl the table to be sorted +-- @treturn table the sorted table +function Public.table_keysort(tbl) + local o = Public.table_keys(tbl,true) + local _tbl = {} + for _,k in pairs(o) do _tbl[k] = tbl[k] end + return _tbl +end + return Public \ No newline at end of file From 3f89bd9e601303a0c8fb2d59223b3e7bce9bd6a8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 11 Apr 2019 18:11:31 +0100 Subject: [PATCH 6/6] Fixed roles list --- config/file_loader.lua | 2 +- locale/en/commands.cfg | 3 ++- modules/commands/roles.lua | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/config/file_loader.lua b/config/file_loader.lua index caa0a1a8..3cbd4fac 100644 --- a/config/file_loader.lua +++ b/config/file_loader.lua @@ -22,7 +22,7 @@ return { 'modules.addons.advanced-starting-items', 'modules.addons.spawn-area', 'modules.addons.compilatron', - 'modules.addons.worn-paths', + 'modules.addons.scorched-earth', -- Config Files 'config.command_auth_admin', -- commands tagged with admin_only are blocked for non admins 'config.command_auth_roles', -- commands must be allowed via the role config diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 01a6e691..54e3a84a 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -9,5 +9,6 @@ chelp-format=/__1__ __2__ - __3__ __4__ chelp-alias=Alias: __1__ chelp-out-of-range=__1__ is an invalid page number. roles-higher-role=The role you tried to assign is higher than your highest. -roles-list=All roles are: +roles-list=All roles are: [color=__1__]__2__ +roles-list-player=[color=__1__]__2__ has: [color=__3__]__4__ roles-list-element=__1__, [color=__2__]__3__ \ No newline at end of file diff --git a/modules/commands/roles.lua b/modules/commands/roles.lua index 278f6c10..9ae0d0b8 100644 --- a/modules/commands/roles.lua +++ b/modules/commands/roles.lua @@ -32,18 +32,27 @@ end) Commands.new_command('list-roles','Lists all roles in they correct order') :add_param('player',true,'player') -:add_alias('lroles','roles') +:add_alias('lsroles','roles') :register(function(player,action_player,raw) local roles = Roles.config.order - if action_player then + local message = {'exp-commands.roles-list'} + if action_player ~= '' then roles = Roles.get_player_roles(action_player) end - local message = {'exp-commands.roles-list'} - for _,role_name in pairs(roles) do - local role = Roles.get_role_by_name(role_name) + for index,role in pairs(roles) do + role = Roles.get_role_from_any(role) local colour = role.custom_color or Colours.white colour = string.format('%d,%d,%d',colour.r,colour.g,colour.b) - message = {'exp-commands.roles-list-element',message,colour,role_name} + if index == 1 then + message = {'exp-commands.roles-list',colour,role.name} + if action_player ~= '' then + local player_colour = action_player.color + player_colour = string.format('%d,%d,%d',player_colour.r*255,player_colour.g*255,player_colour.b*255) + message = {'exp-commands.roles-list-player',player_colour,action_player.name,colour,role.name} + end + else + message = {'exp-commands.roles-list-element',message,colour,role.name} + end end return Commands.success(message) end) \ No newline at end of file