]> git.pond.sub.org Git - eow/blob - web.lisp
remove key event logging (bogs down performance)
[eow] / web.lisp
1 (in-package :empire-web)
2
3 (declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
4
5
6 (defvar *this-file* (load-time-value
7                      (or #.*compile-file-pathname* *load-pathname*)))
8
9 (defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
10                                   :device (pathname-device *this-file*)
11                                   :directory (pathname-directory *this-file*)))
12
13 (defparameter +templates-root+ (namestring *this-dir*))
14 (defparameter +root-url+ "/eow/")
15 (defparameter +web-root-base+ "/eow")
16 (defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
17 (defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
18 (defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
19 (defparameter +login-page+ (concatenate 'string +static-web-root+ "login.html"))
20 (defparameter +root-page-file+ "root.html")
21
22 (defun string-starts-with (string prefix)
23   ;; (from Hunchentoot)
24   (let ((mismatch (mismatch string prefix :test #'char=)))
25     (or (null mismatch)
26         (>= mismatch (length prefix)))))
27
28 (defun serve-static ()
29   "Handle a request for a file under static/ directory"
30   (let* ((script-name (script-name))
31          (fname (subseq script-name (length +static-web-root+)))
32          (fullname (concatenate 'string +static-files-root+ fname)))
33     (handle-static-file fullname)))
34
35 (defclass session ()
36   ((update-queue
37     :accessor update-queue
38     :initform (locked-queue:create)
39     :documentation "Updates to be sent to the browser")
40    (connection
41    :accessor connection
42    :documentation "Connection to the empire game server")))
43
44 (defun make-session (username password)
45   (let* ((session (make-instance 'session))
46          (connection (empire:connect :user username
47                                      :password password
48                                      :session session)))
49     (setf (slot-value session 'connection) connection)
50     session))
51
52 (defgeneric send (session string))
53 (defgeneric prompt (session minutes btus))
54 (defgeneric data (session message))
55
56 (defvar *session*)
57
58 (defun update ()
59   "Send stream of updates to client"
60   (with-session
61     (locked-queue:dequeue (update-queue *session*))))
62
63 (defmethod send ((s session) string)
64   "Push a javascript update fragment to the client."
65   (locked-queue:enqueue (update-queue s) string))
66
67 (defmethod prompt ((s session) minutes btus)
68   (send s (format nil "prompt(~a,~a);~%" minutes btus)))
69
70 (defmethod data ((s session) message)
71   (send s (parenscript:ps* `(msg ,message))))
72
73 (defmacro with-session (&body body)
74   `(let ((*session* (session-value 'session)))
75      (if *session*
76          (progn ,@body)
77          (redirect +login-page+))))
78
79 ;; destination of login-form
80 (defun login-action ()
81   (handler-case
82       (let ((session (make-session (post-parameter "username")
83                                    (post-parameter "password"))))
84         (setf (session-value 'session) session)
85         (redirect +root-url+))
86     (usocket:connection-refused-error (e)
87       (format nil "Connection error: ~a~%" e))))
88
89 (defun command-action ()
90   (with-session
91       (empire:send-message (connection *session*) (get-parameter "q"))))
92
93 (defun root-page ()
94   (with-session
95     (handle-static-file (concatenate 'string +static-files-root+ +root-page-file+))))
96
97 (defun dispatch (request)
98   (let ((script-name (script-name request)))
99     (cond
100       ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
101       ((string= script-name "/eow/update") 'update)
102       ((string= script-name "/eow/login") 'login-action)
103       ((string= script-name "/eow/command") 'command-action)
104       ((or (string-equal script-name +web-root-base+)
105            (string-equal script-name +web-root+)) 'root-page) ; go to the start page
106       ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
107
108
109 (defun start ()
110   (pushnew 'dispatch *dispatch-table* :test #'eq))
111