csmit195

CamingLord: Resident List

Jun 7th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. -- Residental List Monitor by csmit195
  2.  
  3. local monitor = peripheral.wrap('right')
  4. local residentList = 'residents.txt'
  5. local authorisedTablet = 476
  6. local cur
  7.  
  8. -- Design Options
  9. monitor.setTextScale(2)
  10. monitor.setBackgroundColor(colors.white)
  11. monitor.clear()
  12.  
  13. -- Important Codes
  14. local termX, termY = term.getSize()
  15. rednet.open('bottom')
  16.  
  17. function showList()
  18.   -- Does List Exist
  19.   if ( fs.exists(residentList) ) then
  20.     local listHandle = fs.open(residentList, 'r')
  21.     local nativeTerm = term.redirect(monitor)
  22.     paintutils.drawFilledBox(1, 1, termX, 3, colors.lightBlue)
  23.     term.setCursorPos(1,2)
  24.     centerText('Current Residents')
  25.     term.setCursorPos(3,5)
  26.     monitor.setBackgroundColor(colors.white)
  27.     monitor.setTextColor(colors.black)
  28.     write(listHandle.readAll():gsub('\n','\n  '))
  29.     term.redirect(nativeTerm)
  30.     listHandle.close()    
  31.   else
  32.     local newList = fs.open(residentList, 'w')
  33.     newList.writeLine('ExampleUser1\nExampleUser2\nExampleUser3')
  34.     newList.close()
  35.     error('Resident List: residents.txt could not be found, one has been created, type "edit residents.txt" to modify it, and press Left+CTRL then Enter to bring up the save menu.')
  36.   end
  37. end
  38.  
  39. function waitForTablet()
  40.     local senderId, message, protocol = rednet.receive('resident_controller')
  41.     print(senderId..', '..message..', '..protocol)
  42.     if ( senderId == authorisedTablet ) then
  43.         if ( message == "rc_load" ) then
  44.             local residentFile = fs.open(residentList, 'r')
  45.             local residents = residentFile.readAll()
  46.             residentFile.close()
  47.             rednet.send(authorisedTablet, residents, 'resident_controller')
  48.         else
  49.             local residentFile = fs.open(residentList, 'w')
  50.             residentFile.write(message)
  51.             residentFile.close()
  52.             monitor.clear()
  53.             showList()
  54.         end
  55.     end
  56.     waitForTablet()
  57. end
  58.  
  59. -- Utility Functions
  60. function centerText(text)
  61.   local x, y = term.getSize()
  62.   local cX, cY = term.getCursorPos()
  63.   term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), cY)
  64.   write(text)  
  65. end
  66. showList()
  67. waitForTablet()
Add Comment
Please, Sign In to add comment