Advertisement
Guest User

portal

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