Exemplo n.º 1
0
func Subscribe(m interfaces.MCP, sub *messages.Subscription) *messages.Error {
	var merr *messages.Error
	if err := m.Call(nil, m.GetResource(), messages.MethodSubscribe, []interface{}{sub}, &[]interface{}{&merr}); err != nil {
		return err
	}
	return merr
}
Exemplo n.º 2
0
func EmitEvent(m interfaces.MCP, ev *messages.Event) *messages.Error {
	var merr *messages.Error
	if err := m.Call(nil, m.GetResource(), messages.MethodEmitEvent, []interface{}{ev}, &[]interface{}{&merr}); err != nil {
		return err
	}
	return merr
}
Exemplo n.º 3
0
func GetLongDesc(m interfaces.MCP, resource string) (string, *messages.Error) {
	var desc string
	var merr *messages.Error
	if err := m.Call(LookAt, resource, messages.MethodGetLongDesc, nil, &[]interface{}{&desc, &merr}); err != nil {
		return "", err
	}
	return desc, merr
}
Exemplo n.º 4
0
func GetContainer(m interfaces.MCP, resource string) (string, *messages.Error) {
	var container string
	var merr *messages.Error
	if err := m.Call(LookUp, resource, messages.MethodGetContainer, nil, &[]interface{}{&container, &merr}); err != nil {
		return "", err
	}
	return container, merr
}
Exemplo n.º 5
0
func SendToClient(m interfaces.MCP, msg string) {
	var merr *messages.Error
	if err := m.Call(nil, m.GetResource(), messages.MethodSendToClient, []string{msg}, &[]interface{}{&merr}); err != nil {
		log.Fatal(err)
	}
	if merr != nil {
		log.Fatal(merr)
	}
}
Exemplo n.º 6
0
func GetShortDesc(m interfaces.MCP, resource string) (*messages.ShortDesc, *messages.Error) {
	desc, found := cache.get(resource)
	if found {
		return desc, nil
	}
	var merr *messages.Error
	if err := m.Call(GlanceAt, resource, messages.MethodGetShortDesc, nil, &[]interface{}{&desc, &merr}); err != nil {
		return nil, err
	}
	if merr != nil {
		return nil, merr
	}
	cache.set(resource, desc)
	return desc, nil
}
Exemplo n.º 7
0
func GetContent(m interfaces.MCP, resource string) ([]string, *messages.Error) {
	container, err := GetContainer(m, m.GetResource())
	if err != nil {
		return nil, err
	}
	verb := LookInto
	if resource == container {
		verb = LookAround
	}
	var content []string
	var merr *messages.Error
	if err := m.Call(verb, resource, messages.MethodGetContent, nil, &[]interface{}{&content, &merr}); err != nil {
		return nil, err
	}
	return content, merr
}
Exemplo n.º 8
0
func Identify(m interfaces.MCP, what string) (mathes []string, err *messages.Error) {
	what = strings.ToLower(what)

	shortDescMap, err := GetShortDescMap(m, m.GetResource())
	if err != nil {
		return nil, err
	}

	// Exact match ("take rock")
	matches := []string{}
	for resource, desc := range shortDescMap {
		if strings.HasPrefix(strings.ToLower(desc), what) {
			matches = append(matches, resource)
		}
	}

	// Number suffix ("take rock 2")
	if len(matches) != 1 {
		found, num, prefix := SplitEndNumber(what)
		if found && num > 0 {
			newMatches := []string{}
			for resource, desc := range shortDescMap {
				if strings.HasPrefix(strings.ToLower(desc), prefix) {
					newMatches = append(newMatches, resource)
				}
			}
			if len(newMatches) >= num {
				matches = []string{newMatches[num-1]}
			}
		}
	}

	// Inside match ("take [large] rock")
	if len(matches) == 0 {
		newMatches := []string{}
		for resource, desc := range shortDescMap {
			parts := SplitWhitespace(desc)
			for _, part := range parts {
				if strings.HasPrefix(strings.ToLower(part), what) {
					newMatches = append(newMatches, resource)
				}
			}
			if len(newMatches) > 0 {
				matches = newMatches
			}
		}
	}

	// Inside number suffix ("take [large] rock 2")
	if len(matches) != 1 {
		found, num, prefix := SplitEndNumber(what)
		if found && num > 0 {
			newMatches := []string{}
			for resource, desc := range shortDescMap {
				parts := SplitWhitespace(desc)
				for _, part := range parts {
					if strings.HasPrefix(strings.ToLower(part), prefix) {
						newMatches = append(newMatches, resource)
					}
				}
				if len(newMatches) >= num {
					matches = []string{newMatches[num-1]}
				}
			}
		}
	}

	return matches, nil
}