Advertisement
gknova61

Shop Registry (team-econ)

Nov 15th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.66 KB | None | 0 0
  1. --Originally by team-econ
  2. os.loadAPI("APIs/config")
  3.  
  4. local function startScreen()
  5. redraw()
  6. end
  7.  
  8. local function newmenu(tList,x,y,height)
  9.         local function maxlen(t)
  10.                 local len=0
  11.                 for i=1,#t do
  12.                         local curlen=string.len(type(t[i])=='table' and t[i][1] or t[i])
  13.                         if curlen>len then len=curlen end
  14.                 end
  15.                 return len
  16.         end
  17.        
  18.         local max=maxlen(tList)
  19.         x=x or 1
  20.         y=y or 1
  21.         y=y-1
  22.         height=height or #tList
  23.         height=height+1
  24.         local selected=1
  25.         local scrolled=0
  26.         local function render()
  27.                 for num,item in ipairs(tList) do
  28.                         if num>scrolled and num<scrolled+height then
  29.                                 term.setCursorPos(x,y+num-scrolled)
  30.                                 local current=(type(item)=='table' and item[1] or item)
  31.                                 write((num==selected and '[' or ' ')..current..(num==selected and ']' or ' ')..(max-#current>0 and string.rep(' ',max-#current) or ''))
  32.                         end
  33.                 end
  34.         end
  35.         while true do
  36.                 render()
  37.                 local evts={os.pullEvent('key')}
  38.                 if evts[1]=="key" and evts[2]==200 and selected>1 then
  39.                         if selected-1<=scrolled then scrolled=scrolled-1 end
  40.                         selected=selected-1
  41.                 elseif evts[1]=="key" and evts[2]==208 and selected<#tList then
  42.                         selected=selected+1
  43.                         if selected>=height+scrolled then scrolled=scrolled+1 end
  44.                 elseif evts[1]=="key" and evts[2]==28 or evts[2]==156 then
  45.                         return (type(tList[selected])=='table' and tList[selected][2](tList[selected][1]) or tList[selected])
  46.                 end
  47.         end
  48. end
  49.  
  50. local function limitRead(limX, rChar)
  51. term.setCursorBlink(true)
  52. local origX, origY = term.getCursorPos()
  53. local returnString = ""
  54. while true do
  55.         local xPos, yPos = term.getCursorPos()
  56.         local event, p1, p2 = os.pullEvent()
  57.         if event == "char" then
  58.                 returnString = returnString..p1
  59.                 if not rChar then
  60.                         if not limX then
  61.                                 write(p1)
  62.                         else
  63.                                 if string.len(returnString) >= limX then
  64.                                         term.setCursorPos(origX, origY)
  65.                                         write(string.sub(returnString, (string.len(returnString)-limX)+1))
  66.                                 elseif string.len(returnString) < limX then
  67.                                         write(p1)
  68.                                 end
  69.                         end
  70.                 else
  71.                         if not limX then
  72.                                 write(rChar)
  73.                         else
  74.                                 if string.len(returnString) >= limX then
  75.                                         term.setCursorPos(origX, origY)
  76.                                         write(string.rep(rChar, limX))
  77.                                 elseif string.len(returnString) < limX then
  78.                                         write(rChar)
  79.                                 end
  80.                         end
  81.                 end
  82.         elseif event == "key" and p1 == 14 then --backspace
  83.                 returnString = string.sub(returnString, 1, (string.len(returnString))-1)
  84.                 term.setCursorPos(xPos-1,yPos)
  85.                 write(" ")
  86.                 term.setCursorPos(origX, origY)
  87.                 if string.len(returnString) >= limX then
  88.                         if not rChar then
  89.                                 write(string.sub(returnString, (string.len(returnString)-limX)+1))
  90.                         else
  91.                                 write(string.rep(rChar,limX))
  92.                         end
  93.                 else
  94.                         if not rChar then
  95.                                 write(returnString)
  96.                         else
  97.                                 write(string.rep(rChar, string.len(returnString)))
  98.                         end
  99.                 end
  100.         elseif event == "key" and p1 == 28 then --enter
  101.                 break
  102.         end
  103. end
  104. term.setCursorBlink(false)
  105. return returnString
  106. end
  107.  
  108. local function clearscreen()
  109.     term.clear()
  110.     term.setCursorPos(1,1)
  111. end
  112. clearscreen()
  113.  
  114. local function accCreate(auserid,adpass,aign)
  115.     rednet.send(dbpc,"acccreate"..auserid..aign)
  116.     local bsenderId, bmessage, bdistance = rednet.receive()
  117.     if bsenderId ~= dbpc or bmessage == "existingid" then
  118.         cardreader.setInsertCardLight(false)
  119.         redraw()
  120.         print("OMG! We'd never thought you'd see this error!")
  121.         print("You've got a 504 error!")
  122.         sleep(0.5)
  123.         print()
  124.         term.setTextColour(colours.red)
  125.         print("*Please contact the owner of this shop and provide them with the error number")
  126.         print()
  127.         sleep(2)
  128.         print("*As a temporary solution, get another blank mag-stripe card and register again")
  129.         term.setTextColour(colours.yellow)
  130.         print("You will receive compensation")
  131.         term.setTextColour(colours.white)
  132.         print()
  133.         print("Press E to continue")
  134.         while true do
  135.             local ev,charpress = os.pullEvent("char")
  136.             if charpress == "e" then
  137.                 break
  138.         end
  139.             end
  140.         print()
  141.         write("Rebooting")
  142.         for i=1,3 do
  143.             sleep(0.5)
  144.             write(". ")
  145.         end
  146.         sleep(0.8)
  147.         os.reboot()
  148.     elseif bsenderId == dbpc and bmessage == "freeid" then
  149.         rednet.send(dbpc,adpass)
  150.     while true do
  151.         local csenderId1, cmessage1, cdistance1 = rednet.receive()
  152.         if csenderId1 == dbpc and cmessage1 == "success" then
  153.             return true
  154.         end
  155.     end
  156.     end
  157. end
  158.  
  159. local function pingdb()
  160.         rednet.send(dbpc,"ping")
  161.         while true do
  162.                 os.startTimer(3)
  163.                 local ev,aId,aMsg,aDist = os.pullEvent()
  164.                 if ev == "timer" then
  165.                         return false
  166.                 elseif ev == "rednet_message" then
  167.                         if aId == dbpc then
  168.                                 if aMsg == "pongdb" then
  169.                                         return true
  170.                                 end
  171.                         end
  172.                 end
  173.         end
  174. end
  175.  
  176. local function loadConfig()
  177.     if fs.exists("config") then
  178.         config.load(nil,"config")
  179.         if config.readVal("DatabaseID") == nil then
  180.             print("Account Registry")
  181.             print()
  182.             write("Please set the Database PC ID: ")
  183.             local dbpcs = read()
  184.             config.writeVal("DatabaseID",dbpcs)
  185.             config.save()
  186.             clearscreen()
  187.             print("Account Registry")
  188.             print()
  189.             print("Database PC ID set to "..dbpcs)
  190.             print("You can change the settings in the config file")
  191.             print("Rebooting...")
  192.             sleep(3)
  193.             os.reboot()
  194.         else
  195.             dbpc = tonumber(config.readVal("DatabaseID"))
  196.         end
  197.     else
  198.         config.load(nil,"config")
  199.         print("Account Registry")
  200.         print()
  201.         write("Please set the Database PC ID: ")
  202.         local dbpcs = read()
  203.         config.writeVal("DatabaseID",dbpcs)
  204.         config.save()
  205.         clearscreen()
  206.         print("Account Registry")
  207.         print()
  208.         print("Database PC ID set to "..dbpcs)
  209.         print("You can change the settings in the config file")
  210.         print("Rebooting...")
  211.         sleep(3)
  212.         os.reboot()
  213.     end
  214. end
  215. loadConfig()
  216.  
  217. local function getModemSide()
  218.     for n,sSidem in pairs(rs.getSides()) do
  219.         if peripheral.getType(sSidem) == "modem" then
  220.             sidem = sSidem
  221.             break
  222.         end
  223.     end
  224.     if sidem ~= nil then
  225.         return sidem
  226.     else
  227.         print("Please attach a modem!")
  228.         sleep(2)
  229.         os.shutdown()
  230.     end
  231. end
  232.  
  233. local function getReaderSide()
  234.     for n,sSider in pairs(rs.getSides()) do
  235.         if peripheral.getType(sSider) == "mag card reader" then
  236.             sider = sSider
  237.             break
  238.         end
  239.     end
  240.     if sider ~= nil then
  241.         return sider
  242.     else
  243.         print("Please attach a mag card reader!")
  244.         sleep(2)
  245.         os.shutdown()
  246.     end
  247. end
  248.  
  249. local function redraw()
  250.     clearscreen()
  251.     print("Account Registry")
  252.     print()
  253. end
  254.  
  255. local modemSide = getModemSide()
  256. rednet.open(modemSide)
  257.  
  258. if pingdb() == false then
  259.         clearscreen()
  260.         print("Database down... please check back later!")
  261.         while true do
  262.                 sleep(5)
  263.                 if pingdb() == true then
  264.                         break
  265.                 end
  266.         end
  267. end
  268.  
  269. local function getStuff()
  270. redraw()
  271. modemSide = getModemSide()
  272. readerSide = getReaderSide()
  273. end
  274.  
  275. local function createAccount()
  276. if peripheral.isPresent(readerSide) == true and peripheral.getType(readerSide) == "mag card reader" then
  277.     cardreader = peripheral.wrap(readerSide)
  278.     redraw()
  279.     print("You must have a blank mag-stripe card!")
  280.     write("IGN: ")
  281.     ign = limitRead(16)
  282.     print()
  283.     write("Desired password (max 16 char.): ")
  284.     print()
  285.     dpass = limitRead(16,"*")
  286.     print()
  287.     print( "*Try restarting (CTRL+R) if it takes too long to register" )
  288.     print()
  289.     print( "*If the problem persists, please contact your local tech support (gknova61)" )
  290.     while true do
  291.         math.randomseed( os.clock()*os.time()+os.computerID() )
  292.         local rand = tostring( math.random(1001,9999) )
  293.         math.randomseed( os.clock()*os.time()+os.computerID()+math.random(0,1000) )
  294.         local rand2 = tostring( math.random(1001,9999) )
  295.         local userid = rand..rand2
  296.             while true do
  297.                     redraw()
  298.                     print("Account registered!")
  299.                     print("Your unique account ID is "..userid)
  300.                     print("Press F2 to take a screenshot of it")
  301.                     print("Please keep it in a safe place")
  302.                     print()
  303.                     print("Please swipe a blank mag-stripe card twice")
  304.                    
  305.                     cardreader.setInsertCardLight(true)
  306.                     while true do
  307.                         local ev, data = os.pullEvent("mag_swipe")
  308.                         if data == "" then
  309.                             cardreader.beginWrite(userid,ign.."\'s card")
  310.                             while true do
  311.                                 local ev, arg1, arg2, arg3 = os.pullEvent()
  312.                                 if ev == "mag_write_done" then
  313.                                     redraw()
  314.                                     print("Card successfully registered!")
  315.                                     cardreader.setInsertCardLight(false)
  316.                                     if accCreate(userid,dpass,ign) == true then
  317.                                         print("Sent")
  318.                                     end
  319.                                     print("Restarting...")
  320.                                     sleep(2)
  321.                                     os.reboot()
  322.                                 end
  323.                             end
  324.                         else
  325.                             print("Please swipe an empty card!")
  326.                    
  327.                 end
  328.             end
  329.         end
  330.     end
  331. else
  332.     print("Registry set up incorrectly.")
  333.     sleep(2)
  334.     os.shutdown()
  335. end
  336. end
  337.  
  338. local function modAccount()
  339.     while true do
  340.         redraw()
  341.         local fMenu=newmenu({"Change Password", "Change Name"},2,({term.getSize()})[2]-16)
  342.     end
  343. end
  344.  
  345. while true do
  346.     redraw()
  347.     local fMenu=newmenu({"Create An Account", "Modify an existing account"},2,({term.getSize()})[2]-16)
  348.     if fMenu == "Create An Account" then
  349.     getStuff()
  350.     createAccount()
  351.     elseif fMenu == "Modify an existing account" then
  352.     getStuff()
  353.     modAccount()
  354.     end
  355. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement