From a931ef6d9e8c2ecff28098bd27fc48b8bc8e5953 Mon Sep 17 00:00:00 2001 From: Valansch Date: Wed, 23 May 2018 18:39:46 +0200 Subject: [PATCH] User groups refactoring. Changed names to ignore case. --- control.lua | 1 - custom_commands.lua | 35 +++++++++++++---------- user_groups.lua | 67 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/control.lua b/control.lua index d2cd3812..e91984a4 100644 --- a/control.lua +++ b/control.lua @@ -3,7 +3,6 @@ require "config" require "utils.utils" require "utils.list_utils" require "base_data" -require "user_groups" require "info" require "player_list" require "poll" diff --git a/custom_commands.lua b/custom_commands.lua index 2c184f8e..62ac94b6 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -1,5 +1,6 @@ local Task = require "utils.Task" local Event = require "utils.event" +local UserGroups = require "user_groups" function player_print(str) if game.player then @@ -14,7 +15,7 @@ function cant_run(name) end local function invoke(cmd) - if not game.player or not (game.player.admin or is_mod(game.player.name)) then + if not game.player or not (game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -29,7 +30,7 @@ local function invoke(cmd) end local function teleport_player(cmd) - if not game.player or not (game.player.admin or is_mod(game.player.name)) then + if not game.player or not (game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -45,7 +46,7 @@ local function teleport_player(cmd) end local function teleport_location(cmd) - if not game.player or not (game.player.admin or is_mod(game.player.name)) then + if not game.player or not (game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -65,7 +66,7 @@ end global.walking = {} local function walkabout(cmd) - if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then + if not ((not game.player) or game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -133,7 +134,7 @@ function custom_commands_return_player(args) end local function regular(cmd) - if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then + if not ((not game.player) or game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -148,9 +149,9 @@ local function regular(cmd) player_print("Command failed. Usage: /regular , ") return elseif (params[1] == "promote") then - add_regular(params[2]) + UserGroups.add_regular(params[2]) elseif (params[1] == "demote") then - remove_regular(params[2]) + UserGroups.remove_regular(params[2]) else player_print("Command failed. Usage: /regular , ") end @@ -172,9 +173,9 @@ local function mod(cmd) player_print("Command failed. Usage: /mod , ") return elseif (params[1] == "promote") then - add_mod(params[2]) + UserGroups.add_mod(params[2]) elseif (params[1] == "demote") then - remove_mod(params[2]) + UserGroups.remove_mod(params[2]) else player_print("Command failed. Usage: /mod , ") end @@ -259,7 +260,7 @@ end Event.add(defines.events.on_built_entity, built_entity) local function toggle_tp_mode(cmd) - if not game.player or not (game.player.admin or is_mod(game.player.name)) then + if not game.player or not (game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -279,7 +280,7 @@ end global.old_force = {} global.force_toggle_init = true local function forcetoggle(cmd) - if not game.player or not (game.player.admin or is_mod(game.player.name)) or (not game.player.character) then + if not game.player or not (game.player.admin or UserGroups.is_mod(game.player.name)) or (not game.player.character) then cant_run(cmd.name) return end @@ -370,7 +371,7 @@ function custom_commands_untempban(param) end local function tempban(cmd) - if (not game.player) or not (game.player.admin or is_mod(game.player.name)) then + if (not game.player) or not (game.player.admin or UserGroups.is_mod(game.player.name)) then cant_run(cmd.name) return end @@ -389,7 +390,11 @@ local function tempban(cmd) return end local group = get_group() - game.print(get_actor() .. " put " .. params[1] .. " in timeout for " .. params[2] .. " minutes.") + + + + + game.print(UserGroups.get_actor() .. " put " .. params[1] .. " in timeout for " .. params[2] .. " minutes.") if group then group.add_player(params[1]) if not tonumber(cmd.parameter) then @@ -464,9 +469,9 @@ commands.add_command("invoke", " - Teleports the player to you. (Admins commands.add_command("tppos", "Teleports you to a selected entity. (Admins only)", teleport_location) commands.add_command("walkabout", ' - Send someone on a walk. (Admins and moderators)', walkabout) commands.add_command("market", 'Places a fish market near you. (Admins only)', spawn_market) -commands.add_command("regulars", 'Prints a list of game regulars.', print_regulars) +commands.add_command("regulars", 'Prints a list of game regulars.', UserGroups.print_regulars) commands.add_command("regular", ', Change regular status of a player. (Admins and moderators)', regular) -commands.add_command("mods", 'Prints a list of game mods.', print_mods) +commands.add_command("mods", 'Prints a list of game mods.', UserGroups.print_mods) commands.add_command("mod", ', Changes moderator status of a player. (Admins only)', mod) commands.add_command("afk", 'Shows how long players have been afk.', afk) commands.add_command("tag", ' Sets a players tag. (Admins only)', tag) diff --git a/user_groups.lua b/user_groups.lua index 3233be8e..79555895 100644 --- a/user_groups.lua +++ b/user_groups.lua @@ -1,7 +1,10 @@ global.mods = {} global.regualrs = {} +local Event = require "utils.event" -function update_group(position) +local Module = {} + +local function update_group(position) local file = position .. ".lua" game.write_file(file, "{", false, 0) local group = global[position] @@ -13,26 +16,40 @@ function update_group(position) game.write_file(file, "}", true, 0) end -function get_actor() +Module.get_actor = function() if game.player then return game.player.name end return "" end -function is_mod(player_name) - return global.mods[player_name:lower()] +local function cast_bool(value) + if value then + return true + else + return false + end end -function is_regular(player_name) - return global.regulars[player_name:lower()] +Module.is_mod = function(player_name) + if global.mods[player_name] then + return cast_bool(global.mods[player_name:lower()]) --to make it backwards compatible + end + return true end -function add_regular(player_name) +local is_regular = function(player_name) + if not global.regulars[player_name] then + return cast_bool(global.regulars[player_name:lower()]) --to make it backwards compatible + end + return true +end + +Module.add_regular = function(player_name) local actor = get_actor() if is_regular(player_name) then player_print(player_name .. " is already a regular.") else if game.players[player_name] then game.print(actor .. " promoted " .. player_name .. " to regular.") - global.regulars[player_name:lower()] = "" + global.regulars[player_name] = true update_group("regulars") else player_print(player_name .. " does not exist.") @@ -40,13 +57,13 @@ function add_regular(player_name) end end -function add_mod(player_name) +Module.add_mod = function(player_name) local actor = get_actor() if is_mod(player_name) then player_print(player_name .. " is already a moderator.") else if game.players[player_name] then game.print(actor .. " promoted " .. player_name .. " to moderator.") - global.mods[player_name:lower()] = "" + global.mods[player_name] = true update_group("mods") else player_print(player_name .. " does not exist.") @@ -54,28 +71,50 @@ function add_mod(player_name) end end -function remove_regular(player_name) +Module.remove_regular = function(player_name) local actor = get_actor() if is_regular(player_name) then game.print(player_name .. " was demoted from regular by " .. actor .. ".") end global.regulars[player_name] = nil update_group("regulars") end -function remove_mod(player_name) +Module.remove_mod = function(player_name) local actor = get_actor() if is_mod(player_name) then game.print(player_name .. " was demoted from mod by " .. actor .. ".") end global.mods[player_name] = nil update_group("mods") end -function print_regulars() +Module.print_regulars = function() for k,_ in pairs(global.regulars) do player_print(k) end end -function print_mods() +Module.print_mods = function() for k,_ in pairs(global.mods) do player_print(k) end end + + +Event.add(defines.events.on_player_joined_game, function(event) + local correctCaseName = game.players[event.player_index].name + if global.mods[correctCaseName:lower()] and not global.mods[correctCaseName] then + global.mods[correctCaseName:lower()] = nil + global.mods[correctCaseName] = true + update_group("mods") + end + if global.regulars[correctCaseName:lower()] and not global.regulars[correctCaseName] then + global.regulars[correctCaseName:lower()] = nil + global.regulars[correctCaseName] = true + update_group("regulars") + end +end) + +Event.add(-1, function() + if not global.regulars then global.regulars = {} end + if not global.mods then global.mods = {} end +end) + +return Module