Пример #1
0
func (eng *Engine) look(obj **object.Object, msg *message.Message) {
	location := obj.GetLocation()
	result := message.MakeMutableMessage("result")

	if msg.Data == "" {
		contents := location.GetContents()
		sort.Sort(object.ObjectSliceByType(contents))

		result.Append("%s (#%d)\n%s\n\n", location.GetAttr("name"), location.GetID(), location.GetAttr("description"))
		result.Append("Contents:\n")

		for _, thing := range contents {
			if thing.GetType() != object.PLAYER || thing.GetWriter() != nil {
				result.Append("%s (#%d)\n", thing.GetAttr("name"), thing.GetID())
			}
		}
	} else {
		subj := location.FindInside("name", msg.Data)
		if subj == nil {
			result.Append("%s not found!", msg.Data)
		} else {
			result.Append("%s (#%d)\n%s\n", subj.GetAttr("name"), subj.GetID(), subj.GetAttr("description"))
		}
	}

	obj.Hear(result.Finalize())
}
Пример #2
0
func HandleClientSocket(eng *engine.Engine, conn net.Conn) {
	reader := bufio.NewReader(conn)
	writer := make(chan *message.Message, WRITE_QUEUE)
	kill := make(chan bool)

	associatedObj := object.NewObject(nil, object.DUMMY_ID)
	associatedObj.SetWriter(writer)

	defer func() {
		conn.Close()
		associatedObj.SetWriter(nil)
		kill <- true
	}()

	go func() {
		socketWriter := bufio.NewWriter(conn)

		for {
			select {
			case writerMsg := <-writer:
				_, err := socketWriter.WriteString(writerMsg.Data + "\n")

				if err != nil {
					log.Println("Unexpected error (write socket): ", err)
				}

				err = socketWriter.Flush()

				if err != nil {
					log.Println("Unexpected error (flush socket): ", err)
				}

			case <-kill:
				return
			}

		}
	}()

	for {
		rawMessage, err := reader.ReadString('\n')
		if err != nil {
			return
		}

		trimmedMessage := strings.TrimSpace(rawMessage)
		splitMessage := strings.SplitN(trimmedMessage, " ", 2)
		msg := message.MakeMutableMessage(splitMessage[0])

		if len(splitMessage) > 1 {
			msg.Append(splitMessage[1])
		}

		msg.Finalize()

		eng.Do(&associatedObj, msg)
	}
}