2018-11-27 14:32:33 +02:00
|
|
|
--- See documentation at https://github.com/Refactorio/RedMew/pull/469
|
|
|
|
|
2018-11-28 00:12:00 +02:00
|
|
|
local Token = require 'utils.token'
|
2018-11-30 02:18:43 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-01-17 16:00:15 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Game = require 'utils.game'
|
2019-01-27 21:40:07 +02:00
|
|
|
local Timestamp = require 'utils.timestamp'
|
2019-02-16 14:57:10 +02:00
|
|
|
local Print = require('utils.print_override')
|
2018-11-19 22:13:45 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local serialize = serpent.serialize
|
|
|
|
local concat = table.concat
|
|
|
|
local remove = table.remove
|
|
|
|
local tostring = tostring
|
2019-02-16 14:57:10 +02:00
|
|
|
local raw_print = Print.raw_print
|
2019-01-07 14:30:59 +02:00
|
|
|
|
|
|
|
local serialize_options = {sparse = true, compact = true}
|
|
|
|
|
2018-09-21 20:48:12 +02:00
|
|
|
local Public = {}
|
|
|
|
|
2018-11-30 17:57:07 +02:00
|
|
|
local server_time = {secs = nil, tick = 0}
|
2018-11-30 02:18:43 +02:00
|
|
|
|
|
|
|
Global.register(
|
|
|
|
server_time,
|
|
|
|
function(tbl)
|
|
|
|
server_time = tbl
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-09-22 18:41:20 +02:00
|
|
|
local discord_tag = '[DISCORD]'
|
|
|
|
local discord_raw_tag = '[DISCORD-RAW]'
|
2018-10-03 22:03:19 +02:00
|
|
|
local discord_bold_tag = '[DISCORD-BOLD]'
|
2018-09-22 18:41:20 +02:00
|
|
|
local discord_admin_tag = '[DISCORD-ADMIN]'
|
|
|
|
local discord_admin_raw_tag = '[DISCORD-ADMIN-RAW]'
|
|
|
|
local discord_embed_tag = '[DISCORD-EMBED]'
|
|
|
|
local discord_embed_raw_tag = '[DISCORD-EMBED-RAW]'
|
|
|
|
local discord_admin_embed_tag = '[DISCORD-ADMIN-EMBED]'
|
|
|
|
local discord_admin_embed_raw_tag = '[DISCORD-ADMIN-EMBED-RAW]'
|
2018-11-17 20:56:05 +02:00
|
|
|
local start_scenario_tag = '[START-SCENARIO]'
|
2018-11-19 22:13:45 +02:00
|
|
|
local ping_tag = '[PING]'
|
2018-11-24 16:15:14 +02:00
|
|
|
local data_set_tag = '[DATA-SET]'
|
|
|
|
local data_get_tag = '[DATA-GET]'
|
|
|
|
local data_get_all_tag = '[DATA-GET-ALL]'
|
|
|
|
local data_tracked_tag = '[DATA-TRACKED]'
|
2018-11-28 19:56:31 +02:00
|
|
|
local ban_sync_tag = '[BAN-SYNC]'
|
|
|
|
local unbanned_sync_tag = '[UNBANNED-SYNC]'
|
2018-12-01 12:49:14 +02:00
|
|
|
local query_players_tag = '[QUERY-PLAYERS]'
|
2019-01-17 16:00:15 +02:00
|
|
|
local player_join_tag = '[PLAYER-JOIN]'
|
|
|
|
local player_leave_tag = '[PLAYER-LEAVE]'
|
2018-09-21 20:48:12 +02:00
|
|
|
|
2018-10-03 22:03:19 +02:00
|
|
|
Public.raw_print = raw_print
|
|
|
|
|
2018-11-24 16:15:14 +02:00
|
|
|
local data_set_handlers = {}
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- The event id for the on_server_started event.
|
|
|
|
-- The event is raised whenever the server goes from the starting state to the running state.
|
|
|
|
-- It provides a good opportunity to request data from the web server.
|
|
|
|
-- Note that if the server is stopped then started again, this event will be raised again.
|
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
|
|
|
-- local Event = require 'utils.event'
|
|
|
|
--
|
2018-11-26 18:07:24 +02:00
|
|
|
-- Event.add(Server.events.on_server_started,
|
|
|
|
-- function()
|
|
|
|
-- Server.try_get_all_data('regulars', callback)
|
|
|
|
-- end)
|
2018-11-27 16:48:45 +02:00
|
|
|
Public.events = {on_server_started = script.generate_event_name()}
|
2018-11-24 16:15:14 +02:00
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a message to the linked discord channel. The message is sanitized of markdown server side.
|
|
|
|
-- @param message<string> message to send.
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-27 14:32:33 +02:00
|
|
|
-- Server.to_discord('Hello from scenario script!')
|
2018-09-21 20:48:12 +02:00
|
|
|
function Public.to_discord(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_tag .. message)
|
2018-09-21 20:48:12 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a message to the linked discord channel. The message is not sanitized of markdown.
|
|
|
|
-- @param message<string> message to send.
|
2018-09-21 20:48:12 +02:00
|
|
|
function Public.to_discord_raw(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_raw_tag .. message)
|
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a message to the linked discord channel. The message is sanitized of markdown server side, then made bold.
|
|
|
|
-- @param message<string> message to send.
|
2018-10-03 22:03:19 +02:00
|
|
|
function Public.to_discord_bold(message)
|
|
|
|
raw_print(discord_bold_tag .. message)
|
2018-09-21 20:48:12 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a message to the linked admin discord channel. The message is sanitized of markdown server side.
|
|
|
|
-- @param message<string> message to send.
|
2018-09-21 20:48:12 +02:00
|
|
|
function Public.to_admin(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_admin_tag .. message)
|
2018-09-21 20:48:12 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a message to the linked admin discord channel. The message is not sanitized of markdown.
|
|
|
|
-- @param message<string> message to send.
|
2018-09-22 18:41:20 +02:00
|
|
|
function Public.to_admin_raw(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_admin_raw_tag .. message)
|
2018-09-22 18:41:20 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a embed message to the linked discord channel. The message is sanitized of markdown server side.
|
|
|
|
-- @param message<string> the content of the embed.
|
2018-09-21 20:48:12 +02:00
|
|
|
function Public.to_discord_embed(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_embed_tag .. message)
|
2018-09-21 20:48:12 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a embed message to the linked discord channel. The message is not sanitized of markdown.
|
|
|
|
-- @param message<string> the content of the embed.
|
2018-09-22 18:41:20 +02:00
|
|
|
function Public.to_discord_embed_raw(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_embed_raw_tag .. message)
|
2018-09-22 18:41:20 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a embed message to the linked admin discord channel. The message is sanitized of markdown server side.
|
|
|
|
-- @param message<string> the content of the embed.
|
2018-09-22 18:41:20 +02:00
|
|
|
function Public.to_admin_embed(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_admin_embed_tag .. message)
|
2018-09-22 18:41:20 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sends a embed message to the linked admin discord channel. The message is not sanitized of markdown.
|
|
|
|
-- @param message<string> the content of the embed.
|
2018-09-22 18:41:20 +02:00
|
|
|
function Public.to_admin_embed_raw(message)
|
2018-10-03 22:03:19 +02:00
|
|
|
raw_print(discord_admin_embed_raw_tag .. message)
|
2018-09-22 18:41:20 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Stops and saves the factorio server and starts the named scenario.
|
|
|
|
-- @param scenario_name<string> The name of the scenario as appears in the scenario table on http://redmew.com/admin
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-27 14:32:33 +02:00
|
|
|
-- Server.start_scenario('my_scenario_name')
|
2018-11-17 20:56:05 +02:00
|
|
|
function Public.start_scenario(scenario_name)
|
2018-11-19 22:13:45 +02:00
|
|
|
if type(scenario_name) ~= 'string' then
|
|
|
|
game.print('start_scenario - scenario_name ' .. tostring(scenario_name) .. ' must be a string.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local message = start_scenario_tag .. scenario_name
|
|
|
|
|
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
local default_ping_token =
|
|
|
|
Token.register(
|
|
|
|
function(sent_tick)
|
|
|
|
local now = game.tick
|
|
|
|
local diff = now - sent_tick
|
2018-11-17 20:56:05 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat({'Pong in ', diff, ' tick(s) ', 'sent tick: ', sent_tick, ' received tick: ', now})
|
2018-11-19 22:13:45 +02:00
|
|
|
game.print(message)
|
2018-11-17 20:56:05 +02:00
|
|
|
end
|
2018-11-19 22:13:45 +02:00
|
|
|
)
|
2018-11-17 20:56:05 +02:00
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Pings the web server.
|
|
|
|
-- @param func_token<token> The function that is called when the web server replies.
|
|
|
|
-- The function is passed the tick that the ping was sent.
|
2018-11-19 22:13:45 +02:00
|
|
|
function Public.ping(func_token)
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat({ping_tag, func_token or default_ping_token, ' ', game.tick})
|
2018-11-17 20:56:05 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local function double_escape(str)
|
|
|
|
-- Excessive escaping because the data is serialized twice.
|
|
|
|
return str:gsub('\\', '\\\\\\\\'):gsub('"', '\\\\\\"'):gsub('\n', '\\\\n')
|
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Sets the web server's persistent data storage. If you pass nil for the value removes the data.
|
|
|
|
-- Data set this will by synced in with other server if they choose to.
|
|
|
|
-- There can only be one key for each data_set.
|
|
|
|
-- @param data_set<string>
|
|
|
|
-- @param key<string>
|
|
|
|
-- @param value<nil|boolean|number|string|table> Any type that is not a function. set to nil to remove the data.
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-27 14:32:33 +02:00
|
|
|
-- Server.set_data('my data set', 'key 1', 123)
|
|
|
|
-- Server.set_data('my data set', 'key 2', 'abc')
|
|
|
|
-- Server.set_data('my data set', 'key 3', {'some', 'data', ['is_set'] = true})
|
|
|
|
--
|
|
|
|
-- Server.set_data('my data set', 'key 1', nil) -- this will remove 'key 1'
|
|
|
|
-- Server.set_data('my data set', 'key 2', 'def') -- this will change the value for 'key 2' to 'def'
|
2018-11-24 16:15:14 +02:00
|
|
|
function Public.set_data(data_set, key, value)
|
|
|
|
if type(data_set) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('data_set must be a string', 2)
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
if type(key) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('key must be a string', 2)
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
data_set = double_escape(data_set)
|
|
|
|
key = double_escape(key)
|
2018-11-26 13:44:35 +02:00
|
|
|
|
2018-11-24 16:15:14 +02:00
|
|
|
local message
|
|
|
|
local vt = type(value)
|
|
|
|
if vt == 'nil' then
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '"}'})
|
2018-11-24 16:15:14 +02:00
|
|
|
elseif vt == 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
value = double_escape(value)
|
2018-11-25 21:18:51 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"\\"', value, '\\""}'})
|
2018-11-25 13:52:50 +02:00
|
|
|
elseif vt == 'number' then
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"', value, '"}'})
|
2018-11-25 13:52:50 +02:00
|
|
|
elseif vt == 'boolean' then
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"', tostring(value), '"}'})
|
2018-11-24 16:15:14 +02:00
|
|
|
elseif vt == 'function' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('value cannot be a function', 2)
|
2018-11-25 21:18:51 +02:00
|
|
|
else -- table
|
2019-01-07 14:30:59 +02:00
|
|
|
value = serialize(value, serialize_options)
|
2018-11-25 21:18:51 +02:00
|
|
|
|
|
|
|
-- Less escaping than the string case as serpent provides one level of escaping.
|
|
|
|
-- Need to escape single quotes as serpent uses double quotes for strings.
|
|
|
|
value = value:gsub('\\', '\\\\'):gsub("'", "\\'")
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, "\",value:'", value, "'}"})
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
|
2018-11-26 13:44:35 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Gets data from the web server's persistent data storage.
|
|
|
|
-- The callback is passed a table {data_set: string, key: string, value: any}.
|
|
|
|
-- If the value is nil, it means there is no stored data for that data_set key pair.
|
|
|
|
-- @param data_set<string>
|
|
|
|
-- @param key<string>
|
|
|
|
-- @param callback_token<token>
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-28 00:12:00 +02:00
|
|
|
-- local Token = require 'utils.token'
|
2018-11-27 14:32:33 +02:00
|
|
|
--
|
|
|
|
-- local callback =
|
|
|
|
-- Token.register(
|
|
|
|
-- function(data)
|
|
|
|
-- local data_set = data.data_set
|
|
|
|
-- local key = data.key
|
|
|
|
-- local value = data.value -- will be nil if no data
|
|
|
|
--
|
|
|
|
-- game.print(data_set .. ':' .. key .. ':' .. tostring(value))
|
|
|
|
-- end
|
|
|
|
-- )
|
|
|
|
--
|
|
|
|
-- Server.try_get_data('my data set', 'key 1', callback)
|
2018-11-26 13:44:35 +02:00
|
|
|
function Public.try_get_data(data_set, key, callback_token)
|
|
|
|
if type(data_set) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('data_set must be a string', 2)
|
2018-11-26 13:44:35 +02:00
|
|
|
end
|
|
|
|
if type(key) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('key must be a string', 2)
|
2018-11-26 13:44:35 +02:00
|
|
|
end
|
|
|
|
if type(callback_token) ~= 'number' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('callback_token must be a number', 2)
|
2018-11-26 13:44:35 +02:00
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
data_set = double_escape(data_set)
|
|
|
|
key = double_escape(key)
|
2018-11-26 13:44:35 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat {data_get_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '"}'}
|
2018-11-26 13:44:35 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Gets all the data for the data_set from the web server's persistent data storage.
|
|
|
|
-- The callback is passed a table {data_set: string, entries: {dictionary key -> value}}.
|
|
|
|
-- If there is no data stored for the data_set entries will be nil.
|
|
|
|
-- @param data_set<string>
|
|
|
|
-- @param callback_token<token>
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-28 00:12:00 +02:00
|
|
|
-- local Token = require 'utils.token'
|
2018-11-27 14:32:33 +02:00
|
|
|
--
|
|
|
|
-- local callback =
|
|
|
|
-- Token.register(
|
|
|
|
-- function(data)
|
|
|
|
-- local data_set = data.data_set
|
|
|
|
-- local entries = data.entries -- will be nil if no data
|
|
|
|
-- local value2 = entries['key 2']
|
|
|
|
-- local value3 = entries['key 3']
|
|
|
|
-- end
|
|
|
|
-- )
|
|
|
|
--
|
|
|
|
-- Server.try_get_all_data('my data set', callback)
|
2018-11-26 13:44:35 +02:00
|
|
|
function Public.try_get_all_data(data_set, callback_token)
|
|
|
|
if type(data_set) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('data_set must be a string', 2)
|
2018-11-26 13:44:35 +02:00
|
|
|
end
|
|
|
|
if type(callback_token) ~= 'number' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('callback_token must be a number', 2)
|
2018-11-26 13:44:35 +02:00
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
data_set = double_escape(data_set)
|
2018-11-26 13:44:35 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat {data_get_all_tag, callback_token, ' {', 'data_set:"', data_set, '"}'}
|
2018-11-24 16:15:14 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function data_set_changed(data)
|
|
|
|
local handlers = data_set_handlers[data.data_set]
|
|
|
|
if handlers == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if _DEBUG then
|
|
|
|
for _, handler in ipairs(handlers) do
|
|
|
|
local success, err = pcall(handler, data)
|
|
|
|
if not success then
|
|
|
|
log(err)
|
2019-01-07 14:30:59 +02:00
|
|
|
error(err, 2)
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _, handler in ipairs(handlers) do
|
|
|
|
local success, err = pcall(handler, data)
|
|
|
|
if not success then
|
|
|
|
log(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Register a handler to be called when the data_set changes.
|
|
|
|
-- The handler is passed a table {data_set:string, key:string, value:any}
|
|
|
|
-- If value is nil that means the key was removed.
|
|
|
|
-- The handler may be called even if the value hasn't changed. It's up to the implementer
|
|
|
|
-- to determine if the value has changed, or not care.
|
|
|
|
-- To prevent desyncs the same handlers must be registered for all clients. The easiest way to do this
|
|
|
|
-- is in the control stage, i.e before on_init or on_load would be called.
|
|
|
|
-- @param data_set<string>
|
|
|
|
-- @param handler<function>
|
2018-11-27 14:32:33 +02:00
|
|
|
-- @usage
|
2018-11-27 18:54:41 +02:00
|
|
|
-- local Server = require 'features.server'
|
2018-11-27 14:32:33 +02:00
|
|
|
-- Server.on_data_set_changed(
|
|
|
|
-- 'my data set',
|
|
|
|
-- function(data)
|
|
|
|
-- local data_set = data.data_set
|
|
|
|
-- local key = data.key
|
|
|
|
-- local value = data.value -- will be nil if data was removed.
|
|
|
|
-- end
|
|
|
|
-- )
|
2018-11-24 16:15:14 +02:00
|
|
|
function Public.on_data_set_changed(data_set, handler)
|
2019-02-18 08:43:59 +02:00
|
|
|
if _LIFECYCLE == _STAGE.runtime then
|
2019-02-04 23:17:00 +02:00
|
|
|
error('cannot call during runtime', 2)
|
|
|
|
end
|
2018-11-24 16:15:14 +02:00
|
|
|
if type(data_set) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('data_set must be a string', 2)
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local handlers = data_set_handlers[data_set]
|
|
|
|
if handlers == nil then
|
|
|
|
handlers = {handler}
|
|
|
|
data_set_handlers[data_set] = handlers
|
|
|
|
else
|
2019-01-07 14:30:59 +02:00
|
|
|
handlers[#handlers + 1] = handler
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-27 18:54:41 +02:00
|
|
|
--- Called by the web server to notify the client that a data_set has changed.
|
2018-11-24 16:15:14 +02:00
|
|
|
Public.raise_data_set = data_set_changed
|
|
|
|
|
2018-11-26 18:07:24 +02:00
|
|
|
--- Called by the web server to determine which data_sets are being tracked.
|
2018-11-24 16:15:14 +02:00
|
|
|
function Public.get_tracked_data_sets()
|
|
|
|
local message = {data_tracked_tag, '['}
|
|
|
|
|
|
|
|
for k, _ in pairs(data_set_handlers) do
|
2019-01-07 14:30:59 +02:00
|
|
|
k = double_escape(k)
|
2018-11-26 13:44:35 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message_length = #message
|
|
|
|
message[message_length + 1] = '"'
|
|
|
|
message[message_length + 2] = k
|
|
|
|
message[message_length + 3] = '"'
|
|
|
|
message[message_length + 4] = ','
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if message[#message] == ',' then
|
2019-01-07 14:30:59 +02:00
|
|
|
remove(message)
|
2018-11-24 16:15:14 +02:00
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message[#message + 1] = ']'
|
2018-11-24 16:15:14 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat(message)
|
2018-11-24 16:15:14 +02:00
|
|
|
raw_print(message)
|
2018-11-25 13:52:50 +02:00
|
|
|
end
|
|
|
|
|
2018-11-28 19:56:31 +02:00
|
|
|
local function escape(str)
|
|
|
|
return str:gsub('\\', '\\\\'):gsub('"', '\\"')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- If the player exists bans the player.
|
2018-11-28 23:35:07 +02:00
|
|
|
-- Regardless of whether or not the player exists the name is synchronized with other servers
|
2018-11-28 19:56:31 +02:00
|
|
|
-- and stored in the database.
|
|
|
|
-- @param username<string>
|
|
|
|
-- @param reason<string?> defaults to empty string.
|
|
|
|
-- @param admin<string?> admin's name, defaults to '<script>'
|
|
|
|
function Public.ban_sync(username, reason, admin)
|
|
|
|
if type(username) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('username must be a string', 2)
|
2018-11-28 19:56:31 +02:00
|
|
|
end
|
|
|
|
|
2018-11-29 00:04:38 +02:00
|
|
|
if reason == nil then
|
2018-11-28 19:56:31 +02:00
|
|
|
reason = ''
|
2018-11-29 00:04:38 +02:00
|
|
|
elseif type(reason) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('reason must be a string or nil', 2)
|
2018-11-28 19:56:31 +02:00
|
|
|
end
|
|
|
|
|
2018-11-29 00:04:38 +02:00
|
|
|
if admin == nil then
|
2018-11-28 19:56:31 +02:00
|
|
|
admin = '<script>'
|
2018-11-29 00:04:38 +02:00
|
|
|
elseif type(admin) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('admin must be a string or nil', 2)
|
2018-11-28 19:56:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- game.ban_player errors if player not found.
|
|
|
|
-- However we may still want to use this function to ban player names.
|
|
|
|
local player = game.players[username]
|
|
|
|
if player then
|
|
|
|
game.ban_player(player, reason)
|
|
|
|
end
|
|
|
|
|
|
|
|
username = escape(username)
|
|
|
|
reason = escape(reason)
|
|
|
|
admin = escape(admin)
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat({ban_sync_tag, '{username:"', username, '",reason:"', reason, '",admin:"', admin, '"}'})
|
2018-11-28 19:56:31 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- If the player exists bans the player else throws error.
|
|
|
|
-- The ban is not synchronized with other servers or stored in the database.
|
|
|
|
-- @param PlayerSpecification
|
|
|
|
-- @param reason<string?> defaults to empty string.
|
|
|
|
function Public.ban_non_sync(PlayerSpecification, reason)
|
|
|
|
game.ban_player(PlayerSpecification, reason)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- If the player exists unbans the player.
|
2018-11-28 23:35:07 +02:00
|
|
|
-- Regardless of whether or not the player exists the name is synchronized with other servers
|
2018-11-28 19:56:31 +02:00
|
|
|
-- and removed from the database.
|
|
|
|
-- @param username<string>
|
|
|
|
-- @param admin<string?> admin's name, defaults to '<script>'. This name is stored in the logs for who removed the ban.
|
|
|
|
function Public.unban_sync(username, admin)
|
|
|
|
if type(username) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('username must be a string', 2)
|
2018-11-28 19:56:31 +02:00
|
|
|
end
|
|
|
|
|
2018-11-29 00:04:38 +02:00
|
|
|
if admin == nil then
|
2018-11-28 19:56:31 +02:00
|
|
|
admin = '<script>'
|
2018-11-29 00:04:38 +02:00
|
|
|
elseif type(admin) ~= 'string' then
|
2019-01-07 14:30:59 +02:00
|
|
|
error('admin must be a string or nil', 2)
|
2018-11-28 19:56:31 +02:00
|
|
|
end
|
|
|
|
|
2018-11-28 23:56:18 +02:00
|
|
|
-- game.unban_player errors if player not found.
|
2018-11-28 19:56:31 +02:00
|
|
|
-- However we may still want to use this function to unban player names.
|
|
|
|
local player = game.players[username]
|
|
|
|
if player then
|
|
|
|
game.unban_player(username)
|
|
|
|
end
|
|
|
|
|
|
|
|
username = escape(username)
|
|
|
|
admin = escape(admin)
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
local message = concat({unbanned_sync_tag, '{username:"', username, '",admin:"', admin, '"}'})
|
2018-11-28 19:56:31 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- If the player exists unbans the player else throws error.
|
|
|
|
-- The ban is not synchronized with other servers or removed from the database.
|
|
|
|
-- @param PlayerSpecification
|
|
|
|
function Public.unban_non_sync(PlayerSpecification)
|
|
|
|
game.unban_player(PlayerSpecification)
|
|
|
|
end
|
|
|
|
|
2018-11-30 02:18:43 +02:00
|
|
|
--- Called by the web server to set the server time.
|
|
|
|
-- @param secs<number> unix epoch timestamp
|
|
|
|
function Public.set_time(secs)
|
|
|
|
server_time.secs = secs
|
|
|
|
server_time.tick = game.tick
|
|
|
|
end
|
|
|
|
|
2018-11-30 17:57:07 +02:00
|
|
|
--- Gets a table {secs:number?, tick:number} with secs being the unix epoch timestamp
|
2018-12-01 12:53:46 +02:00
|
|
|
-- for the server time and ticks the number of game ticks ago it was set.
|
2018-11-30 02:18:43 +02:00
|
|
|
-- @return table
|
|
|
|
function Public.get_time_data_raw()
|
|
|
|
return server_time
|
|
|
|
end
|
|
|
|
|
2018-11-30 17:57:07 +02:00
|
|
|
--- Gets an estimate of the current server time as a unix epoch timestamp.
|
|
|
|
-- If the server time has not been set returns nil.
|
2018-11-30 02:18:43 +02:00
|
|
|
-- The estimate may be slightly off if within the last minute the game has been paused, saving or overwise,
|
|
|
|
-- or the game speed has been changed.
|
2018-11-30 17:57:07 +02:00
|
|
|
-- @return number?
|
2018-11-30 02:18:43 +02:00
|
|
|
function Public.get_current_time()
|
2018-11-30 17:57:07 +02:00
|
|
|
local secs = server_time.secs
|
|
|
|
if secs == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2018-11-30 02:18:43 +02:00
|
|
|
local diff = game.tick - server_time.tick
|
2018-11-30 17:57:07 +02:00
|
|
|
return math.floor(secs + diff / game.speed / 60)
|
2018-11-30 02:18:43 +02:00
|
|
|
end
|
|
|
|
|
2018-12-01 12:49:14 +02:00
|
|
|
--- Called be the web server to re sync which players are online.
|
|
|
|
function Public.query_online_players()
|
|
|
|
local message = {query_players_tag, '['}
|
|
|
|
|
|
|
|
for _, p in ipairs(game.connected_players) do
|
2019-01-07 14:30:59 +02:00
|
|
|
message[#message + 1] = '"'
|
2018-12-01 12:49:14 +02:00
|
|
|
local name = escape(p.name)
|
2019-01-07 14:30:59 +02:00
|
|
|
message[#message + 1] = name
|
|
|
|
message[#message + 1] = '",'
|
2018-12-01 12:49:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if message[#message] == '",' then
|
|
|
|
message[#message] = '"'
|
|
|
|
end
|
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message[#message + 1] = ']'
|
2018-12-01 12:49:14 +02:00
|
|
|
|
2019-01-07 14:30:59 +02:00
|
|
|
message = concat(message)
|
2018-12-01 12:49:14 +02:00
|
|
|
raw_print(message)
|
|
|
|
end
|
|
|
|
|
2019-01-27 21:40:07 +02:00
|
|
|
--- Sets the server time as the scenario version. Imperfect since we ideally want the commit,
|
|
|
|
-- but an easy way to at least establish a baseline.
|
|
|
|
local function set_scenario_version()
|
|
|
|
-- A 1 hour buffer is in place to account for potential playtime pre-upload.
|
|
|
|
if game.tick < 216000 and not global.redmew_version then
|
|
|
|
local time_string = Timestamp.to_string(Public.get_current_time())
|
2019-02-02 23:42:24 +02:00
|
|
|
global.redmew_version = string.format('Time of map launch: %s UTC', time_string)
|
2019-01-27 21:40:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(Public.events.on_server_started, set_scenario_version)
|
|
|
|
|
2019-01-17 16:00:15 +02:00
|
|
|
--- The [JOIN] nad [LEAVE] messages Factorio sends to stdout aren't sent in all cases of
|
|
|
|
-- players joining or leaving. So we send our own [PLAYER-JOIN] and [PLAYER-LEAVE] tags.
|
|
|
|
Event.add(
|
|
|
|
defines.events.on_player_joined_game,
|
|
|
|
function(event)
|
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
raw_print(player_join_tag .. player.name)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Event.add(
|
|
|
|
defines.events.on_player_left_game,
|
|
|
|
function(event)
|
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
raw_print(player_leave_tag .. player.name)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-09-21 20:48:12 +02:00
|
|
|
return Public
|