:directory (pathname-directory *this-file*)))
(defparameter +templates-root+ (namestring *this-dir*))
-(defparameter +root-url+ "/eow/static/test.html")
+(defparameter +root-url+ "/eow/")
(defparameter +web-root-base+ "/eow")
(defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
(defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
(defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
+(defparameter +login-page+ (concatenate 'string +static-web-root+ "login.html"))
+(defparameter +root-page-file+ "root.html")
(defun string-starts-with (string prefix)
;; (from Hunchentoot)
(fullname (concatenate 'string +static-files-root+ fname)))
(handle-static-file fullname)))
+(defclass session ()
+ ((update-queue
+ :accessor update-queue
+ :initform (locked-queue:create)
+ :documentation "Updates to be sent to the browser")
+ (connection
+ :accessor connection
+ :documentation "Connection to the empire game server")))
+
+(defun make-session (username password)
+ (let* ((session (make-instance 'session))
+ (connection (empire:connect :user username
+ :password password
+ :session session)))
+ (setf (slot-value session 'connection) connection)
+ session))
+
+(defgeneric send (session string))
+(defgeneric prompt (session minutes btus))
+(defgeneric data (session message))
+
+(defvar *empire-session*)
+
+(defmacro with-session (&body body)
+ `(let ((*empire-session* (session-value 'session)))
+ (if *empire-session*
+ (progn ,@body)
+ (redirect +login-page+))))
+
+(defun update ()
+ "Send stream of updates to client"
+ (with-session
+ (locked-queue:dequeue (update-queue *empire-session*))))
+
+(defmethod send ((s session) string)
+ "Push a javascript update fragment to the client."
+ (locked-queue:enqueue (update-queue s) string))
+
+(defmethod prompt ((s session) minutes btus)
+ (send s (format nil "prompt(~a,~a);~%" minutes btus)))
+
+(defmethod data ((s session) message)
+ (send s (parenscript:ps* `(msg ,message))))
+
+;; destination of login-form
+(defun login-action ()
+ (handler-case
+ (let ((session (make-session (post-parameter "username")
+ (post-parameter "password"))))
+ (setf (session-value 'session) session)
+ (redirect +root-url+))
+ (usocket:connection-refused-error (e)
+ (format nil "Connection error: ~a~%" e))))
+
+(defun command-action ()
+ (with-session
+ (empire:send-message (connection *empire-session*) (get-parameter "q"))))
+
+(defun root-page ()
+ (with-session
+ (handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
+
(defun dispatch (request)
(let ((script-name (script-name request)))
(cond
((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
+ ((string= script-name "/eow/update") 'update)
+ ((string= script-name "/eow/login") 'login-action)
+ ((string= script-name "/eow/command") 'command-action)
((or (string-equal script-name +web-root-base+)
- (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page
+ (string-equal script-name +web-root+)) 'root-page) ; go to the start page
((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file