zeshan

Untitled

Dec 23rd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.54 KB | None | 0 0
  1. -- Simple Pin Pad by Cranium --
  2.  
  3. --[[ Config ]]--
  4.  
  5. local door = true
  6. --change this to true if using this as a door lock.
  7. local doorSide = "left"
  8. --change to the side your door is on if using as door lock
  9. local bios = false
  10. --change this to true if using this as an OS lock.
  11.  
  12. os.pullEvent = os.pullEventRaw
  13. local tX, tY = term.getSize()
  14.  
  15. --check if PIN exists
  16. if not fs.exists(".PIN") then
  17. local PIN = {"0","0","0","0"}
  18. local writeFile = fs.open(".PIN", "w")
  19. writeFile.write(textutils.serialize(PIN))
  20. writeFile.close()
  21. end
  22.  
  23. --gui functions
  24. local scannerFrame = {
  25. '+-+-+-+-+-+-+-+-+-+-+',
  26. '| |',
  27. '+ +',
  28. '| |',
  29. '+ +',
  30. '| |',
  31. '+ +',
  32. '| |',
  33. '+ +',
  34. '| |',
  35. '+ +',
  36. '| |',
  37. '+-+-+-+-+-+-+-+-+-+-+'
  38. }
  39. local scanner = {
  40. ' | | | | | | | | | ',
  41. '-+-+-+-+-+-+-+-+-+-',
  42. ' | | | | | | | | | ',
  43. '-+-+-+-+-+-+-+-+-+-',
  44. ' | | | | | | | | | ',
  45. '-+-+-+-+-+-+-+-+-+-',
  46. ' | | | | | | | | | ',
  47. '-+-+-+-+-+-+-+-+-+-',
  48. ' | | | | | | | | | ',
  49. '-+-+-+-+-+-+-+-+-+-',
  50. ' | | | | | | | | | '
  51. }
  52.  
  53.  
  54. -- lay out the button labels
  55. local labels = {
  56. ' 1 ', ' 2 ', ' 3 ',
  57. ' 4 ', ' 5 ', ' 6 ',
  58. ' 7 ', ' 8 ', ' 9 ',
  59. 'CLR', ' 0 ', 'ENT'
  60. }
  61.  
  62. -- generate the objects
  63. local objects = {}
  64. for i=1, #labels do
  65. table.insert(objects, {
  66. x = (((i - 1)%3 + 1)*5) - 2;
  67. y = (math.ceil(i/3) * 3) + 3;
  68. label = labels[i];
  69. -- make CLR red and ENT green
  70. color = i==10 and colors.red or i==12 and colors.green or colors.lightGray;
  71. })
  72. end
  73.  
  74. local function draw()
  75. term.setBackgroundColor(colors.black)
  76. term.clear()
  77. for i=1, #objects do
  78. local obj = objects[i]
  79. term.setTextColor(colors.gray)
  80.  
  81. for num, line in pairs{'+---+','| |','+---+'} do
  82. term.setCursorPos(obj.x, obj.y + num - 1)
  83. write(line)
  84. end
  85.  
  86. term.setCursorPos(obj.x+1, obj.y+1)
  87. term.setTextColor(obj.color)
  88. write(obj.label)
  89. end
  90. end
  91.  
  92. local function gui()
  93. term.setTextColor(colors.red)
  94. term.setCursorPos(1,1)
  95. write("."..string.rep("-", tX-2)..".")
  96. for i = 2, tY - 1 do
  97. term.setCursorPos(1,i)
  98. write("|")
  99. term.setCursorPos(tX,i)
  100. write("|")
  101. end
  102. term.setCursorPos(1,tY)
  103. write("'"..string.rep("-", tX-2).."'")
  104. term.setTextColor(colors.yellow)
  105. end
  106.  
  107. local function display(scan)
  108. term.setTextColor(colors.black)
  109. term.setBackgroundColor(colors.lightGray)
  110. for i = 3, 5 do
  111. term.setCursorPos(3,i)
  112. write(string.rep(" ", 15))
  113. end
  114. term.setTextColor(colors.gray)
  115. term.setBackgroundColor(colors.black)
  116. for i = 1, #scannerFrame do
  117. term.setCursorPos(24,i + 4)
  118. write(scannerFrame[i])
  119. end
  120. term.setTextColor(scan and colors.gray or colors.lightGray)
  121. term.setBackgroundColor(scan and colors.lime or colors.green)
  122. for i = 1, #scanner do
  123. term.setCursorPos(25,i + 5)
  124. write(scanner[i])
  125. end
  126. end
  127.  
  128. --PIN functions
  129. --retrieve PIN from file
  130. local function retrieve()
  131. local readFile = fs.open(".PIN", "r")
  132. local pinText = readFile.readAll()
  133. readFile.close()
  134. local pinTab = textutils.unserialize(pinText)
  135. return pinTab
  136. end
  137.  
  138. --create new PIN
  139. local function saveNew(pin)
  140. if fs.exists(".PIN") then
  141. fs.delete(".PIN")
  142. end
  143. local newFile = fs.open(".PIN", "w")
  144. newFile.write(textutils.serialize(pin))
  145. newFile.close()
  146. end
  147.  
  148. local function takeNumbers()
  149. local verPin = {}
  150. while true do
  151. local events = {os.pullEvent()}
  152. if events[1] == "mouse_click" and events[2] == 1 then
  153. if events[3] >= 3 and events[3] <= 7 and events[4] >= 6 and events[4] <= 8 then
  154. table.insert(verPin, 1)
  155. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 6 and events[4] <= 8 then
  156. table.insert(verPin, 2)
  157. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 6 and events[4] <= 8 then
  158. table.insert(verPin, 3)
  159. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 9 and events[4] <= 11 then
  160. table.insert(verPin, 4)
  161. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 9 and events[4] <= 11 then
  162. table.insert(verPin, 5)
  163. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 9 and events[4] <= 11 then
  164. table.insert(verPin, 6)
  165. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 12 and events[4] <= 14 then
  166. table.insert(verPin, 7)
  167. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 12 and events[4] <= 14 then
  168. table.insert(verPin, 8)
  169. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 12 and events[4] <= 14 then
  170. table.insert(verPin, 9)
  171. elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 15 and events[4] <= 17 then
  172. verPin = {}
  173. elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 15 and events[4] <= 17 then
  174. table.insert(verPin, 0)
  175. elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 15 and events[4] <= 17 then
  176. if #verPin == 4 then break end
  177. end
  178. if # verPin >= 5 then table.remove(verPin, #verPin) end
  179. display()
  180. for i = 1, #verPin do
  181. term.setTextColor(colors.red)
  182. term.setBackgroundColor(colors.lightGray)
  183. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,3)
  184. write("\\|/")
  185. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,4)
  186. write("-+-")
  187. term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,5)
  188. write("/|\\")
  189. end
  190. end
  191. end
  192. return verPin
  193. end
  194.  
  195. --verification
  196. local function verify()
  197. local curPin = retrieve()
  198. if tonumber(curPin[1]) == 0 and tonumber(curPin[2]) == 0 and tonumber(curPin[3]) == 0 and tonumber(curPin[4]) == 0 then
  199. term.setCursorPos(5,4)
  200. term.setTextColor(colors.yellow)
  201. term.setBackgroundColor(colors.lightGray)
  202. write("Create PIN")
  203. local newPin = takeNumbers()
  204. saveNew(newPin)
  205. return "NEW"
  206. else
  207. term.setCursorPos(5,4)
  208. term.setTextColor(colors.yellow)
  209. term.setBackgroundColor(colors.lightGray)
  210. write("Enter PIN")
  211. local checkPin = takeNumbers()
  212. if tonumber(checkPin[1]) == tonumber(curPin[1]) and tonumber(checkPin[2]) == tonumber(curPin[2]) and tonumber(checkPin[3]) == tonumber(curPin[3]) and tonumber(checkPin[4]) == tonumber(curPin[4]) then
  213. return true
  214. else
  215. return false
  216. end
  217. end
  218. end
  219.  
  220. --operation loop
  221. if door and bios then
  222. print("Error occured in configuration.")
  223. print(shell.getRunningProgram().." cannot be used as a door lock as well as an OS protection")
  224. print("Please check config options and try again")
  225. return
  226. end
  227.  
  228. while true do
  229. draw()
  230. gui()
  231. display(false)
  232. local state = verify()
  233. if door and state == true then
  234. display(true)
  235. term.setCursorPos(5,3)
  236. term.setTextColor(colors.yellow)
  237. term.setBackgroundColor(colors.lightGray)
  238. write("Place hand")
  239. term.setCursorPos(5,5)
  240. write("on scanner")
  241. while true do
  242. local events = {os.pullEvent()}
  243. if events[1] == "mouse_click" and events[2] == 1 then
  244. if events[3] >= 25 and events[3] <= 43 then
  245. if events[4] >= 6 and events[4] <= 16 then
  246. for i = 1, #scanner do
  247. term.setTextColor(colors.gray)
  248. term.setCursorPos(25,i + 5)
  249. term.setBackgroundColor(colors.yellow)
  250. write(scanner[i])
  251. sleep(.1)
  252. term.setCursorPos(25, i + 5)
  253. term.setBackgroundColor(colors.lime)
  254. write(scanner[i])
  255. end
  256. for i = #scanner, 1, -1 do
  257. term.setTextColor(colors.gray)
  258. term.setCursorPos(25,i + 5)
  259. term.setBackgroundColor(colors.yellow)
  260. write(scanner[i])
  261. sleep(.1)
  262. term.setCursorPos(25, i + 5)
  263. term.setBackgroundColor(colors.lime)
  264. write(scanner[i])
  265. end
  266. break
  267. end
  268. end
  269. end
  270. end
  271. rs.setOutput(doorSide, true)
  272. sleep(3)
  273. rs.setOutput(doorSide, false)
  274. elseif bios and state == true then
  275. display(true)
  276. term.setCursorPos(5,3)
  277. term.setTextColor(colors.yellow)
  278. term.setBackgroundColor(colors.lightGray)
  279. write("Place hand")
  280. term.setCursorPos(5,5)
  281. write("on scanner")
  282. while true do
  283. local events = {os.pullEvent()}
  284. if events[1] == "mouse_click" and events[2] == 1 then
  285. if events[3] >= 25 and events[3] <= 43 then
  286. if events[4] >= 6 and events[4] <= 16 then
  287. for i = 1, #scanner do
  288. term.setTextColor(colors.gray)
  289. term.setCursorPos(25,i + 5)
  290. term.setBackgroundColor(colors.yellow)
  291. write(scanner[i])
  292. sleep(.1)
  293. term.setCursorPos(25, i + 5)
  294. term.setBackgroundColor(colors.lime)
  295. write(scanner[i])
  296. end
  297. for i = #scanner, 1, -1 do
  298. term.setTextColor(colors.gray)
  299. term.setCursorPos(25,i + 5)
  300. term.setBackgroundColor(colors.yellow)
  301. write(scanner[i])
  302. sleep(.1)
  303. term.setCursorPos(25, i + 5)
  304. term.setBackgroundColor(colors.lime)
  305. write(scanner[i])
  306. end
  307. break
  308. end
  309. end
  310. end
  311. end
  312. term.setTextColor(colors.white)
  313. term.setBackgroundColor(colors.black)
  314. term.clear()
  315. term.setCursorPos(1,1)
  316. break
  317. elseif not state == true then
  318. display(false)
  319. term.setCursorPos(6,4)
  320. term.setTextColor(colors.yellow)
  321. term.setBackgroundColor(colors.lightGray)
  322. write("Incorrect")
  323. sleep(2)
  324. end
  325. end
Advertisement
Add Comment
Please, Sign In to add comment