Advertisement
Feaders

Central gare

Feb 28th, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1.  
  2. local modemSide   = "left"
  3. local monitorSide = "back"
  4. local statiosnPatesbin= "KbzpU2JQ"
  5.  
  6. rednet.open(modemSide)
  7. local monitor     = peripheral.wrap(monitorSide)
  8. monitor.setBackgroundColor(colors.black)
  9. monitor.setTextColor(colors.white)
  10. monitor.clear()
  11. monitor.setCursorPos(1,1)
  12. monitor.write("Chargement...")
  13.  
  14. local function loadStations(file)
  15.   local t = {}
  16.   if fs.exists(file) then
  17.     local f = fs.open(file, "r")
  18.     local content = f.readAll()
  19.     f.close()
  20.     for line in content:gmatch("[^\r\n]+") do
  21.       local name, x, y = line:match("([^,]+),([^,]+),([^,]+)")
  22.       if name and x and y then
  23.       print(name..", ".. x..", "..y)
  24.         table.insert(t, {
  25.           name = name,
  26.           x = tonumber(x),
  27.           y = tonumber(y)
  28.         })
  29.       end
  30.     end
  31.   else
  32.     shell.run("pastebin", "get", statiosnPatesbin, "stations.txt")
  33.     print("no file, downloading....")
  34.     t = loadStations(file)
  35.   end
  36.   return t
  37. end
  38.  
  39. local function drawSquare(m, numero,sx, sy, color)
  40.   m.setBackgroundColor(color)
  41.  
  42.   for dx = 0, 2 do
  43.     m.setCursorPos(sx + dx, sy)
  44.     m.write(" ")
  45.   end
  46.   m.setCursorPos(sx + 1, sy)
  47.   m.write(tostring(numero))
  48.   m.setBackgroundColor(colors.black)
  49. end
  50.  
  51. local function drawLinks(m, stList)
  52.   for i = 1, #stList - 1 do
  53.     local s1 = stList[i]
  54.     local s2 = stList[i + 1]
  55.    
  56.     -- On va tracer une ligne horizontale puis verticale
  57.     -- (ou l'inverse) entre s1 et s2
  58.     m.setBackgroundColor(colors.black)
  59.    
  60.     -- Ligne horizontale
  61.     for px = math.min(s1.x, s2.x), math.max(s1.x, s2.x) do
  62.       m.setCursorPos(px, s1.y)
  63.       m.write("-")
  64.     end
  65.     -- Ligne verticale
  66.     for py = math.min(s1.y, s2.y), math.max(s1.y, s2.y) do
  67.       m.setCursorPos(s2.x, py)
  68.       m.write("|")
  69.     end
  70.   end
  71. end
  72.  
  73. local function getStationInfo(stName, data)
  74.   for _, info in pairs(data) do
  75.     if info.station == stName then
  76.       return info
  77.     end
  78.   end
  79.   return { color = colors.gray, name = "", station = stName }
  80. end
  81.  
  82. local function showStations()
  83.   local listLine = 6
  84.   monitor.setCursorPos(1, listLine)
  85.   monitor.setBackgroundColor(colors.black)
  86.   monitor.setTextColor(colors.white)
  87.   monitor.write("Liste des gares :")
  88.   for idx, st in ipairs(stations) do
  89.     monitor.setCursorPos(1, listLine + idx)
  90.     monitor.write(idx..") "..st.name)
  91.   end
  92. end
  93. local function showStationGraph(msg)
  94.  
  95.   local station, colorNum, locoName = msg:match("^([^|]+)|([^|]+)|(.*)$")
  96.   colorNum = tonumber(colorNum)
  97.  
  98.   if station and colorNum and locoName then
  99.     print(locoName.."_"..colorNum.."_"..station)
  100.     locoData[locoName.."_"..colorNum] = {
  101.         color   = colorMap[colorNum] or colors.gray,
  102.         name    = locoName,
  103.         station = station
  104.     }
  105.   end
  106.  
  107.   monitor.clear()
  108.   -- monitor.setTextScale(0.5) -- optionnel si beaucoup de stations
  109.   drawLinks(monitor, stations)
  110.   local i=1
  111.   for _, st in ipairs(stations) do
  112.     local info = getStationInfo(st.name, locoData)
  113.     drawSquare(monitor, i,st.x, st.y, info.color)
  114.     i=i+1
  115.   end
  116. end
  117. -- Table de correspondance entre nombres (0..15) et noms de couleur
  118. colorMap = {
  119.   [0]  = colors.black,
  120.   [1]  = colors.red,
  121.   [2]  = colors.green,
  122.   [3]  = colors.brown,
  123.   [4]  = colors.blue,
  124.   [5]  = colors.purple,
  125.   [6]  = colors.cyan,
  126.   [7]  = colors.lightGray,
  127.   [8]  = colors.gray,
  128.   [9]  = colors.pink,
  129.   [10] = colors.lime,
  130.   [11] = colors.yellow,
  131.   [12] = colors.lightBlue,
  132.   [13] = colors.magenta,
  133.   [14] = colors.orange,
  134.   [15] = colors.white
  135. }
  136.  
  137.  
  138. stations = loadStations("stations.txt")
  139.  
  140. locoData = {}
  141. while true do
  142.   local senderID, msg = rednet.receive(1)
  143.   if msg then
  144.     showStationGraph(msg)
  145.   end
  146.   showStations(stations)
  147.  
  148. end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement