Advertisement
Darking560

RFtools teleporter

Jun 20th, 2015
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. --1.1.3
  2.  
  3. local filtre = "Darking"
  4. local xmax
  5. local xmin
  6. local pas
  7. local maxline
  8. local pageMax
  9. local page
  10.  
  11. local continuer = true
  12. local monSize = nil
  13. local dialSize = nil
  14. local destination= {}
  15. local listPerso = true
  16.  
  17. local current_version = "1.1.3"
  18. local code_pastebin = "bM8xpdHW"
  19. function version(version,code)
  20.  
  21.     local function versionSuperieur(ver1,ver2,loc)
  22.             if loc <= string.len(ver2) then
  23.                     nb1 = tonumber(string.sub(ver1,loc,loc))
  24.                     nb2 = tonumber(string.sub(ver2,loc,loc))
  25.                     if nb1 == nil then nb1=0 end
  26.                     if nb1 > nb2 then
  27.                             return false
  28.                     elseif nb1 < nb2 then
  29.                             return true
  30.                     else
  31.                             return versionSuperieur(ver1,ver2,loc+2)
  32.                     end
  33.             else
  34.                     return false
  35.             end
  36.     end
  37.  
  38.     local data = http.get("http://pastebin.com/raw.php?i="..code)
  39.     if data then
  40.             print("Download successful")
  41.             local line = data.readLine()
  42.             line = string.sub(line,3)
  43.             if versionSuperieur(version,line,1) then
  44.                     print("Mise a jour en cour, ...")
  45.                     fs.move("startup","delete")
  46.                     local file = fs.open("startup","w")
  47.                     file.write(data.readAll())
  48.                     file.close()
  49.                     print("Mise a jour reussite, reboot !")
  50.                     sleep(1)
  51.                     os.reboot()
  52.             else
  53.                     print("Pas de nouvelle Version disponible !")
  54.             end
  55.     else
  56.             print("Erreur connection Pastebin, echec recherche update")
  57.     end
  58.     if fs.exists("delete") then
  59.             print("Mise a jour completement etablie")
  60.             fs.delete("delete")
  61.     end
  62.     print("Version: "..current_version)
  63. end
  64. version(current_version,code_pastebin)
  65.  
  66. function loadFiltre()
  67.     if not fs.exists("config") then
  68.         config = fs.open("config","w")
  69.         print("Vous n'avez pas configurer de filtre :")
  70.         filtre = read()
  71.         config.write(filtre)
  72.         config.close()
  73.     end
  74.     config = fs.open("config","r")
  75.     filtre = config.readLine()
  76.     config.close()
  77. end
  78. loadFiltre()
  79.  
  80. function checkPeripheral()
  81.     print("-------Check Peripheral-------")
  82.     for k,v in pairs(peripheral.getNames()) do
  83.         if string.find(v,"monitor") then
  84.             monSize = v
  85.         elseif string.find(v,"dialing_device") then
  86.             dialSize = v
  87.         end
  88.         for n,m in pairs(rs.getSides())  do
  89.             if m == v then
  90.                 if peripheral.getType(v) == "monitor" then
  91.                     monSize = v
  92.                 elseif peripheral.getType(v) == "dialing_device" then
  93.                     dialSize = v
  94.                 end
  95.             end
  96.         end
  97.     end
  98.     if monSize ~= nil then
  99.         print("Found monitor as "..monSize)
  100.     else
  101.         print("ERROR:Monitor don't found")
  102.         return(false)
  103.     end
  104.     if dialSize ~= nil then
  105.         print("Found Dialing Device as "..dialSize)
  106.     else
  107.         print("ERROR:Dialing Device don't found")
  108.         return(false)
  109.     end
  110.     print("-------End Check Peripheral-------")
  111.     return(true)
  112. end
  113.  
  114. function setDestination()
  115.     print("-------Set Destination-------")
  116.     destination = {}
  117.     for i=0, dial.getReceiverCount()-1 do
  118.         name = dial.getReceiverName(i)
  119.         if name ~= dial.getTransmitterName(0) then
  120.             list = {["name"]=name;["number"]=i}
  121.             print("Add "..name.." at number "..i)
  122.             if string.find(name, filtre) then
  123.                 list["perso"] = true
  124.             else
  125.                 list["perso"] = false
  126.             end
  127.             table.insert(destination, list)
  128.         end
  129.     end
  130.     print("-------End Set Destination-------")
  131. end
  132.  
  133. function dialOnce(nb)
  134.     dial.dialOnce(0,nb)
  135. end
  136.  
  137. function heading(text)
  138.    w, h = mon.getSize()
  139.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  140.    mon.write(text)
  141. end
  142.  
  143. function button(text, line, color, xmin, xmax)
  144.     mon.setBackgroundColor(color)
  145.         local xspot = math.floor((xmax - xmin - string.len(text)) /2) +1
  146.         mon.setCursorPos(xmin, line)
  147.         for k = 0, xmax - xmin - string.len(text) +1 do
  148.             if k == xspot then
  149.                 mon.write(text)
  150.             else
  151.                 mon.write(" ")
  152.             end
  153.         end
  154.         mon.setBackgroundColor(colors.black)
  155. end
  156.  
  157. function listing()
  158.     local listTemp = {}
  159.     if listPerso then
  160.         for i,item in pairs(destination) do
  161.             if item["perso"] then
  162.                 table.insert(listTemp, item)
  163.             end
  164.         end
  165.     else
  166.         listTemp = destination
  167.     end
  168.     local pageMax = math.ceil(#listTemp/maxline)
  169.     local list = {}
  170.     for i=((page-1)*maxline)+1, (page*maxline) do
  171.         table.insert(list,listTemp[i])
  172.     end
  173.     return list, pageMax
  174. end
  175.  
  176. function screen(pageMax, list)
  177.     mon.setBackgroundColor(colors.black)
  178.     mon.clear()
  179.     heading("Choose you destination")
  180.    
  181.     button("Perso", 3, persoColor, xmin-1, xmin+6)
  182.     button("Tout", 3, totalColor, xmax-6, xmax+1)
  183.    
  184.     if page > 1 then
  185.         button("Prev.", heigh, colors.red, xmin-1, xmin+6)
  186.     elseif page < pageMax then
  187.         button("Next", heigh, colors.red, xmax-6, xmax+1)
  188.     end
  189.     for i, item in pairs(list) do
  190.         button(item["name"], 5+(2*(i-1)), colors.red, xmin, xmax)
  191.     end
  192. end
  193.  
  194. function checkClic(x, y, list,pageMax)
  195.     for i, item in pairs(list) do
  196.       if x>=xmin and  x <= xmax then
  197.          if y == (5+(2*(i-1))) then
  198.             print("Dial with "..item["name"])
  199.             dialOnce(item["number"])
  200.             button(item["name"], (5+(2*(i-1))), colors.lime, xmin, xmax)
  201.             sleep(0.5)
  202.          end
  203.       end
  204.    end
  205.    if y == 3 then
  206.         if x>=xmin-1 and  x <= xmin+6 then
  207.             print("Button : Perso clic")
  208.             persoColor = colors.lime
  209.             totalColor = colors.red
  210.             listPerso = true
  211.             page = 1
  212.         elseif x>=xmax-6 and  x <= xmax+1 then
  213.             print("Button : Tout clic")
  214.             persoColor = colors.red
  215.             totalColor = colors.lime
  216.             listPerso = false
  217.             page = 1
  218.         end
  219.     elseif y == heigh then
  220.         if x>=xmin-1 and  x <= xmin+6 and page > 1 then
  221.             print("Button : previous clic")
  222.             page = page - 1
  223.         elseif x>=xmax-6 and  x <= xmax+1 and page < pageMax then
  224.             print("Button : next clic")
  225.             page = page + 1
  226.         end
  227.     end
  228.  end
  229.  
  230. continuer = checkPeripheral()
  231.  
  232. mon = peripheral.wrap(monSize)
  233. dial = peripheral.wrap(dialSize)
  234.  
  235. width, heigh = mon.getSize()
  236. xmax = width-3
  237. xmin = 3
  238. pas = 2
  239. maxline = math.floor(((heigh-2)-4)/pas)
  240.  
  241. setDestination()
  242. local timerID = os.startTimer(30)
  243. local timer2ID = os.startTimer(60)
  244.  
  245. persoColor = colors.lime
  246. totalColor = colors.red
  247. page = 1
  248.  
  249. while continuer do
  250.     listActive, pageMax = listing()
  251.     screen(pageMax, listActive)
  252.     event, arg2, x, y = os.pullEvent()
  253.     if event == "monitor_touch" then
  254.         checkClic(x, y, listActive,pageMax)
  255.     elseif event == "timer" then
  256.         setDestination()
  257.         os.cancelTimer(timer2ID)
  258.         timerID = os.startTimer(30)
  259.         timer2ID = os.startTimer(60)
  260.     end
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement