From 111e6829b8f4ef5ecadc1ec9e0e3fbeee882bfb9 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 16:50:44 +0000 Subject: [PATCH 1/8] Created config dir --- .../admin-only-auth.lua => config/command_auth_admin.lua | 0 expcore/common_parse.lua => config/command_parse_general.lua | 0 modules/commands/admin-chat.lua | 4 ++-- modules/commands/cheat-mode.lua | 4 ++-- modules/commands/help.lua | 2 +- modules/commands/interface.lua | 2 +- modules/commands/kill.lua | 4 ++-- modules/commands/tag.lua | 2 +- modules/commands/teleport.lua | 4 ++-- 9 files changed, 11 insertions(+), 11 deletions(-) rename modules/commands/admin-only-auth.lua => config/command_auth_admin.lua (100%) rename expcore/common_parse.lua => config/command_parse_general.lua (100%) diff --git a/modules/commands/admin-only-auth.lua b/config/command_auth_admin.lua similarity index 100% rename from modules/commands/admin-only-auth.lua rename to config/command_auth_admin.lua diff --git a/expcore/common_parse.lua b/config/command_parse_general.lua similarity index 100% rename from expcore/common_parse.lua rename to config/command_parse_general.lua diff --git a/modules/commands/admin-chat.lua b/modules/commands/admin-chat.lua index a178349cfb..00ae2d5d85 100644 --- a/modules/commands/admin-chat.lua +++ b/modules/commands/admin-chat.lua @@ -1,6 +1,6 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' -require 'modules.commands.admin-only-auth' +require 'config.command_parse_general' +require 'config.command_auth_admin' Commands.new_command('admin-chat','Sends a message in chat that only admins can see.') :add_param('message',false) -- the message to send in the admin chat diff --git a/modules/commands/cheat-mode.lua b/modules/commands/cheat-mode.lua index c2a1b206b1..71ea9d1570 100644 --- a/modules/commands/cheat-mode.lua +++ b/modules/commands/cheat-mode.lua @@ -1,6 +1,6 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' -require 'modules.commands.admin-only-auth' +require 'config.command_parse_general' +require 'config.command_auth_admin' Commands.new_command('toggle-cheat-mode','Toggles cheat mode for your player, or another player.') :add_param('player',true,'player') -- player to toggle chest mode of, can be nil for self diff --git a/modules/commands/help.lua b/modules/commands/help.lua index 432560f682..07e1c572d4 100644 --- a/modules/commands/help.lua +++ b/modules/commands/help.lua @@ -1,6 +1,6 @@ local Commands = require 'expcore.commands' local Global = require 'utils.global' -require 'expcore.common_parse' +require 'config.command_parse_general' local results_per_page = 5 diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 43306a41e4..b04e35e4b1 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -1,7 +1,7 @@ local Commands = require 'expcore.commands' local Global = require 'utils.global' local Common = require 'expcore.common' -require 'modules.commands.admin-only-auth' +require 'config.command_auth_admin' -- modules that are loaded into the interface env to be accessed local interface_modules = { diff --git a/modules/commands/kill.lua b/modules/commands/kill.lua index a5c990c313..7ecb5c9016 100644 --- a/modules/commands/kill.lua +++ b/modules/commands/kill.lua @@ -1,6 +1,6 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' -require 'modules.commands.admin-only-auth' +require 'config.command_parse_general' +require 'config.command_auth_admin' Commands.new_command('kill','Kills yourself or another player.') :add_param('player',true,'player-alive') -- the player to kill, must be alive to be valid diff --git a/modules/commands/tag.lua b/modules/commands/tag.lua index f8f8d1d9d9..97ae6c2172 100644 --- a/modules/commands/tag.lua +++ b/modules/commands/tag.lua @@ -1,5 +1,5 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' +require 'config.command_parse_general' Commands.new_command('tag','Sets your player tag.') :add_param('tag',false,'string-max-length',20) -- new tag for your player max 20 char diff --git a/modules/commands/teleport.lua b/modules/commands/teleport.lua index bcb64ee840..05f33a76bf 100644 --- a/modules/commands/teleport.lua +++ b/modules/commands/teleport.lua @@ -1,6 +1,6 @@ local Commands = require 'expcore.commands' -require 'expcore.common_parse' -require 'modules.commands.admin-only-auth' +require 'config.command_parse_general' +require 'config.command_auth_admin' local function teleport(from_player,to_player) local surface = to_player.surface From 7c40d82cb2c09bed2814c23885eb4cdbd774b63a Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 18:07:47 +0000 Subject: [PATCH 2/8] Added main File --- config/permission_groups.lua | 1 + expcore/permission_groups.lua | 164 ++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 config/permission_groups.lua create mode 100644 expcore/permission_groups.lua diff --git a/config/permission_groups.lua b/config/permission_groups.lua new file mode 100644 index 0000000000..e75df02d0e --- /dev/null +++ b/config/permission_groups.lua @@ -0,0 +1 @@ +local Permission_Groups = require 'expcore.permission_groups' \ No newline at end of file diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua new file mode 100644 index 0000000000..e1d4800133 --- /dev/null +++ b/expcore/permission_groups.lua @@ -0,0 +1,164 @@ +local Game = require 'utils.game' +local Event = require 'utils.event' + +local Permissions_Groups = { + groups={}, -- store for the different groups that are created + _prototype={} -- stores functions that are used on group instances +} + +function Permissions_Groups.new_group(name) + local group = setmetatable({ + name=name, + actions={}, + allow_all_actions=true + },{ + __index= Permissions_Groups._prototype + }) + Permissions_Groups.groups[name] = group + return group +end + +function Permissions_Groups.get_group_by_name(name) + return Permissions_Groups.groups[name] +end + +function Permissions_Groups.get_group_from_player(player) + player = Game.get_player_from_any(player) + local group = player.permission_group + if group then + return Permissions_Groups.groups[group.name] + end +end + +function Permissions_Groups.reload_permissions() + for _,group in pairs(Permissions_Groups.groups) do + group:create() + end +end + +function Permissions_Groups.lockdown_permissions(exempt) + if type(exempt) ~= 'table' then + exempt = {exempt} + end + for _,group in pairs(exempt) do + if type(group) == 'string' then + exempt[group:lower()] = true + elseif type(group) == 'table' then + exempt[group.name:lower()] = true + end + end + for _,group in pairs(game.permissions.groups) do + if not exempt(group.name:lower()) then + for _,action in pairs(defines.input_action) do + group.set_allows_action(action,false) + end + end + end +end + +function Permissions_Groups.set_player_group(player,group) + player = Game.get_player_from_any(player) + local group = Permissions_Groups.get_group_by_name(group) + group:add_player(player) +end + +function Permissions_Groups._prototype:set_action(action,state) + self.actions[action] = state + return self +end + +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) + end + return self +end + +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) + end + return self +end + +function Permissions_Groups._prototype:allow_all() + self.allow_all_actions = true + return self +end + +function Permissions_Groups._prototype:disallow_all() + self.allow_all_actions = false + return self +end + +function Permissions_Groups._prototype:is_allowed(action) + local state = self.actions[action] + if state == nil then + state = self.allow_all_actions + end + return state +end + +function Permissions_Groups._prototype:get_raw() + return game.permissions.get_group(self.name) +end + +function Permissions_Groups._prototype:create() + local group = self:get_raw() + 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)) + end + return group +end + +function Permissions_Groups._prototype:add_player(player) + player = Game.get_player_from_any(player) + local group = self:get_raw() + group.add_player(player) +end + +function Permissions_Groups._prototype:remove_player(player) + player = Game.get_player_from_any(player) + local group = self:get_raw() + group.remove_player(player) +end + +function Permissions_Groups._prototype:get_players(online) + local players = {} + local group = self:get_raw() + if group then + if online == nil then + return group.players + else + for _,player in pairs(group.players) do + if player.connected == online then + table.insert(player,player) + end + end + end + end + return players +end + +function Permissions_Groups._prototype:print(message) + local players = self:get_players(true) + for _,player in pairs(players) do + player.print(message) + end + return #players +end + +Event.on_init(function() + Permissions_Groups.reload_permissions() +end) + +return Permissions_Groups \ No newline at end of file From 2d634303a968c7e752e1822aa32ec7afe6793d8b Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 18:30:50 +0000 Subject: [PATCH 3/8] Added Config --- config/permission_groups.lua | 96 ++++++++++++++++++++++++++++++++++- control.lua | 1 + expcore/permission_groups.lua | 6 +++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/config/permission_groups.lua b/config/permission_groups.lua index e75df02d0e..0c1bf87d10 100644 --- a/config/permission_groups.lua +++ b/config/permission_groups.lua @@ -1 +1,95 @@ -local Permission_Groups = require 'expcore.permission_groups' \ No newline at end of file +local Permission_Groups = require 'expcore.permission_groups' + +Permission_Groups.new_group('admin') +:allow_all() +:disallow{ + 'add_permission_group', -- admin + 'delete_permission_group', + 'edit_permission_group', + 'import_permissions_string', + 'map_editor_action', + 'toggle_map_editor', + 'change_multiplayer_config', + 'set_heat_interface_mode', + 'set_heat_interface_temperature', + 'set_infinity_container_filter_item', + 'set_infinity_container_remove_unfiltered_items', + 'set_infinity_pipe_filter' +} + +Permission_Groups.new_group('trusted') +:allow_all() +:disallow{ + 'add_permission_group', -- admin + 'delete_permission_group', + 'edit_permission_group', + 'import_permissions_string', + 'map_editor_action', + 'toggle_map_editor', + 'change_multiplayer_config', + 'set_heat_interface_mode', + 'set_heat_interface_temperature', + 'set_infinity_container_filter_item', + 'set_infinity_container_remove_unfiltered_items', + 'set_infinity_pipe_filter', + 'admin_action' -- trusted +} + +Permission_Groups.new_group('standard') +:allow_all() +:disallow{ + 'add_permission_group', -- admin + 'delete_permission_group', + 'edit_permission_group', + 'import_permissions_string', + 'map_editor_action', + 'toggle_map_editor', + 'change_multiplayer_config', + 'set_heat_interface_mode', + 'set_heat_interface_temperature', + 'set_infinity_container_filter_item', + 'set_infinity_container_remove_unfiltered_items', + 'set_infinity_pipe_filter', + 'admin_action', -- trusted + 'change_programmable_speaker_alert_parameters', -- standard + 'drop_item', + 'reset_assembling_machine', + 'set_auto_launch_rocket', + 'cancel_research' +} + +Permission_Groups.new_group('guest') +:allow_all() +:disallow{ + 'add_permission_group', -- admin + 'delete_permission_group', + 'edit_permission_group', + 'import_permissions_string', + 'map_editor_action', + 'toggle_map_editor', + 'change_multiplayer_config', + 'set_heat_interface_mode', + 'set_heat_interface_temperature', + 'set_infinity_container_filter_item', + 'set_infinity_container_remove_unfiltered_items', + 'set_infinity_pipe_filter', + 'admin_action', -- trusted + 'change_programmable_speaker_alert_parameters', -- standard + 'drop_item', + 'reset_assembling_machine', + 'set_auto_launch_rocket', + 'cancel_research', + 'change_programmable_speaker_parameters', -- guest + 'change_train_stop_station', + 'deconstruct', + 'remove_cables', + 'remove_train_station', + 'reset_assembling_machine', + 'rotate_entity', + 'use_artillery_remote', + 'launch_rocket' +} + +Permission_Groups.new_group('restricted') +:disallow_all() +:allow('write_to_console') \ No newline at end of file diff --git a/control.lua b/control.lua index fa24c3113f..aff18c15f1 100644 --- a/control.lua +++ b/control.lua @@ -24,6 +24,7 @@ local files = { 'modules.commands.cheat-mode', 'modules.commands.interface', 'modules.commands.help', + 'config.permission_groups' } -- Loads all files in array above and logs progress diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua index e1d4800133..739f955b72 100644 --- a/expcore/permission_groups.lua +++ b/expcore/permission_groups.lua @@ -63,6 +63,9 @@ function Permissions_Groups.set_player_group(player,group) end function Permissions_Groups._prototype:set_action(action,state) + if type(action) == 'string' then + action = defines.input_action[action] + end self.actions[action] = state return self end @@ -98,6 +101,9 @@ function Permissions_Groups._prototype:disallow_all() end function Permissions_Groups._prototype:is_allowed(action) + if type(action) == 'string' then + action = defines.input_action[action] + end local state = self.actions[action] if state == nil then state = self.allow_all_actions From 9f0ee951937b119d0fa773197bdfeb5b03a32c72 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 18:46:48 +0000 Subject: [PATCH 4/8] Added Assign Events --- config/permission_groups.lua | 49 +++++++++++++++++++++++++++++----- expcore/permission_groups.lua | 2 +- modules/commands/interface.lua | 3 ++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/config/permission_groups.lua b/config/permission_groups.lua index 0c1bf87d10..60e60ae5c4 100644 --- a/config/permission_groups.lua +++ b/config/permission_groups.lua @@ -1,11 +1,13 @@ +local Event = require 'utils.event' +local Game = require 'utils.game' local Permission_Groups = require 'expcore.permission_groups' -Permission_Groups.new_group('admin') +Permission_Groups.new_group('Admin') :allow_all() :disallow{ 'add_permission_group', -- admin 'delete_permission_group', - 'edit_permission_group', + --'edit_permission_group', -- removed for admin till role script added 'import_permissions_string', 'map_editor_action', 'toggle_map_editor', @@ -17,7 +19,7 @@ Permission_Groups.new_group('admin') 'set_infinity_pipe_filter' } -Permission_Groups.new_group('trusted') +Permission_Groups.new_group('Trusted') :allow_all() :disallow{ 'add_permission_group', -- admin @@ -35,7 +37,7 @@ Permission_Groups.new_group('trusted') 'admin_action' -- trusted } -Permission_Groups.new_group('standard') +Permission_Groups.new_group('Standard') :allow_all() :disallow{ 'add_permission_group', -- admin @@ -58,7 +60,7 @@ Permission_Groups.new_group('standard') 'cancel_research' } -Permission_Groups.new_group('guest') +Permission_Groups.new_group('Guest') :allow_all() :disallow{ 'add_permission_group', -- admin @@ -90,6 +92,39 @@ Permission_Groups.new_group('guest') 'launch_rocket' } -Permission_Groups.new_group('restricted') +Permission_Groups.new_group('Restricted') :disallow_all() -:allow('write_to_console') \ No newline at end of file +:allow('write_to_console') + +--- These events are used until a role system is added to make it easier for our admins + +local trusted_time = 60*60*60*10 -- 10 hour +local standard_time = 60*60*60*3 -- 3 hour +local function assign_group(player) + if player.admin then + Permission_Groups.set_player_group(player,'Admin') + elseif player.online_time > trusted_time then + Permission_Groups.set_player_group(player,'Trusted') + elseif player.online_time > standard_time then + Permission_Groups.set_player_group(player,'Standard') + else + Permission_Groups.set_player_group(player,'Guest') + end +end + +Event.add('on_player_joined_game',function(event) + local player = Game.get_player_by_index(event.player_index) + assign_group(player) +end) + +Event.add({'on_player_promoted','on_player_demoted'},function(event) + local player = Game.get_player_by_index(event.player_index) + assign_group(player) +end) + +local check_interval = 60*60*15 -- 15 minutes +Event.on_nth_tick(check_interval,function(event) + for _,player in pairs(game.connected_players) do + assign_group(player) + end +end) \ No newline at end of file diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua index 739f955b72..89563628a6 100644 --- a/expcore/permission_groups.lua +++ b/expcore/permission_groups.lua @@ -48,7 +48,7 @@ function Permissions_Groups.lockdown_permissions(exempt) end end for _,group in pairs(game.permissions.groups) do - if not exempt(group.name:lower()) then + if not exempt[group.name:lower()] then for _,action in pairs(defines.input_action) do group.set_allows_action(action,false) end diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index b04e35e4b1..d4354721cf 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -7,7 +7,8 @@ require 'config.command_auth_admin' local interface_modules = { ['Game']='utils.game', ['Commands']=Commands, - ['output']=Common.player_return + ['output']=Common.player_return, + ['Group']='expcore.permission_groups' } -- loads all the modules given in the above table From 07fef40bb3390395ea82773662dc9e80ffad87c2 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 18:54:07 +0000 Subject: [PATCH 5/8] Fixed handler events --- config/permission_groups.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/config/permission_groups.lua b/config/permission_groups.lua index 60e60ae5c4..d171bdcc0f 100644 --- a/config/permission_groups.lua +++ b/config/permission_groups.lua @@ -112,12 +112,17 @@ local function assign_group(player) end end -Event.add('on_player_joined_game',function(event) +Event.add(defines.events.on_player_joined_game,function(event) local player = Game.get_player_by_index(event.player_index) assign_group(player) end) -Event.add({'on_player_promoted','on_player_demoted'},function(event) +Event.add(defines.events.on_player_promoted,function(event) + local player = Game.get_player_by_index(event.player_index) + assign_group(player) +end) + +Event.add(defines.events.on_player_demoted,function(event) local player = Game.get_player_by_index(event.player_index) assign_group(player) end) From 30c523f2999b413a4c84bde87066a1153b672a9c Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 14 Mar 2019 19:28:06 +0000 Subject: [PATCH 6/8] Added Comments --- expcore/permission_groups.lua | 91 ++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua index 89563628a6..8e62e56a66 100644 --- a/expcore/permission_groups.lua +++ b/expcore/permission_groups.lua @@ -1,3 +1,32 @@ +--- Permission group making for factorio so you never have to make one by hand again +-- @author Cooldude2606 +-- @module Permissions_Groups +--[[ +>>>>Example Group (Allow All) + + -- here we will create an admin group however we do not want them to use the map editor or mess with the permission groups + Permission_Groups.new_group('Admin') -- this defines a new group called "Admin" + :allow_all() -- this makes the default to allow any input action unless set other wise + :disallow{ -- here we disallow the input action we dont want them to use + 'add_permission_group', + 'delete_permission_group', + 'import_permissions_string', + 'map_editor_action', + 'toggle_map_editor' + } + +>>>>Example Group (Disallow All) + + -- here we will create a group that cant do anything but talk in chat + Permission_Groups.new_group('Restricted') -- this defines a new group called "Restricted" + :disallow_all() -- this makes the default to disallow any input action unless set other wise + :allow('write_to_console') -- here we allow them to chat, {} can be used here if we had more than one action + +>>>>Functions List (see function for more detail): + +]] + + local Game = require 'utils.game' local Event = require 'utils.event' @@ -6,6 +35,9 @@ local Permissions_Groups = { _prototype={} -- stores functions that are used on group instances } +--- Defines a new permission group that can have it actions set in the config +-- @tparam name string the name of the new group +-- @treturn Permissions_Groups._prototype the new group made with function to allow and disallow actions function Permissions_Groups.new_group(name) local group = setmetatable({ name=name, @@ -18,10 +50,16 @@ function Permissions_Groups.new_group(name) return group end +--- Returns the group with the given name, case sensitive +-- @tparam name string the name of the group to get +-- @treturn ?Permissions_Groups._prototype|nil the group with that name or nil if non found function Permissions_Groups.get_group_by_name(name) return Permissions_Groups.groups[name] end +--- Returns the group that a player is in +-- @tparam LuaPlayer the player to get the group of can be LuaPlayer name index etc +-- @treturn ?Permissions_Groups._prototype|nil the group with that player or nil if non found function Permissions_Groups.get_group_from_player(player) player = Game.get_player_from_any(player) local group = player.permission_group @@ -30,13 +68,18 @@ function Permissions_Groups.get_group_from_player(player) end end +--- Reloads/creates all permission groups and sets them to they configured state function Permissions_Groups.reload_permissions() for _,group in pairs(Permissions_Groups.groups) do group:create() end end +--- Removes all permissions from every permission group except for "Default" and any passed as exempt +-- @tparam string|Array any groups that you want to be except, "Default" is always exempt +-- @treturn number the number of groups that had they permissions removed function Permissions_Groups.lockdown_permissions(exempt) + local count = 0 if type(exempt) ~= 'table' then exempt = {exempt} end @@ -48,20 +91,32 @@ function Permissions_Groups.lockdown_permissions(exempt) end end for _,group in pairs(game.permissions.groups) do - if not exempt[group.name:lower()] then + if not exempt[group.name:lower()] and not group.name == 'Default' then + count = count +1 for _,action in pairs(defines.input_action) do group.set_allows_action(action,false) end end end + return count end +--- Sets a player's group to the one given, a player can only have one group at a time +-- @tparam LuaPlayer the player to effect can be LuaPlayer name index etc +-- @tparam string the name of the group to give to the player +-- @treturn boolean true if the player was added successfully, false other wise function Permissions_Groups.set_player_group(player,group) player = Game.get_player_from_any(player) local group = Permissions_Groups.get_group_by_name(group) + if not group or not player then return false end group:add_player(player) + return true end +--- Sets the allow state of an action for this group, used internally but is safe to use else where +-- @tparam action ?string|defines.input_action the action that you want to set the state of +-- @tparam state boolean the state that you want to set it to, true = allow, false = disallow +-- @treturn Permissions_Groups._prototype returns self so function can be chained function Permissions_Groups._prototype:set_action(action,state) if type(action) == 'string' then action = defines.input_action[action] @@ -70,6 +125,9 @@ function Permissions_Groups._prototype:set_action(action,state) return self end +--- Sets an action or actions to be allowed for this group even with disallow_all triggered, Do not use in runtime +-- @tparam string|Array the action or actions that you want to allow for this group +-- @treturn Permissions_Groups._prototype returns self so function can be chained function Permissions_Groups._prototype:allow(actions) if type(actions) ~= 'table' then actions = {actions} @@ -80,6 +138,9 @@ function Permissions_Groups._prototype:allow(actions) return self end +--- Sets an action or actions to be disallowed for this group even with allow_all triggered, Do not use in runtime +-- @tparam string|Array the action or actions that you want to disallow for this group +-- @treturn Permissions_Groups._prototype returns self so function can be chained function Permissions_Groups._prototype:disallow(actions) if type(actions) ~= 'table' then actions = {actions} @@ -90,16 +151,23 @@ function Permissions_Groups._prototype:disallow(actions) return self end +--- Sets the default state for any actions not given to be allowed, useful with :disallow +-- @treturn Permissions_Groups._prototype returns self so function can be chained function Permissions_Groups._prototype:allow_all() self.allow_all_actions = true return self end +--- Sets the default state for any action not given to be disallowed, useful with :allow +-- @treturn Permissions_Groups._prototype returns self so function can be chained function Permissions_Groups._prototype:disallow_all() self.allow_all_actions = false return self end +--- Returns if an input action is allowed for this group +-- @tparam action ?string|defines.input_action the action that you want to test for +-- @treturn boolean true if the group is allowed the action, false other wise function Permissions_Groups._prototype:is_allowed(action) if type(action) == 'string' then action = defines.input_action[action] @@ -111,10 +179,14 @@ function Permissions_Groups._prototype:is_allowed(action) return state end +--- Returns the LuaPermissionGroup that was created with this group object, used internally +-- @treturn LuaPermissionGroup the raw lua permission group function Permissions_Groups._prototype:get_raw() return game.permissions.get_group(self.name) end +--- Creates or updates the permission group with the configured actions, used internally +-- @treturn LuaPermissionGroup the permission group that was created function Permissions_Groups._prototype:create() local group = self:get_raw() if not group then @@ -126,18 +198,31 @@ function Permissions_Groups._prototype:create() return group end +--- Adds a player to this group +-- @tparam player LuaPlayer the player you want to add to this group can be LuaPlayer name or index etc +-- @treturn boolean true if the player was added successfully, false other wise function Permissions_Groups._prototype:add_player(player) player = Game.get_player_from_any(player) local group = self:get_raw() + if not group or not player then return false end group.add_player(player) + return true end +--- Removes a player from this group +-- @tparam player LuaPlayer the player you want to remove from this group can be LuaPlayer name or index etc +-- @treturn boolean true if the player was removed successfully, false other wise function Permissions_Groups._prototype:remove_player(player) player = Game.get_player_from_any(player) local group = self:get_raw() + if not group or not player then return false end group.remove_player(player) + return true end +--- Returns all player that are in this group with the option to filter to online/offline only +-- @tparam[opt] online boolean if nil returns all players, if true online players only, if false returns online players only +-- @treturn table a table of players that are in this group; filtered if online param is given function Permissions_Groups._prototype:get_players(online) local players = {} local group = self:get_raw() @@ -155,6 +240,9 @@ function Permissions_Groups._prototype:get_players(online) return players end +--- Prints a message to every player in this group +-- @tparam message string the message that you want to send to the players +-- @treturn number the number of players that received the message function Permissions_Groups._prototype:print(message) local players = self:get_players(true) for _,player in pairs(players) do @@ -163,6 +251,7 @@ function Permissions_Groups._prototype:print(message) return #players end +-- when the game starts it will make the permission groups Event.on_init(function() Permissions_Groups.reload_permissions() end) From d082e724414259de6c7ec672837c31ca805d331c Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Fri, 15 Mar 2019 15:27:59 +0000 Subject: [PATCH 7/8] Added to common and added more comments to groups --- expcore/common.lua | 59 +++++++++++++++++++++++++++++++++++ expcore/permission_groups.lua | 24 ++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/expcore/common.lua b/expcore/common.lua index 23d3678d09..2067407333 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -13,6 +13,44 @@ function Public.type_check(value,test_type) return test_type and value and type(value) == test_type or not test_type and not value or false end +--- Raises an error if invalid type is given +-- @tparam value any the value that you want to test the type of +-- @tparam test_type string the type that the value should be +-- @tparam error_message string the error message that is returned +-- @tparam level number the level to call the error on (level = 1 means the caller) +-- @treturn boolean true if no error was called +function Public.type_check_error(value,test_type,error_message,level) + level = level and level+1 or 2 + return Public.test_type(value,test_type) or error(error_message,level) +end + +--- Raises an error with type error with a consistent error message +-- @tparam value any the value that you want to test the type of +-- @tparam test_type string the type that the value should be +-- @tparam param_name string the name of the param +-- @tparam param_number number the number param it is +-- @treturn boolean true if no error was raised +function Public.param_check(value,test_type,param_name,param_number) + if not Public.test_type(value,test_type) then + local function_name = debug.getinfo(2,'n').name or '' + local error_message = string.format('Invalid param #%2d given to %s; %s is not of type %s',param_number,function_name,param_name,test_type) + return error(error_message,3) + end + return true +end + +--- Extracts certain keys from a table +-- @tparam tbl table the table which contains the keys +-- @tparam ... string the names of the keys you want extracted +-- @return the keys in the order given +function Public.extract_keys(tbl,...) + local values = {} + for _,key in pairs({...}) do + table.insert(values,tbl[key]) + end + return unpack(values) +end + --- Will return a value of any type to the player/server console, allows colour for in-game players -- @usage player_return('Hello, World!') -- returns 'Hello, World!' to game.player or server console -- @usage player_return('Hello, World!','green') -- returns 'Hello, World!' to game.player with colour green or server console @@ -54,4 +92,25 @@ function Public.player_return(value,colour,player) else rcon.print(returnAsString) end end +--- Calls a require that will not error if the file is not found +-- @tparam path string the path that you want to require +-- @return the returns from that file or nil, error if not loaded +function Public.opt_require(path) + local success, rtn = pcall(require,path) + if success then return rtn + else return nil,rtn end +end + +--- Calls a require and returns only the keys given, file must return a table +-- @tparam path string the path that you want to require +-- @tparam ... string the name of the keys that you want returned +-- @return the keys in the order given +function Public.ext_require(path,...) + local rtn = require(path) + if type(rtn) ~= 'table' then + error('File did not return a table, can not extract keys.',2) + end + return Public.extract_keys(rtn,...) +end + return Public \ No newline at end of file diff --git a/expcore/permission_groups.lua b/expcore/permission_groups.lua index 8e62e56a66..a237bf8067 100644 --- a/expcore/permission_groups.lua +++ b/expcore/permission_groups.lua @@ -23,7 +23,30 @@ :allow('write_to_console') -- here we allow them to chat, {} can be used here if we had more than one action >>>>Functions List (see function for more detail): + Permissions_Groups.new_group(name) --- Defines a new permission group that can have it actions set in the config + Permissions_Groups.get_group_by_name(name) --- Returns the group with the given name, case sensitive + Permissions_Groups.get_group_from_player(player) --- Returns the group that a player is in + Permissions_Groups.reload_permissions() --- Reloads/creates all permission groups and sets them to they configured state + Permissions_Groups.lockdown_permissions(exempt) --- Removes all permissions from every permission group except for "Default" and any passed as exempt + + Permissions_Groups.set_player_group(player,group) --- Sets a player's group to the one given, a player can only have one group at a time + + Permissions_Groups._prototype:set_action(action,state) --- Sets the allow state of an action for this group, used internally but is safe to use else where + Permissions_Groups._prototype:allow(actions) --- Sets an action or actions to be allowed for this group even with disallow_all triggered, Do not use in runtime + Permissions_Groups._prototype:disallow(actions) --- Sets an action or actions to be disallowed for this group even with allow_all triggered, Do not use in runtime + Permissions_Groups._prototype:allow_all() --- Sets the default state for any actions not given to be allowed, useful with :disallow + Permissions_Groups._prototype:disallow_all() --- Sets the default state for any action not given to be disallowed, useful with :allow + Permissions_Groups._prototype:is_allowed(action) --- Returns if an input action is allowed for this group + + Permissions_Groups._prototype:get_raw() --- Returns the LuaPermissionGroup that was created with this group object, used internally + Permissions_Groups._prototype:create() --- Creates or updates the permission group with the configured actions, used internally + + Permissions_Groups._prototype:add_player(player) --- Adds a player to this group + Permissions_Groups._prototype:remove_player(player) --- Removes a player from this group + Permissions_Groups._prototype:get_players(online) --- Returns all player that are in this group with the option to filter to online/offline only + + Permissions_Groups._prototype:print(message) --- Prints a message to every player in this group ]] @@ -62,6 +85,7 @@ end -- @treturn ?Permissions_Groups._prototype|nil the group with that player or nil if non found function Permissions_Groups.get_group_from_player(player) player = Game.get_player_from_any(player) + if not player then return end local group = player.permission_group if group then return Permissions_Groups.groups[group.name] From 2982fc993e0adab72db190d447a52bd59859e292 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Fri, 15 Mar 2019 15:38:48 +0000 Subject: [PATCH 8/8] Added more comments to common --- expcore/common.lua | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/expcore/common.lua b/expcore/common.lua index 2067407333..1f7f6448bc 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -1,11 +1,28 @@ +--- Adds some commonly used functions used in many modules +-- @author cooldude2606 +-- @module Public +--[[ +>>>>Functions List (see function for more detail): + Public.type_check(value,test_type) --- Compare types faster for faster validation of prams + Public.type_check_error(value,test_type,error_message,level) --- Raises an error if the value is of the incorrect type + Public.param_check(value,test_type,param_name,param_number) --- Raises an error when the value is the incorrect type, uses a consistent error message format + + Public.extract_keys(tbl,...) --- Extracts certain keys from a table + + Public.player_return(value,colour,player) --- Will return a value of any type to the player/server console, allows colour for in-game players + + Public.opt_require(path) --- Calls a require that will not error if the file is not found + Public.ext_require(path,...) --- Calls a require and returns only the keys given, file must return a table +]] + local Colours = require 'resources.color_presets' local Game = require 'utils.game' local Public = {} ---- Compare types faster for faster validation of prams --- @usage is_type('foo','string') -- return true --- @usage is_type('foo') -- return false +--- Compare types faster for faster validation of params +-- @usage type_check('foo','string') -- return true +-- @usage type_check('foo') -- return false -- @param v the value to be tested -- @tparam[opt=nil] string test_type the type to test for if not given then it tests for nil -- @treturn boolean is v of type test_type @@ -13,7 +30,8 @@ function Public.type_check(value,test_type) return test_type and value and type(value) == test_type or not test_type and not value or false end ---- Raises an error if invalid type is given +--- Raises an error if the value is of the wrong type +-- @usage type_check_error('foo','number','Value must be a number') -- will raise error "Value must be a number" -- @tparam value any the value that you want to test the type of -- @tparam test_type string the type that the value should be -- @tparam error_message string the error message that is returned @@ -24,7 +42,8 @@ function Public.type_check_error(value,test_type,error_message,level) return Public.test_type(value,test_type) or error(error_message,level) end ---- Raises an error with type error with a consistent error message +--- Raises an error when the value is the incorrect type, uses a consistent error message format +-- @usage param_check('foo','number','repeat_count',2) -- will raise error "Invalid param #02 given to ; repeat_count is not of type number" -- @tparam value any the value that you want to test the type of -- @tparam test_type string the type that the value should be -- @tparam param_name string the name of the param @@ -40,6 +59,7 @@ function Public.param_check(value,test_type,param_name,param_number) end --- Extracts certain keys from a table +-- @usage local key_three, key_one = extract({key_one='foo',key_two='bar',key_three=true},'key_three','key_one') -- @tparam tbl table the table which contains the keys -- @tparam ... string the names of the keys you want extracted -- @return the keys in the order given @@ -93,6 +113,7 @@ function Public.player_return(value,colour,player) end --- Calls a require that will not error if the file is not found +-- @usage local file = opt_require('file.not.present') -- will not cause any error -- @tparam path string the path that you want to require -- @return the returns from that file or nil, error if not loaded function Public.opt_require(path) @@ -102,6 +123,7 @@ function Public.opt_require(path) end --- Calls a require and returns only the keys given, file must return a table +-- @useage local extract, param_check = ext_require('expcore.common','extract','param_check') -- @tparam path string the path that you want to require -- @tparam ... string the name of the keys that you want returned -- @return the keys in the order given