Updated watch and control.lua

This commit is contained in:
Caesar2011
2026-05-16 01:29:21 +02:00
parent 3601076f49
commit 6e3499812e
2 changed files with 59 additions and 81 deletions

View File

@@ -1,5 +1,5 @@
local EXPORTER_NAME = "signal-exporter"
local EXPORT_INTERVAL = 18000 -- Ticks between file writes (18000 = 5 minutes)
local EXPORT_INTERVAL = 1800 -- Ticks between file writes (1800 = 30s at 60 UPS)
local function init_global()
global.exporters = global.exporters or {}
@@ -17,6 +17,7 @@ local function export_combinator(unit_number, data)
end
local signals = {
game_tick = game.tick,
circuit_network = {
red = {},
green = {}
@@ -114,17 +115,17 @@ script.on_event(defines.events.on_gui_opened, function(event)
local frame = screen.add{type = "frame", name = "signal_exporter_gui", caption = "Configure Signal Exporter", direction = "vertical"}
frame.force_auto_center()
local name_flow = frame.add{type = "flow", name = "name_flow", direction = "horizontal"}
name_flow.style.vertical_align = "center"
name_flow.style.bottom_margin = 8
name_flow.add{type = "label", caption = "Combinator Name: "}
name_flow.add{type = "textfield", name = "exporter_name_input", text = data.name}
local button_flow = frame.add{type = "flow", name = "button_flow", direction = "horizontal"}
button_flow.add{type = "button", name = "exporter_save_button", caption = "Save & Close"}
button_flow.add{type = "button", name = "exporter_refresh_button", caption = "Export Now"}
player.opened = frame
global.open_guis[player.index] = entity.unit_number
end
@@ -135,20 +136,20 @@ script.on_event(defines.events.on_gui_click, function(event)
if element.name == "exporter_save_button" or element.name == "exporter_refresh_button" then
local player = game.players[event.player_index]
local unit_number = global.open_guis[player.index]
if unit_number and global.exporters[unit_number] then
local frame = player.gui.screen["signal_exporter_gui"]
if frame then
local new_name = frame.name_flow.exporter_name_input.text
global.exporters[unit_number].name = new_name
end
if element.name == "exporter_refresh_button" then
export_combinator(unit_number, global.exporters[unit_number])
player.print("Data manually exported to signals_" .. global.exporters[unit_number].name .. ".json")
end
end
if element.name == "exporter_save_button" then
if player.gui.screen["signal_exporter_gui"] then
player.gui.screen["signal_exporter_gui"].destroy()
@@ -165,41 +166,30 @@ script.on_event(defines.events.on_gui_closed, function(event)
end
end)
-- Function to export all item prototypes to a CSV file.
local function export_item_names_csv(event)
local player = game.players[event.player_index]
local csv_lines = { '"item_key","localised_name"' }
-- Helper function to safely escape double quotes for CSV format.
local function escape_csv_field(value)
if value == nil then return "" end
return string.gsub(tostring(value), '"', '""')
end
-- Iterate through all registered item prototypes.
for _, item_prototype in pairs(game.item_prototypes) do
local key = item_prototype.name
-- Use player.localise() to resolve the LocalisedString object (item_prototype.localised_name)
-- into the final, human-readable text for the current player's language.
local resolved_name = player.localise(item_prototype.localised_name)
local escaped_key = escape_csv_field(key)
local escaped_name = escape_csv_field(resolved_name)
table.insert(csv_lines, '"' .. escaped_key .. '","' .. escaped_name .. '"')
end
local csv_content = table.concat(csv_lines, "\n")
game.write_file("item_names.csv", csv_content, false, player.index)
player.print("Export complete. File saved to 'script-output/item_names.csv'.")
end
-- Register a console command to trigger the export.
commands.add_command(
"export-item-names",
"Exports the internal name (key) and localised name for all game items to a CSV file.",
export_item_names_csv
)
)