'===========================================================================
' Subject: STORE USER RECORDS W/GET & PUT     Date: 06-03-97 (23:56)       
'  Author: Brian Bacon                        Code: QB, QBasic, PDS        
'  Origin: kyberteknik@geocities.com        Packet: MISC.ABC
'===========================================================================
'RECORD.BAS  by Brian Bacon
'
' This is a simple demo of using basic's GET and PUT (file i/o) commands
' for keeping User/Player databases.  It isn't commented well, but it
' should be fairly easy to understand.
'
' If you wish to use this code (or any parts of it) in a program
' of your own, then do so, but realize this is under no warranty
' of any kind. The only thing I ask you to do is visit my web page
' if you can (some people still aren't on the net).
'   www.geocities.com/SiliconValley/Lakes/2213
' It will eventually contain some usefull BASIC related stuff..
' but right now it only has a few links and some BASIC files you
' can download (like a paint program and a few other cool things)
'
'   oh and E-mail me if you want to:  kyberteknik@geocities.com
'
TYPE UserIndex   'Just an index (keep only important stuff here)
         UserID AS LONG
       UserName AS STRING * 30
       Password AS STRING * 15
END TYPE

TYPE UserRecord 'this one can have other information
         UserID AS LONG
       UserName AS STRING * 30
       Password AS STRING * 15
        TimesOn AS INTEGER
END TYPE

DIM UsrIdx AS UserIndex
DIM User AS UserRecord

Top:                                    ' All this just waits for a logon
CLS                                     ' or and exit command from the
PRINT "Waiting for logon..."            ' keyboard
PRINT "(L)ogin (E)xit"
DO
  DO: A$ = INKEY$: LOOP UNTIL LEN(A$)
  SELECT CASE UCASE$(A$)
         CASE "L": GOTO Login
         CASE "E": CLS : END
  END SELECT
LOOP

Login:
CLS                                     ' first, lets greet the user
PRINT "Welcome to this system."
PRINT "Please enter your user name.  If you have never logged on before, enter"
PRINT "the name you would like to use in the future(max 30 char)."
PRINT
LoginNoCls:
LINE INPUT "Enter your name:", uname$   ' get user's name
                                        ' then open the index file
OPEN "ALLUSERS.IDX" FOR RANDOM LOCK WRITE AS #1 LEN = LEN(UsrIdx)
found = 0                               ' not found yet
FOR I = 1 TO LOF(1)                     ' if you use len=len(var) then
                                        ' lof(#) will return the number
                                        ' of records in the file
    GET #1, , UsrIdx                    ' get next record and compare
                                        ' names, if found, exit the loop
    IF RTRIM$(UCASE$(UsrIdx.UserName)) = UCASE$(uname$) THEN found = 1: EXIT FOR
NEXT I
CLOSE 1
IF found = 0 THEN                       ' if they aren't found after going
                                        ' through the entire index file then
                                        ' they must be new or they misspelled
                                        ' their name, lets ask them.
AskIfnewUser:
   INPUT "Are you a new user?(Y/N)", yn$
   IF UCASE$(LEFT$(LTRIM$(yn$), 1)) = "N" THEN GOTO LoginNoCls
   IF UCASE$(LEFT$(LTRIM$(yn$), 1)) <> "Y" THEN GOTO AskIfnewUser
   PRINT "Welcome " + uname$ + " this is your first login."
   UsrIdx.UserName = LTRIM$(uname$)     ' if they are new, then put the
   UsrIdx.UserID = I + 1                ' new name and ID in the index var
   LINE INPUT "Enter a password(max 15 char):", psw$    ' get new password
   UsrIdx.Password = psw$                               ' put it in index
   PRINT "Please write down your name and password and dont forget them."
   OPEN "ALLUSERS.IDX" FOR RANDOM LOCK READ AS #1 LEN = LEN(UsrIdx)
        PUT #1, UsrIdx.UserID, UsrIdx   ' open the index and put new record
   CLOSE 1
  
   User.UserName = UsrIdx.UserName      ' next we copy the index info
   User.UserID = UsrIdx.UserID          ' into the other type that
   User.Password = UsrIdx.Password      ' holds everything (this isn't
   User.TimesOn = 0                     ' manditory)
   OPEN "ALLUSERS.DAT" FOR RANDOM LOCK READ AS #1 LEN = LEN(User)
        PUT #1, UsrIdx.UserID, User     ' open the database file and
   CLOSE 1                              ' store the new user
ELSE
   pwa = 0                              ' password attempts
   DO
     PRINT "Enter your password:";      ' ask for password
     COLOR 0                            ' change to color 0 (so others
     LINE INPUT "", psw$                ' can't see the password)
     COLOR 7
     IF UCASE$(RTRIM$(UsrIdx.Password)) = UCASE$(psw$) THEN
        PRINT "Logging on..."           ' If the entered password is the
        EXIT DO                         ' same as the one in the index
                                        ' then go logon
     ELSE
        PRINT "Wrong password"          ' otherwise tell them and
        pwa = pwa + 1                   ' increment the password attempts
     END IF
     IF pwa = 3 THEN                    ' if they have had 3 attempts
        PRINT "Illeagal logon attempt." ' then boot them off
        CLOSE 1
        SLEEP 1
        GOTO Top
     END IF
  LOOP
  OPEN "ALLUSERS.DAT" FOR RANDOM LOCK WRITE AS #1 LEN = LEN(User)
       GET #1, UsrIdx.UserID, User      ' now they are logged in, so
                                        ' get the users info from the
                                        ' database
  CLOSE 1
END IF

'
'  From this point down, the program is just fancy demo stuff...
'  If any changes are made to the Index or Record variables, make
'  sure to save them.
'
CLS
PRINT "Welcome " + RTRIM$(User.UserName) + " you have logged on"; User.TimesOn; "time(s) before."
User.TimesOn = User.TimesOn + 1
DO
  LINE INPUT "*>", Cmd$
  Cmd$ = LTRIM$(RTRIM$(Cmd$))
  IF UCASE$(Cmd$) = "BYE" THEN
     OPEN "ALLUSERS.IDX" FOR RANDOM LOCK READ AS #1 LEN = LEN(UsrIdx)
          PUT #1, UsrIdx.UserID, UsrIdx
     CLOSE 1
     OPEN "ALLUSERS.DAT" FOR RANDOM LOCK READ AS #1 LEN = LEN(User)
          PUT #1, UsrIdx.UserID, User
     CLOSE 1
     GOTO Top
  END IF
  IF UCASE$(Cmd$) = "HELP" THEN
     PRINT "----------------------------------------------------"
     PRINT " HELP         - Show help"
     PRINT " BYE          - Logoff"
     PRINT " NAMECHNG     - Change your logon name"
     PRINT " PWCHNG       - Change your logon password"
     PRINT "----------------------------------------------------"
  END IF
  IF UCASE$(Cmd$) = "NAMECHNG" THEN
     LINE INPUT "Enter your new name:", uname$
     uname$ = LTRIM$(RTRIM$(uname$))
     IF uname$ <> "" THEN
        User.UserName = uname$
        UsrIdx.UserName = uname$
        PRINT "Logon name changed"
     ELSE
        PRINT "Name change aborted."
     END IF
  END IF
  IF UCASE$(Cmd$) = "PWCHNG" THEN
     LINE INPUT "Enter a new password(max 15 char):", pw$
     pw$ = LTRIM$(RTRIM$(pw$))
     IF pw$ <> "" THEN
        User.Password = pw$
        UsrIdx.Password = pw$
        PRINT "Logon password changed"
     ELSE
        PRINT "Password change aborted."
     END IF
  END IF
LOOP
