1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

more functions

This commit is contained in:
grilledham 2018-11-24 14:15:14 +00:00
parent 163f04789d
commit f4612a7046
3 changed files with 139 additions and 1 deletions

View File

@ -47,3 +47,35 @@ require 'features.gui.blueprint_helper'
require 'features.gui.paint'
require 'features.gui.score'
require 'features.gui.popup'
Server.on_data_set_changed(
'Test',
function(data)
game.print(serpent.block(data))
end
)
Server.on_data_set_changed(
'Test 2',
function(data)
game.print(serpent.block(data))
end
)
Server.on_data_set_changed(
'Test,2',
function(data)
game.print(serpent.block(data))
end
)
local Event = require('utils.event')
Event.add(
Server.events.on_server_started,
function(tbl)
game.print('on_server_started')
print('on_server_started')
game.print(serpent.block(tbl))
print(serpent.block(tbl))
end
)

View File

@ -1,4 +1,5 @@
local Token = require 'utils.global_token'
local Event = require 'utils.event'
local Public = {}
@ -21,9 +22,19 @@ local regular_deomote_tag = '[REGULAR-DEOMOTE]'
local donator_set_tag = '[DONATOR-SET]'
local start_scenario_tag = '[START-SCENARIO]'
local ping_tag = '[PING]'
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]'
Public.raw_print = raw_print
local data_set_handlers = {}
defines.events.on_server_started = script.generate_event_name()
Public.events = {on_server_started = defines.events.on_server_started}
function Public.to_discord(message)
raw_print(discord_tag .. message)
end
@ -100,7 +111,7 @@ local default_ping_token =
local now = game.tick
local diff = now - sent_tick
local message = table.concat({'Ping in ', diff, ' ticks ', 'sent: ', sent_tick, ' received: ', now})
local message = table.concat({'Pong in ', diff, ' tick(s) ', 'sent tick: ', sent_tick, ' received tick: ', now})
game.print(message)
end
)
@ -110,4 +121,91 @@ function Public.ping(func_token)
raw_print(message)
end
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, '\\""}'})
elseif vt == 'number' or vt == 'boolean' then
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"', value, '"}'})
elseif vt == 'function' then
error('value cannot be a function')
else
value = serpent.line(value)
message = table.concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, '",value:"', value, '"}'})
end
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
return Public

View File

@ -1,6 +1,7 @@
local Poll = require 'features.gui.poll'
local UserGroups = require 'features.user_groups'
local Token = require 'utils.global_token'
local Server = require 'server'
local Public = {}
@ -17,4 +18,11 @@ function Public.raise_callback(func_token, data)
func(data)
end
Public.raise_data_set = Server.raise_data_set
Public.get_tracked_data_sets = Server.get_tracked_data_sets
function Public.server_started()
script.raise_event(Server.events.on_server_started, {})
end
return Public