1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

User groups refactoring. Changed names to ignore case.

This commit is contained in:
Valansch 2018-05-23 18:39:46 +02:00
parent 57ca9ddd6b
commit a931ef6d9e
3 changed files with 73 additions and 30 deletions

View File

@ -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"

View File

@ -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 <promote, demote>, <player>")
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 <promote, demote>, <player>")
end
@ -172,9 +173,9 @@ local function mod(cmd)
player_print("Command failed. Usage: /mod <promote, demote>, <player>")
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 <promote, demote>, <player>")
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", "<player> - Teleports the player to you. (Admins
commands.add_command("tppos", "Teleports you to a selected entity. (Admins only)", teleport_location)
commands.add_command("walkabout", '<player> <duration> - 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", '<promote, demote>, <player> 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", '<promote, demote>, <player> 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", '<player> <tag> Sets a players tag. (Admins only)', tag)

View File

@ -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 "<server>"
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