Metropoli BBS
VIEWER: vault.pps MODE: TEXT (CP437)
'
' VAULT.PPE - Written by Dan Shore
'             June 5, 1996
'
' Purpose:  To allow a user to GAMBLE time or bytes
'
' To install:
'
'         1.  Put VAULT.PPE in it's own subdirectory off of the BANK.PPE
'
'         2.  Edit GAMBLE.DAT - This file contains the name of the
'                               gambling modules (PPE) and a description.
'                               This file resides in the same directory
'                               as BANK.PPE
'
'                               Each gamble ppe that is used with
'                               BANK.PPE will have two lines entered in
'                               this configuration file.  The first is
'                               the full path and name of the PPE.  The
'                               second is a brief description of the
'                               gamble module ppe.
'
'             Example:
'                          C:\PCB\PPE\BANK\VAULT\VAULT.PPE
'                          Brief one line description of game
'
'
'         3.  Edit VAULT.CFG - Consists of four lines
'                                 Line #1 = Use modules once per day
'                                 Line #2 = Time Gamble setup
'                                 Line #3 = Byte Gamble setup
'                                 Line #4 = Time;Byte Amount in Vault
'                                 Line #5 = Vault Combination
'
'             Field #1 = TRUE or FALSE to all that type of gamble
'             Field #2 = Maximum amount that can be gambled
'             Field #3 = Win percentage
'
'             Example:      TRUE
'                           TRUE;30;.5
'                           TRUE;500000;.5
'                           60;1000000
'                           3-19-12
'
' Note about gambling add-ons:
'
'         1.  The gambling module PPE *MUST* read & write to a file called
'             BANK????.INF (???? = node number).  This file contains
'             bank balance information about the current user.
'
'             The "PATH" to this file is passed at the 1st command
'             line parameter (See first line of code).
'
'             This information in this file will be as follows:
'
'               Line #1 = Current Bank Time
'               Line #2 = Current Bank Bytes
'
'──────────────────────────────────────────────────────────────────────────────
'
'  Declare our variables
'
STRING hold                    ' Generic STRING variable
STRING hold2                   ' Generic STRING variable
STRING main_prompt             ' Generic prompt
STRING user_input              ' Generic user input
STRING bank_inf_path           ' Path to BANK????.INF file
STRING mod_name                ' Name of Gambling Modules

STRING allow_time              ' Allow time gamble
STRING time_max                ' Max time to bet
STRING time_win_perc           ' Amount of time when user wins
STRING allow_byte              ' Allow byte gamble
STRING byte_max                ' Max bytes to bet
STRING byte_win_perc           ' Amount of bytes when user wins
STRING bank_txt                ' External Text file
STRING vault_combo             ' Combination to Vault

INTEGER size                   ' Generic INTEGER variable
INTEGER bank_bytes             ' Current Bank Bytes
INTEGER bank_time              ' Current Bank Time
INTEGER gamble_amount          ' Amount user gambled
INTEGER byte_amount            ' Byte amount in vault

INT  x                         ' Generic INT variable
INT gamble_type                ' Time and/or Byte gamble allowed
                               ' 1 = TIME, 2 = BYTE, 3 = BOTH
INT time_amount                ' Time amount in vault

FLOAT win_percentage           ' Show user win percentages

BOOLEAN do_time                ' Time or byte gamble
BOOLEAN did_win                ' Flag to show user won
BOOLEAN already_gambled        ' Flag to show user already gambled today
BOOLEAN allow_once             ' Flag to allow player to play more than once
'──────────────────────────────────────────────────────────────────────────────
'
'  Declare our Gambling Procedure.
'
DECLARE PROCEDURE DO_GAMBLE (VAR BOOLEAN did_win)
DECLARE PROCEDURE ADD_COMMAS (VAR STRING hold2)
DECLARE PROCEDURE NEW_COMBO ()

*$USEFUNCS

BEGIN
  '
  '  Retrieve path to bank????.inf file
  '
  bank_inf_path = GETTOKEN()

  '
  '  Define external text file - Support Language Files
  '
  bank_txt = PPEPATH() + "GAMBTXT" + LANGEXT()
 
  '
  '  Open our Log File and log username
  '
  FAPPEND 7, PPEPATH() + "NODE" + STRING(PCBNODE()) + ".LOG", O_RW, S_DN
  FPUTLN 7, "────────────── " + MIXED(U_NAME()) + " ──────────────"
 
  '
  '  This is the name of the Gambling Module
  '
  mod_name = "The Vault"
 
  '
  '  Read configuration file for VAULT
  '
  GOSUB READ_CFG
 
  '
  '  Check to see if user has already gambled today
  '
  IF (allow_once) GOSUB CHECK_IF_PLAYED

  '
  '  Read the .INF file
  '
  GOSUB READ_BANK_INF

  '
  '  Display intro file to explain to user what the game is about.
  '
  '  *** Make sure to tell user they can ONLY gamble what is in the BANK
  '
  '
  DISPFILE PPEPATH() + "INTRO", GRAPH+LANG+SEC
  NEWLINE

  '
  '  Show user if they are allowed to gamble time and/or bytes
  '
  GOSUB ALLOWED_BET

  PRINTLN READLINE(bank_txt,57)
  PRINTLN READLINE(bank_txt,58)
  NEWLINE

  '
  '  Ask user if they wish to gamble time or bytes
  '
  GOSUB ASK_TIME_BYTE

  '  Write username to GAM-USER.DAT - This file keeps track of which
  '  users have played the gamble game already today.
  '
  IF (allow_once) THEN
    '
    FPUTLN 2, U_NAME()
    FCLOSE 2
    '
  END IF
 
  '
  '  Run the gambling game
  '
  DO_GAMBLE (did_win)
 
  '
  '  Write information back to BANK????.INF file
  '
  GOSUB WRITE_INF
 
  '
  '  Exit the PPE
  '
  GOTO EXIT_PROG
 
END
 
'────────────────────────────────────────────────────
'                Start of Subroutines
'────────────────────────────────────────────────────
 
'
'  Show user if they are allowed to gamble time and/or bytes
'
:ALLOWED_BET
  '
  '  Inform the user if they are allowed to gamble time or bytes
  '
  IF (allow_time = "TRUE" && bank_time != 0) THEN
    PRINTLN READLINE(bank_txt, 1)
    FPUTLN 7, READLINE(bank_txt, 2)
    gamble_type = 1
  END IF
 
  IF (allow_byte = "TRUE" && bank_bytes != 0) THEN
    PRINTLN READLINE(bank_txt, 3)
    FPUTLN 7, READLINE(bank_txt, 4)
    IF (gamble_type) THEN
      gamble_type = 3
    ELSE
      gamble_type = 2
    END IF
  END IF
  IF (bank_time = 0 && bank_bytes = 0) THEN
    PRINTLN READLINE(bank_txt, 5)
    FPUTLN 7, READLINE(bank_txt, 6)
    NEWLINE
    WAIT
    GOTO EXIT_PROG
  END IF
  NEWLINE
  RETURN
 
'
'  Show user what the winners received for time and byte bets
'
:SHOW_WIN_PERCENTAGE
 
   '
   '  Show time win percentage
   '
   IF (gamble_type = 1 || gamble_type = 3) THEN
     x = TOFLOAT(time_win_perc) * 100
     hold = STRING(x) + "%"
     PRINTLN READLINE(bank_txt, 7), hold, READLINE(bank_txt, 8)
     FPUTLN 7, READLINE(bank_txt, 9), hold
   END IF
 
   '
   '  Show byte win percentage
   '
   IF (gamble_type > 1) THEN
     x = TOFLOAT(byte_win_perc) * 100
     hold = STRING(x) + "%"
     PRINTLN READLINE(bank_txt, 10), hold, READLINE(bank_txt, 8)
     FPUTLN 7, READLINE(bank_txt, 11), hold
   END IF
 
   NEWLINE
   RETURN
 
'
'  Write new information to BANK????.INF file
'
:WRITE_INF
 
   '
   '  Determine amount won.  Take the amount gambled and multiply
   '  by the percentage defined in the .CFG file
   '
   '  If user loses gamble, deduct amount from bank balance
   '
   IF (did_win) THEN
     IF (do_time) THEN
       '
       '  Calculate winning amount
       '
       gamble_amount = gamble_amount * TOFLOAT(time_win_perc)
       FPUTLN 7, READLINE(bank_txt, 12), gamble_amount, READLINE(bank_txt, 13)
       bank_time = bank_time + gamble_amount
     ELSE
       '
       '  Calculate winning amount
       '
       gamble_amount = gamble_amount * TOFLOAT(byte_win_perc)
       FPUTLN 7, READLINE(bank_txt, 12), gamble_amount, READLINE(bank_txt, 14)
       bank_bytes = bank_bytes + gamble_amount
     END IF
   ELSE
     '
     '  Calculate loss
     '
     IF (do_time) THEN
       bank_time = bank_time - gamble_amount
     ELSE
       bank_bytes = bank_bytes - gamble_amount
     END IF
     '
   END IF
 
   '
   '  Delete the INF file and write a new file with the new information.
   '
   '  Bank.PPE will read this file when it reloads
   '
   DELETE bank_inf_path + "BANK" + STRING(PCBNODE()) + ".INF"
   FOPEN 1, bank_inf_path + "BANK" + STRING(PCBNODE()) + ".INF", O_WR, S_DN
   FPUTLN 1, STRING(bank_time)
   FPUTLN 1, STRING(bank_bytes)
   FCLOSE 1
 
   RETURN
 
 
'
'  Ask user if they wish to gamble time or bytes. Then ask them how much
'
:ASK_TIME_BYTE
 
   WHILE (1) DO
 
     '
     '  If user can gamble time or bytes, prompt them for which type
     '  If user can only bet time OR bytes, then pass this section
     '
     user_input = ""
     IF (gamble_type = 3) THEN
       main_prompt = READLINE(bank_txt, 15)
       INPUTSTR main_prompt, user_input, @X07, 1, "TB", UPCASE+LFAFTER+GUIDE+FIELDLEN
     ELSE IF (gamble_type = 1) THEN
       user_input = "T"
'       FPUTLN 7, READLINE(bank_txt, 16)
     ELSE IF (gamble_type = 2) THEN
       user_input = "B"
'       FPUTLN 7, READLINE(bank_txt, 17)
     END IF
 
     '
     '  Process user request for time or byte bet.  Or jump to time
     '  or byte bet right away due to user only being able to bet
     '  one type (time or byte) due to bank restrictions.
     '
     IF (user_input != "") THEN
       SELECT CASE (user_input)
 
         '
         '  Time bet.  Build variables based on time (prompt and max amount)
         '  for next prompt shown to user
         '
         CASE "T"
           FPUTLN 7, READLINE(bank_txt, 16)
           hold = READLINE(bank_txt, 18)
           IF (TOINTEGER(time_max) > bank_time) THEN
             hold2 = STRING(bank_time)
           ELSE
             hold2 = time_max
           END IF
           do_time = TRUE
         '
         '  Byte bet.  Build variables based on bytes (prompt and max amount)
         '  for next prompt shown to user
         '
         CASE "B"
           FPUTLN 7, READLINE(bank_txt, 17)
           hold = READLINE(bank_txt, 19)
           IF (TOINTEGER(byte_max) > bank_bytes) THEN
             hold2 = STRING(bank_bytes)
           ELSE
             hold2 = byte_max
           END IF
           do_time = FALSE
       END SELECT
       user_input = ""
 
       '
       '  Ask user how much they wish to bet
       '
       WHILE (user_input = "") DO
         user_input = ""
         main_prompt = READLINE(bank_txt, 20) + hold + READLINE(bank_txt, 21) + hold2 + READLINE(bank_txt, 22)
         INPUTSTR main_prompt, user_input, @X07, 9, "0123456789", UPCASE+LFAFTER+GUIDE+FIELDLEN
       END WHILE
 
       '
       '  See if user wishes to quit.  If not, log bet amount
       '
       IF (user_input = "0" || user_input = "") GOTO EXIT_PROG
       FPUTLN 7, "Amount of bet = ", user_input
 
       '
       '  See if user entered too large a value
       '
       IF (TOINTEGER(user_input) > TOINTEGER(hold2)) THEN
         NEWLINE
         IF (do_time) THEN
           PRINTLN READLINE(bank_txt, 23), hold2
         ELSE
           PRINTLN READLINE(bank_txt, 24), hold2
         END IF
         FPUTLN 7, READLINE(bank_txt, 25)
         CONTINUE
       END IF
 
       '
       '  Put gamble amount in gamble variable and exit loop
       '
       gamble_amount = TOINTEGER(user_input)
       BREAK
       '
     ELSE
       '
       '  User hit enter to QUIT gamble
       '
       GOTO EXIT_PROG
       '
     END IF
   END WHILE
   RETURN

'
'  Gamble Module code - Add your gambling code here.  The only thing
'  the gamble code must do is:
'
'  1.  Set "did_win = TRUE" for a winner
'  2.  Log if user wins or loses (FPUTLN 7)
'  3.  Log all gambling activity (easier to track what the program/user did)
'
'
'  You can also add your text to GAMBTXT or leave it in the .PPS file
'
PROCEDURE DO_GAMBLE (VAR BOOLEAN did_win)

  '
  '  Define local variables
  '
  INT count                ' Counter for user input
  STRING key               ' User input
  STRING msg_text          ' Text file to import into conference 0

  '
  '  Define file for message to sysop,  Message informs the vault
  '  has been solved, and the combination has been changed.
  '
  msg_text = PPEPATH() + "WONGAME.DOC"

  '
  '  Ask user for input
  '
  CLS
  NEWLINE
  user_input = ""
  count = 1
  PRINTLN READLINE(bank_txt,30)
  NEWLINE
  PRINTLN READLINE(bank_txt,31), time_amount, READLINE(bank_txt,32)
  add_commas (hold2)
  PRINTLN READLINE(bank_txt,31), hold2, READLINE(bank_txt,33)
  NEWLINES 2
  PRINTLN READLINE(bank_txt,34)
  NEWLINE
  PRINTLN READLINE(bank_txt,35)
  PRINTLN READLINE(bank_txt,36)
  NEWLINE
  '
  '  If user want to view other players guesses.
  '
  IF (EXIST(PPEPATH() + "GUESS.LST")) THEN
    NEWLINE
    main_prompt = READLINE(bank_txt,37)
    INPUTYN main_prompt, user_input, @X07
    '
    '  Show previous guesses to user
    '
    IF (user_input = YESCHAR()) THEN
      NEWLINES 2
      PRINTLN READLINE(bank_txt,38)
      NEWLINE
      PRINTLN READLINE(bank_txt,39)
      NEWLINE
      PRINTLN READLINE(bank_txt,38)
      NEWLINE
      DISPFILE PPEPATH() + "GUESS.LST", SEC+GRAPH+LANG
    ELSE
      NEWLINE
    END IF
    '
    user_input = ""
    '
  END IF

  '
  '  Get input from user
  '
  NEWLINE
  PRINTLN READLINE(bank_txt,40)
  NEWLINE
  PRINT  READLINE(bank_txt,41)

  '
  '  Process users input
  '
  WHILE (1) DO
    key = UPPER(INKEY())
    IF (key >= "0" && key <= "9") THEN
      '
      '  Add input to users response
      '
      user_input = user_input + key
      PRINT key
      INC count
      key = ""
      IF (count = 9) BREAK
      IF (count = 3 || count = 6) THEN
        PRINT "-"
        user_input = user_input + "-"
        INC count
      END IF
    END IF
    DELAY 3
  END WHILE

  '
  '  Log users guess
  '
  FPUTLN 7, READLINE(bank_txt,42), " ", user_input
  FPUTLN 7, READLINE(bank_txt,43), " ", vault_combo
  NEWLINES 2

  '
  '  See if user picked the correct number
  '
  IF (user_input = vault_combo) THEN
    '
    PRINTLN READLINE(bank_txt,44)
    did_win = TRUE
    FPUTLN 7, READLINE(bank_txt,45)
    MESSAGE 0, READLINE(bank_txt,46), READLINE(bank_txt,47), MIXED(U_NAME()) + READLINE(bank_txt,48), "R", 0, FALSE, FALSE, msg_text
    '
    '  Create semphore file so other users cannot play once game is won
    '
    FOPEN 2, PPEPATH() + "GAMEWON", O_WR, S_DN
    FCLOSE 2
    '
    WAIT
    '
    '  Reward user with Vault balances.  Give them TIME OR BYTES depending
    '  on what they gambled with.
    '
    IF (do_time) THEN
      bank_time = bank_time + time_amount
      NEWLINE
      PRINTLN "@X0F", time_amount, READLINE(bank_txt,49)
      NEWLINES 2
      FPUTLN 7, time_amount, READLINE(bank_txt,50)
      WAIT
    ELSE
      bank_bytes = bank_bytes - byte_amount
      NEWLINE
      PRINTLN "@X0F", byte_amount, READLINE(bank_txt,51)
      NEWLINES 2
      FPUTLN 7, byte_amount, READLINE(bank_txt,52)
      WAIT
    END IF
    '
    '  Generate new combination and clear semaphore file and guess log
    '  for next combination to solve
    '
    NEW_COMBO()
    DELETE PPEPATH() + "GAMEWON"
    DELETE PPEPATH() + "GUESS.LST"
    '
  ELSE
    '
    '  Tell user if numbers were a match
    '
    hold2 = REPLACESTR (user_input, "-", ";")
    TOKENIZE hold2

    FOR x = 1 to 3
      hold = GETTOKEN()
      count = INSTR(vault_combo, hold)
      IF (count > 0)  PRINTLN READLINE(bank_txt,53), hold, READLINE(bank_txt,54)
    NEXT

    '
    ' Add users guess to the guess log
    '
    FAPPEND 2, PPEPATH() + "GUESS.LST", O_WR, S_DW
    FPUTLN 2, user_input
    FCLOSE 2
    NEWLINE
    PRINTLN READLINE(bank_txt,55)
    NEWLINE
    FPUTLN 7, READLINE(bank_txt,56)
    NEWLINE
    WAIT
  END IF

ENDPROC

'
'  Read the BANK????.INF file for current user information
'
:READ_BANK_INF

   FOPEN 1, bank_inf_path + "BANK" + STRING(PCBNODE()) + ".INF", O_RD, S_DN
   FGET 1, hold
   bank_time = TOINTEGER(hold)
   FGET 1, hold
   bank_bytes = TOINTEGER(hold)
   FCLOSE 1
   RETURN

'
'  Common exit point for program
'
:EXIT_PROG

   FPUTLN 7, READLINE(bank_txt, 26)
   FPUTLN 7
   FCLOSE 7
   END

'
'  Read gambling configuration file to determine if time or bytes
'  are allowed to bet.  If so, maximum amounts to bet, and the winning
'  percentage are all defined in this file
'
:READ_CFG

   '
   '  Check for existance of .CFG file
   '
   IF (!EXIST(PPEPATH() + "VAULT.CFG")) THEN
     FAPPEND 6, bank_inf_path + "NODE" + STRING(PCBNODE()) + ".LOG", O_RW, S_DN
     FPUTLN 6, "Could not find VAULT.CFG - Module Aborted"
     FCLOSE 6
     GOTO EXIT_PROG
   END IF
   FOPEN 1, PPEPATH() + "VAULT.CFG", O_RD, S_DN

   '
   '  Continuous Loop
   '
   WHILE (1) DO
     '
     '  See if player is allowed to play more than once per day
     '
     FGET 1, hold
     '
     '  See if line is a comment or blank line
     '
     IF (LEFT(hold,1) = "'" || LEFT(hold,1) = "") CONTINUE
     IF (hold = "TRUE") allow_once = TRUE
     '
     '  Process Time information
     '
     FGET 1, hold
     TOKENIZE hold
     hold = UPPER(hold)
     allow_time = GETTOKEN()
     time_max = GETTOKEN()
     time_win_perc = GETTOKEN()
     '
     '  Process Byte information
     '
     FGET 1, hold
     TOKENIZE hold
     allow_byte = GETTOKEN()
     byte_max = GETTOKEN()
     byte_win_perc = GETTOKEN()
     '
     '  Get time and byte amount in vault
     '
     FGET 1, hold
     TOKENIZE hold
     time_amount = GETTOKEN()
     byte_amount = GETTOKEN()
     '
     '  Get the vault combination.  NOTE:  This combination must
     '  be in the format:  xx-xx-xx  where x is a number from
     '  1 to 25.  Examples:  1-21-4    21-9-25    8-1-3
     '
     FGET 1, vault_combo
     '
     BREAK
     '
   END WHILE

   FCLOSE 1
   RETURN

