Advertisement
Guest User

startup

a guest
Apr 24th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. --von Direwolf20 --Uebersetzung und Erklaerung von Sp8ter
  2. os.loadAPI("button")
  3. m = peripheral.wrap("back")
  4. --Monitor auf der Rueckseite
  5. m.clear()
  6. rednet.open("right")
  7. --Wirelessmodem auf der rechten Seite
  8. local page = 1
  9. local pages = 0
  10. local names = {}
  11. local turtles = {}
  12. local remove = false
  13.  
  14. function fillTurtles()
  15.    turtles[1] = 7
  16. --Turtle ID eintragen  
  17. --   turtles[2] = 114
  18. --   turtles[3] = 117
  19. end
  20.  
  21. function fillTable()
  22.    m.clear()
  23.    button.clearTable()
  24.    local totalrows = 0
  25.    local numNames = 0
  26.    local col = 2
  27.    local row = 12
  28.    local countRow = 1
  29.    local currName = 0
  30.    local npp = 12 --names per page
  31.    for turt, data in pairs(names) do
  32.       for i,j in pairs(data) do
  33.          totalrows = totalrows+1
  34.       end
  35.    end
  36.    pages = math.ceil(totalrows/npp)
  37.    print(totalrows)
  38.    for turt, data in pairs(names) do
  39.       currName = 0
  40.       for slot, name in pairs(data) do
  41.        currName = currName + 1
  42.        if currName > npp*(page-1) and currName < npp*page+1 then
  43.          row = 4+(countRow)
  44.          names[turt][slot] = string.sub(name, 0, 17)
  45.          button.setTable(string.sub(name, 0, 17), runStuff, turt..":"..slot, col, col+17 , row, row)
  46.          if col == 21 then
  47.            col = 2
  48.            countRow = countRow + 2
  49.          else
  50.            col = col+19
  51.          end
  52.        end
  53.       end
  54.    end
  55.    button.setTable("Seite vor", nextPage, "", 21, 38, 1, 1)
  56.    button.setTable("Seite zurueck", prevPage, "", 2, 19, 1, 1)
  57.    button.setTable("Refresh", checkNames, "", 21, 38, 19, 19)
  58. --   button.setTable("Remove Book", removeIt, "", 2, 19, 19, 19)
  59.    button.label(15,3, "Seite: "..tostring(page).." of "..tostring(pages))
  60.    button.screen()
  61. end      
  62.  
  63. function nextPage()
  64.    if page+1 <= pages then page = page+1 end
  65.    fillTable()
  66. end
  67.  
  68. function prevPage()
  69.    if page-1 >= 1 then page = page-1 end
  70.    fillTable()
  71. end  
  72.                            
  73. function getNames()
  74.    names = {}
  75.    for x, y in pairs(turtles) do
  76.       names[y] = {}
  77.       rednet.send(y, "getNames")
  78.       local id, msg, dist = rednet.receive(2)
  79. --      print(msg)
  80.       names[y] = textutils.unserialize(msg)
  81.    end
  82. end
  83.  
  84. function removeIt()
  85.    remove = not remove
  86. --   print(remove)
  87.    button.toggleButton("Remove Book")
  88. end
  89.  
  90. function runStuff(info)
  91.    if remove == true then
  92.       removeBook(info)
  93.    else
  94.       openPortal(info)
  95.    end      
  96. end
  97.  
  98. function removeBook(info)
  99.    local turt, slot = string.match(info, "(%d+):(%d+)")
  100.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  101.    data = "remove"..tostring(slot)
  102.    rednet.send(tonumber(turt), data)
  103.    rednet.receive()
  104.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  105.    remove=false
  106.    button.toggleButton("Remove Book")
  107. --   sleep(1)
  108.    checkNames()
  109. end  
  110.  
  111. function openPortal(info)
  112.    local turt,slot = string.match(info, "(%d+):(%d+)")
  113. --   print(names[tonumber(turt)][tonumber(slot)])
  114.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  115.    print(names[tonumber(turt)][tonumber(slot)])
  116.    data = "books"..tostring(slot)
  117.    rednet.send(tonumber(turt), data)
  118.    rednet.receive()
  119.    button.toggleButton(names[tonumber(turt)][tonumber(slot)])
  120. end
  121.  
  122. function checkNames()
  123.    button.flash("Refresh")
  124.    for num, turt in pairs(turtles) do
  125.      rednet.send(turt, "checkSlots")
  126.      msg = ""
  127.      while msg ~= "done" do
  128.        id, msg, dist = rednet.receive()
  129.        if msg == "getName" then
  130.           m.clear()
  131.           m.setCursorPos(5, 12)
  132.           m.write("New book detected.")
  133.           m.setCursorPos(5, 14)
  134.           m.write("Please enter the name")
  135.           m.setCursorPos(5, 16)
  136.           m.write("On the computer")
  137.           m.setCursorPos(5, 18)
  138.           m.write("<<----")
  139.           term.clear()
  140.           term.write("Please enter a name for the new book: ")
  141.           name = read()
  142.           rednet.send(id, name)
  143.        end
  144.      end
  145.    end
  146.    getNames()
  147.    fillTable()
  148. end
  149.  
  150. function getClick()
  151.    event, side, x,y = os.pullEvent()
  152.    if event == "monitor_touch" then
  153.       button.checkxy(x,y)
  154.    elseif event == "redstone" then
  155.       print("redstone")
  156.       sleep(5)
  157.       checkNames()      
  158.    end
  159. end
  160.  
  161. fillTurtles()
  162. fillTable()
  163. checkNames()
  164.  
  165.  
  166. while true do
  167.    getClick()
  168. --   checkNames()
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement