Update all code styles

This commit is contained in:
Cooldude2606
2024-09-28 01:56:54 +01:00
parent 5e2a62ab27
commit 292c1a1b68
194 changed files with 9817 additions and 9703 deletions

View File

@@ -4,122 +4,124 @@ local Groups = require("modules/exp_groups")
local pending_updates = {}
Global.register(pending_updates, function(tbl)
pending_updates = tbl
pending_updates = tbl
end)
local function on_permission_group_added(event)
if not event.player_index then return end
pending_updates[event.group.name] = {
created = true,
sync_all = true,
tick = event.tick,
permissions = {},
players = {},
}
if not event.player_index then return end
pending_updates[event.group.name] = {
created = true,
sync_all = true,
tick = event.tick,
permissions = {},
players = {},
}
end
local function on_permission_group_deleted(event)
if not event.player_index then return end
local existing = pending_updates[event.group_name]
pending_updates[event.group_name] = nil
if not existing or not existing.created then
clusterio_api.send_json("exp_groups-permission_group_delete", {
group = event.group_name,
})
end
if not event.player_index then return end
local existing = pending_updates[event.group_name]
pending_updates[event.group_name] = nil
if not existing or not existing.created then
clusterio_api.send_json("exp_groups-permission_group_delete", {
group = event.group_name,
})
end
end
local function on_permission_group_edited(event)
if not event.player_index then return end
local pending = pending_updates[event.group.name]
if not pending then
pending = {
tick = event.tick,
permissions = {},
players = {},
}
pending_updates[event.group.name] = pending
end
pending.tick = event.tick
if not event.player_index then return end
local pending = pending_updates[event.group.name]
if not pending then
pending = {
tick = event.tick,
permissions = {},
players = {},
}
pending_updates[event.group.name] = pending
end
pending.tick = event.tick
if event.type == "add-permission" then
if not pending.sync_all then
pending.permissions[event.action] = true
end
elseif event.type == "remove-permission" then
if not pending.sync_all then
pending.permissions[event.action] = false
end
elseif event.type == "enable-all" then
pending.sync_all = true
elseif event.type == "disable-all" then
pending.sync_all = true
elseif event.type == "add-player" then
local player = game.get_player(event.other_player_index) --- @cast player -nil
pending.players[player.name] = true
elseif event.type == "remove-player" then
local player = game.get_player(event.other_player_index) --- @cast player -nil
pending.players[player.name] = nil
elseif event.type == "rename" then
pending.created = true
pending.sync_all = true
local old = pending_updates[event.old_name]
if old then pending.players = old.players end
on_permission_group_deleted{
tick = event.tick, player_index = event.player_index, group_name = event.old_name
}
end
if event.type == "add-permission" then
if not pending.sync_all then
pending.permissions[event.action] = true
end
elseif event.type == "remove-permission" then
if not pending.sync_all then
pending.permissions[event.action] = false
end
elseif event.type == "enable-all" then
pending.sync_all = true
elseif event.type == "disable-all" then
pending.sync_all = true
elseif event.type == "add-player" then
local player = game.get_player(event.other_player_index) --- @cast player -nil
pending.players[player.name] = true
elseif event.type == "remove-player" then
local player = game.get_player(event.other_player_index) --- @cast player -nil
pending.players[player.name] = nil
elseif event.type == "rename" then
pending.created = true
pending.sync_all = true
local old = pending_updates[event.old_name]
if old then pending.players = old.players end
on_permission_group_deleted{
tick = event.tick, player_index = event.player_index, group_name = event.old_name,
}
end
end
local function send_updates()
local tick = game.tick - 600 -- 10 Seconds
local done = {}
for group_name, pending in pairs(pending_updates) do
if pending.tick < tick then
done[group_name] = true
if pending.sync_all then
clusterio_api.send_json("exp_groups-permission_group_create", {
group = group_name, defiantion = Groups.get_group(group_name):to_json(true)
})
else
if next(pending.players) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "assign_players", group = group_name, changes = table.get_keys(pending.players)
})
end
local add, remove = {}, {}
for permission, state in pairs(pending.permissions) do
if state then
add[#add + 1] = permission
else
remove[#remove + 1] = permission
end
end
if next(add) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "add_permissions", group = group_name, changes = Groups.actions_to_names(add)
})
end
if next(remove) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "remove_permissions", group = group_name, changes = Groups.actions_to_names(remove)
})
end
end
end
end
for group_name in pairs(done) do
pending_updates[group_name] = nil
end
local tick = game.tick - 600 -- 10 Seconds
local done = {}
for group_name, pending in pairs(pending_updates) do
if pending.tick < tick then
done[group_name] = true
if pending.sync_all then
clusterio_api.send_json("exp_groups-permission_group_create", {
group = group_name, defiantion = Groups.get_group(group_name):to_json(true),
})
else
if next(pending.players) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "assign_players", group = group_name, changes = table.get_keys(pending.players),
})
end
local add, remove = {}, {}
for permission, state in pairs(pending.permissions) do
if state then
add[#add + 1] = permission
else
remove[#remove + 1] = permission
end
end
if next(add) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "add_permissions", group = group_name, changes = Groups.actions_to_names(add),
})
end
if next(remove) then
clusterio_api.send_json("exp_groups-permission_group_edit", {
type = "remove_permissions", group = group_name, changes = Groups.actions_to_names(remove),
})
end
end
end
end
for group_name in pairs(done) do
pending_updates[group_name] = nil
end
end
return {
events = {
[defines.events.on_permission_group_added] = on_permission_group_added,
[defines.events.on_permission_group_deleted] = on_permission_group_deleted,
[defines.events.on_permission_group_edited] = on_permission_group_edited,
},
on_nth_tick = {
[300] = send_updates,
}
events = {
[defines.events.on_permission_group_added] = on_permission_group_added,
[defines.events.on_permission_group_deleted] = on_permission_group_deleted,
[defines.events.on_permission_group_edited] = on_permission_group_edited,
},
on_nth_tick = {
[300] = send_updates,
},
}

View File

@@ -3,46 +3,47 @@ local Async = require("modules/exp_util/async")
--- Top level module table, contains event handlers and public methods
local Groups = {}
---@class ExpGroup
---@field group LuaPermissionGroup The permission group for this group proxy
--- @class ExpGroup
--- @field group LuaPermissionGroup The permission group for this group proxy
Groups._prototype = {}
Groups._metatable = {
__index = setmetatable(Groups._prototype, {
__index = function(self, key)
return self.group[key]
end
}),
__class = "ExpGroup"
__index = setmetatable(Groups._prototype, {
__index = function(self, key)
return self.group[key]
end,
}),
__class = "ExpGroup",
}
local action_to_name = {}
for name, action in pairs(defines.input_action) do
action_to_name[action] = name
action_to_name[action] = name
end
--- Async Functions
-- These are required to allow bypassing edit_permission_group
--- Add a player to a permission group, requires edit_permission_group
---@param player LuaPlayer Player to add to the group
---@param group LuaPermissionGroup Group to add the player to
--- @param player LuaPlayer Player to add to the group
--- @param group LuaPermissionGroup Group to add the player to
local function add_player_to_group(player, group)
return group.add_player(player)
return group.add_player(player)
end
--- Add a players to a permission group, requires edit_permission_group
---@param players LuaPlayer[] Players to add to the group
---@param group LuaPermissionGroup Group to add the players to
--- @param players LuaPlayer[] Players to add to the group
--- @param group LuaPermissionGroup Group to add the players to
local function add_players_to_group(players, group)
local add_player = group.add_player
if not add_player(players[1]) then
return false
end
for i = 2, #players do
add_player(players[i])
end
return true
local add_player = group.add_player
if not add_player(players[1]) then
return false
end
for i = 2, #players do
add_player(players[i])
end
return true
end
-- Async will bypass edit_permission_group but takes at least one tick
@@ -52,225 +53,230 @@ local add_players_to_group_async = Async.register(add_players_to_group)
--- Static methods for gettings, creating and removing permission groups
--- Gets the permission group proxy with the given name or group ID.
---@param group_name string|uint32 The name or id of the permission group
--- @param group_name string|uint32 The name or id of the permission group
function Groups.get_group(group_name)
local group = game.permissions.get_group(group_name)
if group == nil then return nil end
return setmetatable({
group = group
}, Groups._metatable)
local group = game.permissions.get_group(group_name)
if group == nil then return nil end
return setmetatable({
group = group,
}, Groups._metatable)
end
--- Gets the permission group proxy for a players group
---@param player LuaPlayer The player to get the group of
--- @param player LuaPlayer The player to get the group of
function Groups.get_player_group(player)
local group = player.permission_group
if group == nil then return nil end
return setmetatable({
group = group
}, Groups._metatable)
local group = player.permission_group
if group == nil then return nil end
return setmetatable({
group = group,
}, Groups._metatable)
end
--- Creates a new permission group, requires add_permission_group
---@param group_name string Name of the group to create
--- @param group_name string Name of the group to create
function Groups.new_group(group_name)
local group = game.permissions.get_group(group_name)
assert(group == nil, "Group already exists with name: " .. group_name)
group = game.permissions.create_group(group_name)
assert(group ~= nil, "Requires permission add_permission_group")
return setmetatable({
group = group
}, Groups._metatable)
local group = game.permissions.get_group(group_name)
assert(group == nil, "Group already exists with name: " .. group_name)
group = game.permissions.create_group(group_name)
assert(group ~= nil, "Requires permission add_permission_group")
return setmetatable({
group = group,
}, Groups._metatable)
end
--- Get or create a permisison group, must use the group name not the group id
---@param group_name string Name of the group to create
--- @param group_name string Name of the group to create
function Groups.get_or_create(group_name)
local group = game.permissions.get_group(group_name)
if group then
return setmetatable({
group = group
}, Groups._metatable)
else
group = game.permissions.create_group(group_name)
assert(group ~= nil, "Requires permission add_permission_group")
return setmetatable({
group = group
}, Groups._metatable)
end
local group = game.permissions.get_group(group_name)
if group then
return setmetatable({
group = group,
}, Groups._metatable)
else
group = game.permissions.create_group(group_name)
assert(group ~= nil, "Requires permission add_permission_group")
return setmetatable({
group = group,
}, Groups._metatable)
end
end
--- Destory a permission group, moves all players to default group
---@param group_name string|uint32 The name or id of the permission group to destroy
---@param move_to_name string|uint32? The name or id of the permission group to move players to
--- @param group_name string|uint32 The name or id of the permission group to destroy
--- @param move_to_name string|uint32? The name or id of the permission group to move players to
function Groups.destroy_group(group_name, move_to_name)
local group = game.permissions.get_group(group_name)
if group == nil then return nil end
local group = game.permissions.get_group(group_name)
if group == nil then return nil end
local players = group.players
if #players > 0 then
local move_to = game.permissions.get_group(move_to_name or "Default")
for _, player in ipairs(players) do
player.permission_group = move_to
end
end
local players = group.players
if #players > 0 then
local move_to = game.permissions.get_group(move_to_name or "Default")
for _, player in ipairs(players) do
player.permission_group = move_to
end
end
local success = group.destroy()
assert(success, "Requires permission delete_permission_group")
local success = group.destroy()
assert(success, "Requires permission delete_permission_group")
end
--- Prototype methods for modifying and working with permission groups
--- Add a player to the permission group
---@param player LuaPlayer The player to add to the group
--- @param player LuaPlayer The player to add to the group
function Groups._prototype:add_player(player)
return add_player_to_group(player, self.group) or add_player_to_group_async(player, self.group)
return add_player_to_group(player, self.group) or add_player_to_group_async(player, self.group)
end
--- Add players to the permission group
---@param players LuaPlayer[] The player to add to the group
--- @param players LuaPlayer[] The player to add to the group
function Groups._prototype:add_players(players)
return add_players_to_group(players, self.group) or add_players_to_group_async(players, self.group)
return add_players_to_group(players, self.group) or add_players_to_group_async(players, self.group)
end
--- Move all players to another group
---@param other_group ExpGroup The group to move players to, default is the Default group
--- @param other_group ExpGroup The group to move players to, default is the Default group
function Groups._prototype:move_players(other_group)
return add_players_to_group(self.group.players, other_group.group) or add_players_to_group_async(self.group.players, other_group.group)
return add_players_to_group(self.group.players, other_group.group) or add_players_to_group_async(self.group.players, other_group.group)
end
--- Allow a set of actions for this group
---@param actions defines.input_action[] Actions to allow
--- @param actions defines.input_action[] Actions to allow
function Groups._prototype:allow_actions(actions)
local set_allow = self.group.set_allows_action
for _, action in ipairs(actions) do
set_allow(action, true)
end
return self
local set_allow = self.group.set_allows_action
for _, action in ipairs(actions) do
set_allow(action, true)
end
return self
end
--- Disallow a set of actions for this group
---@param actions defines.input_action[] Actions to disallow
--- @param actions defines.input_action[] Actions to disallow
function Groups._prototype:disallow_actions(actions)
local set_allow = self.group.set_allows_action
for _, action in ipairs(actions) do
set_allow(action, false)
end
return self
local set_allow = self.group.set_allows_action
for _, action in ipairs(actions) do
set_allow(action, false)
end
return self
end
--- Reset the allowed state of all actions
---@param allowed boolean? default true for allow all actions, false to disallow all actions
--- @param allowed boolean? default true for allow all actions, false to disallow all actions
function Groups._prototype:reset(allowed)
local set_allow = self.group.set_allows_action
if allowed == nil then allowed = true end
for _, action in pairs(defines.input_action) do
set_allow(action, allowed)
end
return self
local set_allow = self.group.set_allows_action
if allowed == nil then allowed = true end
for _, action in pairs(defines.input_action) do
set_allow(action, allowed)
end
return self
end
--- Returns if the group is allowed a given action
---@param action string|defines.input_action Actions to test
--- @param action string|defines.input_action Actions to test
function Groups._prototype:allows(action)
if type(action) == "string" then
return self.group.allows_action(defines.input_action[action])
end
return self.group.allows_action(action)
if type(action) == "string" then
return self.group.allows_action(defines.input_action[action])
end
return self.group.allows_action(action)
end
--- Print a message to all players in the group
function Groups._prototype:print(...)
for _, player in ipairs(self.group.players) do
player.print(...)
end
for _, player in ipairs(self.group.players) do
player.print(...)
end
end
--- Static and Prototype methods for use with IPC
--- Convert an array of strings into an array of action names
---@param actions_names string[] An array of action names
--- @param actions_names string[] An array of action names
local function names_to_actions(actions_names)
local actions, invalid, invalid_i = {}, {}, 1
for i, action_name in ipairs(actions_names) do
local action = defines.input_action[action_name]
if action then
actions[i] = action
else
invalid[invalid_i] = i
invalid_i = invalid_i + 1
end
end
local actions, invalid, invalid_i = {}, {}, 1
for i, action_name in ipairs(actions_names) do
local action = defines.input_action[action_name]
if action then
actions[i] = action
else
invalid[invalid_i] = i
invalid_i = invalid_i + 1
end
end
local last = #actions
for _, i in ipairs(invalid) do
actions[i] = actions[last]
last = last - 1
end
local last = #actions
for _, i in ipairs(invalid) do
actions[i] = actions[last]
last = last - 1
end
return actions
return actions
end
--- Get the action names from the action numbers
function Groups.actions_to_names(actions)
local names = {}
for i, action in ipairs(actions) do
names[i] = action_to_name[action]
end
return names
local names = {}
for i, action in ipairs(actions) do
names[i] = action_to_name[action]
end
return names
end
--- Get all input actions that are defined
function Groups.get_actions_json()
local rtn, rtn_i = {}, 1
for name in pairs(defines.input_action) do
rtn[rtn_i] = name
rtn_i = rtn_i + 1
end
return game.table_to_json(rtn)
local rtn, rtn_i = {}, 1
for name in pairs(defines.input_action) do
rtn[rtn_i] = name
rtn_i = rtn_i + 1
end
return game.table_to_json(rtn)
end
--- Convert a json string array into an array of input actions
---@param json string A json string representing a string array of actions
--- @param json string A json string representing a string array of actions
function Groups.json_to_actions(json)
local tbl = game.json_to_table(json)
assert(tbl, "Invalid Json String")
---@cast tbl string[]
return names_to_actions(tbl)
local tbl = game.json_to_table(json)
assert(tbl, "Invalid Json String")
--- @cast tbl string[]
return names_to_actions(tbl)
end
--- Returns the shortest defination of the allowed actions
-- The first value of the return can be passed to :reset
function Groups._prototype:to_json(raw)
local allow, disallow = {}, {}
local allow_i, disallow_i = 1, 1
local allows = self.group.allows_action
for name, action in pairs(defines.input_action) do
if allows(action) then
allow[allow_i] = name
allow_i = allow_i + 1
else
disallow[disallow_i] = name
disallow_i = disallow_i + 1
end
end
local allow, disallow = {}, {}
local allow_i, disallow_i = 1, 1
local allows = self.group.allows_action
for name, action in pairs(defines.input_action) do
if allows(action) then
allow[allow_i] = name
allow_i = allow_i + 1
else
disallow[disallow_i] = name
disallow_i = disallow_i + 1
end
end
if allow_i >= disallow_i then
return raw and {true, disallow} or game.table_to_json{ true, disallow }
end
return raw and {false, allow} or game.table_to_json{ false, allow }
if allow_i >= disallow_i then
return raw and { true, disallow } or game.table_to_json{ true, disallow }
end
return raw and { false, allow } or game.table_to_json{ false, allow }
end
--- Restores this group to the state given in a json string
---@param json string The json string to restore from
--- @param json string The json string to restore from
function Groups._prototype:from_json(json)
local tbl = game.json_to_table(json)
assert(tbl and type(tbl[1]) == "boolean" and type(tbl[2]) == "table", "Invalid Json String")
local tbl = game.json_to_table(json)
assert(tbl and type(tbl[1]) == "boolean" and type(tbl[2]) == "table", "Invalid Json String")
if tbl[1] then
return self:reset(true):disallow_actions(names_to_actions(tbl[2]))
end
return self:reset(false):allow_actions(names_to_actions(tbl[2]))
if tbl[1] then
return self:reset(true):disallow_actions(names_to_actions(tbl[2]))
end
return self:reset(false):allow_actions(names_to_actions(tbl[2]))
end
return Groups