streqicity

Untitled

Jan 26th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. function CUI(m)
  2. n=1
  3. l=#m
  4. while true do
  5. term.clear()
  6. term.setCursorPos(1,2)
  7. for i=1, l, 1 do
  8. if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end
  9. end
  10. print("Select a number[arrow up/arrow down]")
  11. a, b= os.pullEventRaw()
  12. if a == "key" then
  13. if b==200 and n>1 then n=n-1 end
  14. if b==208 and n<=l then n=n+1 end
  15. if b==28 then break end
  16. end
  17. end
  18. term.clear() term.setCursorPos(1,1)
  19. return n
  20. end
  21. function displayMenu()
  22. local options={
  23. "TODO List",
  24. "Ender Dictionary",
  25. "Shit Post",
  26. "Exit"
  27. }
  28. local n=CUI(options)
  29. return n
  30. end
  31. function checkLock(doc)
  32. local file = fs.open("Locked.lua","r")
  33. local check = file.readLine()
  34. file.close()
  35. print(doc)
  36. if (check=="none") then
  37. local modem = peripheral.wrap("back")
  38. modem.open(998)
  39. modem.transmit(999,998,doc)
  40. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  41. print(message)
  42. if (message == "Locked") then
  43. return "Remote"
  44. else
  45. return "none"
  46. end
  47. elseif (""..check==""..doc) then
  48. saveAndUnlockDoc(doc)
  49. return "Local"
  50. end
  51.  
  52.  
  53.  
  54. end
  55. function saveAndUnlockDoc(doc)
  56.  
  57. local modem = peripheral.wrap("back")
  58. modem.open(994)
  59. local file = fs.open(doc..".lua","r")
  60. modem.transmit(995,994,doc..""..file.readAll())
  61. file.close()
  62.  
  63. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  64. print(message)
  65. if (message =="Success") then
  66. local lFile = fs.open("Locked.lua","w")
  67. lFile.write("none")
  68. lFile.close()
  69. print("Doc Saved")
  70. a, b= os.pullEvent("key")
  71. return
  72. else
  73. print("Doc Failed to save try again")
  74. a, b= os.pullEvent("key")
  75. return
  76. end
  77.  
  78. end
  79. function editAndLockDoc(doc)
  80.  
  81. local modem = peripheral.wrap("back")
  82. modem.open(996)
  83. modem.transmit(997,996,doc)
  84. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  85. local file = fs.open(doc..".lua","w")
  86. file.write(message)
  87. file.close()
  88. local lFile = fs.open("Locked.lua","w")
  89. lFile.write(doc.."")
  90. lFile.close()
  91. error("Document saved locally as "..doc..".lua Please make edits then come back to save and release lock")
  92.  
  93. end
  94. while true do
  95.  
  96. local task = displayMenu()
  97. if(task=="Exit")then
  98. error("Exited");
  99. end
  100. local lockedBy = checkLock(task)
  101. print(lockedBy)
  102. if (lockedBy=="none") then
  103. editAndLockDoc(task)
  104. elseif (lockedBy=="Local") then
  105. print("Document was locked by you")
  106. else
  107. print("Document is currently locked by another user")
  108. print("Press any key to go back to menu")
  109. a, b= os.pullEvent("key")
  110. end
  111. end
Add Comment
Please, Sign In to add comment