Create a gist now

Instantly share code, notes, and snippets.

Patching Adam Petersen's `retro-games.lisp` into `game-voter.lisp`
--- old/retro-games.lisp 2011-06-14 00:08:07.000000000 -0400
+++ new/retro-games.lisp 2011-06-14 00:07:33.000000000 -0400
@@ -1,21 +1,24 @@
(in-package :cl-user)
-(defpackage :retro-games
- (:use :cl :cl-who :hunchentoot :parenscript :elephant))
+(defpackage :game-voter
+ (:use :cl :cl-who :hunchentoot :parenscript :elephant)
+ (:import-from :css-lite :css)
+ (:import-from :json :encode-json-to-string))
-(in-package :retro-games)
+(in-package :game-voter)
;; Start our web server.
-(setf *web-server* (start-server :port 8080))
+(setf *web-server* (start (make-instance 'hunchentoot:acceptor :port 8080)))
;; Publish all static content.
-(push (create-static-file-dispatcher-and-handler "/logo.jpg" "imgs/Commodore64.jpg") *dispatch-table*)
-(push (create-static-file-dispatcher-and-handler "/retro.css" "css/retro.css") *dispatch-table*)
+(push (create-static-file-dispatcher-and-handler "/GameVoter.png" "statics/imgs/GameVoter.png") *dispatch-table*)
+(push (create-static-file-dispatcher-and-handler "/site.css" "statics/css/site.css") *dispatch-table*)
;; Launch Elephant
-(open-store '(:BDB "/home/adam/temp/gamedb/"))
+(setf *store* (open-store '(:clsql (:sqlite3 "/tmp/game-voter.db"))))
-;; Represent each game as an instance of the Elephant persistant class.
+;; Represent each game as an instance of the Elephant persistant
+;; class.
(defpclass persistent-game ()
((name :reader name :initarg :name :index t)
(votes :accessor votes :initarg :votes :initform 0 :index t)))
@@ -40,49 +43,50 @@
(defun games ()
(nreverse (get-instances-by-range 'persistent-game 'votes nil nil)))
-;; Automatically creates a Hunchentoot handler for the given URL (plus .htm) associating
-;; it with a function of the same name.
+;; Automatically creates a Hunchentoot handler for the given URL (plus
+;; .htm) associating it with a function of the same name.
(defmacro define-url-fn ((name) &body body)
`(progn
(defun ,name ()
,@body)
- (push (create-prefix-dispatcher ,(format nil "/~(~a~).htm" name) ',name) *dispatch-table*)))
+ (push (create-prefix-dispatcher ,(format nil "/~(~a~)" name) ',name) *dispatch-table*)))
-;; All pages on the Retro Games site will use the following macro; less to type and
-;; a uniform look of the pages (defines the header and the stylesheet).
+;; All pages on the Game Voter site will use the following macro; less
+;; to type and a uniform look of the pages (defines the header and the
+;; stylesheet).
(defmacro standard-page ((&key title) &body body)
`(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
(:html :xmlns "http://www.w3.org/1999/xhtml" :xml\:lang "en" :lang "en"
(:head
(:meta :http-equiv "Content-Type" :content "text/html;charset=utf-8")
(:title ,title)
- (:link :type "text/css" :rel "stylesheet" :href "/retro.css"))
+ (:link :type "text/css" :rel "stylesheet" :href "/site.css"))
(:body
(:div :id "header" ; Start all pages with our header.
- (:img :src "/logo.jpg" :alt "Commodore 64" :class "logo")
- (:span :class "strapline" "Vote on your favourite Retro Game"))
+ (:img :src "/GameVoter.png" :alt "Game Voter Logo" :class "logo")
+ (:span :class "strapline" "Vote for your favourite Video Game"))
,@body))))
;;
;; The functions responsible for generating the HTML go here.
;;
-(define-url-fn (retro-games)
- (standard-page (:title "Top Retro Games")
- (:h1 "Vote on your all time favourite retro games!")
- (:p "Missing a game? Make it available for votes " (:a :href "new-game.htm" "here"))
+(define-url-fn (index)
+ (standard-page (:title "Game Voter")
+ (:h1 "Vote on your all time favourite games!")
+ (:p "Missing a game? Make it available for votes " (:a :href "new-game" "here"))
(:h2 "Current stand")
(:div :id "chart" ; Used for CSS styling of the links.
(:ol
(dolist (game (games))
(htm
- (:li (:a :href (format nil "vote.htm?name=~a" (name game)) "Vote!")
+ (:li (:a :href (format nil "vote?name=~a" (name game)) "Vote!")
(fmt "~A with ~d votes" (name game) (votes game)))))))))
(define-url-fn (new-game)
(standard-page (:title "Add a new game")
(:h1 "Add a new game to the chart")
- (:form :action "/game-added.htm" :method "post"
+ (:form :action "/game-added" :method "post"
:onsubmit (ps-inline ; Client-side validation.
(when (= name.value "")
(alert "Please enter a name.")
@@ -95,10 +99,10 @@
(let ((name (parameter "name")))
(unless (or (null name) (zerop (length name))) ; In case JavaScript is turned off.
(add-game name))
- (redirect "/retro-games.htm"))) ; Display the front page.
+ (redirect "/index"))) ; Display the front page.
(define-url-fn (vote)
(let ((game (game-from-name (parameter "name"))))
(if game
(vote-for game))
- (redirect "/retro-games.htm"))) ; Back to the front page.
+ (redirect "/index"))) ; Back to the front page.
@tielimu
tielimu commented Feb 21, 2012

Very good! I'm studying the code.Thanks!

@PhilipElsas

Matthew: great job! My compliments.

Just a few extra patches:

Before line 101, please add the following 4 lines from Adam Petersen's original coding:

         (return false)))
   (:p "What is the name of the game?" (:br)
   (:input :type "text" :name "name" :class "txt"))
   (:p (:input :type "submit" :value "Add" :class "btn")))))

And directly after line 101, please add the following original line:

(define-url-fn (game-added)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment