Browse Source

bit of cleanup, switched some commands to use regular expressions for parsing input

Keelan Lightfoot 8 years ago
parent
commit
19d416cb19
2 changed files with 68 additions and 101 deletions
  1. 60
    97
      client.go
  2. 8
    4
      object.go

+ 60
- 97
client.go View File

@@ -5,6 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"log"
7 7
 	"net"
8
+	"regexp"
8 9
 	"strings"
9 10
 	"unicode"
10 11
 )
@@ -232,13 +233,13 @@ func (c *Client) handlePlayPhase(message string) {
232 233
 	case strings.HasPrefix(message, "@create "):
233 234
 		c.createCmd(strings.TrimPrefix(message, "@create "))
234 235
 	case strings.HasPrefix(message, "@dig "):
235
-		c.digCmd(strings.TrimPrefix(message, "@dig "))
236
+		c.digCmd(message)
236 237
 	case strings.HasPrefix(message, "@open "):
237
-		c.openCmd(strings.TrimPrefix(message, "@open "))
238
+		c.openCmd(message)
238 239
 	case strings.HasPrefix(message, "@name "):
239
-		c.nameCmd(strings.TrimPrefix(message, "@name "))
240
+		c.nameCmd(message)
240 241
 	case strings.HasPrefix(message, "@desc "):
241
-		c.descCmd(strings.TrimPrefix(message, "@desc "))
242
+		c.descCmd(message)
242 243
 	case strings.HasPrefix(message, "@tel "):
243 244
 		c.telCmd(strings.TrimPrefix(message, "@tel "))
244 245
 	case strings.HasPrefix(message, "@dump "):
@@ -523,34 +524,27 @@ func (c *Client) createCmd(message string) {
523 524
 
524 525
 }
525 526
 
526
-func (c *Client) openCmd(message string) {
527
+func (c *Client) openCmd(input string) {
527 528
 	// @open <in1;in2;in3;etc>=#<room>,<out1;out2;out3;etc>
528 529
 
529
-	slicey := strings.SplitN(message, "=", 2)
530
+	r, _ := regexp.Compile(`^@open\pZ+([^=]*[^=\pZ]+)\pZ*=#([0-9]+)(?:\pZ*,\pZ*([^,]*[^,\pZ]+)\pZ*)?`)
531
+	params := r.FindStringSubmatch(input)
530 532
 
531
-	if len(slicey) < 2 {
532
-		c.write("Bad command or file name.\n")
533
+	if params == nil {
533 534
 		return
534 535
 	}
535 536
 
536
-	srcExitSpec := strings.TrimSpace(slicey[0])
537
+	inExit, roomIDStr, outExit := params[1], params[2], params[3]
537 538
 
538
-	stuff := strings.Split(slicey[1], ",")
539
-	if len(stuff) > 2 {
539
+	if len(inExit) == 0 || len(roomIDStr) == 0 {
540 540
 		c.write("Bad command or file name.\n")
541 541
 		return
542 542
 	}
543 543
 
544
-	makeReturnExit := false
545
-	var returnExitSpec string
546
-	if len(stuff) == 2 {
547
-		makeReturnExit = true
548
-		returnExitSpec = strings.TrimSpace(stuff[1])
549
-	}
550
-
551
-	target, err := NewDBRefFromHashRef(strings.TrimSpace(stuff[0]))
552
-	if err != nil {
553
-		c.write("Bad target DBRef.\n")
544
+	targetID, _ := NewDBRefFromString(roomIDStr) // this will never fail, the regexp guarantees that
545
+	targetRoom, found := c.db.Fetch(targetID)
546
+	if !found {
547
+		c.write("Target not found.\n")
554 548
 		return
555 549
 	}
556 550
 
@@ -560,63 +554,41 @@ func (c *Client) openCmd(message string) {
560 554
 		return
561 555
 	}
562 556
 
563
-	targetRoom, found := c.db.Fetch(target)
564
-	if !found {
565
-		c.write("Target not found.\n")
566
-		return
567
-	}
568
-
569
-	toExit := c.factory.NewExit()
570
-	toExit.Name = srcExitSpec
571
-	toExit.Next = targetRoom.ID
572
-	toExit.Owner = c.player.ID
557
+	toExit := c.factory.NewExit(inExit, targetRoom.ID, c.player.ID)
573 558
 	toExit.Commit()
574
-
575
-	err = room.Contains(&toExit)
559
+	err := room.Contains(&toExit)
576 560
 	if err != nil {
577 561
 		return
578 562
 	}
579
-
580 563
 	c.write("%s Created.\n", toExit.DetailedName())
581 564
 
582
-	if makeReturnExit {
583
-		fromExit := c.factory.NewExit()
584
-		fromExit.Name = returnExitSpec
585
-		fromExit.Next = room.ID
586
-		fromExit.Owner = c.player.ID
565
+	if len(outExit) > 0 {
566
+		fromExit := c.factory.NewExit(outExit, room.ID, c.player.ID)
587 567
 		fromExit.Commit()
588
-
589 568
 		err = targetRoom.Contains(&fromExit)
590 569
 		if err != nil {
591 570
 			return
592 571
 		}
593
-
594 572
 		c.write("%s Created.\n", fromExit.DetailedName())
595 573
 	}
596 574
 
597 575
 }
598 576
 
599
-func (c *Client) digCmd(message string) {
577
+func (c *Client) digCmd(input string) {
600 578
 	// @dig <Room name>=<in1;in2;in3;etc>,<out1;out2;out3;etc>
601
-	//@dig foo=<F>oo;foo;f,<B>ack;back;b
602
-	slicey := strings.SplitN(message, "=", 2)
579
+	//@dig foo bar  = <F>oo;foo;f,<B>ack;back;b
603 580
 
604
-	if len(slicey) < 1 {
605
-		c.write("Rooms can't not have names.\n")
581
+	r, _ := regexp.Compile(`^@dig\pZ+([^=]*[^=\pZ]+)(\pZ*=\pZ*(?:([^,]*[^,\pZ]+)\pZ*)?(?:\pZ*,\pZ*([^,]*[^,\pZ]+)\pZ*)?)?`)
582
+	params := r.FindStringSubmatch(input)
583
+	if params == nil {
606 584
 		return
607 585
 	}
608 586
 
609
-	roomName := strings.TrimSpace(slicey[0])
587
+	roomName, inExit, outExit := params[1], params[2], params[3]
610 588
 
611
-	exits := make([]string, 0)
612
-
613
-	if len(slicey) == 2 {
614
-		exitSpec := strings.TrimSpace(slicey[1])
615
-		exits = strings.Split(exitSpec, ",")
616
-		if len(exits) > 2 {
617
-			c.write("You've listed more than two exits. That doesn't even make sense.\n")
618
-			return
619
-		}
589
+	if len(roomName) == 0 {
590
+		c.write("Rooms can't not have names.\n")
591
+		return
620 592
 	}
621 593
 
622 594
 	newRoom := c.factory.NewRoom()
@@ -626,61 +598,50 @@ func (c *Client) digCmd(message string) {
626 598
 
627 599
 	c.write("%s Created.\n", newRoom.DetailedName())
628 600
 
629
-	if len(exits) > 0 {
601
+	if len(inExit) > 0 || len(outExit) > 0 {
630 602
 		roomID, found := c.db.GetParent(c.player.ID)
631 603
 		room, found := c.db.Fetch(roomID)
632 604
 		if !found {
633 605
 			return
634 606
 		}
635
-		toExit := c.factory.NewExit()
636
-		toExit.Name = exits[0]
637
-		toExit.Next = newRoom.ID
638
-		toExit.Owner = c.player.ID
639
-		toExit.Commit()
640
-
641
-		err := room.Contains(&toExit)
642
-		if err != nil {
643
-			return
607
+		if len(inExit) > 0 {
608
+			toExit := c.factory.NewExit(inExit, newRoom.ID, c.player.ID)
609
+			toExit.Commit()
610
+			err := room.Contains(&toExit)
611
+			if err != nil {
612
+				return
613
+			}
614
+			c.write("%s Created.\n", toExit.DetailedName())
644 615
 		}
645
-
646
-		c.write("%s Created.\n", toExit.DetailedName())
647
-
648
-		if len(exits) == 2 {
649
-			fromExit := c.factory.NewExit()
650
-			fromExit.Name = exits[1]
651
-			fromExit.Next = room.ID
652
-			fromExit.Owner = c.player.ID
616
+		if len(outExit) > 0 {
617
+			fromExit := c.factory.NewExit(outExit, room.ID, c.player.ID)
653 618
 			fromExit.Commit()
654
-
655
-			err = newRoom.Contains(&fromExit)
619
+			err := newRoom.Contains(&fromExit)
656 620
 			if err != nil {
657 621
 				return
658 622
 			}
659
-
660 623
 			c.write("%s Created.\n", fromExit.DetailedName())
661
-
662 624
 		}
663 625
 	}
664 626
 
665 627
 }
666 628
 
667
-func (c *Client) nameCmd(message string) {
668
-	roomID, found := c.db.GetParent(c.player.ID)
669
-	room, found := c.db.Fetch(roomID)
670
-	if !found {
629
+func (c *Client) nameCmd(input string) {
630
+
631
+	r, _ := regexp.Compile(`^@name\pZ+([^=]*[^=\pZ]{1})\pZ*=\pZ*(.*)\pZ*$`)
632
+	params := r.FindStringSubmatch(input)
633
+	if params == nil {
671 634
 		return
672 635
 	}
673 636
 
674
-	slicey := strings.SplitN(message, "=", 2)
637
+	objectName, name := params[1], params[2]
675 638
 
676
-	if len(slicey) == 1 {
677
-		c.write("Things can't not have names.\n")
639
+	roomID, found := c.db.GetParent(c.player.ID)
640
+	room, found := c.db.Fetch(roomID)
641
+	if !found {
678 642
 		return
679 643
 	}
680 644
 
681
-	objectName := strings.TrimSpace(slicey[0])
682
-	name := strings.TrimSpace(slicey[1])
683
-
684 645
 	candidate, matchType := room.MatchLinkNames(objectName, c.player.ID, false).ExactlyOne()
685 646
 	switch matchType {
686 647
 	case MatchOne:
@@ -695,20 +656,22 @@ func (c *Client) nameCmd(message string) {
695 656
 	}
696 657
 
697 658
 }
698
-func (c *Client) descCmd(message string) {
699 659
 
700
-	roomID, found := c.db.GetParent(c.player.ID)
701
-	room, found := c.db.Fetch(roomID)
702
-	if !found {
660
+func (c *Client) descCmd(input string) {
661
+
662
+	r, _ := regexp.Compile(`^@desc\pZ+([^=]*[^=\pZ]{1})\pZ*=\pZ*(.*)\pZ*$`)
663
+	params := r.FindStringSubmatch(input)
664
+	if params == nil {
703 665
 		return
704 666
 	}
705 667
 
706
-	slicey := strings.SplitN(message, "=", 2)
668
+	objectName, description := params[1], params[2]
669
+
670
+	roomID, found := c.db.GetParent(c.player.ID)
671
+	room, found := c.db.Fetch(roomID)
707 672
 
708
-	objectName := strings.TrimSpace(slicey[0])
709
-	description := ""
710
-	if len(slicey) > 1 {
711
-		description = strings.TrimSpace(slicey[1])
673
+	if !found {
674
+		return
712 675
 	}
713 676
 
714 677
 	var editObject *Object

+ 8
- 4
object.go View File

@@ -39,11 +39,15 @@ func (f *ObjectFactory) NewObject() Object {
39 39
 	return o
40 40
 }
41 41
 
42
-func (f *ObjectFactory) NewExit() Object {
43
-	o := Object{}
42
+func (f *ObjectFactory) NewExit(name string, next DBRef, owner DBRef) Object {
43
+	o := Object{
44
+		Type:  "exit",
45
+		Name:  name,
46
+		Next:  next,
47
+		Owner: owner,
48
+		db:    f.db,
49
+	}
44 50
 	o.ID, _ = f.db.Allocate()
45
-	o.Type = "exit"
46
-	o.db = f.db
47 51
 	return o
48 52
 }
49 53