Advertisement
konalisp

secrettv.lua

May 27th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | None | 0 0
  1. tArgs = {...}
  2. local monitor = term
  3. local side = "front"
  4. local input = ""
  5. local code = "1234"
  6. local countdown = 3
  7. local r = false
  8. local unlocked = false
  9.  
  10.  
  11. if tArgs[1] ~= nil then
  12.     if tArgs[1] == "term" then monitor = tArgs[1] else
  13.     monitor = peripheral.wrap(tArgs[1]) end
  14. end
  15. if tArgs[2] ~= nil then code = tostring(tArgs[2]) end
  16. if tArgs[3] ~= nil then side = tArgs[3] end
  17.  
  18. local function render_keypad()
  19.     monitor.setBackgroundColor(colors.gray)
  20.     monitor.setTextColor(colors.lightGray)
  21.     monitor.clear()
  22.     monitor.setCursorPos(1,1)
  23.     monitor.write(" 1 2 3")
  24.     monitor.setCursorPos(1,2)
  25.     monitor.write(" 4 5 6")
  26.     monitor.setCursorPos(1,3)
  27.     monitor.write(" 7 8 9")
  28.     monitor.setCursorPos(1,4)
  29.     monitor.setTextColor(colors.red)
  30.     monitor.write(" C")
  31.     monitor.setTextColor(colors.lightGray)
  32.     monitor.write(" 0")
  33.     monitor.setTextColor(colors.green)
  34.     monitor.write(" E")
  35.    
  36.     if string.len(tostring(input)) < 8 then
  37.         monitor.setCursorPos(1,5)
  38.         monitor.setTextColor(colors.white)
  39.         monitor.write(input)
  40.     end
  41.    
  42.     monitor.setTextColor(colors.lightGray)
  43. end
  44.  
  45. local function render_error()
  46.     monitor.setBackgroundColor(colors.red)
  47.     monitor.setTextColor(colors.white)
  48.     monitor.clear()
  49.     monitor.setCursorPos(1,3)
  50.     monitor.write(" ERROR")
  51.     os.sleep(3)
  52.     return true
  53. end
  54.  
  55. local function render_success()
  56.     local cd = countdown
  57.     if cd > 9 then cd = 9 end
  58.     monitor.setBackgroundColor(colors.blue)
  59.     monitor.setTextColor(colors.white)
  60.     monitor.clear()
  61.     monitor.setCursorPos(1,2)
  62.     monitor.write("CORRECT")
  63.     monitor.setCursorPos(1,4)
  64.     monitor.write("Tap To")
  65.     while cd > 0 do
  66.         monitor.setCursorPos(1,5)
  67.         monitor.write("Lock(" .. cd .. ")")
  68.         os.sleep(1)
  69.         cd = cd - 1
  70.     end
  71.     monitor.setCursorPos(1,5)
  72.     monitor.write("Lock   ")
  73.     return true
  74. end
  75.  
  76. local function render_tv(b)
  77.     if b == nil then b = false end
  78.     monitor.setBackgroundColor(colors.white)
  79.     monitor.setTextColor(colors.white)
  80.     monitor.clear()
  81.     monitor.setCursorPos(1,1)
  82.     monitor.setBackgroundColor(colors.cyan)
  83.     monitor.write("       ")
  84.     monitor.setCursorPos(1,2)
  85.     monitor.setBackgroundColor(colors.lightBlue)
  86.     monitor.write("       ")
  87.     monitor.setCursorPos(1,2)
  88.     monitor.setBackgroundColor(colors.cyan)
  89.     monitor.write(" ")
  90.     monitor.setCursorPos(7,2)
  91.     monitor.write(" ")
  92.     monitor.setBackgroundColor(colors.lightBlue)
  93.     monitor.setCursorPos(1,3)
  94.     monitor.write("       ")
  95.     monitor.setCursorPos(3,3)
  96.     monitor.setBackgroundColor(colors.yellow)
  97.     monitor.write("   ") --sun
  98.     monitor.setCursorPos(1,4)
  99.     monitor.setBackgroundColor(colors.lime)
  100.     monitor.write("       ")
  101.     monitor.setCursorPos(1,5)
  102.     monitor.setBackgroundColor(colors.green)
  103.     monitor.write("       ")
  104.    
  105.     monitor.setCursorPos(1,1)
  106.     monitor.setBackgroundColor(colors.cyan)
  107.     if r == false then
  108.         monitor.write("       ")
  109.     else
  110.         monitor.write(" RELAX ")
  111.     end
  112.     if b == true then os.sleep(1) end
  113.     r = not r
  114. end
  115.  
  116. local function get_keys()
  117.     local event, eside, x, y = os.pullEvent("monitor_touch")
  118.     local keys = {
  119.         {1, 2, 1 },
  120.         {1, 4, 2 },
  121.         {1, 6, 3 },
  122.         {2, 2, 4 },
  123.         {2, 4, 5 },
  124.         {2, 6, 6 },
  125.         {3, 2, 7 },
  126.         {3, 4, 8 },
  127.         {3, 6, 9 },
  128.         {4, 2, 10},
  129.         {4, 4, 0 },
  130.         {4, 6, 11},
  131.     }
  132.    
  133.     for _, i in pairs(keys) do
  134.         if y == i[1] and x == i[2] then return i[3] end
  135.     end
  136.     return 12
  137. end
  138.  
  139. local function check_code()
  140.     if input == code then
  141.         redstone.setOutput(side, true)
  142.         render_success()
  143.         input = ""
  144.         return true
  145.     else
  146.         render_error()
  147.         input = ""
  148.         render_keypad()
  149.         return false
  150.     end
  151. end
  152.  
  153. local function chkfile(cnffile)
  154.     if not fs.exists(cnffile) then
  155.         local h = fs.open(cnffile, "w")
  156.         h.write("f")
  157.         h.close()
  158.     end
  159. end
  160.  
  161. local function save(modeo)
  162.     local cnffile = "keypad.reg"
  163.     chkfile(cnffile)
  164.     local h = nil
  165.     if modeo == "t" then
  166.         h = fs.open(cnffile , "w")
  167.         h.write("t")
  168.     elseif modeo == "f" then
  169.         h = fs.open(cnffile , "w")
  170.         h.write("f")
  171.     elseif modeo == "c" then
  172.         h = fs.open(cnffile , "r")
  173.         local r = h.readLine()
  174.         h.close()
  175.         return r
  176.     else
  177.         h = fs.open(cnffile , "w")
  178.         if modeo == nil then modeo = "" end
  179.         h.write(modeo)
  180.     end
  181.     h.close()
  182.     return true
  183. end
  184.  
  185. local function unlock()
  186.     save("t")
  187.     local event, eside, x, y = os.pullEvent("monitor_touch")
  188.     save("f")
  189.     redstone.setOutput(side, false)
  190.     --render_keypad()
  191.     unlocked = false
  192. end
  193.  
  194. local function main()
  195.     local st = save("c")
  196.     if st == "t" then
  197.         redstone.setOutput(side, true)
  198.         render_success()
  199.         unlock()
  200.     elseif st ~= "f" and st ~= "t" then
  201.         input = st
  202.     end
  203.    
  204.     while (true) do
  205.         if unlocked == false then
  206.             while(true) do
  207.                 local timeup = os.startTimer(1)
  208.                 local bb = false
  209.                 (function()
  210.                     render_tv()
  211.                     local event, par1, x, y = os.pullEvent()
  212.                     if event == "timer" and par1 == timeup then return
  213.                     elseif event == "monitor_touch" then bb = true end
  214.                 end)()
  215.                 if bb == true then break end
  216.             end
  217.             unlocked = true
  218.         else
  219.         render_keypad()
  220.         local p = get_keys()
  221.         if p <= 9 then
  222.             if input == nil then input = "" end
  223.             input = input .. tostring(p)
  224.             if string.len(tostring(input)) > 7 then input = string.sub(input, 2) end
  225.             save(input)
  226.         elseif p == 10 then
  227.             input = ""
  228.             save(input)
  229.         elseif p == 11 then
  230.             if check_code() == true then
  231.                 save(input)
  232.                 unlock()
  233.             end
  234.         else end
  235.         end
  236.     end
  237. end
  238.  
  239. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement