;;; File:
;;;   bizz-buzz-two.ss
;;; Version:
;;;   1.0 of February 2002
;;; Author:
;;;   Samuel A. Rebelsky
;;; Contents:
;;;   A starting point for a version of the Bizz-Buzz game.  In this
;;;   version, each "round" appears on a different page.  The advantage
;;;   of this strategy is that it goes on forever.  The disadvantage
;;;   is that it can be a pain to keep clicking "Next".
;;; Organization:
;;;   Preparation - Stuff I need to do to get started
;;;   Page Generators - Procedures for generating specific kinds of
;;;     pages (when given particular values)
;;;   Interface - Stuff to extract important values and then
;;;     call the page generators.  You'll find (page) here.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Preparation

;;; Look!  We're going to use Sam's Web Utilities!
(load "/home/rebelsky/Web/Scheme/webutils.ss")


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Page Generators

;;; Procedure:
;;;   bizz-buzz-page
;;; Parameters:
;;;   buzzer, the value used in selecting bizzes and buzzes
;;;   current, the current number in the game.
;;; Purpose:
;;;   Generates an HTML page that generates the current value
;;;   in Bizz-Buzz and gives a link to the next page.
;;; Produces:
;;;   page-code, a string that corresponds to that page.
;;; Preconditions:
;;;   buzzer must be an integer between 1 and 9, inclusive.
;;;   current must be a positive integer.
;;; Postconditions:
;;;   page-code is valid HTML.
;;;   page-code reports on the current number in Bizz-Buzz.
(define bizz-buzz-page
  (lambda (buzzer current)
    (make-page 
      (head 
        (string-append "Bizz Buzz " (number->string buzzer)))
      (body 
        (string-append
          (heading 1 "Bizz Buzz")
          (string #\newline)
          (paragraph (string-append "Um ... is it "
                                    (number->string current)
                                    "?")) 
          (string #\newline)
          (form "bizz-buzz-two.cgi"
                "Next Value"
                (paragraph
                  (string-append 
                    (hidden "buzzer" (number->string buzzer))
                    (hidden "current" (number->string (+ 1 current))))))
          (form "bizz-buzz-two.html"
            "Start Over"
            ""))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interface

;;; Procedure:
;;;   page
;;; Parameters:
;;;   None
;;; Purpose:
;;;   Builds an HTML page according to specifications.
;;; Produces:
;;;   A string that corresponds to the HTML page.
;;; Preconditions:
;;;   [Standard]
;;; Postconditions:
;;;   The returned page is valid HTML.
;;;   The returned page shows the current round of bizz-buzz
;;;     (as specified by CGI variables buzzer and current).
(define page
  (lambda ()
    (bizz-buzz-page 
      (string->number (get-nonempty-cgi-variable 'buzzer "3"))
      (string->number (get-nonempty-cgi-variable 'current "1")))))