'
'  See if the user has already gambled today
'
:CHECK_IF_PLAYED

   '
   '  Check the date file to see if it is a new day.  If it is
   '  delete the file containing the usernames who have gambled today
   '
   '  If the date file does not exist, we will create it
   '
   FOPEN 2, PPEPATH() + "CKDATE.DAT", O_RW, S_DN
   FGET 2, hold
   IF (TODATE(hold) < DATE() || hold = "") THEN
     FREWIND 2
     FPUT 2, DATE()
     DELETE PPEPATH() + "GAM-USER.DAT"
   END IF
   FCLOSE 2

   '
   '  Check to see if user has already gambled today
   '
   FOPEN 2, PPEPATH() + "GAM-USER.DAT", O_RW, S_DN
   WHILE (1) DO
     FGET 2, hold
     IF (FERR(2)) BREAK
     IF (INSTR(hold, U_NAME())) THEN
       NEWLINES 5
       PRINTLN READLINE(bank_txt, 27), mod_name, READLINE(bank_txt, 28)
       NEWLINE
       WAIT
       already_gambled = TRUE
     END IF
   END WHILE

   '
   '  If user already played, exit the program
   '
   IF (already_gambled) THEN
     FPUTLN 7, READLINE(bank_txt, 29)
     GOTO EXIT_PROG
   END IF
   RETURN

'
'
'
PROCEDURE NEW_COMBO()

   '
   '  Define local variable
   '
   INT y

   '
   '  Set out counter and open our files
   '
   x = 1
   FOPEN 1, PPEPATH() + "VAULT.CFG", O_RD, S_DN
   FOPEN 2, PPEPATH() + "VAULT.TMP", O_WR, S_Dn

   '
   '  Run continuous loop to read .CFG and write new .TMP
   '
   WHILE (1) DO
     FGET 1, hold
     '
     '  When at end of file, stop continuous loop
     '
     IF (FERR(1)) BREAK
     '
     '  When we read the 5th entry from the .CFG process new combo
     '
     IF (x = 5) THEN
       hold = ""
       '
       '  Generate three new numbers for combo
       '
       FOR y = 1 TO 3
         hold2 = STRING(RANDOM(24) + 1)
         IF (LEN(hold2) < 2) hold2 = "0" + hold2
         hold = hold + hold2
         IF (y < 3) hold = hold + "-"
       NEXT
       FPUTLN 2, hold
     ELSE
       '
       '  Write line by line of .CFG to .TMP file.  When we have
       '  read an entry (not a comment) increment our counter
       '
       IF (LEFT(hold,1) != "" && LEFT(hold,1) != "'") INC x
       FPUTLN 2, hold
     END IF
   END WHILE
   '
   '  Close both files, and copy .TMP over .CFG
   '
   COPY PPEPATH() + "VAULT.TMP", PPEPATH() + "VAULT.CFG"
   DELETE PPEPATH() + "VAULT.TMP"
   '
END PROC

'
'  Add Commas to display of numeric information
'
PROCEDURE ADD_COMMAS (VAR STRING hold2)

   STRING temp

   temp = TRIM(STRING(byte_amount)," ")
   x = LEN(temp)
   '
   '  If the number is less than 4 characters in length, no need to add
   '  any commas to it.
   '
   IF (x < 4) THEN
     hold2 = temp
     RETURN
   END IF
   '
   '  Left pad the string with spaces (easy to add commas this way)
   '
   temp = SPACE(9-x) + temp

   IF (INSTR(temp,"-")) THEN
     '
     '  String has a negative symbol in it...Do not put a comma after it
     '
     IF (INSTR(MID(temp,1,3),"-")) THEN
       hold2 = MID(temp,1,3) + MID(temp,4,3) + "," + MID(temp,7,3)
     ELSE IF (INSTR(MID(temp,4,3),"-")) THEN
       hold2 = MID(temp,4,3) + MID(temp,7,3)
     END IF
   ELSE
     '
     '  Rebuild string with comma(s) in proper places
     '
     IF (MID(temp,1,3) != "   ") THEN
       hold2 = MID(temp,1,3) + "," + MID(temp,4,3) + "," + MID(temp,7,3)
     ELSE IF (MID(temp,4,3) != "   ") THEN
       hold2 = MID(temp,4,3) + "," + MID(temp,7,3)
     END IF
   END IF
   '
   '  Remove the left padded spaces we added above
   '
   hold2 = LTRIM(hold2," ")
   RETURN

ENDPROC
[ RETURN TO DIRECTORY ]