Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSide = "left"
- local monitorSide = "back"
- local statiosnPatesbin= "KbzpU2JQ"
- rednet.open(modemSide)
- local monitor = peripheral.wrap(monitorSide)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write("Chargement...")
- local function loadStations(file)
- local t = {}
- if fs.exists(file) then
- local f = fs.open(file, "r")
- local content = f.readAll()
- f.close()
- for line in content:gmatch("[^\r\n]+") do
- local name, x, y = line:match("([^,]+),([^,]+),([^,]+)")
- if name and x and y then
- print(name..", ".. x..", "..y)
- table.insert(t, {
- name = name,
- x = tonumber(x),
- y = tonumber(y)
- })
- end
- end
- else
- shell.run("pastebin", "get", statiosnPatesbin, "stations.txt")
- print("no file, downloading....")
- t = loadStations(file)
- end
- return t
- end
- local function drawSquare(m, numero,sx, sy, color)
- m.setBackgroundColor(color)
- for dx = 0, 2 do
- m.setCursorPos(sx + dx, sy)
- m.write(" ")
- end
- m.setCursorPos(sx + 1, sy)
- m.write(tostring(numero))
- m.setBackgroundColor(colors.black)
- end
- local function drawLinks(m, stList)
- for i = 1, #stList - 1 do
- local s1 = stList[i]
- local s2 = stList[i + 1]
- -- On va tracer une ligne horizontale puis verticale
- -- (ou l'inverse) entre s1 et s2
- m.setBackgroundColor(colors.black)
- -- Ligne horizontale
- for px = math.min(s1.x, s2.x), math.max(s1.x, s2.x) do
- m.setCursorPos(px, s1.y)
- m.write("-")
- end
- -- Ligne verticale
- for py = math.min(s1.y, s2.y), math.max(s1.y, s2.y) do
- m.setCursorPos(s2.x, py)
- m.write("|")
- end
- end
- end
- local function getStationInfo(stName, data)
- for _, info in pairs(data) do
- if info.station == stName then
- return info
- end
- end
- return { color = colors.gray, name = "", station = stName }
- end
- local function showStations()
- local listLine = 6
- monitor.setCursorPos(1, listLine)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.write("Liste des gares :")
- for idx, st in ipairs(stations) do
- monitor.setCursorPos(1, listLine + idx)
- monitor.write(idx..") "..st.name)
- end
- end
- local function showStationGraph(msg)
- local station, colorNum, locoName = msg:match("^([^|]+)|([^|]+)|(.*)$")
- colorNum = tonumber(colorNum)
- if station and colorNum and locoName then
- print(locoName.."_"..colorNum.."_"..station)
- locoData[locoName.."_"..colorNum] = {
- color = colorMap[colorNum] or colors.gray,
- name = locoName,
- station = station
- }
- end
- monitor.clear()
- -- monitor.setTextScale(0.5) -- optionnel si beaucoup de stations
- drawLinks(monitor, stations)
- local i=1
- for _, st in ipairs(stations) do
- local info = getStationInfo(st.name, locoData)
- drawSquare(monitor, i,st.x, st.y, info.color)
- i=i+1
- end
- end
- -- Table de correspondance entre nombres (0..15) et noms de couleur
- colorMap = {
- [0] = colors.black,
- [1] = colors.red,
- [2] = colors.green,
- [3] = colors.brown,
- [4] = colors.blue,
- [5] = colors.purple,
- [6] = colors.cyan,
- [7] = colors.lightGray,
- [8] = colors.gray,
- [9] = colors.pink,
- [10] = colors.lime,
- [11] = colors.yellow,
- [12] = colors.lightBlue,
- [13] = colors.magenta,
- [14] = colors.orange,
- [15] = colors.white
- }
- stations = loadStations("stations.txt")
- locoData = {}
- while true do
- local senderID, msg = rednet.receive(1)
- if msg then
- showStationGraph(msg)
- end
- showStations(stations)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement