mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-01-08 00:39:30 +02:00
4af6aa4be7
Fixed an error: player time could be abused by reconnecting.
148 lines
3.3 KiB
Lua
148 lines
3.3 KiB
Lua
-- luacheck: ignore
|
|
|
|
local Global = require 'utils.global'
|
|
local Game = require 'utils.game'
|
|
local Token = require 'utils.token'
|
|
local Server = require 'utils.server'
|
|
local Event = require 'utils.event'
|
|
local table = require 'utils.table'
|
|
local Print = require('utils.print_override')
|
|
local raw_print = Print.raw_print
|
|
|
|
local session_data_set = 'sessions'
|
|
local session = {}
|
|
local online_track = {}
|
|
local set_data = Server.set_data
|
|
local try_get_data = Server.try_get_data
|
|
local concat = table.concat
|
|
local nth_tick = 54001 -- nearest prime to 15 minutes in ticks
|
|
|
|
Global.register({
|
|
session=session,
|
|
online_track=online_track},
|
|
function(tbl)
|
|
session = tbl.session
|
|
online_track = tbl.online_track
|
|
end
|
|
)
|
|
|
|
local Public = {}
|
|
|
|
if _DEBUG then
|
|
printinfo =
|
|
Token.register(
|
|
function(data)
|
|
game.print(serpent.block(data))
|
|
end
|
|
)
|
|
end
|
|
|
|
local fetch =
|
|
Token.register(
|
|
function(data)
|
|
local key = data.key
|
|
local value = data.value
|
|
if value then
|
|
session[key] = value
|
|
else
|
|
session[key] = 0
|
|
end
|
|
end
|
|
)
|
|
|
|
local function tick()
|
|
for _, p in pairs(game.connected_players) do
|
|
Public.send(p, p.name)
|
|
end
|
|
end
|
|
|
|
--- Tries to get data from the webpanel and updates the dataset with values.
|
|
-- @param player name
|
|
function Public.send(player, name)
|
|
if not online_track[name] then
|
|
online_track[name] = 0
|
|
end
|
|
if session[name] then
|
|
local old_time = session[name]
|
|
local new_time = old_time + player.online_time - online_track[name]
|
|
set_data(session_data_set, name, new_time)
|
|
online_track[name] = player.online_time
|
|
end
|
|
end
|
|
|
|
--- Tries to get data from the webpanel and updates the local table with values.
|
|
-- @param data_set player token
|
|
function Public.fetch(key)
|
|
try_get_data(session_data_set, key, fetch)
|
|
end
|
|
|
|
--- Checks if a player exists within the table
|
|
-- @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
|
|
-- @return <table>
|
|
function Public.get_session_table()
|
|
return session
|
|
end
|
|
|
|
--- Returns the table of online_track
|
|
-- @return <table>
|
|
function Public.get_tracker_table()
|
|
return online_track
|
|
end
|
|
|
|
Event.add(
|
|
defines.events.on_player_joined_game,
|
|
function(event)
|
|
local player = game.get_player(event.player_index)
|
|
if not player then
|
|
return
|
|
end
|
|
if game.is_multiplayer() then
|
|
Public.fetch(player.name)
|
|
Public.send(player, player.name)
|
|
else
|
|
session[player.name] = player.online_time
|
|
end
|
|
end
|
|
)
|
|
|
|
Event.add(
|
|
defines.events.on_player_left_game,
|
|
function(event)
|
|
local player = game.get_player(event.player_index)
|
|
if not player then
|
|
return
|
|
end
|
|
if game.is_multiplayer() then
|
|
Public.send(player, player.name)
|
|
end
|
|
end
|
|
)
|
|
|
|
Event.on_nth_tick(nth_tick, tick)
|
|
|
|
Server.on_data_set_changed(
|
|
session_data_set,
|
|
function(data)
|
|
session[data.key] = data.value
|
|
end
|
|
)
|
|
|
|
return Public |