refactor(gate): streamline gate installers and support multi-station codes via splitCsv
This commit is contained in:
+14
-2
@@ -13,6 +13,17 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local function splitCsv(s)
|
||||||
|
local out = {}
|
||||||
|
s = trim(s)
|
||||||
|
if #s == 0 then return out end
|
||||||
|
for part in s:gmatch("[^,/%s]+") do
|
||||||
|
local v = trim(part)
|
||||||
|
if #v > 0 then table.insert(out, v) end
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
local function writeFile(path, content, binary)
|
local function writeFile(path, content, binary)
|
||||||
local mode = binary and "wb" or "w"
|
local mode = binary and "wb" or "w"
|
||||||
local f = fs.open(path, mode)
|
local f = fs.open(path, mode)
|
||||||
@@ -86,7 +97,7 @@ term.setCursorPos(1, 1)
|
|||||||
print("Ticket Gate Installer")
|
print("Ticket Gate Installer")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
local stationCode = trim(prompt("Station code: "))
|
local stationCode = trim(prompt("Station code(s) [comma or / separated]: "))
|
||||||
if #stationCode == 0 then
|
if #stationCode == 0 then
|
||||||
print("No station code provided.")
|
print("No station code provided.")
|
||||||
return
|
return
|
||||||
@@ -94,7 +105,8 @@ end
|
|||||||
|
|
||||||
local modeRaw = prompt("Gate mode (entry/exit): ")
|
local modeRaw = prompt("Gate mode (entry/exit): ")
|
||||||
local mode = (trim(modeRaw):lower() == "exit") and "exit" or "entry"
|
local mode = (trim(modeRaw):lower() == "exit") and "exit" or "entry"
|
||||||
local cfg = { mode = mode, station_codes = { stationCode }, station_code = stationCode }
|
local parsedCodes = splitCsv(stationCode)
|
||||||
|
local cfg = { mode = mode, station_codes = parsedCodes, station_code = parsedCodes[1] or stationCode }
|
||||||
local okCfg, cfgJson = pcall(textutils.serializeJSON, cfg)
|
local okCfg, cfgJson = pcall(textutils.serializeJSON, cfg)
|
||||||
if not okCfg then
|
if not okCfg then
|
||||||
print("Config serialize failed.")
|
print("Config serialize failed.")
|
||||||
|
|||||||
+6
-5
@@ -97,7 +97,7 @@ term.setCursorPos(1, 1)
|
|||||||
print("Bidirectional Ticket Gate Installer")
|
print("Bidirectional Ticket Gate Installer")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
local stationCode = trim(prompt("Station code: "))
|
local stationCode = trim(prompt("Station code(s) [comma or / separated]: "))
|
||||||
if #stationCode == 0 then
|
if #stationCode == 0 then
|
||||||
print("No station code provided.")
|
print("No station code provided.")
|
||||||
return
|
return
|
||||||
@@ -110,16 +110,17 @@ local backRaw = prompt("Back mode (entry/exit): ")
|
|||||||
local frontMode = normalizeMode(frontRaw)
|
local frontMode = normalizeMode(frontRaw)
|
||||||
local backMode = normalizeMode(backRaw)
|
local backMode = normalizeMode(backRaw)
|
||||||
|
|
||||||
|
local parsedCodes = splitCsv(stationCode)
|
||||||
local cfg = {
|
local cfg = {
|
||||||
station_codes = { stationCode },
|
station_codes = parsedCodes,
|
||||||
station_code = stationCode,
|
station_code = parsedCodes[1] or stationCode,
|
||||||
side_modes = {
|
side_modes = {
|
||||||
front = frontMode,
|
front = frontMode,
|
||||||
back = backMode,
|
back = backMode,
|
||||||
},
|
},
|
||||||
side_station_codes = {
|
side_station_codes = {
|
||||||
front = stationCode,
|
front = parsedCodes[1] or stationCode,
|
||||||
back = stationCode,
|
back = parsedCodes[1] or stationCode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
local okCfg, cfgJson = pcall(textutils.serializeJSON, cfg)
|
local okCfg, cfgJson = pcall(textutils.serializeJSON, cfg)
|
||||||
|
|||||||
Reference in New Issue
Block a user