Guest User

portal

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