1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-08 00:39:30 +02:00
ComfyFactorio/utils/jail_data.lua

402 lines
11 KiB
Lua
Raw Normal View History

2020-06-28 14:40:39 +02:00
local Global = require 'utils.global'
local Session = require 'utils.session_data'
local Game = require 'utils.game'
local Token = require 'utils.token'
local Task = require 'utils.task'
local Server = require 'utils.server'
local Event = require 'utils.event'
2020-07-09 14:26:07 +02:00
local Utils = require 'utils.core'
2020-06-28 14:40:39 +02:00
local jailed_data_set = 'jailed'
local jailed = {}
2020-07-09 14:26:07 +02:00
local votejail = {}
2020-07-11 17:04:53 +02:00
local votefree = {}
2020-07-29 10:23:41 +02:00
local votejail_count = 5
2020-06-28 14:40:39 +02:00
local set_data = Server.set_data
local try_get_data = Server.try_get_data
local concat = table.concat
2020-06-29 15:50:01 +02:00
local valid_commands = {
['free'] = true,
['jail'] = true
}
2020-06-28 14:40:39 +02:00
Global.register(
2020-07-09 14:26:07 +02:00
{
jailed = jailed,
2020-07-11 17:04:53 +02:00
votejail = votejail,
votefree = votefree
2020-07-09 14:26:07 +02:00
},
2020-06-28 14:40:39 +02:00
function(t)
2020-07-09 14:26:07 +02:00
jailed = t.jailed
votejail = t.votejail
2020-07-11 17:04:53 +02:00
votefree = t.votefree
2020-06-28 14:40:39 +02:00
end
)
local Public = {}
2020-07-29 10:23:41 +02:00
local validate_playtime = function(player)
local tracker = Session.get_session_table()
local playtime = player.online_time
if tracker[player.name] then
playtime = player.online_time + tracker[player.name]
end
return playtime
end
local validate_trusted = function(player)
local trusted = Session.get_trusted_table()
local is_trusted = false
if trusted[player.name] then
is_trusted = true
end
return is_trusted
end
2020-07-11 17:04:53 +02:00
local validate_args = function(player, griefer)
if not game.players[griefer] then
Utils.print_to(player, 'Invalid name.')
return false
end
if votejail[player.name] and not player.admin then
Utils.print_to(player, 'You are currently being investigated since you have griefed.')
return false
end
if votefree[player.name] and not player.admin then
Utils.print_to(player, 'You are currently being investigated since you have griefed.')
return false
end
if jailed[player.name] and not player.admin then
Utils.print_to(player, 'You are jailed, you can´t run this command.')
return false
end
2020-07-30 17:47:50 +02:00
if player.name == griefer and not player.admin then
2020-07-11 17:04:53 +02:00
Utils.print_to(player, 'You can´t select yourself.')
return false
end
if game.players[griefer].admin and not player.admin then
Utils.print_to(player, 'You can´t select an admin.')
return false
end
return true
end
local vote_to_jail = function(player, griefer)
if not votejail[griefer] then
2020-07-29 10:23:41 +02:00
votejail[griefer] = {index = 0, actor = player.name}
2020-07-11 17:04:53 +02:00
local message = player.name .. ' has started a vote to jail player ' .. griefer
Utils.print_to(nil, message)
end
if not votejail[griefer][player.name] then
votejail[griefer][player.name] = true
votejail[griefer].index = votejail[griefer].index + 1
Utils.print_to(player, 'You have voted to jail player ' .. griefer .. '.')
if
votejail[griefer].index >= votejail_count or
(votejail[griefer].index == #game.connected_players - 1 and
#game.connected_players > votejail[griefer].index)
then
2020-07-29 10:23:41 +02:00
Public.try_ul_data(griefer, true, votejail[griefer].actor)
2020-06-28 14:40:39 +02:00
end
2020-07-11 17:04:53 +02:00
else
Utils.print_to(player, 'You have already voted to kick ' .. griefer .. '.')
end
end
local vote_to_free = function(player, griefer)
if votejail[griefer] and not votefree[griefer] then
2020-07-29 10:23:41 +02:00
votefree[griefer] = {index = 0, actor = player.name}
2020-07-11 17:04:53 +02:00
local message = player.name .. ' has started a vote to free player ' .. griefer
Utils.print_to(nil, message)
end
if not votefree[griefer][player.name] then
votefree[griefer][player.name] = true
votefree[griefer].index = votefree[griefer].index + 1
Utils.print_to(player, 'You have voted to free player ' .. griefer .. '.')
if
votefree[griefer].index >= votejail_count or
(votefree[griefer].index == #game.connected_players - 1 and
#game.connected_players > votefree[griefer].index)
then
2020-07-29 10:23:41 +02:00
Public.try_ul_data(griefer, false, votefree[griefer].actor)
2020-07-11 17:04:53 +02:00
votejail[griefer] = nil
votefree[griefer] = nil
end
else
Utils.print_to(player, 'You have already voted to free ' .. griefer .. '.')
end
return
end
local jail = function(player, griefer)
player = player or 'script'
if jailed[griefer] then
return false
2020-06-28 14:40:39 +02:00
end
2020-07-09 14:26:07 +02:00
2020-07-11 17:04:53 +02:00
if not game.players[griefer] then
2020-07-09 14:26:07 +02:00
return
end
2020-06-28 14:40:39 +02:00
local permission_group = game.permissions.get_group('prisoner')
if not permission_group then
permission_group = game.permissions.create_group('prisoner')
for action_name, _ in pairs(defines.input_action) do
permission_group.set_allows_action(defines.input_action[action_name], false)
end
permission_group.set_allows_action(defines.input_action.write_to_console, true)
permission_group.set_allows_action(defines.input_action.gui_click, true)
permission_group.set_allows_action(defines.input_action.gui_selection_state_changed, true)
end
2020-07-11 17:04:53 +02:00
permission_group.add_player(griefer)
2020-07-29 19:30:45 +02:00
local message = griefer .. ' has been jailed by ' .. player .. '.'
2020-07-07 16:31:01 +02:00
if
2020-07-11 17:04:53 +02:00
game.players[griefer].character and game.players[griefer].character.valid and
game.players[griefer].character.driving
2020-07-07 16:31:01 +02:00
then
2020-07-11 17:04:53 +02:00
game.players[griefer].character.driving = false
2020-07-07 16:31:01 +02:00
end
2020-07-11 17:04:53 +02:00
jailed[griefer] = {jailed = true, actor = player}
set_data(jailed_data_set, griefer, {jailed = true, actor = player})
2020-07-09 14:26:07 +02:00
Utils.print_to(nil, message)
Utils.action_warning_embed('{Jailed}', message)
2020-07-11 17:04:53 +02:00
Utils.print_admins('Jailed ' .. griefer, player)
2020-07-07 23:42:44 +02:00
2020-07-11 17:04:53 +02:00
game.players[griefer].clear_console()
Utils.print_to(griefer, message)
2020-06-28 14:40:39 +02:00
return true
end
2020-07-11 17:04:53 +02:00
local free = function(player, griefer)
player = player or 'script'
if not jailed[griefer] then
return false
2020-06-28 14:40:39 +02:00
end
2020-07-09 14:26:07 +02:00
2020-07-11 17:04:53 +02:00
if not game.players[griefer] then
2020-07-09 14:26:07 +02:00
return
end
2020-06-28 14:40:39 +02:00
local permission_group = game.permissions.get_group('Default')
2020-07-11 17:04:53 +02:00
permission_group.add_player(griefer)
2020-07-29 19:30:45 +02:00
local message = griefer .. ' was set free from jail by ' .. player .. '.'
2020-07-07 23:42:44 +02:00
2020-07-11 17:04:53 +02:00
jailed[griefer] = nil
2020-07-09 14:26:07 +02:00
2020-07-11 17:04:53 +02:00
set_data(jailed_data_set, griefer, nil)
if votejail[griefer] then
votejail[griefer] = nil
end
if votefree[griefer] then
votefree[griefer] = nil
2020-07-09 14:26:07 +02:00
end
Utils.print_to(nil, message)
Utils.action_warning_embed('{Jailed}', message)
2020-07-11 17:04:53 +02:00
Utils.print_admins('Free´d ' .. griefer .. ' from jail.', player)
2020-06-28 14:40:39 +02:00
return true
end
local is_jailed =
Token.register(
function(data)
local key = data.key
local value = data.value
if value then
2020-07-09 14:26:07 +02:00
if value.jailed then
2020-07-11 17:04:53 +02:00
jail(value.actor, key)
2020-07-09 14:26:07 +02:00
end
2020-06-28 14:40:39 +02:00
end
end
)
local update_jailed =
Token.register(
function(data)
local key = data.key
2020-07-11 17:04:53 +02:00
local value = data.value or false
2020-07-09 14:26:07 +02:00
local player = data.player or 'script'
2020-06-28 14:40:39 +02:00
if value then
2020-07-11 17:04:53 +02:00
jail(player, key)
2020-06-28 14:40:39 +02:00
else
2020-07-11 17:04:53 +02:00
free(player, key)
2020-06-28 14:40:39 +02:00
end
end
)
--- Tries to get data from the webpanel and updates the local table with values.
-- @param data_set player token
function Public.try_dl_data(key)
key = tostring(key)
2020-07-09 14:26:07 +02:00
2020-06-28 14:40:39 +02:00
local secs = Server.get_current_time()
2020-07-09 14:26:07 +02:00
2020-07-07 16:31:01 +02:00
if not secs then
2020-06-28 14:40:39 +02:00
return
else
try_get_data(jailed_data_set, key, is_jailed)
end
end
--- Tries to get data from the webpanel and updates the local table with values.
-- @param data_set player token
function Public.try_ul_data(key, value, player)
2020-07-30 17:47:50 +02:00
if type(key) == 'table' then
key = key.name
end
2020-06-28 14:40:39 +02:00
key = tostring(key)
2020-07-09 14:26:07 +02:00
2020-06-28 14:40:39 +02:00
local data = {
key = key,
value = value,
2020-07-11 17:04:53 +02:00
player = player
2020-06-28 14:40:39 +02:00
}
2020-07-09 14:26:07 +02:00
Task.set_timeout_in_ticks(1, update_jailed, data)
2020-06-28 14:40:39 +02:00
end
--- Checks if a player exists within the table
-- @param player_name <string>
-- @return <boolean>
function Public.exists(player_name)
return jailed[player_name] ~= nil
end
--- Prints a list of all players in the player_jailed table.
function Public.print_jailed()
local result = {}
for k, _ in pairs(jailed) do
result[#result + 1] = k
end
result = concat(result, ', ')
Game.player_print(result)
end
--- Returns the table of jailed
-- @return <table>
function Public.get_jailed_table()
return jailed
end
Event.add(
defines.events.on_player_joined_game,
function(event)
local player = game.get_player(event.player_index)
2020-07-09 14:26:07 +02:00
if not player or not player.valid then
2020-07-07 16:31:01 +02:00
return
end
2020-07-09 14:26:07 +02:00
Public.try_dl_data(player.name)
2020-06-28 14:40:39 +02:00
end
)
Event.add(
defines.events.on_console_command,
function(event)
local cmd = event.command
2020-07-29 10:23:41 +02:00
local five_days = 25920000 -- 5 days
2020-07-30 17:47:50 +02:00
local twenty_days = 103680000 -- 20 days
2020-06-28 14:40:39 +02:00
2020-06-29 15:50:01 +02:00
if not valid_commands[cmd] then
return
end
2020-06-28 14:40:39 +02:00
local griefer = event.parameters
if not griefer then
return
end
if event.player_index then
local player = game.players[event.player_index]
2020-07-29 10:23:41 +02:00
local playtime = validate_playtime(player)
local trusted = validate_trusted(player)
2020-07-09 14:26:07 +02:00
2020-07-11 17:04:53 +02:00
local success = validate_args(player, griefer)
2020-07-09 14:26:07 +02:00
2020-07-11 17:04:53 +02:00
if not success then
return
2020-07-09 14:26:07 +02:00
end
2020-06-28 14:40:39 +02:00
2020-07-11 17:04:53 +02:00
if game.players[griefer] then
griefer = game.players[griefer].name
2020-07-09 14:26:07 +02:00
end
2020-07-30 17:47:50 +02:00
if not trusted and not player.admin or playtime <= five_days and not player.admin then
2020-07-29 10:23:41 +02:00
return Utils.print_to(player, 'You are not trusted enough to run this command.')
end
2020-07-30 17:47:50 +02:00
if trusted and playtime >= five_days and playtime < twenty_days and not player.admin then
2020-07-11 17:04:53 +02:00
if cmd == 'jail' then
vote_to_jail(player, griefer)
return
elseif cmd == 'free' then
vote_to_free(player, griefer)
return
2020-07-09 14:26:07 +02:00
end
2020-07-07 16:31:01 +02:00
end
2020-07-30 17:47:50 +02:00
if player.admin or playtime >= twenty_days then
2020-07-29 10:45:41 +02:00
if cmd == 'jail' then
Public.try_ul_data(griefer, true, player.name)
return
elseif cmd == 'free' then
Public.try_ul_data(griefer, false, player.name)
return
end
2020-06-28 14:40:39 +02:00
end
end
end
)
Server.on_data_set_changed(
jailed_data_set,
function(data)
if data and data.value then
2020-07-09 14:26:07 +02:00
if data.value.jailed and data.value.actor then
2020-07-11 17:04:53 +02:00
jail(data.value.actor, data.key)
2020-07-09 14:26:07 +02:00
end
2020-06-28 14:40:39 +02:00
else
2020-07-11 17:04:53 +02:00
free('script', data.key)
2020-06-28 14:40:39 +02:00
end
end
)
commands.add_command(
'jail',
'Sends the player to gulag!',
function()
return
end
)
commands.add_command(
'free',
'Brings back the player from gulag.',
function()
return
end
)
return Public