1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00
ComfyFactorio/utils/datastore/session_data.lua

406 lines
12 KiB
Lua
Raw Normal View History

2023-11-11 23:59:17 +01:00
-- created by Gerkiz for ComfyFactorio
local Global = require 'utils.global'
local Core = require 'utils.core'
local Color = require 'utils.color_presets'
local Game = require 'utils.game'
local Token = require 'utils.token'
2020-09-04 16:57:17 +02:00
local Task = require 'utils.task'
local Server = require 'utils.server'
local Event = require 'utils.event'
local table = require 'utils.table'
2020-09-04 16:57:17 +02:00
local set_timeout_in_ticks = Task.set_timeout_in_ticks
2024-05-27 20:30:03 +02:00
local session_data_set = 'sessions'
local session = {}
local online_track = {}
local trusted = {}
2020-07-31 12:04:46 +02:00
local settings = {
2024-05-27 20:30:03 +02:00
trusted_value = 24 * 60 * 3600, -- 24h
required_only_time_to_save_time = 10 * 3600,
nth_tick = 5 * 3600
2020-07-31 12:04:46 +02:00
}
local set_data = Server.set_data
local try_get_data = Server.try_get_data
local try_get_data_and_print = Server.try_get_data_and_print
local concat = table.concat
Global.register(
{
2020-05-13 08:18:14 +02:00
session = session,
online_track = online_track,
2020-07-31 12:04:46 +02:00
trusted = trusted,
settings = settings
},
2024-06-04 23:27:12 +02:00
function (tbl)
session = tbl.session
online_track = tbl.online_track
trusted = tbl.trusted
2020-07-31 12:04:46 +02:00
settings = tbl.settings
end
)
2021-05-18 21:11:28 +02:00
local Public = {
events = {
on_player_removed = Event.generate_event_name('on_player_removed')
}
}
local try_download_data_token =
Token.register(
2024-06-04 23:27:12 +02:00
function (data)
local player_index = data.key
local value = data.value
if value then
session[player_index] = value
if value > settings.trusted_value then
trusted[player_index] = true
end
else
local player = game.get_player(player_index)
if not player or not player.valid then
return
end
session[player_index] = 0
trusted[player_index] = false
-- we don't want to clutter the database with players less than 10 minutes played.
if player.online_time >= settings.required_only_time_to_save_time then
set_data(session_data_set, player_index, session[player_index])
end
2021-05-18 21:11:28 +02:00
end
2019-08-16 14:44:06 +02:00
end
2024-06-04 23:27:12 +02:00
)
2019-08-16 14:44:06 +02:00
local try_upload_data_token =
2019-08-16 14:44:06 +02:00
Token.register(
2024-06-04 23:27:12 +02:00
function (data)
local player_index = data.key
if not player_index then
return
end
local value = data.value
local player = game.get_player(player_index)
if not player or not player.valid then
2021-05-18 21:11:28 +02:00
return
end
2024-06-04 23:27:12 +02:00
if value then
-- we don't want to clutter the database with players less than 10 minutes played.
if player.online_time <= settings.required_only_time_to_save_time then
return
end
2020-07-06 17:28:57 +02:00
2024-06-04 23:27:12 +02:00
local old_time_ingame = value
2020-07-06 17:28:57 +02:00
2024-06-04 23:27:12 +02:00
if not online_track[player_index] then
online_track[player_index] = 0
end
2021-05-18 21:11:28 +02:00
2024-06-04 23:27:12 +02:00
if online_track[player_index] > player.online_time then
-- instance has been reset but scenario owner did not clear the player.
-- so we clear it here and return.
online_track[player_index] = 0
return
end
local new_time = old_time_ingame + player.online_time - online_track[player_index]
if new_time <= 0 then
new_time = old_time_ingame + player.online_time
online_track[player_index] = 0
print('[ERROR] ' .. player_index .. ' had new time set as negative value: ' .. new_time)
return
end
if new_time > settings.trusted_value then
trusted[player_index] = true
end
set_data(session_data_set, player_index, new_time)
session[player_index] = new_time
online_track[player_index] = player.online_time
else
if player.online_time >= settings.required_only_time_to_save_time then
if not session[player_index] then
session[player_index] = 0
end
set_data(session_data_set, player_index, session[player_index])
2021-05-18 21:11:28 +02:00
end
end
end
2024-06-04 23:27:12 +02:00
)
local get_total_playtime_token =
Token.register(
2024-06-04 23:27:12 +02:00
function (data)
if not data then
return
end
if not data.to_print then
return
end
2024-06-04 23:27:12 +02:00
local player_index = data.key
local value = data.value
local to_print = data.to_print
local player = game.get_player(to_print)
if player and player.valid then
if player_index then
if value then
player.play_sound { path = 'utility/scenario_message', volume_modifier = 1 }
player.print('[color=blue]' .. player_index .. '[/color] has a total playtime of: ' .. Core.get_formatted_playtime(value))
else
player.play_sound { path = 'utility/cannot_build', volume_modifier = 1 }
player.print('[color=red]' .. player_index .. '[/color] was not found.')
end
end
end
end
2024-06-04 23:27:12 +02:00
)
2020-09-04 16:57:17 +02:00
local nth_tick_token =
Token.register(
2024-06-04 23:27:12 +02:00
function (data)
local player_name = data.name
Public.try_ul_data(player_name)
end
)
2020-09-04 16:57:17 +02:00
2020-07-06 17:28:57 +02:00
--- Uploads each connected players play time to the dataset
local function upload_data()
2020-09-04 16:57:17 +02:00
local players = game.connected_players
local count = 0
for i = 1, #players do
2021-05-18 21:11:28 +02:00
count = count + 10
2020-09-04 16:57:17 +02:00
local player = players[i]
2024-06-04 23:27:12 +02:00
set_timeout_in_ticks(count, nth_tick_token, { name = player.name })
end
end
2020-07-06 17:28:57 +02:00
--- Prints out game.tick to real hour/minute
2022-08-16 19:31:45 +02:00
---@param ticks int
---@param h int
---@param m int
2020-08-27 13:27:34 +02:00
function Public.format_time(ticks, h, m)
2020-07-06 17:28:57 +02:00
local seconds = ticks / 60
local minutes = math.floor((seconds) / 60)
local hours = math.floor((minutes) / 60)
local min = math.floor(minutes - 60 * hours)
2020-08-27 13:27:34 +02:00
if h and m then
return string.format('%dh:%02dm', hours, minutes, min)
elseif h then
return string.format('%dh', hours)
elseif m then
return string.format('%02dm', minutes, min)
end
2020-07-06 17:28:57 +02:00
end
--- Tries to get data from the webpanel and prints it out to the player
2022-08-16 19:31:45 +02:00
---@param player LuaPlayer
---@param target_player string
function Public.get_and_print_to_player(player, target_player)
if not (player and player.valid) then
return
end
local p = player.print
2022-08-16 19:31:45 +02:00
if not target_player then
p('[ERROR] No player was provided.', Color.fail)
return
end
if not player.admin then
p("[ERROR] You're not admin.", Color.fail)
return
end
local secs = Server.get_current_time()
2023-12-10 20:42:30 +01:00
if secs == nil or secs == false then
return
else
2022-08-16 19:31:45 +02:00
try_get_data_and_print(session_data_set, target_player, player.name, get_total_playtime_token)
end
end
2019-08-16 14:44:06 +02:00
--- Tries to get data from the webpanel and updates the local table with values.
2022-08-16 19:31:45 +02:00
---@param player_index string
function Public.try_dl_data(player_index)
player_index = tostring(player_index)
2019-08-16 14:44:06 +02:00
local secs = Server.get_current_time()
2023-12-10 20:42:30 +01:00
if secs == nil or secs == false then
2023-11-11 23:59:17 +01:00
session[player_index] = game.get_player(player_index).online_time
2019-08-16 14:44:06 +02:00
return
else
2022-08-16 19:31:45 +02:00
try_get_data(session_data_set, player_index, try_download_data_token)
2020-05-13 08:18:14 +02:00
end
end
--- Tries to get data from the webpanel and updates the local table with values.
2022-08-16 19:31:45 +02:00
---@param player_index string
function Public.try_ul_data(player_index)
player_index = tostring(player_index)
2019-08-16 14:44:06 +02:00
local secs = Server.get_current_time()
2023-12-10 20:42:30 +01:00
if secs == nil or secs == false then
2019-08-16 14:44:06 +02:00
return
else
2022-08-16 19:31:45 +02:00
try_get_data(session_data_set, player_index, try_upload_data_token)
2020-05-13 08:18:14 +02:00
end
end
--- Checks if a player exists within the table
2022-08-16 19:31:45 +02:00
---@param player_name string
---@return boolean
function Public.exists(player_name)
return session[player_name] ~= nil
end
--- Prints a list of all players in the player_session table.
function Public.print_sessions()
local result = {}
for k, _ in pairs(session) do
result[#result + 1] = k
end
result = concat(result, ', ')
Game.player_print(result)
end
--- Returns the table of session
2022-08-16 19:31:45 +02:00
---@return table
function Public.get_session_table()
return session
end
--- Returns the table of online_track
2022-08-16 19:31:45 +02:00
---@return table
function Public.get_tracker_table()
return online_track
end
--- Returns the table of trusted
2022-08-16 19:31:45 +02:00
---@return table
function Public.get_trusted_table()
return trusted
end
2021-07-01 21:30:29 +02:00
--- Returns the table of trusted
2024-06-04 23:27:12 +02:00
---@param player? LuaPlayer
2022-08-16 19:31:45 +02:00
---@return table|boolean
2021-07-01 21:30:29 +02:00
function Public.get_trusted_player(player)
2021-07-01 21:34:27 +02:00
return trusted and player and player.valid and trusted[player.name] or false
2021-07-01 21:30:29 +02:00
end
2021-12-05 22:15:49 +01:00
--- Set a player as trusted
2022-08-16 19:31:45 +02:00
---@param player LuaPlayer
function Public.set_trusted_player(player)
if trusted and player and player.valid then
trusted[player.name] = true
end
end
2021-12-05 22:15:49 +01:00
--- Set a player as untrusted
2022-08-16 19:31:45 +02:00
---@param player LuaPlayer
2021-12-05 22:15:49 +01:00
function Public.set_untrusted_player(player)
if trusted and player and player.valid then
trusted[player.name] = nil
end
end
--- Returns the table of session
2024-06-04 23:27:12 +02:00
---@param player? LuaPlayer
2022-08-16 19:31:45 +02:00
---@return table|boolean
function Public.get_session_player(player)
local secs = Server.get_current_time()
if secs == nil or secs == false then
return false
end
return session and player and player.valid and session[player.name] or false
end
2020-08-27 13:27:34 +02:00
--- Returns the table of settings
2022-08-16 19:31:45 +02:00
---@return table
2020-08-27 13:27:34 +02:00
function Public.get_settings_table()
return settings
end
2020-08-26 11:08:12 +02:00
--- Clears a given player from the session tables.
2022-12-22 23:28:43 +01:00
---@param player LuaPlayer?
2020-08-26 11:08:12 +02:00
function Public.clear_player(player)
2021-05-18 21:11:28 +02:00
if player and player.valid then
local name = player.name
local connected = player.connected
if not connected then
2023-11-12 22:39:24 +01:00
session[name] = nil
online_track[name] = nil
trusted[name] = nil
2021-05-18 21:11:28 +02:00
end
2020-08-26 11:08:12 +02:00
end
2021-05-18 21:11:28 +02:00
end
--- Resets a given player from the online_track table.
2022-08-16 19:31:45 +02:00
---@param player LuaPlayer
2021-05-18 21:11:28 +02:00
function Public.reset_online_track(player)
local name = player.name
2023-11-12 22:39:24 +01:00
online_track[name] = 0
2020-08-26 11:08:12 +02:00
end
2021-05-18 21:11:28 +02:00
--- It's vital that we reset the online_track so we
--- don't calculate the values wrong.
Event.add(
Public.events.on_player_removed,
2024-06-04 23:27:12 +02:00
function ()
2021-05-18 21:11:28 +02:00
for name, _ in pairs(online_track) do
local player = game.get_player(name)
Public.clear_player(player)
end
end
)
Event.add(
defines.events.on_player_joined_game,
2024-06-04 23:27:12 +02:00
function (event)
local player = game.get_player(event.player_index)
2020-09-04 23:37:44 +02:00
if not player or not player.valid then
return
end
2020-07-07 23:42:44 +02:00
Public.try_dl_data(player.name)
end
)
Event.add(
defines.events.on_player_left_game,
2024-06-04 23:27:12 +02:00
function (event)
local player = game.get_player(event.player_index)
2020-09-04 23:37:44 +02:00
if not player or not player.valid then
return
end
2020-07-07 23:42:44 +02:00
Public.try_ul_data(player.name)
end
)
2020-07-31 12:04:46 +02:00
Event.on_nth_tick(settings.nth_tick, upload_data)
Server.on_data_set_changed(
session_data_set,
2024-06-04 23:27:12 +02:00
function (data)
2022-08-18 20:55:29 +02:00
local player = game.get_player(data.key)
2021-05-18 21:11:28 +02:00
if player and player.valid then
2022-08-18 21:09:13 +02:00
session[data.key] = data.value
2021-05-18 21:11:28 +02:00
if data.value > settings.trusted_value then
2022-08-18 21:09:13 +02:00
trusted[data.key] = true
2021-05-18 21:11:28 +02:00
else
2022-08-18 21:09:13 +02:00
if trusted[data.key] then
trusted[data.key] = false
2021-05-18 21:11:28 +02:00
end
2020-07-06 15:45:09 +02:00
end
end
end
)
2021-05-18 21:11:28 +02:00
Public.upload_data = upload_data
2020-05-13 08:18:14 +02:00
return Public