2a072db8f7e7af7d9dd164ec84040432669071e9 — Thomas Letan 1 year, 1 month ago 3d62bd8 master
feature: Add a new backend method to fetch puppet (character) info

We factorize code with the previously defined function `get-player`,
thanks to the `get-resource` function. As for the terminology,
`character` suffers the same limitation of `map`, hence we have to
rename it. On the one hand, `map` became `scene`, on the other hand
`character` becomes `puppet`.

Signed-off-by: Thomas Letan <contact@thomasletan.fr>
1 files changed, 35 insertions(+), 10 deletions(-)

M backend.lisp
M backend.lisp => backend.lisp +35 -10
@@ 17,31 17,56 @@ (defpackage :lycan/backend
    (:nicknames :lb)
    (:use :cl :alexandria :flexi-streams)
-   (:export get-player))
+   (:export player
+            get-player
+            puppets
+ 
+            puppet
+            get-puppet
+            name))
  
  (cl:in-package :lycan/backend)
  
  (defclass player ()
-   ((characters :initarg :characters
+   ((puppets :initarg :puppets
                 :initform nil
-                :accessor characters)))
- 
- (defun get-player-url (name)
-   (concatenate 'string "http://0.0.0.0:4001/player/" name))
+                :accessor puppets)))
  
- (defun get-player (name &key then event-cb)
+ (defun get-resource (id get-url unserialize then &optional event-cb)
    (if then
        (blackbird:catcher
         (blackbird:multiple-promise-bind (body status)
-            (das:http-request (get-player-url name))
+            (das:http-request (funcall get-url id))
           (if (eq status 200)
-              (funcall then (byte-array-to-player body))))
+              (funcall then (funcall unserialize body))))
         (error (e)
                (when event-cb
                  (funcall event-cb e))))
        (error "need to provide a callback to use the result")))
  
+ (defun get-player (name &key then event-cb)
+   (get-resource name #'get-player-url #'byte-array-to-player then event-cb))
+ 
+ (defun get-player-url (name)
+   (concatenate 'string "http://0.0.0.0:4001/player/" name))
+ 
  (defun byte-array-to-player (bytes)
    (let* ((json-str (octets-to-string bytes :external-format :utf-8))
           (json (jsown:parse json-str)))
-     (make-instance 'player :characters (jsown:val json "characters"))))
+     (make-instance 'player :puppets (jsown:val json "puppets"))))
+ 
+ (defclass puppet ()
+   ((name :initarg :name
+          :initform (error "must supply a name to create a puppet")
+          :accessor name)))
+ 
+ (defun get-puppet (key &key then event-cb)
+   (get-resource key #'get-puppet-url #'byte-array-to-puppet then event-cb))
+ 
+ (defun get-puppet-url (key)
+   (concatenate 'string "http://0.0.0.0:4001/character/" key))
+ 
+ (defun byte-array-to-puppet (bytes)
+   (let* ((json-str (octets-to-string bytes :external-format :utf-8))
+          (json (jsown:parse json-str)))
+     (make-instance 'puppet :name (jsown:val json "name"))))