Advertisement
PaymentOption

Login for Payment

Sep 7th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.73 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. -- PaymentOption's door lock.
  3. nScreenWidth, nScreenHeight = term.getSize()
  4. sUsername = "Payment"
  5. sPassword = "8907022"
  6.  
  7. -- GUI Methods --
  8. function ClearScreen()
  9.     term.clear()
  10.     term.setCursorPos( 1, 1 )
  11. end
  12.  
  13. -- +--------+
  14. -- |        |
  15. -- +--------+
  16. function PrintBorder( )
  17.    
  18.     local function PrintHorizontalLine( x, y)
  19.         term.setCursorPos( x, y )
  20.         term.write( "+" .. string.rep( "-", nScreenWidth - 2 ) .. "+" )
  21.     end
  22.    
  23.     PrintHorizontalLine( 1, 1 ) -- Draw the top line.
  24.     -- Draw the center lines |    |
  25.     for nHeight = 2, nScreenHeight - 1 do
  26.         term.setCursorPos( 1, nHeight )
  27.         term.write( "|" )
  28.         term.setCursorPos( nScreenWidth, nHeight )
  29.         term.write( "|" )
  30.     end
  31.     --
  32.     PrintHorizontalLine( 1, nScreenHeight ) -- Draw the bottom line.
  33. end
  34.  
  35. -- xPos, yPos are the position of the upper left hand corner of the box.
  36. function PrintInfoBox( xPos, yPos, nBoxWidth, nBoxHeight )
  37.     term.setCursorPos( xPos, yPos )
  38.    
  39.     local function PrintHorizontalLine( x, y )
  40.         term.setCursorPos( x, y )
  41.         term.write( "+" .. string.rep( "-", nBoxWidth - 2 ) .. "+" )
  42.     end
  43.    
  44.     PrintHorizontalLine( xPos, yPos ) -- Draw the top line of the box.
  45.     -- Draw the body of the box. |  |
  46.     for nHeight = yPos + 1, ( nBoxHeight + yPos ) - 1 do
  47.         term.setCursorPos( xPos, nHeight )
  48.         term.write( "|" )
  49.         term.setCursorPos( xPos + nBoxWidth - 1, nHeight )
  50.         term.write( "|" )
  51.     end
  52.     --
  53.     PrintHorizontalLine( xPos, yPos + nBoxHeight )
  54. end
  55.  
  56. -- bExactCenter determines whether or not the box should be printed in the center width and height, or on the centered width but specified height.
  57. function PrintCenteredInfoBox( yPos, nBoxWidth, nBoxHeight, bExactCenter )
  58.     if not yPos and not bExactCenter then
  59.         error( "Cannot draw a non centered box with a nil y position." )
  60.     end
  61.    
  62.     -- If the box should be in the exact center of the screen.
  63.     if bExactCenter then
  64.         PrintInfoBox( nScreenWidth/2 - nBoxWidth/2, nScreenHeight/2 - nBoxHeight/2, nBoxWidth, nBoxHeight )
  65.     -- If the box shouldn't be in the exact center of the screen, but rather on a specified y coordinate.
  66.     else
  67.         PrintInfoBox( nScreenWidth/2 - nBoxWidth/2, yPos, nBoxWidth, nBoxHeight )
  68.     end
  69. end
  70.  
  71. -- Draw the PaymentOption logo at the x and y
  72. function PrintPaymentLogo( x, y, bCentered )
  73.     if bCentered then
  74.         x = nScreenWidth/2 - string.len( "|  __/ _` | | | | '_ ` _ \ / _ | '_ \| __|" )/2
  75.     end
  76.    
  77.     local function Reposition( nY_Offset )
  78.         term.setCursorPos( x, y + nY_Offset )
  79.     end
  80.    
  81.     -- Begin the drawing.
  82.     Reposition( 0 )
  83.     print( "______                                _   " )
  84.     Reposition( 1 )
  85.     print( "| ___ \\                              | | " )
  86.     Reposition( 2 )
  87.     print( "| |_/ __ _ _   _ _ __ ___   ___ _ __ | |_ " )
  88.     Reposition( 3 )
  89.     print( "|  __/ _' | | | | '_ ' _ \\ / _ | '_ \\| __|" )
  90.     Reposition( 4 )
  91.     print( "| | | (_| | |_| | | | | | |  __| | | | |_ " )
  92.     Reposition( 5 )
  93.     print( "\\_|  \\__,_|\\__, |_| |_| |_|\\___|_| |_|\\__|" )
  94.     Reposition( 6 )
  95.     print( "            __/ |                         " )
  96.     Reposition( 7 )
  97.     print( "           |___/                          " )
  98. end
  99.  
  100. function PrintLoginBox()
  101.     PrintCenteredInfoBox( 10, 25, 6, false )
  102.    
  103.     local xPos_ForUsername = ( nScreenWidth/2 - 25/2 ) + 1
  104.     local yPos_ForUsername = 12
  105.    
  106.     term.setCursorPos( xPos_ForUsername, yPos_ForUsername )
  107.     print( "Username: " )
  108.     term.setCursorPos( xPos_ForUsername, yPos_ForUsername + 1 )
  109.     print( "Password: " )
  110. end
  111.  
  112. function PrintWelcomeScreen()
  113.     PrintBorder()
  114.     cPrint( 3, "Welcome" )
  115.     PrintPaymentLogo( nil, 4, true )
  116. end
  117.  
  118. function PrintRejectionScreen()
  119.     PrintBorder()
  120.     cPrint( 3, "You are not" )
  121.     PrintPaymentLogo( nil, 4, true )
  122. end
  123. -----------------
  124.  
  125. -- Utility methods --
  126. function cPrint( nHeight, sString )
  127.     term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
  128.     term.write( sString )
  129. end
  130.  
  131. function limitRead( nLength, cReplaceChar )
  132.         term.setCursorBlink( true )
  133.        
  134.         nLength = nLength or -1 -- -1 is unlimited
  135.         sReturnString = ""
  136.        
  137.         xPos, yPos = term.getCursorPos()
  138.        
  139.         while true do
  140.             event, char = os.pullEvent()
  141.                
  142.                 if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
  143.                
  144.                 if event == "char" then sReturnString = sReturnString .. char
  145.                 elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
  146.                 elseif event == "key" and char == 14 then -- Backspace
  147.                         term.setCursorPos( xPos, yPos )
  148.                         term.write( string.rep( " ", string.len( sReturnString ) ) )
  149.                         sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
  150.                         term.setCursorPos( xPos, yPos )
  151.                        
  152.                         if not cReplaceChar then term.write( sReturnString )
  153.                         else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  154.                 end
  155.                
  156.                 term.setCursorPos( xPos, yPos )
  157.                 term.write( string.rep( " ", string.len( sReturnString ) ) )
  158.                 term.setCursorPos( xPos, yPos )
  159.                 if not cReplaceChar then term.write( sReturnString )
  160.                 else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  161.         end
  162. end
  163. ---------------------
  164.  
  165. -- Information grabbing methods --
  166. -- RETURNS:
  167. -- True or false depending if the given info is correct.
  168. function GetLoginInfo()
  169.     local xPos_ForUsername = ( nScreenWidth/2 - 25/2 ) + string.len( "Username: " ) + 1
  170.     local yPos_ForUsername = 12
  171.    
  172.     local sGivenUsername = ""
  173.     local sGivenPassword = ""
  174.    
  175.     -- Checks if the given information is accurate.
  176.     local function CheckInformation( sName, sPass )
  177.         if sName == sUsername and sPass == sPassword then
  178.             return true
  179.         else
  180.             return false
  181.         end
  182.     end
  183.    
  184.     -- Get the login information.
  185.     -- Username
  186.     term.setCursorPos( xPos_ForUsername, yPos_ForUsername )
  187.     sGivenUsername = limitRead( 11 )
  188.     -- Password
  189.     term.setCursorPos( xPos_ForUsername, yPos_ForUsername + 1 )
  190.     sGivenPassword = limitRead( 11, '*' )
  191.    
  192.     if CheckInformation( sGivenUsername, sGivenPassword ) then
  193.         return true
  194.     else
  195.         return false
  196.     end
  197. end
  198. ----------------------------------
  199.  
  200. -- Main function --
  201. function RunProgram()
  202.     ClearScreen()
  203.     PrintBorder()
  204.     PrintPaymentLogo( nil, 2, true )
  205.     PrintLoginBox()
  206.  
  207.     if GetLoginInfo() then
  208.         ClearScreen()
  209.         PrintWelcomeScreen()
  210.         sleep( 3 )
  211.        
  212.         ClearScreen()
  213.         print( os.version() )
  214.     else
  215.         ClearScreen()
  216.         PrintRejectionScreen()
  217.         sleep( 3 )
  218.        
  219.         os.reboot()
  220.     end
  221. end
  222. -------------------
  223.  
  224. RunProgram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement