|
@@ -0,0 +1,881 @@
|
|
1
|
+package funmow
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "fmt"
|
|
5
|
+ "regexp"
|
|
6
|
+ "strings"
|
|
7
|
+ "time"
|
|
8
|
+)
|
|
9
|
+
|
|
10
|
+type ExecutionContext struct {
|
|
11
|
+ actor Object
|
|
12
|
+ inbound chan PlayerEvent
|
|
13
|
+ outbound chan PlayerEvent
|
|
14
|
+ eventDistributor *EventDistributor
|
|
15
|
+ db *DB
|
|
16
|
+ factory *ObjectFactory
|
|
17
|
+ forceContext bool
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+func NewForceContext(e *EventDistributor, w *DB, outbound chan PlayerEvent) *ExecutionContext {
|
|
21
|
+
|
|
22
|
+ c := new(ExecutionContext)
|
|
23
|
+
|
|
24
|
+ c.outbound = outbound
|
|
25
|
+ c.eventDistributor = e
|
|
26
|
+ c.db = w
|
|
27
|
+ c.factory = NewObjectFactory(w)
|
|
28
|
+ c.forceContext = true
|
|
29
|
+ return c
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+func NewExecutionContext(actorID DBRef, e *EventDistributor, w *DB, outbound chan PlayerEvent) *ExecutionContext {
|
|
33
|
+
|
|
34
|
+ c := new(ExecutionContext)
|
|
35
|
+
|
|
36
|
+ actor, found := w.Fetch(actorID)
|
|
37
|
+ if !found {
|
|
38
|
+ return nil
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ c.actor = actor
|
|
42
|
+ c.outbound = outbound
|
|
43
|
+ c.eventDistributor = e
|
|
44
|
+ c.db = w
|
|
45
|
+ c.factory = NewObjectFactory(w)
|
|
46
|
+
|
|
47
|
+ return c
|
|
48
|
+}
|
|
49
|
+
|
|
50
|
+func (c *ExecutionContext) StartInboundChannel() chan PlayerEvent {
|
|
51
|
+ c.inbound = make(chan PlayerEvent)
|
|
52
|
+ inboundBuffer := make(chan PlayerEvent)
|
|
53
|
+ c.outbound = c.outbound
|
|
54
|
+
|
|
55
|
+ go func() {
|
|
56
|
+ //inbound event buffer to protect the event distributor from long running tasks
|
|
57
|
+ // the alternative is to run each inbound event in a separate goroutine
|
|
58
|
+ // but that has potential problems (ie, two emits arrive in order. the first one
|
|
59
|
+ // requires an extra DB lookup to sort out context. now the second one ends up
|
|
60
|
+ // sending its output event before the first, and the client sees things backwards
|
|
61
|
+ // this isn't just an edge case, it happens quite frequently.
|
|
62
|
+ // The alternative is to buffer inbound events, which is the safest, as it doesn't
|
|
63
|
+ // impact the eventdistributor.
|
|
64
|
+
|
|
65
|
+ queue := make([]*PlayerEvent, 0)
|
|
66
|
+ running := true
|
|
67
|
+ for running {
|
|
68
|
+ if len(queue) == 0 {
|
|
69
|
+ select {
|
|
70
|
+ case newEvent, ok := <-c.inbound:
|
|
71
|
+ if !ok {
|
|
72
|
+ running = false
|
|
73
|
+ break
|
|
74
|
+ }
|
|
75
|
+ queue = append(queue, &newEvent)
|
|
76
|
+ }
|
|
77
|
+ } else {
|
|
78
|
+ event := queue[0]
|
|
79
|
+ select {
|
|
80
|
+ case newEvent, ok := <-c.inbound:
|
|
81
|
+ if !ok {
|
|
82
|
+ running = false
|
|
83
|
+ break
|
|
84
|
+ }
|
|
85
|
+ queue = append(queue, &newEvent)
|
|
86
|
+ case inboundBuffer <- *event:
|
|
87
|
+ queue = queue[1:]
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ // closure of c.inbound is the signal from the event distributor that we need to die.
|
|
92
|
+ // we then close inboundBuffer to tell the event goroutine to die
|
|
93
|
+ fmt.Println("close(inboundBuffer)")
|
|
94
|
+ close(inboundBuffer)
|
|
95
|
+ }()
|
|
96
|
+
|
|
97
|
+ go func() {
|
|
98
|
+ for {
|
|
99
|
+ event, ok := <-inboundBuffer
|
|
100
|
+ if !ok {
|
|
101
|
+ break
|
|
102
|
+ }
|
|
103
|
+ c.HandleEvent(event)
|
|
104
|
+ }
|
|
105
|
+ // and finally we tell the event distributor to delete us.
|
|
106
|
+ fmt.Println("Sending EventTypeTeardownComplete")
|
|
107
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: c.actor.ID, messageType: EventTypeTeardownComplete}
|
|
108
|
+
|
|
109
|
+ }()
|
|
110
|
+
|
|
111
|
+ return c.inbound
|
|
112
|
+}
|
|
113
|
+
|
|
114
|
+func (c *ExecutionContext) HandleEvent(m PlayerEvent) {
|
|
115
|
+ inside, _ := c.db.GetParent(c.actor.ID)
|
|
116
|
+ switch m.messageType {
|
|
117
|
+ case EventTypeEmit:
|
|
118
|
+ fallthrough
|
|
119
|
+ case EventTypeOEmit:
|
|
120
|
+ if m.dst == inside {
|
|
121
|
+ c.output("%s", m.message)
|
|
122
|
+ }
|
|
123
|
+ case EventTypePEmit:
|
|
124
|
+ c.output("%s", m.message)
|
|
125
|
+ case EventTypeWall:
|
|
126
|
+ speaker, found := c.db.Fetch(m.src)
|
|
127
|
+ if !found {
|
|
128
|
+ break
|
|
129
|
+ }
|
|
130
|
+ c.output("In the distance, you hear %s bellow out \"%s\"", speaker.Name, m.message)
|
|
131
|
+ case EventTypePage:
|
|
132
|
+ speaker, found := c.db.Fetch(m.src)
|
|
133
|
+ if !found {
|
|
134
|
+ break
|
|
135
|
+ }
|
|
136
|
+ c.output("%s pages you: \"%s\"", speaker.Name, m.message)
|
|
137
|
+ case EventTypeSay:
|
|
138
|
+ if m.dst == inside {
|
|
139
|
+ speaker, found := c.db.Fetch(m.src)
|
|
140
|
+ if !found {
|
|
141
|
+ break
|
|
142
|
+ }
|
|
143
|
+ c.output("%s says \"%s\"", speaker.Name, m.message)
|
|
144
|
+ }
|
|
145
|
+ case EventTypePose:
|
|
146
|
+ if m.dst == inside {
|
|
147
|
+ if m.src == c.actor.ID {
|
|
148
|
+ c.output("%s %s", c.actor.Name, m.message)
|
|
149
|
+ } else {
|
|
150
|
+ speaker, found := c.db.Fetch(m.src)
|
|
151
|
+ if !found {
|
|
152
|
+ break
|
|
153
|
+ }
|
|
154
|
+ c.output("%s %s", speaker.Name, m.message)
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+ case EventTypeForce:
|
|
158
|
+ actor, found := c.db.Fetch(m.dst)
|
|
159
|
+ if !found {
|
|
160
|
+ break
|
|
161
|
+ }
|
|
162
|
+ c.actor = actor
|
|
163
|
+ c.evaluateCommand(m)
|
|
164
|
+ case EventTypeCommand:
|
|
165
|
+ c.evaluateCommand(m)
|
|
166
|
+ }
|
|
167
|
+}
|
|
168
|
+
|
|
169
|
+func (c *ExecutionContext) evaluateCommand(m PlayerEvent) {
|
|
170
|
+ message := m.message
|
|
171
|
+ c.actor.Refresh()
|
|
172
|
+ switch {
|
|
173
|
+ case message == "l":
|
|
174
|
+ c.lookCmd("")
|
|
175
|
+ case message == "look":
|
|
176
|
+ c.lookCmd("")
|
|
177
|
+ case strings.HasPrefix(message, "l "): // look at
|
|
178
|
+ c.lookCmd(strings.TrimPrefix(message, "l "))
|
|
179
|
+ case strings.HasPrefix(message, "look "): // look at
|
|
180
|
+ c.lookCmd(strings.TrimPrefix(message, "look "))
|
|
181
|
+ case strings.HasPrefix(message, "ex "):
|
|
182
|
+ c.examineCmd(strings.TrimPrefix(message, "ex "))
|
|
183
|
+ case strings.HasPrefix(message, "examine "):
|
|
184
|
+ c.examineCmd(strings.TrimPrefix(message, "examine "))
|
|
185
|
+ case strings.HasPrefix(message, "\""):
|
|
186
|
+ c.sayCmd(strings.TrimPrefix(message, "\""))
|
|
187
|
+ case strings.HasPrefix(message, "say "):
|
|
188
|
+ c.sayCmd(strings.TrimPrefix(message, "say "))
|
|
189
|
+ case strings.HasPrefix(message, ":"):
|
|
190
|
+ c.poseCmd(strings.TrimPrefix(message, ":"))
|
|
191
|
+ case strings.HasPrefix(message, "pose "):
|
|
192
|
+ c.poseCmd(strings.TrimPrefix(message, "pose "))
|
|
193
|
+ case message == "i":
|
|
194
|
+ c.inventoryCmd()
|
|
195
|
+ case message == "inventory":
|
|
196
|
+ c.inventoryCmd()
|
|
197
|
+ case strings.HasPrefix(message, "get "):
|
|
198
|
+ c.getCmd(strings.TrimPrefix(message, "get "))
|
|
199
|
+ case strings.HasPrefix(message, "drop "):
|
|
200
|
+ c.dropCmd(strings.TrimPrefix(message, "drop "))
|
|
201
|
+ case strings.HasPrefix(message, "enter "):
|
|
202
|
+ c.enterCmd(strings.TrimPrefix(message, "enter "))
|
|
203
|
+ case message == "leave":
|
|
204
|
+ c.leaveCmd()
|
|
205
|
+ case message == "quit":
|
|
206
|
+ c.quitCmd(m.connectionID)
|
|
207
|
+ case message == "WHO":
|
|
208
|
+ c.whoCmd()
|
|
209
|
+ case message == "HOWLONG":
|
|
210
|
+ time.Sleep(5 * time.Second)
|
|
211
|
+ case strings.HasPrefix(message, "@create "):
|
|
212
|
+ c.createCmd(strings.TrimPrefix(message, "@create "))
|
|
213
|
+ case strings.HasPrefix(message, "@dig "):
|
|
214
|
+ c.digCmd(message)
|
|
215
|
+ case strings.HasPrefix(message, "@open "):
|
|
216
|
+ c.openCmd(message)
|
|
217
|
+ case strings.HasPrefix(message, "@name "):
|
|
218
|
+ c.nameCmd(message)
|
|
219
|
+ case strings.HasPrefix(message, "@desc "):
|
|
220
|
+ c.descCmd(message)
|
|
221
|
+ case strings.HasPrefix(message, "@tel "):
|
|
222
|
+ c.telCmd(strings.TrimPrefix(message, "@tel "))
|
|
223
|
+ case strings.HasPrefix(message, "@dump "):
|
|
224
|
+ c.dumpCmd(strings.TrimPrefix(message, "@dump "))
|
|
225
|
+ case strings.HasPrefix(message, "@destroy "):
|
|
226
|
+ c.destroyCmd(strings.TrimPrefix(message, "@destroy "))
|
|
227
|
+ case strings.HasPrefix(message, "@force "):
|
|
228
|
+ c.forceCmd(message)
|
|
229
|
+ default:
|
|
230
|
+ if !c.goCmd(message) {
|
|
231
|
+ c.output("What?\n")
|
|
232
|
+ }
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+}
|
|
236
|
+
|
|
237
|
+func (c *ExecutionContext) lookCmd(at string) {
|
|
238
|
+
|
|
239
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
240
|
+ room, found := c.db.Fetch(roomID)
|
|
241
|
+ if !found {
|
|
242
|
+ c.output("True Limbo (#NaN)\nThere's nothing to see here. You are inside an object that doesn't exist.")
|
|
243
|
+ return
|
|
244
|
+ }
|
|
245
|
+
|
|
246
|
+ if len(at) > 0 {
|
|
247
|
+ object, matchType := room.MatchLinkNames(at, c.actor.ID).ExactlyOne()
|
|
248
|
+ switch matchType {
|
|
249
|
+ case MatchOne:
|
|
250
|
+ c.output("%s\n%s", object.DetailedName(), object.Description)
|
|
251
|
+ case MatchNone:
|
|
252
|
+ c.output("I don't see that here.")
|
|
253
|
+ case MatchMany:
|
|
254
|
+ c.output("I don't now which one you're trying to look at.")
|
|
255
|
+ }
|
|
256
|
+ } else {
|
|
257
|
+ c.output("%s\n%s", room.DetailedName(), room.Description)
|
|
258
|
+ lookLinks := func(linkType string, pretty string) {
|
|
259
|
+ linknames := room.GetLinkNames(linkType, DBRefList{c.actor.ID})
|
|
260
|
+ if len(linknames) > 0 {
|
|
261
|
+ c.output("%s\n %s", pretty, strings.Join(linknames, "\n "))
|
|
262
|
+ }
|
|
263
|
+ }
|
|
264
|
+ lookLinks("thing", "Things:")
|
|
265
|
+ lookLinks("player", "Players:")
|
|
266
|
+ exits := room.GetLinkNames("exit", nil)
|
|
267
|
+ if len(exits) > 0 {
|
|
268
|
+ c.output("Exits:")
|
|
269
|
+ exitList := make([]string, 0)
|
|
270
|
+ for _, e := range exits {
|
|
271
|
+ aliases := strings.Split(e, ";")
|
|
272
|
+ exitList = append(exitList, aliases[0])
|
|
273
|
+ }
|
|
274
|
+ c.output(strings.Join(exitList, " "))
|
|
275
|
+ }
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+}
|
|
279
|
+
|
|
280
|
+func (c *ExecutionContext) objectName(id DBRef) string {
|
|
281
|
+ o, found := c.db.Fetch(id)
|
|
282
|
+ if found {
|
|
283
|
+ return o.DetailedName()
|
|
284
|
+ } else {
|
|
285
|
+ return fmt.Sprintf("MISSING OBJECT (#%d)", id)
|
|
286
|
+ }
|
|
287
|
+}
|
|
288
|
+
|
|
289
|
+func (c *ExecutionContext) examineCmd(at string) {
|
|
290
|
+
|
|
291
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
292
|
+ room, found := c.db.Fetch(roomID)
|
|
293
|
+ if !found {
|
|
294
|
+ return
|
|
295
|
+ }
|
|
296
|
+
|
|
297
|
+ object, matchType := room.MatchLinkNames(at, c.actor.ID).ExactlyOne()
|
|
298
|
+
|
|
299
|
+ switch matchType {
|
|
300
|
+ case MatchOne:
|
|
301
|
+
|
|
302
|
+ objectParentID, _ := c.db.GetParent(object.ID)
|
|
303
|
+
|
|
304
|
+ c.output("%s", object.DetailedName())
|
|
305
|
+ c.output("ID: %d", object.ID)
|
|
306
|
+ c.output("Type: %s", object.Type)
|
|
307
|
+ c.output("@name: %s", object.Name)
|
|
308
|
+ c.output("@desc: %s", object.Description)
|
|
309
|
+ c.output("Inside: %s", c.objectName(objectParentID))
|
|
310
|
+ c.output("Next: %s", c.objectName(object.Next))
|
|
311
|
+ c.output("Owner: %s", c.objectName(object.Owner))
|
|
312
|
+
|
|
313
|
+ inventory := object.GetLinkNames("*", nil)
|
|
314
|
+ if len(inventory) > 0 {
|
|
315
|
+ c.output("Contents:\n %s", strings.Join(inventory, "\n "))
|
|
316
|
+ }
|
|
317
|
+ case MatchNone:
|
|
318
|
+ c.output("I don't see that here.")
|
|
319
|
+ case MatchMany:
|
|
320
|
+ c.output("I don't know which one you're trying to examine.")
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+}
|
|
324
|
+
|
|
325
|
+func (c *ExecutionContext) forceCmd(input string) {
|
|
326
|
+
|
|
327
|
+ r, _ := regexp.Compile(`^@force\pZ+([^=]*[^=\pZ]{1})\pZ*=\pZ*(.*)\pZ*$`)
|
|
328
|
+ params := r.FindStringSubmatch(input)
|
|
329
|
+ if params == nil {
|
|
330
|
+ return
|
|
331
|
+ }
|
|
332
|
+
|
|
333
|
+ objectName, command := params[1], params[2]
|
|
334
|
+
|
|
335
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
336
|
+ room, found := c.db.Fetch(roomID)
|
|
337
|
+ if !found {
|
|
338
|
+ return
|
|
339
|
+ }
|
|
340
|
+
|
|
341
|
+ objectID, err := NewDBRefFromHashRef(objectName)
|
|
342
|
+ wizard := false
|
|
343
|
+ if err == nil {
|
|
344
|
+ object, found := c.db.Fetch(objectID)
|
|
345
|
+ if !found {
|
|
346
|
+ c.output("I can't force what doesn't exist.")
|
|
347
|
+ }
|
|
348
|
+ if object.Type == "thing" || (object.Type == "player" && wizard) {
|
|
349
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: object.ID, message: command, messageType: EventTypeForce}
|
|
350
|
+ } else {
|
|
351
|
+ c.output("Some things just can't be forced.")
|
|
352
|
+ }
|
|
353
|
+ } else {
|
|
354
|
+
|
|
355
|
+ object, matchType := room.MatchLinkNames(objectName, c.actor.ID).ExactlyOne()
|
|
356
|
+
|
|
357
|
+ switch matchType {
|
|
358
|
+ case MatchOne:
|
|
359
|
+ if object.Type == "thing" || (object.Type == "player" && wizard) {
|
|
360
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: object.ID, message: command, messageType: EventTypeForce}
|
|
361
|
+ } else {
|
|
362
|
+ c.output("Some things just can't be forced.")
|
|
363
|
+ }
|
|
364
|
+ case MatchNone:
|
|
365
|
+ c.output("I don't see that here.")
|
|
366
|
+ case MatchMany:
|
|
367
|
+ c.output("I don't know which one you're trying to examine.")
|
|
368
|
+ }
|
|
369
|
+ }
|
|
370
|
+}
|
|
371
|
+
|
|
372
|
+func (c *ExecutionContext) sayCmd(message string) {
|
|
373
|
+
|
|
374
|
+ inside, _ := c.db.GetParent(c.actor.ID)
|
|
375
|
+ c.output("You say \"%s\"", message)
|
|
376
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: inside, message: message, messageType: EventTypeSay}
|
|
377
|
+
|
|
378
|
+}
|
|
379
|
+
|
|
380
|
+func (c *ExecutionContext) poseCmd(message string) {
|
|
381
|
+
|
|
382
|
+ inside, _ := c.db.GetParent(c.actor.ID)
|
|
383
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: inside, message: message, messageType: EventTypePose}
|
|
384
|
+
|
|
385
|
+}
|
|
386
|
+
|
|
387
|
+func (c *ExecutionContext) inventoryCmd() {
|
|
388
|
+
|
|
389
|
+ inventory := c.actor.GetLinkNames("*", nil)
|
|
390
|
+
|
|
391
|
+ if len(inventory) > 0 {
|
|
392
|
+ c.output("Inventory:\n %s", strings.Join(inventory, "\n "))
|
|
393
|
+ } else {
|
|
394
|
+ c.output("You're not carrying anything.")
|
|
395
|
+ }
|
|
396
|
+
|
|
397
|
+}
|
|
398
|
+
|
|
399
|
+func (c *ExecutionContext) getCmd(message string) {
|
|
400
|
+
|
|
401
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
402
|
+ room, found := c.db.Fetch(roomID)
|
|
403
|
+ if !found {
|
|
404
|
+ return
|
|
405
|
+ }
|
|
406
|
+
|
|
407
|
+ object, matchType := room.MatchLinkNames(message, c.actor.ID).ExactlyOne()
|
|
408
|
+
|
|
409
|
+ switch matchType {
|
|
410
|
+ case MatchOne:
|
|
411
|
+ if object.ID == c.actor.ID {
|
|
412
|
+ c.output("You can't pick yourself up.")
|
|
413
|
+ return
|
|
414
|
+ }
|
|
415
|
+ err := c.actor.Contains(&object)
|
|
416
|
+ if err != nil {
|
|
417
|
+ return
|
|
418
|
+ }
|
|
419
|
+ //c.actor.Refresh()
|
|
420
|
+ c.output("You pick up %s.", object.Name)
|
|
421
|
+ c.pemit(object.ID, "%s picked you up.", c.actor.Name)
|
|
422
|
+ c.oemit(room.ID, "%s picks up %s.", c.actor.Name, object.Name)
|
|
423
|
+ case MatchNone:
|
|
424
|
+ c.output("I don't see that here.")
|
|
425
|
+ case MatchMany:
|
|
426
|
+ c.output("I don't know which one.")
|
|
427
|
+ }
|
|
428
|
+
|
|
429
|
+}
|
|
430
|
+
|
|
431
|
+func (c *ExecutionContext) dropCmd(message string) {
|
|
432
|
+
|
|
433
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
434
|
+ room, found := c.db.Fetch(roomID)
|
|
435
|
+ if !found {
|
|
436
|
+ return
|
|
437
|
+ }
|
|
438
|
+
|
|
439
|
+ object, matchType := c.actor.MatchLinkNames(message, c.actor.ID).ExactlyOne()
|
|
440
|
+
|
|
441
|
+ switch matchType {
|
|
442
|
+ case MatchOne:
|
|
443
|
+ err := room.Contains(&object)
|
|
444
|
+ if err != nil {
|
|
445
|
+ return
|
|
446
|
+ }
|
|
447
|
+ inside, _ := c.db.GetParent(c.actor.ID)
|
|
448
|
+ c.output("You drop %s.", object.Name)
|
|
449
|
+ c.pemit(object.ID, "%s drops you.", c.actor.Name)
|
|
450
|
+ c.oemit(inside, "%s drops %s.", c.actor.Name, object.Name)
|
|
451
|
+ case MatchNone:
|
|
452
|
+ c.output("You're not carrying that.")
|
|
453
|
+ case MatchMany:
|
|
454
|
+ c.output("I don't now which one.")
|
|
455
|
+ }
|
|
456
|
+
|
|
457
|
+}
|
|
458
|
+
|
|
459
|
+func (c *ExecutionContext) enterCmd(message string) {
|
|
460
|
+
|
|
461
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
462
|
+ room, found := c.db.Fetch(roomID)
|
|
463
|
+ if !found {
|
|
464
|
+ return
|
|
465
|
+ }
|
|
466
|
+
|
|
467
|
+ object, matchType := room.MatchLinkNames(message, c.actor.ID).ExactlyOne()
|
|
468
|
+
|
|
469
|
+ switch matchType {
|
|
470
|
+ case MatchOne:
|
|
471
|
+ if object.ID == c.actor.ID {
|
|
472
|
+ c.output("Entering yourself would be a bad idea.")
|
|
473
|
+ return
|
|
474
|
+ }
|
|
475
|
+ err := object.Contains(&c.actor)
|
|
476
|
+ if err != nil {
|
|
477
|
+ return
|
|
478
|
+ }
|
|
479
|
+ c.output("You climb into %s.", object.Name)
|
|
480
|
+ c.oemit(room.ID, "%s climbs into %s.", c.actor.Name, object.Name)
|
|
481
|
+ c.oemit(object.ID, "%s squeezes into %s with you.", c.actor.Name, object.Name)
|
|
482
|
+ case MatchNone:
|
|
483
|
+ c.output("I don't see that here.")
|
|
484
|
+ case MatchMany:
|
|
485
|
+ c.output("I don't now which one.")
|
|
486
|
+ }
|
|
487
|
+}
|
|
488
|
+
|
|
489
|
+func (c *ExecutionContext) leaveCmd() {
|
|
490
|
+
|
|
491
|
+ inside, _ := c.db.GetParent(c.actor.ID)
|
|
492
|
+ object, found := c.db.Fetch(inside)
|
|
493
|
+ if !found {
|
|
494
|
+ return
|
|
495
|
+ }
|
|
496
|
+ objectInside, _ := c.db.GetParent(inside)
|
|
497
|
+ if objectInside == 0 { // probably trying to 'leave' a room
|
|
498
|
+ c.output("You can't leave here.")
|
|
499
|
+ return
|
|
500
|
+ }
|
|
501
|
+
|
|
502
|
+ room, found := c.db.Fetch(objectInside)
|
|
503
|
+ if !found {
|
|
504
|
+ return
|
|
505
|
+ }
|
|
506
|
+
|
|
507
|
+ err := room.Contains(&c.actor)
|
|
508
|
+ if err != nil {
|
|
509
|
+ return
|
|
510
|
+ }
|
|
511
|
+
|
|
512
|
+ //c.actor.Refresh()
|
|
513
|
+
|
|
514
|
+ c.output("You climb out of %s.", object.Name)
|
|
515
|
+ c.oemit(object.ID, "%s climbs out of %s.", c.actor.Name, object.Name)
|
|
516
|
+ c.oemit(room.ID, "%s climbs out of %s.", c.actor.Name, object.Name)
|
|
517
|
+
|
|
518
|
+}
|
|
519
|
+
|
|
520
|
+func (c *ExecutionContext) quitCmd(connectionID int) {
|
|
521
|
+
|
|
522
|
+ c.output("So long, it's been good to know yah.")
|
|
523
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: c.actor.ID, messageType: EventTypeQuit, connectionID: connectionID}
|
|
524
|
+
|
|
525
|
+}
|
|
526
|
+
|
|
527
|
+func (c *ExecutionContext) whoCmd() {
|
|
528
|
+ // onlinePlayers := c.eventDistributor.OnlinePlayers()
|
|
529
|
+
|
|
530
|
+ c.output("Currently Online:\n")
|
|
531
|
+
|
|
532
|
+ //for _, ref := range onlinePlayers {
|
|
533
|
+ // c.output("%s\n", c.db.GetName(ref))
|
|
534
|
+ //}
|
|
535
|
+}
|
|
536
|
+
|
|
537
|
+func (c *ExecutionContext) createCmd(message string) {
|
|
538
|
+
|
|
539
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
540
|
+ room, found := c.db.Fetch(roomID)
|
|
541
|
+ if !found {
|
|
542
|
+ return
|
|
543
|
+ }
|
|
544
|
+
|
|
545
|
+ o := c.factory.NewThing()
|
|
546
|
+ o.Name = strings.TrimSpace(message)
|
|
547
|
+ o.Owner = c.actor.ID
|
|
548
|
+ o.Commit()
|
|
549
|
+
|
|
550
|
+ err := room.Contains(&o)
|
|
551
|
+ if err != nil {
|
|
552
|
+ return
|
|
553
|
+ }
|
|
554
|
+
|
|
555
|
+ c.oemit(room.ID, "A %s appears out of the ether.", o.Name)
|
|
556
|
+ c.output("%s Created.", o.DetailedName())
|
|
557
|
+
|
|
558
|
+}
|
|
559
|
+
|
|
560
|
+func (c *ExecutionContext) openCmd(input string) {
|
|
561
|
+ // @open <in1;in2;in3;etc>=#<room>,<out1;out2;out3;etc>
|
|
562
|
+
|
|
563
|
+ r, _ := regexp.Compile(`^@open\pZ+([^=]*[^=\pZ]+)\pZ*=#([0-9]+)(?:\pZ*,\pZ*([^,]*[^,\pZ]+)\pZ*)?`)
|
|
564
|
+ params := r.FindStringSubmatch(input)
|
|
565
|
+
|
|
566
|
+ if params == nil {
|
|
567
|
+ return
|
|
568
|
+ }
|
|
569
|
+
|
|
570
|
+ inExit, roomIDStr, outExit := params[1], params[2], params[3]
|
|
571
|
+
|
|
572
|
+ if len(inExit) == 0 || len(roomIDStr) == 0 {
|
|
573
|
+ c.output("Bad command or file name.")
|
|
574
|
+ return
|
|
575
|
+ }
|
|
576
|
+
|
|
577
|
+ targetID, _ := NewDBRefFromString(roomIDStr) // this will never fail, the regexp guarantees that
|
|
578
|
+ targetRoom, found := c.db.Fetch(targetID)
|
|
579
|
+ if !found {
|
|
580
|
+ c.output("Target not found.")
|
|
581
|
+ return
|
|
582
|
+ }
|
|
583
|
+
|
|
584
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
585
|
+ room, found := c.db.Fetch(roomID)
|
|
586
|
+ if !found {
|
|
587
|
+ return
|
|
588
|
+ }
|
|
589
|
+
|
|
590
|
+ toExit := c.factory.NewExit(inExit, targetRoom.ID, c.actor.ID)
|
|
591
|
+ toExit.Commit()
|
|
592
|
+ err := room.Contains(&toExit)
|
|
593
|
+ if err != nil {
|
|
594
|
+ return
|
|
595
|
+ }
|
|
596
|
+ c.output("%s Created.", toExit.DetailedName())
|
|
597
|
+
|
|
598
|
+ if len(outExit) > 0 {
|
|
599
|
+ fromExit := c.factory.NewExit(outExit, room.ID, c.actor.ID)
|
|
600
|
+ fromExit.Commit()
|
|
601
|
+ err = targetRoom.Contains(&fromExit)
|
|
602
|
+ if err != nil {
|
|
603
|
+ return
|
|
604
|
+ }
|
|
605
|
+ c.output("%s Created.", fromExit.DetailedName())
|
|
606
|
+ }
|
|
607
|
+
|
|
608
|
+}
|
|
609
|
+
|
|
610
|
+func (c *ExecutionContext) digCmd(input string) {
|
|
611
|
+ // @dig <Room name>=<in1;in2;in3;etc>,<out1;out2;out3;etc>
|
|
612
|
+ //@dig foo bar = <F>oo;foo;f,<B>ack;back;b
|
|
613
|
+
|
|
614
|
+ r, _ := regexp.Compile(`^@dig\pZ+([^=]*[^=\pZ]+)(\pZ*=\pZ*(?:([^,]*[^,\pZ]+)\pZ*)?(?:\pZ*,\pZ*([^,]*[^,\pZ]+)\pZ*)?)?`)
|
|
615
|
+ params := r.FindStringSubmatch(input)
|
|
616
|
+ if params == nil {
|
|
617
|
+ return
|
|
618
|
+ }
|
|
619
|
+
|
|
620
|
+ roomName, inExit, outExit := params[1], params[2], params[3]
|
|
621
|
+
|
|
622
|
+ if len(roomName) == 0 {
|
|
623
|
+ c.output("Rooms can't not have names.")
|
|
624
|
+ return
|
|
625
|
+ }
|
|
626
|
+
|
|
627
|
+ newRoom := c.factory.NewRoom()
|
|
628
|
+ newRoom.Name = roomName
|
|
629
|
+ newRoom.Owner = c.actor.ID
|
|
630
|
+ newRoom.Commit()
|
|
631
|
+
|
|
632
|
+ c.output("%s Created.", newRoom.DetailedName())
|
|
633
|
+
|
|
634
|
+ if len(inExit) > 0 || len(outExit) > 0 {
|
|
635
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
636
|
+ room, found := c.db.Fetch(roomID)
|
|
637
|
+ if !found {
|
|
638
|
+ return
|
|
639
|
+ }
|
|
640
|
+ if len(inExit) > 0 {
|
|
641
|
+ toExit := c.factory.NewExit(inExit, newRoom.ID, c.actor.ID)
|
|
642
|
+ toExit.Commit()
|
|
643
|
+ err := room.Contains(&toExit)
|
|
644
|
+ if err != nil {
|
|
645
|
+ return
|
|
646
|
+ }
|
|
647
|
+ c.output("%s Created.", toExit.DetailedName())
|
|
648
|
+ }
|
|
649
|
+ if len(outExit) > 0 {
|
|
650
|
+ fromExit := c.factory.NewExit(outExit, room.ID, c.actor.ID)
|
|
651
|
+ fromExit.Commit()
|
|
652
|
+ err := newRoom.Contains(&fromExit)
|
|
653
|
+ if err != nil {
|
|
654
|
+ return
|
|
655
|
+ }
|
|
656
|
+ c.output("%s Created.", fromExit.DetailedName())
|
|
657
|
+ }
|
|
658
|
+ }
|
|
659
|
+
|
|
660
|
+}
|
|
661
|
+
|
|
662
|
+func (c *ExecutionContext) nameCmd(input string) {
|
|
663
|
+
|
|
664
|
+ r, _ := regexp.Compile(`^@name\pZ+([^=]*[^=\pZ]{1})\pZ*=\pZ*(.*)\pZ*$`)
|
|
665
|
+ params := r.FindStringSubmatch(input)
|
|
666
|
+ if params == nil {
|
|
667
|
+ return
|
|
668
|
+ }
|
|
669
|
+
|
|
670
|
+ objectName, name := params[1], params[2]
|
|
671
|
+
|
|
672
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
673
|
+ room, found := c.db.Fetch(roomID)
|
|
674
|
+ if !found {
|
|
675
|
+ return
|
|
676
|
+ }
|
|
677
|
+
|
|
678
|
+ candidate, matchType := room.MatchLinkNames(objectName, c.actor.ID).ExactlyOne()
|
|
679
|
+ switch matchType {
|
|
680
|
+ case MatchOne:
|
|
681
|
+ candidate.Name = name
|
|
682
|
+ candidate.Commit()
|
|
683
|
+ c.output("Name set.")
|
|
684
|
+ //c.actor.Refresh()
|
|
685
|
+ case MatchNone:
|
|
686
|
+ c.output("I don't see that here.")
|
|
687
|
+ case MatchMany:
|
|
688
|
+ c.output("I don't now which one.")
|
|
689
|
+ }
|
|
690
|
+
|
|
691
|
+}
|
|
692
|
+
|
|
693
|
+func (c *ExecutionContext) descCmd(input string) {
|
|
694
|
+
|
|
695
|
+ r, _ := regexp.Compile(`^@desc\pZ+([^=]*[^=\pZ]{1})\pZ*=\pZ*(.*)\pZ*$`)
|
|
696
|
+ params := r.FindStringSubmatch(input)
|
|
697
|
+ if params == nil {
|
|
698
|
+ return
|
|
699
|
+ }
|
|
700
|
+
|
|
701
|
+ objectName, description := params[1], params[2]
|
|
702
|
+
|
|
703
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
704
|
+ room, found := c.db.Fetch(roomID)
|
|
705
|
+
|
|
706
|
+ if !found {
|
|
707
|
+ return
|
|
708
|
+ }
|
|
709
|
+
|
|
710
|
+ var editObject *Object
|
|
711
|
+ switch objectName {
|
|
712
|
+ case "here":
|
|
713
|
+ editObject = &room
|
|
714
|
+ case "me":
|
|
715
|
+ editObject = &c.actor
|
|
716
|
+ default:
|
|
717
|
+ candidate, matchType := room.MatchLinkNames(objectName, c.actor.ID).ExactlyOne()
|
|
718
|
+ switch matchType {
|
|
719
|
+ case MatchOne:
|
|
720
|
+ editObject = &candidate
|
|
721
|
+ case MatchNone:
|
|
722
|
+ c.output("I don't see that here.")
|
|
723
|
+ return
|
|
724
|
+ case MatchMany:
|
|
725
|
+ c.output("I don't now which one.")
|
|
726
|
+ return
|
|
727
|
+ }
|
|
728
|
+ }
|
|
729
|
+
|
|
730
|
+ editObject.Description = description
|
|
731
|
+ editObject.Commit()
|
|
732
|
+ //c.actor.Refresh()
|
|
733
|
+ c.output("Description set.")
|
|
734
|
+
|
|
735
|
+}
|
|
736
|
+
|
|
737
|
+func (c *ExecutionContext) telCmd(destStr string) {
|
|
738
|
+
|
|
739
|
+ dest, err := NewDBRefFromHashRef(destStr)
|
|
740
|
+
|
|
741
|
+ if err != nil {
|
|
742
|
+ c.output("That doesn't look like a DBRef.")
|
|
743
|
+ return
|
|
744
|
+ }
|
|
745
|
+
|
|
746
|
+ newRoom, found := c.db.Fetch(dest)
|
|
747
|
+ if !found {
|
|
748
|
+ c.output("That doesn't exist.")
|
|
749
|
+ return
|
|
750
|
+ }
|
|
751
|
+
|
|
752
|
+ c.output("You feel an intense wooshing sensation.")
|
|
753
|
+ err = newRoom.Contains(&c.actor)
|
|
754
|
+ if err != nil {
|
|
755
|
+ return
|
|
756
|
+ }
|
|
757
|
+
|
|
758
|
+ c.actor.Refresh()
|
|
759
|
+ c.lookCmd("")
|
|
760
|
+
|
|
761
|
+}
|
|
762
|
+
|
|
763
|
+func (c *ExecutionContext) dumpCmd(refStr string) {
|
|
764
|
+
|
|
765
|
+ ref, err := NewDBRefFromHashRef(refStr)
|
|
766
|
+
|
|
767
|
+ if err != nil {
|
|
768
|
+ c.output("That doesn't look like a DBRef.")
|
|
769
|
+ return
|
|
770
|
+ }
|
|
771
|
+
|
|
772
|
+ obj, found := c.db.Fetch(ref)
|
|
773
|
+ if !found {
|
|
774
|
+ c.output("That doesn't exist.")
|
|
775
|
+ return
|
|
776
|
+ }
|
|
777
|
+
|
|
778
|
+ c.output("%s", c.db.DumpObject(obj.ID))
|
|
779
|
+
|
|
780
|
+}
|
|
781
|
+
|
|
782
|
+func (c *ExecutionContext) destroyCmd(message string) {
|
|
783
|
+
|
|
784
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
785
|
+ room, found := c.db.Fetch(roomID)
|
|
786
|
+ if !found {
|
|
787
|
+ return
|
|
788
|
+ }
|
|
789
|
+
|
|
790
|
+ object, matchType := room.MatchLinkNames(message, c.actor.ID).ExactlyOne()
|
|
791
|
+
|
|
792
|
+ switch matchType {
|
|
793
|
+ case MatchOne:
|
|
794
|
+ if object.ID == c.actor.ID {
|
|
795
|
+ c.output("There are alternatives to suicide.")
|
|
796
|
+ return
|
|
797
|
+ }
|
|
798
|
+ if object.Type == "player" {
|
|
799
|
+ c.output("I didn't think homicide was your thing.")
|
|
800
|
+ return
|
|
801
|
+ }
|
|
802
|
+ name := object.DetailedName()
|
|
803
|
+ err := object.Delete()
|
|
804
|
+ if err == nil {
|
|
805
|
+ //c.actor.Refresh()
|
|
806
|
+ c.output("%s vanishes into thin air.", name)
|
|
807
|
+ }
|
|
808
|
+ case MatchNone:
|
|
809
|
+ c.output("I don't see that here.")
|
|
810
|
+ case MatchMany:
|
|
811
|
+ c.output("I don't know which one.")
|
|
812
|
+ }
|
|
813
|
+
|
|
814
|
+}
|
|
815
|
+
|
|
816
|
+func (c *ExecutionContext) goCmd(dir string) bool {
|
|
817
|
+
|
|
818
|
+ roomID, found := c.db.GetParent(c.actor.ID)
|
|
819
|
+ room, found := c.db.Fetch(roomID)
|
|
820
|
+ if !found {
|
|
821
|
+ return false
|
|
822
|
+ }
|
|
823
|
+
|
|
824
|
+ exit, matchType := room.MatchExitNames(dir).ExactlyOne()
|
|
825
|
+ switch matchType {
|
|
826
|
+ case MatchOne:
|
|
827
|
+ if exit.Next.Valid() {
|
|
828
|
+ newRoom, found := c.db.Fetch(exit.Next)
|
|
829
|
+ if !found {
|
|
830
|
+ c.output("That exit appears to be broken!")
|
|
831
|
+ return true
|
|
832
|
+ }
|
|
833
|
+ err := newRoom.Contains(&c.actor)
|
|
834
|
+ if err != nil {
|
|
835
|
+ return false
|
|
836
|
+ }
|
|
837
|
+ //c.actor.Refresh()
|
|
838
|
+ c.output("You head towards %s.", newRoom.Name)
|
|
839
|
+ c.oemit(room.ID, "%s leaves the room.", c.actor.Name)
|
|
840
|
+ c.oemit(newRoom.ID, "%s enters the room.", c.actor.Name)
|
|
841
|
+ return true
|
|
842
|
+ }
|
|
843
|
+ case MatchNone:
|
|
844
|
+ return false
|
|
845
|
+ case MatchMany:
|
|
846
|
+ c.output("Ambiguous exit names are ambiguous.")
|
|
847
|
+ return true
|
|
848
|
+ }
|
|
849
|
+
|
|
850
|
+ return false
|
|
851
|
+
|
|
852
|
+}
|
|
853
|
+
|
|
854
|
+func (c *ExecutionContext) oemit(audience DBRef, format string, a ...interface{}) {
|
|
855
|
+
|
|
856
|
+ message := fmt.Sprintf(format, a...)
|
|
857
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: audience, message: message, messageType: EventTypeOEmit}
|
|
858
|
+
|
|
859
|
+}
|
|
860
|
+
|
|
861
|
+func (c *ExecutionContext) output(format string, a ...interface{}) {
|
|
862
|
+
|
|
863
|
+ message := fmt.Sprintf(format, a...)
|
|
864
|
+ if !c.forceContext {
|
|
865
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: c.actor.ID, message: message, messageType: EventTypeOutput}
|
|
866
|
+ }
|
|
867
|
+}
|
|
868
|
+
|
|
869
|
+func (c *ExecutionContext) pemit(target DBRef, format string, a ...interface{}) {
|
|
870
|
+
|
|
871
|
+ message := fmt.Sprintf(format, a...)
|
|
872
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: target, message: message, messageType: EventTypePEmit}
|
|
873
|
+
|
|
874
|
+}
|
|
875
|
+
|
|
876
|
+func (c *ExecutionContext) emit(audience DBRef, format string, a ...interface{}) {
|
|
877
|
+
|
|
878
|
+ message := fmt.Sprintf(format, a...)
|
|
879
|
+ c.outbound <- PlayerEvent{src: c.actor.ID, dst: audience, message: message, messageType: EventTypeEmit}
|
|
880
|
+
|
|
881
|
+}
|