1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-11-27 22:38:18 +02:00

Remove unused function and change output communication

This commit is contained in:
Gerkiz
2023-11-25 20:48:06 +01:00
parent b5f0bec461
commit 4952c2d868
4 changed files with 65 additions and 155 deletions

View File

@@ -59,11 +59,9 @@ globals = {
'Debug',
'_LIFECYCLE',
'_STAGE',
'get_game_version',
'is_loaded',
'is_loaded_bool',
'is_game_modded',
'is_mod_loaded',
'require'
}
max_line_length = LINE_LENGTH

View File

@@ -882,69 +882,10 @@ local function move_room_to_train(icw, train, wagon)
end
end
local function get_connected_rolling_stock(entity, direction, carriages)
--thanks Boskid
local first_stock, second_stock
for k, v in pairs(carriages) do
if v == entity then
first_stock = carriages[k - 1]
second_stock = carriages[k + 1]
break
end
end
if not first_stock then
first_stock, second_stock = second_stock, nil
end
if not first_stock then
return nil
end
local angle = math.atan2(-(entity.position.x - first_stock.position.x), entity.position.y - first_stock.position.y) / (2 * math.pi) - entity.orientation
if direction == defines.rail_direction.back then
angle = angle + 0.5
end
while angle < -0.5 do
angle = angle + 1
end
while angle > 0.5 do
angle = angle - 1
end
local connected_stock
if angle > -0.25 and angle < 0.25 then
connected_stock = first_stock
else
connected_stock = second_stock
end
if not connected_stock then
return nil
end
angle = math.atan2(-(connected_stock.position.x - entity.position.x), connected_stock.position.y - entity.position.y) / (2 * math.pi) - connected_stock.orientation
while angle < -0.5 do
angle = angle + 1
end
while angle > 0.5 do
angle = angle - 1
end
local joint_of_connected_stock
if angle > -0.25 and angle < 0.25 then
joint_of_connected_stock = defines.rail_direction.front
else
joint_of_connected_stock = defines.rail_direction.back
end
return connected_stock, joint_of_connected_stock
end
function Public.construct_train(icw, locomotive, carriages)
for i, carriage in pairs(carriages) do
if carriage == locomotive then
local stock
local experimental = get_game_version()
if experimental then
stock = locomotive.get_connected_rolling_stock(defines.rail_direction.front)
else
stock = get_connected_rolling_stock(locomotive, defines.rail_direction.front, carriages)
end
local stock = locomotive.get_connected_rolling_stock(defines.rail_direction.front)
if stock ~= carriages[i - 1] then
local n = 1
local m = #carriages

View File

@@ -14,6 +14,7 @@ local remove = table.remove
local tostring = tostring
local len = string.len
local gmatch = string.gmatch
local newline = '\n'
local raw_print = Print.raw_print
local minutes_to_ticks = 60 * 60
@@ -32,6 +33,9 @@ local instances = {
data = {}
}
local requests = {}
local jailed_data_set = 'jailed'
local data_set_handlers = {}
local scenario_handlers = {}
Global.register(
{
@@ -95,12 +99,14 @@ local player_leave_tag = '[PLAYER-LEAVE]'
Public.raw_print = raw_print
--- Jail dataset.
local jailed_data_set = 'jailed'
local data_set_handlers = {}
local scenario_handlers = {}
local function output_data(...)
if start_data and start_data.output then
local write = game.write_file
write(start_data.output, ... .. newline, true, 0)
else
raw_print(...)
end
end
local function assert_non_empty_string_and_no_spaces(str, argument_name)
if type(str) ~= 'string' then
@@ -116,7 +122,7 @@ local function assert_non_empty_string_and_no_spaces(str, argument_name)
end
end
local function getOnlineAdmins()
local function get_online_admins()
local online = game.connected_players
local i = 0
for _, p in pairs(online) do
@@ -132,7 +138,7 @@ local function build_embed_data()
time = Public.format_time(game.ticks_played),
onlinePlayers = #game.connected_players,
totalPlayers = #game.players,
onlineAdmins = getOnlineAdmins()
onlineAdmins = get_online_admins()
}
return d
end
@@ -165,7 +171,7 @@ function Public.to_discord(message, locale)
if locale then
print(message, discord_tag)
else
raw_print(discord_tag .. message)
output_data(discord_tag .. message)
end
end
@@ -176,7 +182,7 @@ function Public.to_discord_raw(message, locale)
if locale then
print(message, discord_raw_tag)
else
raw_print(discord_raw_tag .. message)
output_data(discord_raw_tag .. message)
end
end
@@ -187,7 +193,7 @@ function Public.to_discord_bold(message, locale)
if locale then
print(message, discord_bold_tag)
else
raw_print(discord_bold_tag .. message)
output_data(discord_bold_tag .. message)
end
end
@@ -195,28 +201,28 @@ end
-- @param message<string> message to send.
function Public.to_discord_named(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
raw_print(concat({discord_named_tag, channel_name, ' ', message}))
output_data(concat({discord_named_tag, channel_name, ' ', message}))
end
--- Sends a message to the named discord channel. The message is not sanitized of markdown.
-- @param message<string> message to send.
function Public.to_discord_named_raw(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
raw_print(concat({discord_named_raw_tag, channel_name, ' ', message}))
output_data(concat({discord_named_raw_tag, channel_name, ' ', message}))
end
--- Sends a message to the named discord channel. The message is sanitized of markdown server side, then made bold.
-- @param message<string> message to send.
function Public.to_discord_named_bold(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
raw_print(concat({discord_named_bold_tag, channel_name, ' ', message}))
output_data(concat({discord_named_bold_tag, channel_name, ' ', message}))
end
--- Sends an embed message to the named discord channel. The message is sanitized of markdown server side.
-- @param message<string> the content of the embed.
function Public.to_discord_named_embed(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
raw_print(concat({discord_named_embed_tag, channel_name, ' ', message}))
output_data(concat({discord_named_embed_tag, channel_name, ' ', message}))
end
--- Sends an embed message that is parsed to the named discord channel. The message is sanitized of markdown server side.
@@ -238,14 +244,14 @@ function Public.to_discord_named_parsed_embed(channel_name, message)
message.channelName = channel_name
raw_print(discord_named_embed_parsed_tag, table_to_json(message))
output_data(discord_named_embed_parsed_tag, table_to_json(message))
end
--- Sends an embed message to the named discord channel. The message is not sanitized of markdown.
-- @param message<string> the content of the embed.
function Public.to_discord_named_embed_raw(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
raw_print(concat({discord_named_embed_raw_tag, channel_name, ' ', message}))
output_data(concat({discord_named_embed_raw_tag, channel_name, ' ', message}))
end
--- Sends a message to the linked admin discord channel. The message is sanitized of markdown server side.
@@ -255,7 +261,7 @@ function Public.to_admin(message, locale)
if locale then
print(message, discord_admin_tag)
else
raw_print(discord_admin_tag .. message)
output_data(discord_admin_tag .. message)
end
end
@@ -266,7 +272,7 @@ function Public.to_banned(message, locale)
if locale then
print(message, discord_banned_tag)
else
raw_print(discord_banned_tag .. message)
output_data(discord_banned_tag .. message)
end
end
--- Sends a message to the linked banned discord channel. The message is sanitized of markdown server side.
@@ -276,7 +282,7 @@ function Public.to_unbanned(message, locale)
if locale then
print(message, discord_unbanned_tag)
else
raw_print(discord_unbanned_tag .. message)
output_data(discord_unbanned_tag .. message)
end
end
@@ -287,7 +293,7 @@ function Public.to_jailed(message, locale)
if locale then
print(message, discord_jailed_tag)
else
raw_print(discord_jailed_tag .. message)
output_data(discord_jailed_tag .. message)
end
end
--- Sends a message to the linked connected discord channel. The message is sanitized of markdown server side.
@@ -297,7 +303,7 @@ function Public.to_unjailed(message, locale)
if locale then
print(message, discord_unjailed_tag)
else
raw_print(discord_unjailed_tag .. message)
output_data(discord_unjailed_tag .. message)
end
end
@@ -308,7 +314,7 @@ function Public.to_admin_raw(message, locale)
if locale then
print(message, discord_admin_raw_tag)
else
raw_print(discord_admin_raw_tag .. message)
output_data(discord_admin_raw_tag .. message)
end
end
@@ -326,7 +332,7 @@ function Public.to_discord_embed_parsed(message)
if not message.description then
return
end
raw_print(discord_embed_parsed_tag .. table_to_json(message))
output_data(discord_embed_parsed_tag .. table_to_json(message))
end
--- Sends a embed message to the linked discord channel. The message is sanitized of markdown server side.
@@ -336,7 +342,7 @@ function Public.to_discord_embed(message, locale)
if locale then
print(message, discord_embed_tag)
else
raw_print(discord_embed_tag .. message)
output_data(discord_embed_tag .. message)
end
end
@@ -347,7 +353,7 @@ function Public.to_discord_embed_raw(message, locale)
if locale then
print(message, discord_embed_raw_tag)
else
raw_print(discord_embed_raw_tag .. message)
output_data(discord_embed_raw_tag .. message)
end
end
@@ -358,7 +364,7 @@ function Public.to_admin_embed(message, locale)
if locale then
print(message, discord_admin_embed_tag)
else
raw_print(discord_admin_embed_tag .. message)
output_data(discord_admin_embed_tag .. message)
end
end
@@ -382,7 +388,7 @@ function Public.to_banned_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_banned_embed_tag .. table_to_json(message))
output_data(discord_banned_embed_tag .. table_to_json(message))
end
end
@@ -403,7 +409,7 @@ function Public.to_unbanned_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_unbanned_embed_tag .. table_to_json(message))
output_data(discord_unbanned_embed_tag .. table_to_json(message))
end
end
@@ -428,7 +434,7 @@ function Public.to_jailed_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_jailed_embed_tag .. table_to_json(message))
output_data(discord_jailed_embed_tag .. table_to_json(message))
end
end
@@ -453,7 +459,7 @@ function Public.to_jailed_named_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_jailed_named_embed_tag .. table_to_json(message))
output_data(discord_jailed_named_embed_tag .. table_to_json(message))
end
end
@@ -474,7 +480,7 @@ function Public.to_unjailed_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_unjailed_embed_tag .. table_to_json(message))
output_data(discord_unjailed_embed_tag .. table_to_json(message))
end
end
@@ -495,7 +501,7 @@ function Public.to_unjailed_named_embed(message, locale)
if not message.admin then
return
end
raw_print(discord_unjailed_named_embed_tag .. table_to_json(message))
output_data(discord_unjailed_named_embed_tag .. table_to_json(message))
end
end
@@ -506,7 +512,7 @@ function Public.to_admin_embed_raw(message, locale)
if locale then
print(message, discord_admin_embed_raw_tag)
else
raw_print(discord_admin_embed_raw_tag .. message)
output_data(discord_admin_embed_raw_tag .. message)
end
end
@@ -523,7 +529,7 @@ function Public.start_scenario(scenario_name)
local message = start_scenario_tag .. scenario_name
raw_print(message)
output_data(message)
end
--- Stops and saves the factorio server.
@@ -533,7 +539,7 @@ end
function Public.stop_scenario()
local message = stop_scenario_tag
raw_print(message)
output_data(message)
end
local default_ping_token =
@@ -552,7 +558,7 @@ local default_ping_token =
-- The function is passed the tick that the ping was sent.
function Public.ping(func_token)
local message = concat({ping_tag, func_token or default_ping_token, ' ', game.tick})
raw_print(message)
output_data(message)
end
--- The backend sets instances with data so a player
@@ -634,7 +640,7 @@ function Public.set_data(data_set, key, value)
message = concat({data_set_tag, '{data_set:"', data_set, '",key:"', key, "\",value:'", value, "'}"})
end
raw_print(message)
output_data(message)
end
local function validate_arguments(data_set, key, callback_token)
@@ -664,14 +670,14 @@ local function send_try_get_data(data_set, key, callback_token)
key = double_escape(key)
local message = concat {data_get_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '"}'}
raw_print(message)
output_data(message)
end
local function send_try_get_ban(username, callback_token)
username = double_escape(username)
local message = concat {ban_get_tag, callback_token, ' {', 'username:"', username, '"}'}
raw_print(message)
output_data(message)
end
local function send_try_get_data_and_print(data_set, key, to_print, callback_token)
@@ -680,7 +686,7 @@ local function send_try_get_data_and_print(data_set, key, to_print, callback_tok
to_print = double_escape(to_print)
local message = concat {data_get_and_print_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '",to_print:"', to_print, '"}'}
raw_print(message)
output_data(message)
end
local cancelable_callback_token =
@@ -903,7 +909,7 @@ function Public.try_get_all_data(data_set, callback_token)
data_set = double_escape(data_set)
local message = concat {data_get_all_tag, callback_token, ' {', 'data_set:"', data_set, '"}'}
raw_print(message)
output_data(message)
end
local function data_set_changed(data)
@@ -1046,7 +1052,7 @@ function Public.get_tracked_data_sets()
message[#message + 1] = ']'
message = concat(message)
raw_print(message)
output_data(message)
end
--- Called by the web server to determine which scenarios is being tracked.
@@ -1066,7 +1072,7 @@ function Public.get_tracked_scenario()
end
message = concat(message)
raw_print(message)
output_data(message)
end
local function escape(str)
@@ -1189,7 +1195,7 @@ function Public.ban_sync(username, reason, admin)
admin = escape(admin)
local message = concat({ban_sync_tag, '{username:"', username, '",reason:"', reason, '",admin:"', admin, '"}'})
raw_print(message)
output_data(message)
end
--- If the player exists bans the player else throws error.
@@ -1227,7 +1233,7 @@ function Public.unban_sync(username, admin)
admin = escape(admin)
local message = concat({unbanned_sync_tag, '{username:"', username, '",admin:"', admin, '"}'})
raw_print(message)
output_data(message)
end
--- If the player exists unbans the player else throws error.
@@ -1244,6 +1250,12 @@ function Public.set_time(secs)
server_time.tick = game.tick
end
--- Called by the web server to set output location.
-- @param data<string>
function Public.set_output(data)
start_data.output = data
end
--- Gets a table {secs:number?, tick:number} with secs being the unix epoch timestamp
-- for the server time and ticks the number of game ticks ago it was set.
-- @return table
@@ -1405,7 +1417,7 @@ function Public.query_online_players()
message[#message + 1] = ']'
message = concat(message)
raw_print(message)
output_data(message)
end
function Public.ban_handler(event)
@@ -1525,7 +1537,7 @@ commands.add_command(
end
end
if err or err == false then
raw_print(err)
output_data(err)
end
end
)
@@ -1540,7 +1552,7 @@ Event.add(
return
end
raw_print(player_join_tag .. player.name)
output_data(player_join_tag .. player.name)
end
)
@@ -1567,7 +1579,7 @@ Event.add(
end
local reason = leave_reason_map[event.reason] or ''
raw_print(player_leave_tag .. player.name .. reason)
output_data(player_leave_tag .. player.name .. reason)
end
)
@@ -1598,7 +1610,7 @@ Event.add(
end
message = concat(message)
raw_print(message)
output_data(message)
end
)

View File

@@ -4,8 +4,6 @@ local Poll = {
}
local Token = require 'utils.token'
local Server = require 'utils.server'
local branch_version = '1.1' -- define what game version we're using
local sub = string.sub
--- This module is for the web server to call functions and raise events.
-- Not intended to be called by scripts.
@@ -34,6 +32,7 @@ function ServerCommands.changes_detected()
end
ServerCommands.set_time = Server.set_time
ServerCommands.set_output = Server.set_output
ServerCommands.set_ups = Server.set_ups
ServerCommands.get_ups = Server.get_ups
ServerCommands.export_stats = Server.export_stats
@@ -42,33 +41,6 @@ ServerCommands.set_instances = Server.set_instances
ServerCommands.query_online_players = Server.query_online_players
ServerCommands.ban_handler = Server.ban_handler
local SC_Interface = {
get_ups = function()
return ServerCommands.get_ups()
end,
set_ups = function(tick)
if tick then
ServerCommands.set_ups(tick)
else
error("Remote call parameter to ServerCommands set_ups can't be nil.")
end
end
}
if not remote.interfaces['ServerCommands'] then
remote.add_interface('ServerCommands', SC_Interface)
end
function get_game_version()
local get_active_branch = sub(game.active_mods.base, 3, 4)
local is_branch_experimental = sub(branch_version, 3, 4)
if get_active_branch >= is_branch_experimental then
return true
else
return false
end
end
function is_loaded(module)
local res = _G.package.loaded[module]
if res then
@@ -99,17 +71,4 @@ function is_game_modded()
return false
end
function is_mod_loaded(module)
if not module then
return false
end
local res = game.active_mods[module]
if res then
return true
else
return false
end
end
return ServerCommands