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