SkyCrafter0

databaserReaderMonitor.lua

Oct 28th, 2020
1,465
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local databaseFile = "disk/shops.skydb"
  2. local adminPassword = "PVSfVF"    
  3.  
  4. local m = peripheral.find("monitor")
  5. local monX,monY = m.getSize()
  6.  
  7. local function save(tbl,name)
  8.     local file = fs.open(name,"w")
  9.     file.write(textutils.serialise(tbl))
  10.     file.close()
  11. end
  12.  
  13. local function load(name)
  14.     local file = fs.open(name,"r")
  15.     local data = file.readAll()
  16.     file.close()
  17.     return textutils.unserialise(data)
  18. end
  19.  
  20. local function centerText(text,y
  21.     local x = math.floor(monX/2) - math.floor(string.len(text)/2)
  22.     m.setCursorPos(x,y)
  23.     m.write(text)
  24. end
  25.  
  26. database = load(databaseFile)
  27. numData = #database
  28. cpn = 1
  29. dataWriting = false
  30.  
  31. local function checkCurNumber()
  32.     if cpn > numData then cpn = numData end
  33.     if cpn < 1 then cpn = 1 end
  34. end
  35. local function handleTouch()
  36.     while true do
  37.         local _,x,y = os.pullEvent("monitor_touch")
  38.         if x == 1 and y == 1 then cpn = cpn - 1 m.clear() end
  39.         if x == monX and y == 1 then cpn = cpn + 1 m.clear() end
  40.     end
  41. end
  42.  
  43. local function main()
  44.     while true do
  45.         m.setCursorPos(1,1)
  46.         m.setBackgroundColour(colours.blue)
  47.         m.clearLine()
  48.         m.setTextColour(colours.white)
  49.         centerText(tostring(cpn) .. "/" .. tostring(numData),1)
  50.         m.setCursorPos(1,1)
  51.         m.write("<")
  52.         m.setCursorPos(monX,1)
  53.         m.write(">")
  54.         m.setBackgroundColour(colours.black)
  55.         checkCurNumber()
  56.         if database[cpn] ~= nil then
  57.             m.setTextColour(colours.red)
  58.             centerText("Owner: "..database[cpn]["owner"],2)
  59.             m.setTextColour(colours.yellow)
  60.             centerText("Name: "..database[cpn]["name"],3)
  61.             m.setTextColour(colours.white)
  62.             m.setCursorPos(1,4)
  63.             print(database[cpn]["desc"])
  64.         end
  65.         sleep(0.1)
  66.     end
  67. end
  68. m.clear()
  69. parallel.waitForAny(handleKeys,main)
RAW Paste Data