6a06cb7bf1af4de62be822c901c8283568cc26ab
[mudsync.git] / mudsync / game-master.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of Mudsync.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
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))
27
28 ;;; The game master!  Runs the world.
29 ;;; =================================
30
31 (define-class <game-master> (<actor>)
32   ;; Directory of "special" objects.
33   (special-dir #:init-thunk make-hash-table
34                #:getter gm-special-dir)
35
36   ;; A mapping of client ids to in-game actors
37   ;; and a reverse ;p
38   (client-dir #:init-thunk make-hash-table
39               #:getter gm-client-dir)
40   (reverse-client-dir #:init-thunk make-hash-table
41                       #:getter gm-reverse-client-dir)
42
43   ;; Network manager
44   (network-manager #:getter gm-network-manager
45                    #:init-value #f)
46
47   ;; How we get a new connection acclimated to the system
48   (new-conn-handler #:getter gm-new-conn-handler
49                     #:init-keyword #:new-conn-handler)
50
51   (message-handler
52    #:init-value
53    (make-action-dispatch
54     (init-world (wrap-apply gm-init-world))
55     (client-input (wrap-apply gm-handle-client-input))
56     (lookup-special (wrap-apply gm-lookup-special))
57     (new-client (wrap-apply gm-new-client))
58     (write-home (wrap-apply gm-write-home))
59     (client-closed (wrap-apply gm-client-closed))
60     (inject-special! (wrap-apply gm-inject-special!)))))
61
62
63 ;;; .. begin world init stuff ..
64
65 (define (gm-init-world gm message)
66   ;; Load database
67   ;;  TODO
68
69   ;; Init basic rooms / structure
70   (gm-init-game-spec gm (message-ref message 'game-spec))
71
72   ;; Restore database-based actors
73   ;;  TODO
74
75   ;; Set up the network
76   (gm-setup-network gm))
77
78 (define (gm-init-game-spec gm game-spec)
79   "Initialize the prebuilt special objects"
80   (define set-locs '())
81   (define specials
82     (map
83      (match-lambda
84        ((symbol class loc args ...)
85         ;; initialize the special object
86         (let ((special-obj
87                (apply create-actor* gm class
88                       ;; set cookie to be the object's symbol
89                       (symbol->string symbol)
90                       #:gm (actor-id gm)
91                       args)))
92           ;; register the object
93           (hash-set! (gm-special-dir gm) symbol special-obj)
94           ;; Give ourselves an instruction to set the location
95           (set! set-locs (cons (cons special-obj loc) set-locs))
96           ;; pass it back to the map
97           special-obj)))
98      game-spec))
99
100   ;; Set all initial locations
101   (for-each
102    (match-lambda
103      ((special-obj . loc)
104       (if loc
105           (<-wait gm special-obj 'set-loc!
106                   #:loc (hash-ref (gm-special-dir gm) loc)))))
107    set-locs)
108
109   ;; now init all the objects
110   (for-each
111    (lambda (special-obj)
112      (format #t "Initializing ~s...\n" (address->string special-obj))
113      (<-wait gm special-obj 'init))
114    specials))
115
116
117 (define (gm-setup-network gm)
118   ;; Create a default network manager if none available
119   (slot-set! gm 'network-manager
120              (create-actor* gm <network-manager> "netman"
121                        #:send-input-to (actor-id gm)))
122
123   ;; TODO: Add host and port options
124   (<-wait gm (gm-network-manager gm) 'start-listening))
125
126 (define (gm-setup-database gm)
127   'TODO)
128
129 ;;; .. end world init stuff ...
130
131 (define-mhandler (gm-new-client actor message client)
132   ;; @@: Maybe more indirection than needed for this
133   ((gm-new-conn-handler actor) actor client))
134
135
136 (define (gm-handle-client-input actor message)
137   "Handle input from a client."
138   (define client-id (message-ref message 'client))
139   (define input (message-ref message 'data))
140   ;; Look up player
141   (define player (hash-ref (gm-client-dir actor) client-id))
142
143   ;; debugging
144   (format #t "DEBUG: From ~s: ~s\n" client-id input)
145
146   (<- actor player 'handle-input
147       #:input input))
148
149 (define-mhandler (gm-lookup-special actor message symbol)
150   (<-reply actor message
151            #:room-id (hash-ref (slot-ref actor 'special-dir) symbol)))
152
153 (define-mhandler (gm-write-home actor message text)
154   (define client-id (hash-ref (gm-reverse-client-dir actor)
155                               (message-from message)))
156   (<- actor (gm-network-manager actor) 'send-to-client
157       #:client client-id
158       #:data text))
159
160 (define-mhandler (gm-client-closed gm message client)
161   ;; Do we have this client registered to an actor?  Get the id if so.
162   (define actor-id (hash-ref (gm-client-dir gm) client))
163
164   ;; Have the actor appropriately disappear / be removed from its
165   ;; room, if we have one.
166   ;; (In some games, if the user never connected)
167   (when actor-id
168     (<-wait gm actor-id 'disconnect-self-destruct)
169     ;; Unregister from the client directories.
170     (gm-unregister-client! gm client)))
171
172
173 (define-mhandler (gm-inject-special! gm message
174                                      special-symbol gameobj-spec)
175   "Inject, possiibly replacing the original, special symbol
176 using the gameobj-spec."
177   (pk 'special-symbol special-symbol)
178   (pk 'gameobj-spec gameobj-spec))
179
180 ;;; GM utilities
181
182 (define (gm-register-client! gm client-id player)
183   (hash-set! (gm-client-dir gm) client-id player)
184   (hash-set! (gm-reverse-client-dir gm) player client-id))
185
186 (define* (gm-unregister-client! gm client-id #:optional destroy-player)
187   "Remove a connection/player combo and ask them to self destruct"
188   (match (hash-remove! (gm-client-dir gm) client-id)  ; Remove from our client dir
189     ((_ . player-id)
190      ;; Remove from reverse table too
191      (hash-remove! (gm-reverse-client-dir gm) client-id)
192      ;; Destroy player 
193      (if destroy-player
194          (<- gm player-id 'self-destruct)))
195     (#f (throw 'no-client-to-unregister
196                "Can't unregister a client that doesn't exist?"
197                client-id))))
198
199 ;;; An easy default
200
201 (define (make-default-room-conn-handler default-room)
202   "Make a handler for a GM that dumps people in a default room
203 with an anonymous persona"
204   (let ((count 0))
205     (lambda (gm client-id)
206       (set! count (+ count 1))
207       (let* ((guest-name (string-append "Guest-"
208                                         (number->string count)))
209              (room-id
210               (hash-ref (gm-special-dir gm) default-room))
211              ;; create and register the player
212              (player
213               (create-actor* gm (@@ (mudsync player) <player>) "player"
214                              #:name guest-name
215                              #:gm (actor-id gm)
216                              #:client client-id)))
217         ;; Register the player in our database of players -> connections
218         (gm-register-client! gm client-id player)
219         ;; Dump the player into the default room
220         (<-wait gm player 'set-loc! #:loc room-id)
221         ;; Initialize the player
222         (<-wait gm player 'init)
223         (<- gm room-id 'tell-room
224             #:text (format #f "You see ~a materialize out of thin air!\n"
225                            guest-name)
226             #:exclude player)))))
227