Updating mudsync for 8sync suspendable-ports refactor
[mudsync.git] / mudsync / player.scm
index 6e4be5738b2f289ed93decb95a258f2df22f0073..172b81072b4e85e98fcf8536f91be5a9a1dccae3 100644 (file)
@@ -40,7 +40,8 @@
    (init (wrap-apply player-init))
    (handle-input (wrap-apply player-handle-input))
    (tell (wrap-apply player-tell))
-   (disconnect-self-destruct (wrap-apply player-disconnect-self-destruct))))
+   (disconnect-self-destruct (wrap-apply player-disconnect-self-destruct))
+   (cmd-inventory (wrap-apply player-cmd-inventory))))
 
 (define player-actions*
   (append player-actions
 (define player-dispatcher
   (simple-dispatcher player-actions*))
 
+(define player-self-commands
+  (list
+   (empty-command "inventory" 'cmd-inventory)
+   ;; aliases...
+   ;; @@: Should use an "alias" system for common aliases?
+   (empty-command "inv" 'cmd-inventory)
+   (empty-command "i" 'cmd-inventory)))
+
 (define-class <player> (<gameobj>)
   (username #:init-keyword #:username
             #:getter player-username)
 
-  (self-commands
-   #:init-value '()
-   #:getter player-self-commands)
+  (self-commands #:init-value player-self-commands)
 
   (message-handler
    #:init-value
-   ;; @@: We're gonna need action inheritance real awful soon, huh?
    (wrap-apply player-dispatcher)))
 
 
                        (slot-ref player 'name))))
   (gameobj-self-destruct player))
 
+(define-mhandler (player-cmd-inventory player message)
+  "Display the inventory for the player"
+  (define inv-names
+    (map
+     (lambda (inv-item)
+       (message-ref (<-wait player inv-item 'get-name)
+                    'val))
+     (gameobj-occupants player)))
+  (define text-to-show
+    (if (eq? inv-names '())
+        "You aren't carrying anything.\n"
+        (apply string-append
+               "You are carrying:\n"
+               (map (lambda (item-name)
+                      (string-append "  * " item-name "\n"))
+                    inv-names))))
+  (<- player (actor-id player) 'tell #:text text-to-show))
+
 
 ;;; Command handling
 ;;; ================
 
   ;; Get all the co-occupants' commands
   (define co-occupant-commands
-    ;; TODO: Switch this to a fold.  Ignore a result if it
-    ;;   returns false for in the command response
     (fold
      (lambda (co-occupant prev)
        (let* ((result (<-wait player co-occupant 'get-commands
 
   ;; Append our own command handlers
   (define our-commands
-    (player-self-commands player))
-
-  ;; TODO: Append our inventory's relevant command handlers
+    (filter
+     (lambda (cmd)
+       (equal? (command-verbs cmd) verb))
+     (val-or-run
+      (slot-ref player 'self-commands))))
+
+  ;; Append our inventory's relevant command handlers
+  (define inv-items
+    (gameobj-occupants player))
+  (define inv-item-commands
+    (fold
+     (lambda (inv-item prev)
+       (let* ((result (<-wait player inv-item
+                              'get-contained-commands
+                              #:verb verb))
+              (commands (message-ref result 'commands))
+              (goes-by (message-ref result 'goes-by)))
+         (append
+          (map (lambda (command)
+                 (list command goes-by inv-item))
+               commands)
+          prev)))
+     '()
+     inv-items))
 
   ;; Now return a big ol sorted list of ((actor-id . command))
   (append
                                player-loc '()) ; room doesn't go by anything
    (sort-commands-multi-actors co-occupant-commands)
    (sort-commands-append-actor our-commands
-                               (actor-id player) '()))) ; nor does player
+                               (actor-id player) '()) ; nor does player
+   (sort-commands-multi-actors inv-item-commands)))
 
 (define (sort-commands-append-actor commands actor-id goes-by)
   (sort-commands-multi-actors