Advertisement
DOGGYWOOF

Untitled

Jun 18th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. local function drawButton(x, y, width, height, text, textColor, bgColor)
  2. paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, bgColor or colors.lightGray)
  3. local textX = x + math.floor((width - #text) / 2)
  4. local textY = y + math.floor(height / 2)
  5. term.setCursorPos(textX, textY)
  6. term.setTextColor(textColor or colors.black)
  7. term.write(text)
  8. term.setTextColor(colors.white) -- Reset to default color
  9. end
  10.  
  11. local function drawExitButton()
  12. local width, _ = term.getSize()
  13. term.setCursorPos(width - 1, 1)
  14. term.setBackgroundColor(colors.black)
  15. term.setTextColor(colors.white)
  16. term.write("X")
  17. term.setBackgroundColor(colors.black)
  18. end
  19.  
  20. local function isInArea(x, y, xMin, yMin, xMax, yMax)
  21. return x >= xMin and x <= xMax and y >= yMin and y <= yMax
  22. end
  23.  
  24. local function getDiskID()
  25. local drive = peripheral.wrap("bottom")
  26. if drive and peripheral.getType("bottom") == "drive" then
  27. if drive.isDiskPresent() then
  28. local diskID = drive.getDiskID()
  29. return diskID
  30. else
  31. return nil, "No disk present in the drive."
  32. end
  33. else
  34. return nil, "No drive found at the bottom."
  35. end
  36. end
  37.  
  38. local function listCards()
  39. term.setCursorPos(1, 1)
  40. term.clear()
  41. drawExitButton()
  42. local cardDir = "/disk/Cards/"
  43. local files = fs.list(cardDir)
  44. if #files > 0 then
  45. print("Cards in /disk/Cards:")
  46. for _, file in ipairs(files) do
  47. print("- " .. file)
  48. end
  49. else
  50. print("No cards found in /disk/Cards.")
  51. end
  52.  
  53. while true do
  54. local event, button, x, y = os.pullEvent("mouse_click")
  55. local width, _ = term.getSize()
  56. if isInArea(x, y, width - 1, 1, width, 1) then
  57. return
  58. end
  59. end
  60. end
  61.  
  62. local function waitForDiskInsertion(cancelCallback)
  63. term.setCursorPos(1, 1)
  64. term.clear()
  65. drawExitButton()
  66. print("Waiting for disk insertion...")
  67. drawButton(2, 8, 12, 2, "Cancel", colors.white, colors.red)
  68.  
  69. while true do
  70. local event, button, x, y = os.pullEvent("mouse_click")
  71. local width, _ = term.getSize()
  72. if isInArea(x, y, width - 1, 1, width, 1) or isInArea(x, y, 2, 8, 13, 9) then
  73. cancelCallback()
  74. return nil, "Cancelled by user."
  75. end
  76.  
  77. local diskID, errorMsg = getDiskID()
  78. if diskID then
  79. return diskID, nil
  80. end
  81.  
  82. sleep(1) -- Wait for a short period before checking again
  83. end
  84. end
  85.  
  86. local function linkCard()
  87. term.setCursorPos(1, 1)
  88. term.clear()
  89. drawExitButton()
  90. print("Link Card:")
  91. drawButton(2, 2, 12, 2, "Insert Card", colors.black)
  92. drawButton(2, 5, 12, 2, "Enter ID", colors.black)
  93. drawButton(2, 8, 12, 2, "Back", colors.white, colors.red)
  94.  
  95. while true do
  96. local event, button, x, y = os.pullEvent("mouse_click")
  97. local width, _ = term.getSize()
  98. if isInArea(x, y, 2, 2, 13, 3) then
  99. local diskID, errorMsg = waitForDiskInsertion(linkCard)
  100. if diskID then
  101. local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
  102. local file = fs.open(cardFileName, "w")
  103. file.close()
  104. print("Card linked successfully.")
  105. sleep(2)
  106.  
  107. -- Eject the disk after linking the card
  108. local drive = peripheral.wrap("bottom")
  109. drive.ejectDisk()
  110.  
  111. return
  112. else
  113. print(errorMsg)
  114. sleep(2)
  115. return
  116. end
  117. elseif isInArea(x, y, 2, 5, 13, 6) then
  118. term.setCursorPos(1, 10)
  119. term.clearLine()
  120. print("Enter the card ID:")
  121. term.setCursorPos(1, 11)
  122. local cardID = read()
  123. local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
  124. local file = fs.open(cardFileName, "w")
  125. file.close()
  126. print("Card linked successfully.")
  127. sleep(2)
  128. return
  129. elseif isInArea(x, y, 2, 8, 13, 9) then
  130. return
  131. elseif isInArea(x, y, width - 1, 1, width, 1) then
  132. return
  133. end
  134. end
  135. end
  136.  
  137. local function unlinkCard()
  138. term.setCursorPos(1, 1)
  139. term.clear()
  140. drawExitButton()
  141. print("Unlink Card:")
  142. drawButton(2, 2, 12, 2, "Insert Card", colors.black)
  143. drawButton(2, 5, 12, 2, "Enter ID", colors.black)
  144. drawButton(2, 8, 12, 2, "Back", colors.white, colors.red)
  145.  
  146. while true do
  147. local event, button, x, y = os.pullEvent("mouse_click")
  148. local width, _ = term.getSize()
  149. if isInArea(x, y, 2, 2, 13, 3) then
  150. local diskID, errorMsg = waitForDiskInsertion(unlinkCard)
  151. if diskID then
  152. local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
  153. if fs.exists(cardFileName) then
  154. fs.delete(cardFileName)
  155. print("Card unlinked successfully.")
  156. else
  157. print("No matching card found to unlink.")
  158. end
  159. sleep(2)
  160.  
  161. -- Eject the disk after unlinking the card
  162. local drive = peripheral.wrap("bottom")
  163. drive.ejectDisk()
  164.  
  165. return
  166. else
  167. print(errorMsg)
  168. sleep(2)
  169. return
  170. end
  171. elseif isInArea(x, y, 2, 5, 13, 6) then
  172. term.setCursorPos(1, 10)
  173. term.clearLine()
  174. print("Enter the card ID:")
  175. term.setCursorPos(1, 11)
  176. local cardID = read()
  177. local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
  178. if fs.exists(cardFileName) then
  179. fs.delete(cardFileName)
  180. print("Card unlinked successfully.")
  181. else
  182. print("No matching card found to unlink.")
  183. end
  184. sleep(2)
  185. return
  186. elseif isInArea(x, y, 2, 8, 13, 9) then
  187. return
  188. elseif isInArea(x, y, width - 1, 1, width, 1) then
  189. return
  190. end
  191. end
  192. end
  193.  
  194. local function mainMenu()
  195. term.clear()
  196. drawExitButton()
  197. drawButton(2, 2, 12, 2, "List Cards", colors.black)
  198. drawButton(2, 5, 12, 2, "Link Card", colors.black)
  199. drawButton(2, 8, 12, 2, "Unlink Card", colors.black)
  200. drawButton(2, 11, 12, 2, "Exit", colors.white, colors.red)
  201.  
  202. while true do
  203. local event, button, x, y = os.pullEvent("mouse_click")
  204. local width, _ = term.getSize()
  205. if isInArea(x, y, 2, 2, 13, 3) then
  206. listCards()
  207. term.clear()
  208. mainMenu()
  209. elseif isInArea(x, y, 2, 5, 13, 6) then
  210. linkCard()
  211. term.clear()
  212. mainMenu()
  213. elseif isInArea(x, y, 2, 8, 13, 9) then
  214. unlinkCard()
  215. term.clear()
  216. mainMenu()
  217. elseif isInArea(x, y, 2, 11, 13, 12) then
  218. os.reboot()
  219. elseif isInArea(x, y, width - 1, 1, width, 1) then
  220. term.clear()
  221. mainMenu()
  222. end
  223. end
  224. end
  225.  
  226. mainMenu()
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement