|
@@ -0,0 +1,821 @@
|
|
1
|
+package funmow
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "bufio"
|
|
5
|
+ "fmt"
|
|
6
|
+ "log"
|
|
7
|
+ "net"
|
|
8
|
+ "strings"
|
|
9
|
+ "unicode"
|
|
10
|
+)
|
|
11
|
+
|
|
12
|
+type Client struct {
|
|
13
|
+ conn net.Conn
|
|
14
|
+ connectionID int
|
|
15
|
+ player Object
|
|
16
|
+ authenticated bool
|
|
17
|
+ connected bool
|
|
18
|
+ reader *bufio.Reader
|
|
19
|
+ commandChan chan string
|
|
20
|
+ inbound chan PlayerEvent
|
|
21
|
+ outbound EventChanSet
|
|
22
|
+ eventDistributor *EventDistributor
|
|
23
|
+ world *Store
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+func NewClient(conn net.Conn, connectionID int, e *EventDistributor, w *Store) *Client {
|
|
27
|
+ c := new(Client)
|
|
28
|
+ c.conn = conn
|
|
29
|
+ c.connectionID = connectionID
|
|
30
|
+ c.reader = bufio.NewReader(conn)
|
|
31
|
+ c.authenticated = false
|
|
32
|
+ c.connected = true
|
|
33
|
+ c.commandChan = make(chan string)
|
|
34
|
+ c.inbound = make(chan PlayerEvent)
|
|
35
|
+ c.eventDistributor = e
|
|
36
|
+ c.world = w
|
|
37
|
+ return c
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+func (c *Client) Run() {
|
|
41
|
+
|
|
42
|
+ log.Print("Received connection from ", c.conn.RemoteAddr())
|
|
43
|
+
|
|
44
|
+ go func() {
|
|
45
|
+ for c.connected {
|
|
46
|
+ message, err := c.reader.ReadString('\n')
|
|
47
|
+ if err != nil {
|
|
48
|
+ c.connected = false
|
|
49
|
+ return
|
|
50
|
+ }
|
|
51
|
+ message = strings.TrimSpace(message)
|
|
52
|
+ if len(message) > 0 {
|
|
53
|
+ c.commandChan <- message
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+ }()
|
|
57
|
+
|
|
58
|
+ c.splash()
|
|
59
|
+
|
|
60
|
+ for c.connected {
|
|
61
|
+ select {
|
|
62
|
+ case m := <-c.inbound:
|
|
63
|
+
|
|
64
|
+ switch m.messageType {
|
|
65
|
+ case EventTypeEmit:
|
|
66
|
+ fallthrough
|
|
67
|
+ case EventTypeOEmit:
|
|
68
|
+ if m.audience == c.player.Inside {
|
|
69
|
+ c.write("%s\n", m.message)
|
|
70
|
+ }
|
|
71
|
+ case EventTypePEmit:
|
|
72
|
+ c.write("%s\n", m.message)
|
|
73
|
+ case EventTypeWall:
|
|
74
|
+ speaker, found := c.world.Fetch(m.src)
|
|
75
|
+ if !found {
|
|
76
|
+ break
|
|
77
|
+ }
|
|
78
|
+ c.write("In the distance, you hear %s bellow out \"%s\"\n", speaker.Name, m.message)
|
|
79
|
+ case EventTypePage:
|
|
80
|
+ speaker, found := c.world.Fetch(m.src)
|
|
81
|
+ if !found {
|
|
82
|
+ break
|
|
83
|
+ }
|
|
84
|
+ c.write("%s pages you: \"%s\"\n", speaker.Name, m.message)
|
|
85
|
+ case EventTypeSay:
|
|
86
|
+ if m.audience == c.player.Inside {
|
|
87
|
+ if m.src == c.player.ID {
|
|
88
|
+ c.write("You say \"%s\"\n", m.message)
|
|
89
|
+ } else {
|
|
90
|
+ speaker, found := c.world.Fetch(m.src)
|
|
91
|
+ if !found {
|
|
92
|
+ break
|
|
93
|
+ }
|
|
94
|
+ c.write("%s says \"%s\"\n", speaker.Name, m.message)
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ case EventTypePose:
|
|
98
|
+ if m.audience == c.player.Inside {
|
|
99
|
+ if m.src == c.player.ID {
|
|
100
|
+ c.write("%s %s\n", c.player.Name, m.message)
|
|
101
|
+ } else {
|
|
102
|
+ speaker, found := c.world.Fetch(m.src)
|
|
103
|
+ if !found {
|
|
104
|
+ break
|
|
105
|
+ }
|
|
106
|
+ c.write("%s %s\n", speaker.Name, m.message)
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ case cmd := <-c.commandChan:
|
|
111
|
+ if c.authenticated {
|
|
112
|
+ c.handlePlayPhase(cmd)
|
|
113
|
+ } else {
|
|
114
|
+ c.handleLoginPhase(cmd)
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+ if c.authenticated {
|
|
119
|
+ c.outbound.shutdownChan <- c.connectionID
|
|
120
|
+ }
|
|
121
|
+ c.conn.Close()
|
|
122
|
+
|
|
123
|
+ log.Print("Lost connection from ", c.conn.RemoteAddr())
|
|
124
|
+
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+func (c *Client) splash() {
|
|
128
|
+
|
|
129
|
+ c.write("Welcome to...\n")
|
|
130
|
+ c.write(" ___ __ __ ___ __ __\n")
|
|
131
|
+ c.write(" | __| _ _ _ _ | \\/ | / _ \\ \\ \\ / /\n")
|
|
132
|
+ c.write(" | _| | || | | ' \\ | |\\/| | | (_) | \\ \\/\\/ /\n")
|
|
133
|
+ c.write(" |_| \\_,_| |_||_| |_| |_| \\___/ \\_/\\_/\n\n")
|
|
134
|
+ c.write("use connect <username> <password> to login.\n")
|
|
135
|
+
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+func (c *Client) handleLoginPhase(message string) {
|
|
139
|
+
|
|
140
|
+ fields := strings.FieldsFunc(message, func(c rune) bool {
|
|
141
|
+ return unicode.IsSpace(c)
|
|
142
|
+ })
|
|
143
|
+ switch fields[0] {
|
|
144
|
+ case "help":
|
|
145
|
+ c.write("Commands:\n\tcon[nect] <username>\n\tquit\n")
|
|
146
|
+ case "con":
|
|
147
|
+ fallthrough
|
|
148
|
+ case "connect":
|
|
149
|
+ if len(fields) == 3 {
|
|
150
|
+ pID, err := c.world.GetPlayerID(fields[1])
|
|
151
|
+ if err != nil {
|
|
152
|
+ c.write("Bad username or password.\n")
|
|
153
|
+ break
|
|
154
|
+ }
|
|
155
|
+ player, found := c.world.Fetch(pID)
|
|
156
|
+ if !found {
|
|
157
|
+ c.write("You appear to be having an existential crisis.\n")
|
|
158
|
+ c.connected = false
|
|
159
|
+ break
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ c.authenticated = true
|
|
163
|
+ c.player = player
|
|
164
|
+
|
|
165
|
+ // when a player authenticates, the ipc channels get turned on
|
|
166
|
+ subReq := EventSubscribeRequest{
|
|
167
|
+ connectionID: c.connectionID,
|
|
168
|
+ playerID: c.player.ID,
|
|
169
|
+ inbound: c.inbound,
|
|
170
|
+ chanSet: make(chan EventChanSet),
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ c.outbound = c.eventDistributor.Subscribe(subReq)
|
|
174
|
+
|
|
175
|
+ c.write("Welcome back, %s!\n", fields[1])
|
|
176
|
+
|
|
177
|
+ c.lookCmd("")
|
|
178
|
+
|
|
179
|
+ } else {
|
|
180
|
+ c.write("What?\n")
|
|
181
|
+ }
|
|
182
|
+ case "quit":
|
|
183
|
+ c.quitCmd()
|
|
184
|
+ default:
|
|
185
|
+ c.write("What?\n")
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+}
|
|
189
|
+
|
|
190
|
+func (c *Client) handlePlayPhase(message string) {
|
|
191
|
+
|
|
192
|
+ switch {
|
|
193
|
+ case message == "l":
|
|
194
|
+ c.lookCmd("")
|
|
195
|
+ case message == "look":
|
|
196
|
+ c.lookCmd("")
|
|
197
|
+ case strings.HasPrefix(message, "l "): // look at
|
|
198
|
+ c.lookCmd(strings.TrimPrefix(message, "l "))
|
|
199
|
+ case strings.HasPrefix(message, "look "): // look at
|
|
200
|
+ c.lookCmd(strings.TrimPrefix(message, "look "))
|
|
201
|
+ case strings.HasPrefix(message, "ex "):
|
|
202
|
+ c.examineCmd(strings.TrimPrefix(message, "ex "))
|
|
203
|
+ case strings.HasPrefix(message, "examine "):
|
|
204
|
+ c.examineCmd(strings.TrimPrefix(message, "examine "))
|
|
205
|
+ case strings.HasPrefix(message, "\""):
|
|
206
|
+ c.sayCmd(strings.TrimPrefix(message, "\""))
|
|
207
|
+ case strings.HasPrefix(message, "say "):
|
|
208
|
+ c.sayCmd(strings.TrimPrefix(message, "say "))
|
|
209
|
+ case strings.HasPrefix(message, ":"):
|
|
210
|
+ c.poseCmd(strings.TrimPrefix(message, ":"))
|
|
211
|
+ case strings.HasPrefix(message, "pose "):
|
|
212
|
+ c.poseCmd(strings.TrimPrefix(message, "pose "))
|
|
213
|
+ case message == "i":
|
|
214
|
+ c.inventoryCmd()
|
|
215
|
+ case message == "inventory":
|
|
216
|
+ c.inventoryCmd()
|
|
217
|
+ case strings.HasPrefix(message, "get "):
|
|
218
|
+ c.getCmd(strings.TrimPrefix(message, "get "))
|
|
219
|
+ case strings.HasPrefix(message, "drop "):
|
|
220
|
+ c.dropCmd(strings.TrimPrefix(message, "drop "))
|
|
221
|
+ case strings.HasPrefix(message, "enter "):
|
|
222
|
+ c.enterCmd(strings.TrimPrefix(message, "enter "))
|
|
223
|
+ case message == "leave":
|
|
224
|
+ c.leaveCmd()
|
|
225
|
+ case message == "quit":
|
|
226
|
+ c.quitCmd()
|
|
227
|
+ case message == "WHO":
|
|
228
|
+ c.whoCmd()
|
|
229
|
+ case strings.HasPrefix(message, "@create "):
|
|
230
|
+ c.createCmd(strings.TrimPrefix(message, "@create "))
|
|
231
|
+ case strings.HasPrefix(message, "@dig "):
|
|
232
|
+ c.digCmd(strings.TrimPrefix(message, "@dig "))
|
|
233
|
+ case strings.HasPrefix(message, "@open "):
|
|
234
|
+ c.openCmd(strings.TrimPrefix(message, "@open "))
|
|
235
|
+ case strings.HasPrefix(message, "@name "):
|
|
236
|
+ c.nameCmd(strings.TrimPrefix(message, "@name "))
|
|
237
|
+ case strings.HasPrefix(message, "@desc "):
|
|
238
|
+ c.descCmd(strings.TrimPrefix(message, "@desc "))
|
|
239
|
+ case strings.HasPrefix(message, "@tel "):
|
|
240
|
+ c.telCmd(strings.TrimPrefix(message, "@tel "))
|
|
241
|
+ case strings.HasPrefix(message, "@dump "):
|
|
242
|
+ c.dumpCmd(strings.TrimPrefix(message, "@dump "))
|
|
243
|
+ default:
|
|
244
|
+ if !c.goCmd(message) {
|
|
245
|
+ c.write("What?\n")
|
|
246
|
+ }
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+}
|
|
250
|
+
|
|
251
|
+func (c *Client) lookCmd(at string) {
|
|
252
|
+
|
|
253
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
254
|
+ if !found {
|
|
255
|
+ c.write("Limbo\nThere's nothing to see here.\n")
|
|
256
|
+ return
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ if len(at) > 0 {
|
|
260
|
+ object, matchType := room.MatchLinkNames(at, c.player.ID, false).ExactlyOne()
|
|
261
|
+ switch matchType {
|
|
262
|
+ case MatchOne:
|
|
263
|
+ c.write("%s\n%s\n", object.DetailedName(), object.Description)
|
|
264
|
+ case MatchNone:
|
|
265
|
+ c.write("I don't see that here.\n")
|
|
266
|
+ case MatchMany:
|
|
267
|
+ c.write("I don't now which one you're trying to look at.\n")
|
|
268
|
+ }
|
|
269
|
+ } else {
|
|
270
|
+ c.write("%s\n%s\n", room.DetailedName(), room.Description)
|
|
271
|
+ c.lookLinks("thing", "Things:")
|
|
272
|
+ c.lookLinks("player", "Players:")
|
|
273
|
+ exits := room.GetLinkNames("exit", nil)
|
|
274
|
+ if len(exits) > 0 {
|
|
275
|
+ c.write("Exits:\n")
|
|
276
|
+ for _, e := range exits {
|
|
277
|
+ aliases := strings.Split(e, ";")
|
|
278
|
+ c.write("%s ", aliases[0])
|
|
279
|
+ }
|
|
280
|
+ c.write("\n")
|
|
281
|
+ }
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+}
|
|
285
|
+
|
|
286
|
+func (c *Client) examineCmd(at string) {
|
|
287
|
+
|
|
288
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
289
|
+ if !found {
|
|
290
|
+ return
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ object, matchType := room.MatchLinkNames(at, c.player.ID, false).ExactlyOne()
|
|
294
|
+
|
|
295
|
+ switch matchType {
|
|
296
|
+ case MatchOne:
|
|
297
|
+ c.write("%s\n", object.DetailedName())
|
|
298
|
+ c.write("ID: %d\n", object.ID)
|
|
299
|
+ c.write("Type: %s\n", object.Type)
|
|
300
|
+ c.write("@name: %s\n", object.Name)
|
|
301
|
+ c.write("@desc: %s\n", object.Description)
|
|
302
|
+ c.write("Inside: %s\n", c.world.GetName(object.Inside))
|
|
303
|
+ c.write("Next: %s\n", c.world.GetName(object.Next))
|
|
304
|
+ c.write("Owner: %s\n", c.world.GetName(object.Owner))
|
|
305
|
+
|
|
306
|
+ inventory := object.GetLinkNames("*", nil)
|
|
307
|
+ if len(inventory) > 0 {
|
|
308
|
+ c.write("Contents:\n %s\n", strings.Join(inventory, "\n "))
|
|
309
|
+ }
|
|
310
|
+ case MatchNone:
|
|
311
|
+ c.write("I don't see that here.\n")
|
|
312
|
+ case MatchMany:
|
|
313
|
+ c.write("I don't know which one you're trying to examine.\n")
|
|
314
|
+ }
|
|
315
|
+
|
|
316
|
+}
|
|
317
|
+
|
|
318
|
+func (c *Client) lookLinks(linkType string, pretty string) {
|
|
319
|
+
|
|
320
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
321
|
+ if !found {
|
|
322
|
+ return
|
|
323
|
+ }
|
|
324
|
+
|
|
325
|
+ linknames := room.GetLinkNames(linkType, DBRefList{c.player.ID})
|
|
326
|
+
|
|
327
|
+ if len(linknames) > 0 {
|
|
328
|
+ c.write("%s\n %s\n", pretty, strings.Join(linknames, "\n "))
|
|
329
|
+ }
|
|
330
|
+
|
|
331
|
+}
|
|
332
|
+
|
|
333
|
+func (c *Client) sayCmd(message string) {
|
|
334
|
+
|
|
335
|
+ c.outbound.messageChan <- PlayerEvent{audience: c.player.Inside, src: c.player.ID, message: message, messageType: EventTypeSay}
|
|
336
|
+
|
|
337
|
+}
|
|
338
|
+
|
|
339
|
+func (c *Client) poseCmd(message string) {
|
|
340
|
+
|
|
341
|
+ c.outbound.messageChan <- PlayerEvent{audience: c.player.Inside, src: c.player.ID, dst: c.player.ID, message: message, messageType: EventTypePose}
|
|
342
|
+
|
|
343
|
+}
|
|
344
|
+
|
|
345
|
+func (c *Client) inventoryCmd() {
|
|
346
|
+
|
|
347
|
+ inventory := c.player.GetLinkNames("*", nil)
|
|
348
|
+
|
|
349
|
+ if len(inventory) > 0 {
|
|
350
|
+ c.write("Inventory:\n %s\n", strings.Join(inventory, "\n "))
|
|
351
|
+ } else {
|
|
352
|
+ c.write("You're not carrying anything.\n")
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+}
|
|
356
|
+
|
|
357
|
+func (c *Client) getCmd(message string) {
|
|
358
|
+
|
|
359
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
360
|
+ if !found {
|
|
361
|
+ return
|
|
362
|
+ }
|
|
363
|
+
|
|
364
|
+ object, matchType := room.MatchLinkNames(message, c.player.ID, true).ExactlyOne()
|
|
365
|
+
|
|
366
|
+ switch matchType {
|
|
367
|
+ case MatchOne:
|
|
368
|
+ err := c.player.Contains(&object)
|
|
369
|
+ if err != nil {
|
|
370
|
+ return
|
|
371
|
+ }
|
|
372
|
+ c.player.Refresh()
|
|
373
|
+ c.write("You pick up %s.\n", object.Name)
|
|
374
|
+ c.oemit(room.ID, "%s picks up %s.", c.player.Name, object.Name)
|
|
375
|
+ case MatchNone:
|
|
376
|
+ c.write("I don't see that here.\n")
|
|
377
|
+ case MatchMany:
|
|
378
|
+ c.write("I don't know which one.\n")
|
|
379
|
+ }
|
|
380
|
+
|
|
381
|
+}
|
|
382
|
+
|
|
383
|
+func (c *Client) dropCmd(message string) {
|
|
384
|
+
|
|
385
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
386
|
+ if !found {
|
|
387
|
+ return
|
|
388
|
+ }
|
|
389
|
+
|
|
390
|
+ object, matchType := c.player.MatchLinkNames(message, c.player.ID, false).ExactlyOne()
|
|
391
|
+
|
|
392
|
+ switch matchType {
|
|
393
|
+ case MatchOne:
|
|
394
|
+ err := room.Contains(&object)
|
|
395
|
+ if err != nil {
|
|
396
|
+ return
|
|
397
|
+ }
|
|
398
|
+ c.player.Refresh()
|
|
399
|
+ c.write("You drop %s.\n", object.Name)
|
|
400
|
+ c.oemit(c.player.Inside, "%s drops %s.", c.player.Name, object.Name)
|
|
401
|
+ case MatchNone:
|
|
402
|
+ c.write("You're not carrying that.\n")
|
|
403
|
+ case MatchMany:
|
|
404
|
+ c.write("I don't now which one.\n")
|
|
405
|
+ }
|
|
406
|
+
|
|
407
|
+}
|
|
408
|
+
|
|
409
|
+func (c *Client) enterCmd(message string) {
|
|
410
|
+
|
|
411
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
412
|
+ if !found {
|
|
413
|
+ return
|
|
414
|
+ }
|
|
415
|
+
|
|
416
|
+ object, matchType := room.MatchLinkNames(message, c.player.ID, true).ExactlyOne()
|
|
417
|
+
|
|
418
|
+ switch matchType {
|
|
419
|
+ case MatchOne:
|
|
420
|
+ err := object.Contains(&c.player)
|
|
421
|
+ if err != nil {
|
|
422
|
+ return
|
|
423
|
+ }
|
|
424
|
+ c.player.Refresh()
|
|
425
|
+ c.write("You climb into %s.\n", object.Name)
|
|
426
|
+ c.oemit(room.ID, "%s climbs into %s.", c.player.Name, object.Name)
|
|
427
|
+ c.oemit(object.ID, "%s squeezes into %s with you.", c.player.Name, object.Name)
|
|
428
|
+ case MatchNone:
|
|
429
|
+ c.write("I don't see that here.\n")
|
|
430
|
+ case MatchMany:
|
|
431
|
+ c.write("I don't now which one.\n")
|
|
432
|
+ }
|
|
433
|
+}
|
|
434
|
+
|
|
435
|
+func (c *Client) leaveCmd() {
|
|
436
|
+
|
|
437
|
+ object, found := c.world.Fetch(c.player.Inside)
|
|
438
|
+ if !found {
|
|
439
|
+ return
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ if object.Inside == 0 { // probably trying to 'leave' a room
|
|
443
|
+ c.write("You can't leave here.\n")
|
|
444
|
+ return
|
|
445
|
+ }
|
|
446
|
+
|
|
447
|
+ room, found := c.world.Fetch(object.Inside)
|
|
448
|
+ if !found {
|
|
449
|
+ return
|
|
450
|
+ }
|
|
451
|
+
|
|
452
|
+ err := room.Contains(&c.player)
|
|
453
|
+ if err != nil {
|
|
454
|
+ return
|
|
455
|
+ }
|
|
456
|
+
|
|
457
|
+ c.player.Refresh()
|
|
458
|
+
|
|
459
|
+ c.write("You climb out of %s.\n", object.Name)
|
|
460
|
+ c.oemit(object.ID, "%s climbs out of %s.", c.player.Name, object.Name)
|
|
461
|
+ c.oemit(room.ID, "%s climbs out of %s.", c.player.Name, object.Name)
|
|
462
|
+
|
|
463
|
+}
|
|
464
|
+
|
|
465
|
+func (c *Client) quitCmd() {
|
|
466
|
+
|
|
467
|
+ c.write("So long, it's been good to know yah.\n")
|
|
468
|
+ c.connected = false
|
|
469
|
+
|
|
470
|
+}
|
|
471
|
+
|
|
472
|
+func (c *Client) whoCmd() {
|
|
473
|
+ onlinePlayers := c.eventDistributor.OnlinePlayers()
|
|
474
|
+
|
|
475
|
+ c.write("Currently Online:\n")
|
|
476
|
+
|
|
477
|
+ for _, ref := range onlinePlayers {
|
|
478
|
+ c.write("%s\n", c.world.GetName(ref))
|
|
479
|
+ }
|
|
480
|
+}
|
|
481
|
+
|
|
482
|
+func (c *Client) createCmd(message string) {
|
|
483
|
+
|
|
484
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
485
|
+ if !found {
|
|
486
|
+ return
|
|
487
|
+ }
|
|
488
|
+
|
|
489
|
+ o, _ := c.world.Allocate("thing")
|
|
490
|
+ o.Name = strings.TrimSpace(message)
|
|
491
|
+ o.Owner = c.player.ID
|
|
492
|
+ o.Commit()
|
|
493
|
+
|
|
494
|
+ err := room.Contains(&o)
|
|
495
|
+ if err != nil {
|
|
496
|
+ return
|
|
497
|
+ }
|
|
498
|
+
|
|
499
|
+ c.emit(room.ID, "A %s appears out of the ether.", o.Name)
|
|
500
|
+ c.write("%s Created.\n", o.DetailedName())
|
|
501
|
+
|
|
502
|
+}
|
|
503
|
+
|
|
504
|
+func (c *Client) openCmd(message string) {
|
|
505
|
+ // @open <in1;in2;in3;etc>=#<room>,<out1;out2;out3;etc>
|
|
506
|
+
|
|
507
|
+ slicey := strings.SplitN(message, "=", 2)
|
|
508
|
+
|
|
509
|
+ if len(slicey) < 2 {
|
|
510
|
+ c.write("Bad command or file name.\n")
|
|
511
|
+ return
|
|
512
|
+ }
|
|
513
|
+
|
|
514
|
+ srcExitSpec := strings.TrimSpace(slicey[0])
|
|
515
|
+
|
|
516
|
+ stuff := strings.Split(slicey[1], ",")
|
|
517
|
+ if len(stuff) > 2 {
|
|
518
|
+ c.write("Bad command or file name.\n")
|
|
519
|
+ return
|
|
520
|
+ }
|
|
521
|
+
|
|
522
|
+ makeReturnExit := false
|
|
523
|
+ var returnExitSpec string
|
|
524
|
+ if len(stuff) == 2 {
|
|
525
|
+ makeReturnExit = true
|
|
526
|
+ returnExitSpec = stuff[1]
|
|
527
|
+ }
|
|
528
|
+
|
|
529
|
+ target, err := NewDBRefFromHashRef(stuff[0])
|
|
530
|
+ if err != nil {
|
|
531
|
+ c.write("Bad target DBRef.\n")
|
|
532
|
+ return
|
|
533
|
+ }
|
|
534
|
+
|
|
535
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
536
|
+ if !found {
|
|
537
|
+ return
|
|
538
|
+ }
|
|
539
|
+
|
|
540
|
+ targetRoom, found := c.world.Fetch(target)
|
|
541
|
+ if !found {
|
|
542
|
+ c.write("Target not found.\n")
|
|
543
|
+ return
|
|
544
|
+ }
|
|
545
|
+
|
|
546
|
+ toExit, _ := c.world.Allocate("exit")
|
|
547
|
+ toExit.Name = srcExitSpec
|
|
548
|
+ toExit.Next = targetRoom.ID
|
|
549
|
+ toExit.Owner = c.player.ID
|
|
550
|
+ toExit.Commit()
|
|
551
|
+
|
|
552
|
+ err = room.Contains(&toExit)
|
|
553
|
+ if err != nil {
|
|
554
|
+ return
|
|
555
|
+ }
|
|
556
|
+
|
|
557
|
+ c.write("%s Created.\n", toExit.DetailedName())
|
|
558
|
+
|
|
559
|
+ if makeReturnExit {
|
|
560
|
+ fromExit, _ := c.world.Allocate("exit")
|
|
561
|
+ fromExit.Name = returnExitSpec
|
|
562
|
+ fromExit.Next = room.ID
|
|
563
|
+ fromExit.Owner = c.player.ID
|
|
564
|
+ fromExit.Commit()
|
|
565
|
+
|
|
566
|
+ err = targetRoom.Contains(&fromExit)
|
|
567
|
+ if err != nil {
|
|
568
|
+ return
|
|
569
|
+ }
|
|
570
|
+
|
|
571
|
+ c.write("%s Created.\n", fromExit.DetailedName())
|
|
572
|
+ }
|
|
573
|
+
|
|
574
|
+}
|
|
575
|
+
|
|
576
|
+func (c *Client) digCmd(message string) {
|
|
577
|
+ // @dig <Room name>=<in1;in2;in3;etc>,<out1;out2;out3;etc>
|
|
578
|
+ //@dig foo=<F>oo;foo;f,<B>ack;back;b
|
|
579
|
+ slicey := strings.SplitN(message, "=", 2)
|
|
580
|
+
|
|
581
|
+ if len(slicey) < 1 {
|
|
582
|
+ c.write("Rooms can't not have names.\n")
|
|
583
|
+ return
|
|
584
|
+ }
|
|
585
|
+
|
|
586
|
+ roomName := strings.TrimSpace(slicey[0])
|
|
587
|
+
|
|
588
|
+ exits := make([]string, 0)
|
|
589
|
+
|
|
590
|
+ if len(slicey) == 2 {
|
|
591
|
+ exitSpec := strings.TrimSpace(slicey[1])
|
|
592
|
+ exits = strings.Split(exitSpec, ",")
|
|
593
|
+ if len(exits) > 2 {
|
|
594
|
+ c.write("You've listed more than two exits. That doesn't even make sense.\n")
|
|
595
|
+ return
|
|
596
|
+ }
|
|
597
|
+ }
|
|
598
|
+
|
|
599
|
+ newRoom, _ := c.world.Allocate("room")
|
|
600
|
+ newRoom.Name = roomName
|
|
601
|
+ newRoom.Owner = c.player.ID
|
|
602
|
+ newRoom.Commit()
|
|
603
|
+
|
|
604
|
+ c.write("%s Created.\n", newRoom.DetailedName())
|
|
605
|
+
|
|
606
|
+ if len(exits) > 0 {
|
|
607
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
608
|
+ if !found {
|
|
609
|
+ return
|
|
610
|
+ }
|
|
611
|
+ toExit, _ := c.world.Allocate("exit")
|
|
612
|
+ toExit.Name = exits[0]
|
|
613
|
+ toExit.Next = newRoom.ID
|
|
614
|
+ toExit.Owner = c.player.ID
|
|
615
|
+ toExit.Commit()
|
|
616
|
+
|
|
617
|
+ err := room.Contains(&toExit)
|
|
618
|
+ if err != nil {
|
|
619
|
+ return
|
|
620
|
+ }
|
|
621
|
+
|
|
622
|
+ c.write("%s Created.\n", toExit.DetailedName())
|
|
623
|
+
|
|
624
|
+ if len(exits) == 2 {
|
|
625
|
+ fromExit, _ := c.world.Allocate("exit")
|
|
626
|
+ fromExit.Name = exits[1]
|
|
627
|
+ fromExit.Next = room.ID
|
|
628
|
+ fromExit.Owner = c.player.ID
|
|
629
|
+ fromExit.Commit()
|
|
630
|
+
|
|
631
|
+ err = newRoom.Contains(&fromExit)
|
|
632
|
+ if err != nil {
|
|
633
|
+ return
|
|
634
|
+ }
|
|
635
|
+
|
|
636
|
+ c.write("%s Created.\n", fromExit.DetailedName())
|
|
637
|
+
|
|
638
|
+ }
|
|
639
|
+ }
|
|
640
|
+
|
|
641
|
+}
|
|
642
|
+
|
|
643
|
+func (c *Client) nameCmd(message string) {
|
|
644
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
645
|
+ if !found {
|
|
646
|
+ return
|
|
647
|
+ }
|
|
648
|
+
|
|
649
|
+ slicey := strings.SplitN(message, "=", 2)
|
|
650
|
+
|
|
651
|
+ if len(slicey) == 1 {
|
|
652
|
+ c.write("Things can't not have names.\n")
|
|
653
|
+ return
|
|
654
|
+ }
|
|
655
|
+
|
|
656
|
+ objectName := strings.TrimSpace(slicey[0])
|
|
657
|
+ name := strings.TrimSpace(slicey[1])
|
|
658
|
+
|
|
659
|
+ candidate, matchType := room.MatchLinkNames(objectName, c.player.ID, false).ExactlyOne()
|
|
660
|
+ switch matchType {
|
|
661
|
+ case MatchOne:
|
|
662
|
+ candidate.Name = name
|
|
663
|
+ candidate.Commit()
|
|
664
|
+ c.write("Name set.\n")
|
|
665
|
+ c.player.Refresh()
|
|
666
|
+ case MatchNone:
|
|
667
|
+ c.write("I don't see that here.\n")
|
|
668
|
+ case MatchMany:
|
|
669
|
+ c.write("I don't now which one.\n")
|
|
670
|
+ }
|
|
671
|
+
|
|
672
|
+}
|
|
673
|
+func (c *Client) descCmd(message string) {
|
|
674
|
+
|
|
675
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
676
|
+ if !found {
|
|
677
|
+ return
|
|
678
|
+ }
|
|
679
|
+
|
|
680
|
+ slicey := strings.SplitN(message, "=", 2)
|
|
681
|
+
|
|
682
|
+ objectName := strings.TrimSpace(slicey[0])
|
|
683
|
+ description := ""
|
|
684
|
+ if len(slicey) > 1 {
|
|
685
|
+ description = strings.TrimSpace(slicey[1])
|
|
686
|
+ }
|
|
687
|
+
|
|
688
|
+ var editObject *Object
|
|
689
|
+ switch objectName {
|
|
690
|
+ case "here":
|
|
691
|
+ editObject = &room
|
|
692
|
+ case "me":
|
|
693
|
+ editObject = &c.player
|
|
694
|
+ default:
|
|
695
|
+ candidate, matchType := room.MatchLinkNames(objectName, c.player.ID, false).ExactlyOne()
|
|
696
|
+ switch matchType {
|
|
697
|
+ case MatchOne:
|
|
698
|
+ editObject = &candidate
|
|
699
|
+ case MatchNone:
|
|
700
|
+ c.write("I don't see that here.\n")
|
|
701
|
+ return
|
|
702
|
+ case MatchMany:
|
|
703
|
+ c.write("I don't now which one.\n")
|
|
704
|
+ return
|
|
705
|
+ }
|
|
706
|
+ }
|
|
707
|
+
|
|
708
|
+ editObject.Description = description
|
|
709
|
+ editObject.Commit()
|
|
710
|
+ c.player.Refresh()
|
|
711
|
+ c.write("Description set.\n")
|
|
712
|
+
|
|
713
|
+}
|
|
714
|
+
|
|
715
|
+func (c *Client) telCmd(destStr string) {
|
|
716
|
+
|
|
717
|
+ dest, err := NewDBRefFromHashRef(destStr)
|
|
718
|
+
|
|
719
|
+ if err != nil {
|
|
720
|
+ c.write("That doesn't look like a DBRef.\n")
|
|
721
|
+ return
|
|
722
|
+ }
|
|
723
|
+
|
|
724
|
+ newRoom, found := c.world.Fetch(dest)
|
|
725
|
+ if !found {
|
|
726
|
+ c.write("That doesn't exist.\n")
|
|
727
|
+ return
|
|
728
|
+ }
|
|
729
|
+
|
|
730
|
+ c.write("You feel an intense wooshing sensation.\n")
|
|
731
|
+ err = newRoom.Contains(&c.player)
|
|
732
|
+ if err != nil {
|
|
733
|
+ return
|
|
734
|
+ }
|
|
735
|
+
|
|
736
|
+ c.player.Refresh()
|
|
737
|
+ c.lookCmd("")
|
|
738
|
+
|
|
739
|
+}
|
|
740
|
+
|
|
741
|
+func (c *Client) dumpCmd(refStr string) {
|
|
742
|
+
|
|
743
|
+ ref, err := NewDBRefFromHashRef(refStr)
|
|
744
|
+
|
|
745
|
+ if err != nil {
|
|
746
|
+ c.write("That doesn't look like a DBRef.\n")
|
|
747
|
+ return
|
|
748
|
+ }
|
|
749
|
+
|
|
750
|
+ obj, found := c.world.Fetch(ref)
|
|
751
|
+ if !found {
|
|
752
|
+ c.write("That doesn't exist.\n")
|
|
753
|
+ return
|
|
754
|
+ }
|
|
755
|
+
|
|
756
|
+ c.write("%s\n", c.world.DumpObject(obj.ID))
|
|
757
|
+
|
|
758
|
+}
|
|
759
|
+
|
|
760
|
+func (c *Client) goCmd(dir string) bool {
|
|
761
|
+
|
|
762
|
+ room, found := c.world.Fetch(c.player.Inside)
|
|
763
|
+ if !found {
|
|
764
|
+ return false
|
|
765
|
+ }
|
|
766
|
+
|
|
767
|
+ exit, matchType := room.MatchExitNames(dir).ExactlyOne()
|
|
768
|
+ switch matchType {
|
|
769
|
+ case MatchOne:
|
|
770
|
+ if exit.Next.Valid() {
|
|
771
|
+ newRoom, found := c.world.Fetch(exit.Next)
|
|
772
|
+ if !found {
|
|
773
|
+ return false
|
|
774
|
+ }
|
|
775
|
+ err := newRoom.Contains(&c.player)
|
|
776
|
+ if err != nil {
|
|
777
|
+ return false
|
|
778
|
+ }
|
|
779
|
+ c.player.Refresh()
|
|
780
|
+ c.write("You head towards %s.\n", newRoom.Name)
|
|
781
|
+ c.oemit(room.ID, "%s leaves the room.", c.player.Name)
|
|
782
|
+ c.oemit(newRoom.ID, "%s enters the room.", c.player.Name)
|
|
783
|
+ return true
|
|
784
|
+ }
|
|
785
|
+ case MatchNone:
|
|
786
|
+ return false
|
|
787
|
+ case MatchMany:
|
|
788
|
+ c.write("Ambiguous exit names are ambiguous.\n")
|
|
789
|
+ return true
|
|
790
|
+ }
|
|
791
|
+
|
|
792
|
+ return false
|
|
793
|
+
|
|
794
|
+}
|
|
795
|
+
|
|
796
|
+func (c *Client) oemit(audience DBRef, format string, a ...interface{}) {
|
|
797
|
+
|
|
798
|
+ message := fmt.Sprintf(format, a...)
|
|
799
|
+ c.outbound.messageChan <- PlayerEvent{audience: audience, src: c.player.ID, dst: c.player.ID, message: message, messageType: EventTypeOEmit}
|
|
800
|
+
|
|
801
|
+}
|
|
802
|
+
|
|
803
|
+func (c *Client) write(format string, a ...interface{}) {
|
|
804
|
+
|
|
805
|
+ fmt.Fprintf(c.conn, format, a...)
|
|
806
|
+
|
|
807
|
+}
|
|
808
|
+
|
|
809
|
+func (c *Client) pemit(audience DBRef, format string, a ...interface{}) {
|
|
810
|
+
|
|
811
|
+ message := fmt.Sprintf(format, a...)
|
|
812
|
+ c.outbound.messageChan <- PlayerEvent{audience: audience, src: c.player.ID, dst: c.player.ID, message: message, messageType: EventTypePEmit}
|
|
813
|
+
|
|
814
|
+}
|
|
815
|
+
|
|
816
|
+func (c *Client) emit(audience DBRef, format string, a ...interface{}) {
|
|
817
|
+
|
|
818
|
+ message := fmt.Sprintf(format, a...)
|
|
819
|
+ c.outbound.messageChan <- PlayerEvent{audience: audience, src: c.player.ID, dst: c.player.ID, message: message, messageType: EventTypeEmit}
|
|
820
|
+
|
|
821
|
+}
|