remove debug message
[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
61
62 ;;; .. begin world init stuff ..
63
64 (define (gm-init-world gm message)
65   ;; Load database
66   ;;  TODO
67
68   ;; Init basic rooms / structure
69   (gm-init-game-spec gm (message-ref message 'game-spec))
70
71   ;; Restore database-based actors
72   ;;  TODO
73
74   ;; Set up the network
75   (gm-setup-network gm))
76
77 (define (gm-init-game-spec gm game-spec)
78   "Initialize the prebuilt special objects"
79   (define set-locs '())
80   (define specials
81     (map
82      (match-lambda
83        ((symbol class loc args ...)
84         ;; initialize the special object
85         (let ((special-obj
86                (apply create-actor* gm class
87                       ;; set cookie to be the object's symbol
88                       (symbol->string symbol)
89                       #:gm (actor-id gm)
90                       args)))
91           ;; register the object
92           (hash-set! (gm-special-dir gm) symbol special-obj)
93           ;; Give ourselves an instruction to set the location
94           (set! set-locs (cons (cons special-obj loc) set-locs))
95           ;; pass it back to the map
96           special-obj)))
97      game-spec))
98
99   ;; Set all initial locations
100   (for-each
101    (match-lambda
102      ((special-obj . loc)
103       (if loc
104           (<-wait gm special-obj 'set-loc!
105                   #:loc (hash-ref (gm-special-dir gm) loc)))))
106    set-locs)
107
108   ;; now init all the objects
109   (for-each
110    (lambda (special-obj)
111      (format #t "Initializing ~s...\n" (address->string special-obj))
112      (<-wait gm special-obj 'init))
113    specials))
114
115
116 (define (gm-setup-network gm)
117   ;; Create a default network manager if none available
118   (slot-set! gm 'network-manager
119              (create-actor* gm <network-manager> "netman"
120                        #:send-input-to (actor-id gm)))
121
122   ;; TODO: Add host and port options
123   (<-wait gm (gm-network-manager gm) 'start-listening))
124
125 (define (gm-setup-database gm)
126   'TODO)
127
128 ;;; .. end world init stuff ...
129
130 (define-mhandler (gm-new-client actor message client)
131   ;; @@: Maybe more indirection than needed for this
132   ((gm-new-conn-handler actor) actor client))
133
134
135 (define (gm-handle-client-input actor message)
136   "Handle input from a client."
137   (define client-id (message-ref message 'client))
138   (define input (message-ref message 'data))
139   ;; Look up player
140   (define player (hash-ref (gm-client-dir actor) client-id))
141
142   ;; debugging
143   (format #t "DEBUG: From ~s: ~s\n" client-id input)
144
145   (<- actor player 'handle-input
146       #:input input))
147
148 (define-mhandler (gm-lookup-special actor message symbol)
149   (<-reply actor message
150            #:room-id (hash-ref (slot-ref actor 'special-dir) symbol)))
151
152 (define-mhandler (gm-write-home actor message text)
153   (define client-id (hash-ref (gm-reverse-client-dir actor)
154                               (message-from message)))
155   (<- actor (gm-network-manager actor) 'send-to-client
156       #:client client-id
157       #:data text))
158
159 (define-mhandler (gm-client-closed gm message client)
160   ;; Do we have this client registered to an actor?  Get the id if so.
161   (define actor-id (hash-ref (gm-client-dir gm) client))
162
163   ;; Have the actor appropriately disappear / be removed from its
164   ;; room, if we have one.
165   ;; (In some games, if the user never connected)
166   (when actor-id
167     (<-wait gm actor-id 'disconnect-self-destruct)
168     ;; Unregister from the client directories.
169     (gm-unregister-client! gm client)))
170
171
172 ;;; GM utilities
173
174 (define (gm-register-client! gm client-id player)
175   (hash-set! (gm-client-dir gm) client-id player)
176   (hash-set! (gm-reverse-client-dir gm) player client-id))
177
178 (define* (gm-unregister-client! gm client-id #:optional destroy-player)
179   "Remove a connection/player combo and ask them to self destruct"
180   (match (hash-remove! (gm-client-dir gm) client-id)  ; Remove from our client dir
181     ((_ . player-id)
182      ;; Remove from reverse table too
183      (hash-remove! (gm-reverse-client-dir gm) client-id)
184      ;; Destroy player 
185      (if destroy-player
186          (<- gm player-id 'self-destruct)))
187     (#f (throw 'no-client-to-unregister
188                "Can't unregister a client that doesn't exist?"
189                client-id))))
190
191 ;;; An easy default
192
193 (define (make-default-room-conn-handler default-room)
194   "Make a handler for a GM that dumps people in a default room
195 with an anonymous persona"
196   (let ((count 0))
197     (lambda (gm client-id)
198       (set! count (+ count 1))
199       (let* ((guest-name (string-append "Guest-"
200                                         (number->string count)))
201              (room-id
202               (hash-ref (gm-special-dir gm) default-room))
203              ;; create and register the player
204              (player
205               (create-actor* gm (@@ (mudsync player) <player>) "player"
206                              #:name guest-name
207                              #:gm (actor-id gm)
208                              #:client client-id)))
209         (display "Are we broke yet?\n")
210         ;; Register the player in our database of players -> connections
211         (gm-register-client! gm client-id player)
212         ;; Dump the player into the default room
213         (<-wait gm player 'set-loc! #:loc room-id)
214         ;; Initialize the player
215         (<- gm player 'init)))))
216