1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/server.lua

244 lines
6.8 KiB
Lua
Raw Normal View History

2018-11-19 22:13:45 +02:00
local Token = require 'utils.global_token'
2018-11-24 16:15:14 +02:00
local Event = require 'utils.event'
2018-11-19 22:13:45 +02:00
2018-09-21 20:48:12 +02:00
local Public = {}
2018-10-03 22:03:19 +02:00
local raw_print = print
function print(str)
raw_print('[PRINT] ' .. str)
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-10-02 01:31:46 +02:00
local regular_promote_tag = '[REGULAR-PROMOTE]'
local regular_deomote_tag = '[REGULAR-DEOMOTE]'
2018-10-04 00:26:33 +02:00
local donator_set_tag = '[DONATOR-SET]'
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-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 = {}
defines.events.on_server_started = script.generate_event_name()
Public.events = {on_server_started = defines.events.on_server_started}
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
function Public.to_discord_raw(message)
2018-10-03 22:03:19 +02:00
raw_print(discord_raw_tag .. message)
end
function Public.to_discord_bold(message)
raw_print(discord_bold_tag .. message)
2018-09-21 20:48:12 +02:00
end
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-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-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-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
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
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-10-02 01:31:46 +02:00
function Public.regular_promote(target, promotor)
2018-10-03 22:03:19 +02:00
local control_message = table.concat {regular_promote_tag, target, ' ', promotor}
local discord_message = table.concat {discord_bold_tag, promotor .. ' promoted ' .. target .. ' to regular.'}
raw_print(control_message)
raw_print(discord_message)
2018-10-02 01:31:46 +02:00
end
2018-10-03 22:03:19 +02:00
function Public.regular_deomote(target, demotor)
local discord_message = table.concat {discord_bold_tag, target, ' was demoted from regular by ', demotor, '.'}
raw_print(regular_deomote_tag .. target)
raw_print(discord_message)
2018-10-02 01:31:46 +02:00
end
2018-10-04 00:26:33 +02:00
function Public.donator_set(target, perks)
perks = perks or 'nil'
local message = table.concat {donator_set_tag, target, ' ', perks}
raw_print(message)
end
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
2018-11-24 16:15:14 +02:00
local message = table.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-19 22:13:45 +02:00
function Public.ping(func_token)
local message = table.concat({ping_tag, func_token or default_ping_token, ' ', game.tick})
2018-11-17 20:56:05 +02:00
raw_print(message)
end
2018-11-24 16:15:14 +02:00
function Public.set_data(data_set, key, value)
if type(data_set) ~= 'string' then
error('data_set must be a string')
end
if type(key) ~= 'string' then
error('key must be a string')
end
local message
local vt = type(value)
if vt == 'nil' then
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '"}'})
elseif vt == 'string' then
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"\\"', value, '\\""}'})
2018-11-25 13:52:50 +02:00
elseif vt == 'number' then
2018-11-24 16:15:14 +02:00
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"', value, '"}'})
2018-11-25 13:52:50 +02:00
elseif vt == 'boolean' then
message =
table.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
error('value cannot be a function')
else
value = serpent.line(value)
2018-11-25 13:52:50 +02:00
value = value:gsub("'", "\\'")
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, "\",value:'", value, "'}"})
2018-11-24 16:15:14 +02:00
end
2018-11-25 13:52:50 +02:00
game.print(message)
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)
error(err)
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
function Public.on_data_set_changed(data_set, handler)
if type(data_set) ~= 'string' then
error('data_set must be a string')
end
local handlers = data_set_handlers[data_set]
if handlers == nil then
handlers = {handler}
data_set_handlers[data_set] = handlers
else
table.insert(handlers, handler)
end
end
Public.raise_data_set = data_set_changed
function Public.get_tracked_data_sets()
local message = {data_tracked_tag, '['}
for k, _ in pairs(data_set_handlers) do
table.insert(message, '"')
table.insert(message, k)
table.insert(message, '"')
table.insert(message, ',')
end
if message[#message] == ',' then
table.remove(message)
end
table.insert(message, ']')
message = table.concat(message)
raw_print(message)
game.print(message)
end
2018-11-25 13:52:50 +02:00
function Public.try_get_data(data_set, key, callback_token)
if type(data_set) ~= 'string' then
error('data_set must be a string')
end
if type(key) ~= 'string' then
error('key must be a string')
end
if type(callback_token) ~= 'number' then
error('callback_token must be a number')
end
local message = table.concat {data_get_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '"}'}
raw_print(message)
end
function Public.try_get_all_data(data_set, callback_token)
if type(data_set) ~= 'string' then
error('data_set must be a string')
end
if type(callback_token) ~= 'number' then
error('callback_token must be a number')
end
local message = table.concat {data_get_all_tag, callback_token, ' {', 'data_set:"', data_set, '"}'}
raw_print(message)
end
2018-09-21 20:48:12 +02:00
return Public