scratch.lisp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (in-package #:event)
  2. (ql:quickload "backgammon")
  3. (progn
  4. (setf hunchentoot:*session-max-time* 86400)
  5. (defvar *id* 0)
  6. (defvar *guest-id* 0)
  7. (defun ensure-user (&optional (session hunchentoot:*session*))
  8. (when (null (hunchentoot:session-value :user session))
  9. (setf (hunchentoot:session-value :user session) (make-instance 'ui::guest-user :id (decf *guest-id*)))))
  10. (defclass server (hunchensocket:websocket-acceptor
  11. hunchentoot:easy-acceptor)
  12. ())
  13. (defvar *server* (make-instance 'server :port 4242))
  14. (setf (hunchentoot:acceptor-document-root *server*) (namestring (merge-pathnames "lisp/backgammon/" (user-homedir-pathname))))
  15. (defvar *tables* (make-hash-table))
  16. (defun find-table (id)
  17. (gethash id *tables*))
  18. (defun register-table (table)
  19. (setf (gethash (table-id table) *tables*) table))
  20. ;; todo more options
  21. (defun set-up-table (id &key limit)
  22. (let ((table (if limit
  23. (make-instance 'match-table :id id :limit limit)
  24. (make-instance 'money-session-table :id id))))
  25. (register-table table)
  26. (game::start-new-game (session table))
  27. table))
  28. #+nil (defvar *connections* (make-hash-table :test 'eq))
  29. #+nil (defmethod ui::connection-user ((connection user))
  30. (gethash connection *connections*))
  31. (defun create-new-table (limit)
  32. (let* ((*event-queue* (make-event-queue))
  33. (table (set-up-table (incf *id*) :limit (if (zerop limit) nil limit))))
  34. (setf (event-queue table) *event-queue*)
  35. (bt:make-thread (lambda ()
  36. (setf *random-state* (make-random-state t))
  37. (table-loop table))
  38. :name (format nil "table ~A" *id*))
  39. *id*))
  40. ;; todo newmatch -> tables
  41. (hunchentoot:define-easy-handler (new-match :uri "/newmatch") (limit)
  42. (hunchentoot:start-session)
  43. (ensure-user)
  44. (if limit
  45. (hunchentoot:redirect (format nil "/tables/~A" (create-new-table (parse-integer limit :junk-allowed t))))
  46. (hunchentoot:handle-static-file "new-match.html")))
  47. (defun table-info (table)
  48. (let* ((session (session table))
  49. (info `(:limit ,(if (typep session 'game::match) (game::limit session) nil)
  50. :score ,(game::score session))))
  51. (dolist (player '(:white :black) info)
  52. (let ((user (ui::player-user player (ui table))))
  53. (when user
  54. (setf info (list* player (ui::name user) info)))))))
  55. (hunchentoot:start *server*)
  56. (defun table-gui-dispatcher ()
  57. (hunchentoot:start-session)
  58. (ensure-user)
  59. (let ((script-name (hunchentoot:script-name* hunchentoot:*request*)))
  60. (if (member script-name
  61. '("/tables" "/tables/")
  62. :test #'string=)
  63. (new-match :limit nil)
  64. (let* ((table-no (parse-integer (subseq script-name 8)))
  65. (table (gethash table-no *tables*)))
  66. (if (and table
  67. (ui::player-user :white (ui table))
  68. (null (ui::player-user :black (ui table))))
  69. (hunchentoot:redirect (format nil "/proposal?table=~A" table-no))
  70. (hunchentoot:handle-static-file "backgammon.html"))))))
  71. (hunchentoot:define-easy-handler (welcome :uri "/tables") ()
  72. (hunchentoot:start-session)
  73. (ensure-user)
  74. (new-match :limit nil))
  75. (push (hunchentoot:create-prefix-dispatcher "/tables/" 'table-gui-dispatcher) hunchentoot:*dispatch-table*)
  76. (defvar *tables* (make-hash-table :test 'equal))
  77. (defun table-dispatcher (request)
  78. (let ((uri (hunchentoot:script-name* request)))
  79. (if (> (length uri) (length "/tables/"))
  80. (find-table (parse-integer (subseq uri (length "/tables/")) :junk-allowed t))
  81. nil)))
  82. (setf hunchensocket:*websocket-dispatch-table* '(table-dispatcher))
  83. (defun table-uri (id)
  84. (format nil "/tables/~A" id))
  85. (hunchentoot:define-easy-handler (tables-info :uri "/tables-info") ()
  86. (setf (hunchentoot:content-type*) "application/json")
  87. (jonathan:to-json (mapcar #'rest (append (sort (loop for id being the hash-keys in *tables*
  88. using (hash-value table)
  89. when (and (not (game::finished-p (session table)))
  90. (null (ui::player-user :black (ui table))))
  91. collect (list* id :uri (table-uri id) (table-info table)))
  92. #'<
  93. :key #'first
  94. )
  95. (sort (loop for id being the hash-keys in *tables*
  96. using (hash-value table)
  97. when (and (not (game::finished-p (session table)))
  98. (ui::player-user :black (ui table)))
  99. collect (list* id :uri (table-uri id) (table-info table)))
  100. #'<
  101. :key #'first
  102. )
  103. )
  104. )))
  105. )
  106. (hunchentoot:define-easy-handler (proposal-web-handler :uri "/proposal") (table)
  107. (hunchentoot:handle-static-file "proposal.html")
  108. )
  109. (hunchentoot:stop *server*)
  110. ;; todo lock
  111. (defvar *table*)
  112. (set-up-table 1)
  113. (setf *event-queue* (make-event-queue))
  114. (setf *table* (set-up-table 1))
  115. (setf hunchensocket:*websocket-dispatch-table* (list (constantly (progn (hunchentoot:start-session) *table*))))
  116. (table-loop *table*)
  117. (let ((*event-queue* (make-event-queue)))
  118. (setf *table* (set-up-table 1))
  119. (setf (event-queue *table*) *event-queue*)
  120. (setf hunchensocket:*websocket-dispatch-table* (list (lambda (request)
  121. (when (string= (hunchentoot:script-name* request) "/table")
  122. *table*))))
  123. (table-loop *table*)
  124. )
  125. (let* ((*event-queue* (make-event-queue))
  126. (table (set-up-table (incf *id*))))
  127. (setf (event-queue table) *event-queue*)
  128. (setf hunchensocket:*websocket-dispatch-table* '(table-dispatcher))
  129. (bt:make-thread (lambda () (table-loop table)) :name (format nil "table ~A" *id*)))