aboutsummaryrefslogtreecommitdiff
path: root/examples/dice-roller.lisp
blob: 45e2979e30704a9e577c09490ff57f5ebea119d5 (plain)
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
(defpackage #:weekend.examples.dice-roller
  (:use #:cl)
  (:local-nicknames
   (#:wknd #:weekend)))

(in-package #:weekend.examples.dice-roller)

(defconstant +digits+ "([0-9]+)")

(defclass roller ()
  ((rolls
    :reader rolls
    :initarg :rolls
    :initform (wknd::slot-required 'roller 'rolls)
    :type integer
    :documentation "The number of rolls")
   (sides
    :reader sides
    :initarg :sides
    :initform (wknd:slot-required 'roller 'rolls)
    :type integer
    :documentation "The number of sides"))
  (:documentation "Rolls a virtual dices with SIDES sides ROLLS number of times.")
  (:metaclass wknd::endpoint)
  (:method . :get)
  (:route-parts "roll" +digits+ "d" +digits+)
  (:extractors (:rolls parse-integer) (:sides parse-integer))
  (:content-type . "text/plain"))

(defmethod wknd::handle ((req roller))
  (with-slots (rolls sides) req
    (format nil "~ad~a ... rolled a ~a"
            rolls sides
            (loop :repeat rolls :sum (1+ (random sides))))))

(defvar *server* (make-instance 'hunchentoot:easy-acceptor
                   :port 8888))

(hunchentoot:start *server*)

;; now visit http://localhost:8888/roll/10/d/4

(ql:quickload :dexador)

(defun get-roll (rolls sides)
  (dexador:get
   (concatenate 'string "http://localhost:8888"
                (wknd::route-to 'roller :rolls rolls :sides sides))))