1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of Mudsync.
6 ;;; Mudsync is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
11 ;;; Mudsync is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;; General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (mudsync game-master)
20 #:use-module (mudsync networking)
21 #:use-module (8sync systems actors)
22 #:use-module (8sync agenda)
23 #:use-module (oop goops)
24 #:use-module (ice-9 match)
25 #:export (<game-master>
26 make-default-room-conn-handler))
28 ;;; The game master! Runs the world.
29 ;;; =================================
31 (define-class <game-master> (<actor>)
32 ;; Directory of "special" objects.
33 (special-dir #:init-thunk make-hash-table
34 #:getter gm-special-dir)
36 ;; Room directory. Room symbols to locations.
37 (room-dir #:init-thunk make-hash-table
40 ;; A mapping of client ids to in-game actors
42 (client-dir #:init-thunk make-hash-table
43 #:getter gm-client-dir)
44 (reverse-client-dir #:init-thunk make-hash-table
45 #:getter gm-reverse-client-dir)
48 (network-manager #:accessor gm-network-manager
51 ;; How we get a new connection acclimated to the system
52 (new-conn-handler #:accessor gm-new-conn-handler
53 #:init-keyword #:new-conn-handler)
58 (init-world (wrap-apply gm-init-world))
59 (client-input (wrap-apply gm-handle-client-input))
60 (lookup-room (wrap-apply gm-lookup-room))
61 (new-client (wrap-apply gm-new-client))
62 (write-home (wrap-apply gm-write-home)))))
65 ;;; .. begin world init stuff ..
67 (define (gm-init-world gm message)
71 ;; Init basic rooms / structure
72 (gm-init-rooms gm (message-ref message 'room-spec))
74 ;; Restore database-based actors
78 (gm-setup-network gm))
80 (define (gm-init-rooms gm rooms-spec)
81 "Initialize the prebuilt rooms"
85 ((room-symbol room-class
87 ;; initialize the room
89 (apply create-actor* gm room-class "room"
93 (hash-set! (gm-room-dir gm) room-symbol room)
94 ;; pass it back to the map
98 ;; now wire up all the exits
101 (format #t "Wiring up ~s...\n" (address->string room))
102 (<-wait gm room 'wire-exits!))
106 (define (gm-setup-network gm)
107 ;; Create a default network manager if none available
108 (set! (gm-network-manager gm)
109 (create-actor* gm <network-manager> "netman"
110 #:send-input-to (actor-id gm)))
112 ;; TODO: Add host and port options
113 (<-wait gm (gm-network-manager gm) 'start-listening))
115 (define (gm-setup-database gm)
118 ;;; .. end world init stuff ...
120 (define-mhandler (gm-new-client actor message client)
121 ;; @@: Maybe more indirection than needed for this
122 ((gm-new-conn-handler actor) actor client))
125 (define (gm-handle-client-input actor message)
126 "Handle input from a client."
127 (define client-id (message-ref message 'client))
128 (define input (message-ref message 'data))
130 (define player (hash-ref (gm-client-dir actor) client-id))
133 (format #t "DEBUG: From ~s: ~s\n" client-id input)
135 (<- actor player 'handle-input
138 (define-mhandler (gm-lookup-room actor message symbol)
139 (<-reply actor message
140 #:room-id (hash-ref (slot-ref actor 'room-dir) symbol)))
142 (define-mhandler (gm-write-home actor message text)
143 (define client-id (hash-ref (gm-reverse-client-dir actor)
144 (message-from message)))
145 (<- actor (gm-network-manager actor) 'send-to-client
152 (define (gm-register-client! gm client-id player)
153 (hash-set! (gm-client-dir gm) client-id player)
154 (hash-set! (gm-reverse-client-dir gm) player client-id))
156 (define (gm-unregister-client! gm client-id)
157 "Remove a connection/player combo and ask them to self destruct"
158 (match (hash-remove! (gm-client-dir gm) client-id) ; Remove from our client dir
160 ;; Remove from reverse table too
161 (hash-remove! (gm-reverse-client-dir gm) client-id)
163 (<- gm player-id 'destroy-self))
164 (#f (throw 'no-client-to-unregister
165 "Can't unregister a client that doesn't exist?"
170 (define (make-default-room-conn-handler default-room)
171 "Make a handler for a GM that dumps people in a default room
172 with an anonymous persona"
173 (display "right before breakage?\n")
175 (lambda (gm client-id)
176 (set! count (+ count 1))
177 (let* ((guest-name (string-append "Guest-"
178 (number->string count)))
180 (hash-ref (gm-room-dir gm) default-room))
181 ;; create and register the player
183 (create-actor* gm (@@ (mudsync player) <player>) "player"
184 #:username guest-name
186 #:client client-id)))
187 (display "Are we broke yet?\n")
188 ;; Register the player in our database of players -> connections
189 (gm-register-client! gm client-id player)
190 ;; Dump the player into the default room
191 (<-wait gm player 'set-loc! #:loc room-id)
192 ;; Initialize the player
193 (<- gm player 'init)))))