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 g = require("simple-graphics")
local W, H = term.getSize()
local console = g.createConsole(W, H-2, colors.white, colors.black, "UP")
-- ONLY FOR DEBUGGING
-- inspect = require("inspect")
local modem = peripheral.wrap("top") or error("Missing top modem")
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 node = {id = id, connections = {}, type = "JUNCTION"}
for _, edgeId in pairs(edgeIds) do
@ -49,22 +58,26 @@ local function loadGraph()
generateStandardNode("Junction-End", {"E1", "E2", "end"}),
generateStandardNode("Junction-Klausville", {"N3", "N4", "klausville"}),
generateStandardNode("Junction-Foundry-West", {"W3", "foundry", "W4"}),
generateStandardNode("Junction-Cam", {"S1", "S2", "cam"}),
generateStationNode("station-klausville", "Klausville", "klausville"),
generateStationNode("station-handievale", "HandieVale", "handievale"),
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 = {
{id = "handievale", length = 16},
{id = "end", length = 48},
{id = "foundry", length = 45},
{id = "klausville", length = 12},
{id = "cam", length = 16},
{id = "N1", length = nil},
{id = "W1", length = 300},
{id = "N2", length = 600},
{id = "E1", length = 75},
{id = "W2", length = nil},
{id = "S1", length = nil},
{id = "S1", length = 420},
{id = "S2", length = nil},
{id = "W3", length = 50},
{id = "W4", length = nil},
{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)"})
return
end
logToConsole("Finding path from "..startNode.id.." to "..endNode.id.."...")
local path = findPath(graph, startNode, endNode)
if not path then
logToConsole("Couldn't find a path!")
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "No valid path"})
return
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})
end
@ -279,14 +301,17 @@ local function handleGetRoutesRequest(graph, replyChannel, msg)
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node"})
return
end
logToConsole("Finding reachable stations from "..startNode.id.."...")
local stations = getReachableStations(graph, startNode)
logToConsole("Found "..#stations.." results.")
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, stations = stations})
end
local function handleRequests(graph)
local function handleRequests(graph, console)
while true do
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
logToConsole("Got request on CH: "..channel..", RCH: "..replyChannel..", CMD: "..msg.command)
if msg.command == "ROUTE" then
handleRouteRequest(graph, replyChannel, msg)
elseif msg.command == "GET_ROUTES" then
@ -298,19 +323,8 @@ local function handleRequests(graph)
end
end
handleRequests(loadGraph())
-- local graph = loadGraph()
-- print("GRAPH:")
-- print(inspect(graph))
-- 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
g.clear(term, colors.black)
g.drawTextCenter(term, W/2, 1, "CC-Rail Central Server", colors.yellow, colors.black)
g.drawXLine(term, 1, W, 2, colors.gray)
logToConsole("Now taking requests.")
handleRequests(loadGraph(), console)

View File

@ -42,7 +42,7 @@ end
local function waitForStation(stationName)
while true do
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
end
end
@ -51,7 +51,7 @@ end
local function listenForAnyStation()
while true do
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)
end
end
@ -140,10 +140,27 @@ local function drawErrorPage(errorMsg)
)
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()
while true do
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)
os.sleep(0.5)
@ -231,7 +248,7 @@ end
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 + 2, "By Andrew", colors.gray, colors.white)
os.sleep(1)
os.sleep(0.5)
while true do
local route = waitForRouteSelection()