More improvements to UI

This commit is contained in:
Andrew Lalis 2023-09-17 07:44:47 -04:00
parent 2fb03f3bb1
commit 3b156442b3
2 changed files with 54 additions and 23 deletions

View File

@ -6,11 +6,20 @@ traffic through.
local RECEIVE_CHANNEL = 45452 local RECEIVE_CHANNEL = 45452
local g = require("simple-graphics")
local W, H = term.getSize()
local console = g.createConsole(W, H-2, colors.white, colors.black, "UP")
-- ONLY FOR DEBUGGING -- ONLY FOR DEBUGGING
-- inspect = require("inspect") -- inspect = require("inspect")
local modem = peripheral.wrap("top") or error("Missing top modem") local modem = peripheral.wrap("top") or error("Missing top modem")
modem.open(RECEIVE_CHANNEL) modem.open(RECEIVE_CHANNEL)
local function logToConsole(text)
local timestamp = os.date("%F %T")
g.appendAndDrawConsole(term, console, timestamp.." "..text, 1, 3)
end
local function generateStandardNode(id, edgeIds) local function generateStandardNode(id, edgeIds)
local node = {id = id, connections = {}, type = "JUNCTION"} local node = {id = id, connections = {}, type = "JUNCTION"}
for _, edgeId in pairs(edgeIds) do for _, edgeId in pairs(edgeIds) do
@ -49,22 +58,26 @@ local function loadGraph()
generateStandardNode("Junction-End", {"E1", "E2", "end"}), generateStandardNode("Junction-End", {"E1", "E2", "end"}),
generateStandardNode("Junction-Klausville", {"N3", "N4", "klausville"}), generateStandardNode("Junction-Klausville", {"N3", "N4", "klausville"}),
generateStandardNode("Junction-Foundry-West", {"W3", "foundry", "W4"}), generateStandardNode("Junction-Foundry-West", {"W3", "foundry", "W4"}),
generateStandardNode("Junction-Cam", {"S1", "S2", "cam"}),
generateStationNode("station-klausville", "Klausville", "klausville"), generateStationNode("station-klausville", "Klausville", "klausville"),
generateStationNode("station-handievale", "HandieVale", "handievale"), generateStationNode("station-handievale", "HandieVale", "handievale"),
generateStationNode("station-end", "End & Biofuel Refinery", "end"), generateStationNode("station-end", "End & Biofuel Refinery", "end"),
generateStationNode("station-foundry", "Jack's Foundry", "foundry") generateStationNode("station-foundry", "Jack's Foundry", "foundry"),
generateStationNode("station-cam", "Camville", "cam")
}, },
edges = { edges = {
{id = "handievale", length = 16}, {id = "handievale", length = 16},
{id = "end", length = 48}, {id = "end", length = 48},
{id = "foundry", length = 45}, {id = "foundry", length = 45},
{id = "klausville", length = 12}, {id = "klausville", length = 12},
{id = "cam", length = 16},
{id = "N1", length = nil}, {id = "N1", length = nil},
{id = "W1", length = 300}, {id = "W1", length = 300},
{id = "N2", length = 600}, {id = "N2", length = 600},
{id = "E1", length = 75}, {id = "E1", length = 75},
{id = "W2", length = nil}, {id = "W2", length = nil},
{id = "S1", length = nil}, {id = "S1", length = 420},
{id = "S2", length = nil},
{id = "W3", length = 50}, {id = "W3", length = 50},
{id = "W4", length = nil}, {id = "W4", length = nil},
{id = "N3", length = 350}, {id = "N3", length = 350},
@ -261,11 +274,20 @@ local function handleRouteRequest(graph, replyChannel, msg)
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node(s)"}) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node(s)"})
return return
end end
logToConsole("Finding path from "..startNode.id.." to "..endNode.id.."...")
local path = findPath(graph, startNode, endNode) local path = findPath(graph, startNode, endNode)
if not path then if not path then
logToConsole("Couldn't find a path!")
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "No valid path"}) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "No valid path"})
return return
end end
local pathStr = ""
for i, segment in pairs(path) do
pathStr = pathStr .. segment.node.id
if i < #path - 1 then pathStr = pathStr .. ", " end
end
logToConsole("Found path: "..pathStr)
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, route = path}) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, route = path})
end end
@ -279,14 +301,17 @@ local function handleGetRoutesRequest(graph, replyChannel, msg)
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node"}) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node"})
return return
end end
logToConsole("Finding reachable stations from "..startNode.id.."...")
local stations = getReachableStations(graph, startNode) local stations = getReachableStations(graph, startNode)
logToConsole("Found "..#stations.." results.")
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, stations = stations}) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, stations = stations})
end end
local function handleRequests(graph) local function handleRequests(graph, console)
while true do while true do
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
if channel == RECEIVE_CHANNEL and msg and msg.command and type(msg.command) == "string" then if channel == RECEIVE_CHANNEL and msg and msg.command and type(msg.command) == "string" then
logToConsole("Got request on CH: "..channel..", RCH: "..replyChannel..", CMD: "..msg.command)
if msg.command == "ROUTE" then if msg.command == "ROUTE" then
handleRouteRequest(graph, replyChannel, msg) handleRouteRequest(graph, replyChannel, msg)
elseif msg.command == "GET_ROUTES" then elseif msg.command == "GET_ROUTES" then
@ -298,19 +323,8 @@ local function handleRequests(graph)
end end
end end
handleRequests(loadGraph()) g.clear(term, colors.black)
g.drawTextCenter(term, W/2, 1, "CC-Rail Central Server", colors.yellow, colors.black)
-- local graph = loadGraph() g.drawXLine(term, 1, W, 2, colors.gray)
-- print("GRAPH:") logToConsole("Now taking requests.")
-- print(inspect(graph)) handleRequests(loadGraph(), console)
-- local startNode = findNodeById(graph, "station-handievale")
-- print(inspect(getReachableStations(graph, startNode)))
-- local endNode = findNodeById(graph, "station-foundry")
-- print("\n\nPATH:")
-- local path = findPath(graph, startNode, endNode)
-- if path then
-- print("Found path!")
-- for i, element in pairs(path) do
-- print(i..". "..element.node.id.." via edge "..inspect(element.via).." @ "..inspect(element.distance))
-- end
-- end

View File

@ -42,7 +42,7 @@ end
local function waitForStation(stationName) local function waitForStation(stationName)
while true do while true do
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
if type(channel) == "number" and channel == STATION_BROADCAST_CHANNEL and isValidStationInfo(msg) and msg.name == stationName and msg.range >= dist then if type(channel) == "number" and channel == STATION_BROADCAST_CHANNEL and isValidStationInfo(msg) and msg.name == stationName and dist ~= nil and msg.range >= dist then
return return
end end
end end
@ -51,7 +51,7 @@ end
local function listenForAnyStation() local function listenForAnyStation()
while true do while true do
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
if type(channel) == "number" and channel == STATION_BROADCAST_CHANNEL and isValidStationInfo(msg) and msg.range >= dist then if type(channel) == "number" and channel == STATION_BROADCAST_CHANNEL and isValidStationInfo(msg) and dist ~= nil and msg.range >= dist then
os.queueEvent("rail_station_nearby", msg, dist) os.queueEvent("rail_station_nearby", msg, dist)
end end
end end
@ -140,10 +140,27 @@ local function drawErrorPage(errorMsg)
) )
end end
local function drawGoingToSleepScreen()
g.clear(term, colors.white)
term.setTextColor(colors.gray)
term.setCursorPos(1, 1)
print("Going to sleep. Click again to wake me.")
os.sleep(2)
os.shutdown()
end
local function handleNearbyStation() local function handleNearbyStation()
while true do while true do
drawLookingForStationScreen() drawLookingForStationScreen()
local event, stationData, dist = os.pullEvent("rail_station_nearby") local event, stationData, dist = nil
parallel.waitForAny(
function () event, stationData, dist = os.pullEvent("rail_station_nearby") end,
function ()
os.sleep(10)
drawGoingToSleepScreen()
end
)
if not stationData then return end
drawStationFoundScreen(stationData.displayName) drawStationFoundScreen(stationData.displayName)
os.sleep(0.5) os.sleep(0.5)
@ -231,7 +248,7 @@ end
g.clear(term, colors.white) g.clear(term, colors.white)
g.drawTextCenter(term, W/2, H/2, "Rail Router", colors.black, colors.white) g.drawTextCenter(term, W/2, H/2, "Rail Router", colors.black, colors.white)
g.drawTextCenter(term, W/2, H/2 + 2, "By Andrew", colors.gray, colors.white) g.drawTextCenter(term, W/2, H/2 + 2, "By Andrew", colors.gray, colors.white)
os.sleep(1) os.sleep(0.5)
while true do while true do
local route = waitForRouteSelection() local route = waitForRouteSelection()