From: Christopher Allan Webber Date: Fri, 6 May 2016 15:05:50 +0000 (-0500) Subject: room level messages, client disconnection X-Git-Tag: fosdem-2017~170 X-Git-Url: https://jxself.org/git/?p=mudsync.git;a=commitdiff_plain;h=902b52f87ea65d6a52cb84afee32ad20d89b2bc3 room level messages, client disconnection --- diff --git a/mudsync/game-master.scm b/mudsync/game-master.scm index 34476b0..89fbaf6 100644 --- a/mudsync/game-master.scm +++ b/mudsync/game-master.scm @@ -55,7 +55,8 @@ (client-input (wrap-apply gm-handle-client-input)) (lookup-special (wrap-apply gm-lookup-special)) (new-client (wrap-apply gm-new-client)) - (write-home (wrap-apply gm-write-home))))) + (write-home (wrap-apply gm-write-home)) + (client-closed (wrap-apply gm-client-closed))))) ;;; .. begin world init stuff .. @@ -155,6 +156,18 @@ #:client client-id #:data text)) +(define-mhandler (gm-client-closed gm message client) + ;; Do we have this client registered to an actor? Get the id if so. + (define actor-id (hash-ref (gm-client-dir gm) client)) + + ;; Have the actor appropriately disappear / be removed from its + ;; room, if we have one. + ;; (In some games, if the user never connected) + (when actor-id + (<-wait gm actor-id 'disconnect-self-destruct) + ;; Unregister from the client directories. + (gm-unregister-client! gm client))) + ;;; GM utilities @@ -162,14 +175,15 @@ (hash-set! (gm-client-dir gm) client-id player) (hash-set! (gm-reverse-client-dir gm) player client-id)) -(define (gm-unregister-client! gm client-id) +(define* (gm-unregister-client! gm client-id #:optional destroy-player) "Remove a connection/player combo and ask them to self destruct" (match (hash-remove! (gm-client-dir gm) client-id) ; Remove from our client dir ((_ . player-id) ;; Remove from reverse table too (hash-remove! (gm-reverse-client-dir gm) client-id) ;; Destroy player - (<- gm player-id 'destroy-self)) + (if destroy-player + (<- gm player-id 'self-destruct))) (#f (throw 'no-client-to-unregister "Can't unregister a client that doesn't exist?" client-id)))) diff --git a/mudsync/gameobj.scm b/mudsync/gameobj.scm index fa35548..91701ac 100644 --- a/mudsync/gameobj.scm +++ b/mudsync/gameobj.scm @@ -32,9 +32,10 @@ gameobj-loc gameobj-gm gameobj-name - gameobj-name-f - gameobj-actions)) + gameobj-occupants + gameobj-actions + gameobj-self-destruct)) ;;; Gameobj ;;; ======= @@ -49,11 +50,13 @@ (get-occupants (wrap-apply gameobj-get-occupants)) (add-occupant! (wrap-apply gameobj-add-occupant!)) (remove-occupant! (wrap-apply gameobj-remove-occupant!)) - (set-loc! (wrap-apply gameobj-set-loc!)) + (set-loc! (wrap-apply gameobj-act-set-loc!)) (get-name (wrap-apply gameobj-get-name)) (get-desc (wrap-apply gameobj-get-desc)) (goes-by (wrap-apply gameobj-act-goes-by)) - (visible-name (wrap-apply gameobj-visible-name)))) + (visible-name (wrap-apply gameobj-visible-name)) + (self-destruct (wrap-apply gameobj-act-self-destruct)) + (tell (wrap-apply gameobj-tell-no-op)))) ;;; *all* game components that talk to players should somehow ;;; derive from this class. @@ -65,8 +68,7 @@ #:getter gameobj-loc) ;; Uses a hash table like a set (values ignored) - (occupants #:init-thunk make-hash-table - #:getter gameobj-occupants) + (occupants #:init-thunk make-hash-table) ;; game master id (gm #:init-keyword #:gm @@ -80,11 +82,6 @@ (desc #:init-value #f #:init-keyword #:desc) - ;; how to print our name - (name-f #:init-keyword #:name-f - #:getter gameobj-name-f - #:init-value (wrap gameobj-simple-name-f)) - ;; Commands we can handle (commands #:init-value '()) @@ -171,28 +168,52 @@ "Remove an occupant from the room." (hash-remove! (slot-ref actor 'occupants) who)) +(define* (gameobj-occupants gameobj #:key exclude) + (hash-fold + (lambda (occupant _ prev) + (define exclude-it? + (match exclude + ;; Empty list and #f are non-exclusion + (() #f) + (#f #f) + ;; A list of addresses... since our address object is (annoyingly) + ;; currently a simple cons cell... + ((exclude-1 ... exclude-rest) + (pk 'failboat (member occupant (pk 'exclude-lst exclude)))) + ;; Must be an individual address! + (_ (equal? occupant exclude)))) + (if exclude-it? + prev + (cons occupant prev))) + '() + (slot-ref gameobj 'occupants))) + (define-mhandler (gameobj-get-occupants actor message) "Get all present occupants of the room." + (define exclude (message-ref message 'exclude #f)) (define occupants - (hash-map->list (lambda (key val) key) - (gameobj-occupants actor))) + (gameobj-occupants actor #:exclude exclude)) (<-reply actor message #:occupants occupants)) -;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc? -(define-mhandler (gameobj-set-loc! actor message loc) +(define (gameobj-set-loc! gameobj loc) "Set the location of this object." - (define old-loc (gameobj-loc actor)) + (define old-loc (gameobj-loc gameobj)) (format #t "DEBUG: Location set to ~s for ~s\n" - loc (actor-id-actor actor)) + loc (actor-id-actor gameobj)) - (slot-set! actor 'loc loc) + (slot-set! gameobj 'loc loc) ;; Change registation of where we currently are (if loc - (<-wait actor loc 'add-occupant! #:who (actor-id actor))) + (<-wait gameobj loc 'add-occupant! #:who (actor-id gameobj))) (if old-loc - (<-wait actor old-loc 'remove-occupant! #:who (actor-id actor)))) + (<-wait gameobj old-loc 'remove-occupant! #:who (actor-id gameobj)))) + +;; @@: Should it really be #:id ? Maybe #:loc-id or #:loc? +(define-mhandler (gameobj-act-set-loc! actor message loc) + "Action routine to set the location." + (gameobj-set-loc! actor loc)) (define gameobj-get-name (simple-slot-getter 'name)) @@ -229,3 +250,18 @@ By default, this is whether or not the generally-visible flag is set." (#f #f)) #f)) (<-reply actor message #:text name-to-return)) + +(define (gameobj-self-destruct gameobj) + "General gameobj self destruction routine" + ;; Unregister from being in any particular room + (gameobj-set-loc! gameobj #f) + ;; Boom! + (self-destruct gameobj)) + +(define-mhandler (gameobj-act-self-destruct gameobj message) + "Action routine for self destruction" + (gameobj-self-destruct gameobj)) + +;; Unless an actor has a tell message, we just ignore it +(define gameobj-tell-no-op + (const 'no-op)) diff --git a/mudsync/networking.scm b/mudsync/networking.scm index fe641cc..8c5955d 100644 --- a/mudsync/networking.scm +++ b/mudsync/networking.scm @@ -145,14 +145,16 @@ "Handle a closed port" (format #t "DEBUG: handled closed port ~x\n" client-id) (8sync-port-remove client) - (hash-remove! (nm-clients nm) client-id)) + (hash-remove! (nm-clients nm) client-id) + (<- nm (nm-send-input-to nm) 'client-closed #:client client-id)) (define-method (nm-handle-port-eof nm client client-id) "Handle seeing an EOF on port" (format #t "DEBUG: handled eof-object on port ~x\n" client-id) (close client) (8sync-port-remove client) - (hash-remove! (nm-clients nm) client-id)) + (hash-remove! (nm-clients nm) client-id) + (<- nm (nm-send-input-to nm) 'client-closed #:client client-id)) (define-method (nm-handle-line nm client client-id line) "Handle an incoming line of input from a client" diff --git a/mudsync/player.scm b/mudsync/player.scm index 3d1ec72..8b9a22f 100644 --- a/mudsync/player.scm +++ b/mudsync/player.scm @@ -39,7 +39,8 @@ (build-actions (init (wrap-apply player-init)) (handle-input (wrap-apply player-handle-input)) - (tell (wrap-apply player-tell)))) + (tell (wrap-apply player-tell)) + (disconnect-self-destruct (wrap-apply player-disconnect-self-destruct)))) (define player-actions* (append player-actions @@ -91,6 +92,16 @@ (<- player (gameobj-gm player) 'write-home #:text text)) +(define-mhandler (player-disconnect-self-destruct player message) + "Action routine for being told to disconnect and self destruct." + (define loc (gameobj-loc player)) + (when loc + (<- player loc 'tell-room + #:exclude (actor-id player) + #:text (format #f "~a disappears in a puff of entropy!\n" + (slot-ref player 'name)))) + (gameobj-self-destruct player)) + ;;; Command handling ;;; ================ diff --git a/mudsync/room.scm b/mudsync/room.scm index 10932c9..0b0e101 100644 --- a/mudsync/room.scm +++ b/mudsync/room.scm @@ -83,6 +83,7 @@ (cmd-go (wrap-apply room-cmd-go)) (cmd-go-where (wrap-apply room-cmd-go-where)) (look-room (wrap-apply room-look-room)) + (tell-room (wrap-apply room-tell-room)) ;; in this case the command is the same version as the normal ;; look-room version (cmd-look-room (wrap-apply room-look-room)) @@ -257,3 +258,19 @@ claim to point to." (else (<- room (message-from message) 'tell #:text "You don't see that here, so you can't look at it.\n")))) + +(define-mhandler (room-tell-room room message text) + "Tell the room some messages." + (define exclude (message-ref message 'exclude #f)) + (define wait-delivery (message-ref message 'wait-delivery #f)) + (define who-to-tell (gameobj-occupants room #:exclude exclude)) + (for-each + (lambda (tell-me) + ;; @@: Does anything really care? + (define deliver-method + (if wait-delivery + <-wait + <-)) + (deliver-method room tell-me 'tell + #:text text)) + who-to-tell))