-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lisp
More file actions
65 lines (52 loc) · 1.98 KB
/
main.lisp
File metadata and controls
65 lines (52 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(ql:quickload :hunchentoot)
(ql:quickload :com.inuoe.jzon)
(load "cl-data-structures.lisp")
(load "quest-generator.lisp")
(defpackage :main
(:use :cl :hunchentoot :com.inuoe.jzon)
(:import-from :cl-data-structures :hashmap)
(:import-from :quest-generator :init-quest-state)
(:import-from :quest-generator :get-current-actions)
(:import-from :quest-generator :get-current-ambients)
(:import-from :quest-generator :get-current-summary)
(:import-from :quest-generator :get-quest-requirements)
(:export :main))
(in-package :main)
(defun create-content-json (answer-list)
(com.inuoe.jzon:stringify
(hashmap "content" answer-list)))
(hunchentoot:define-easy-handler (init :uri "/init") ()
(progn
(init-quest-state)
(setf (hunchentoot:content-type*) "application/json")
(com.inuoe.jzon:stringify
(hashmap "status" "OK!"))))
(hunchentoot:define-easy-handler (actions :uri "/actions") ()
(setf (hunchentoot:content-type*) "application/json")
(create-content-json (get-current-actions)))
(hunchentoot:define-easy-handler (ambients :uri "/ambients") ()
(setf (hunchentoot:content-type*) "application/json")
(create-content-json (get-current-ambients)))
(hunchentoot:define-easy-handler (summary :uri "/summary") ()
(setf (hunchentoot:content-type*) "application/json")
(create-content-json (get-current-summary)))
(hunchentoot:define-easy-handler (requirements :uri "/requirements") ()
(setf (hunchentoot:content-type*) "application/json")
(create-content-json (get-quest-requirements)))
;; Start the server
(defvar *server* nil)
(defun start-server (&optional (port 4242))
(setf *server*
(make-instance 'hunchentoot:easy-acceptor :port 4242))
(hunchentoot:start *server*)
(format t "Server started on http://localhost:~a/hello~%" port))
(defun stop-server ()
(when *server*
(hunchentoot:stop *server*)
(setf *server* nil)
(format t "Server stopped.~%")))
(defun main ()
(init-quest-state)
(start-server)
(loop (sleep 60)))
;;(stop-server)