Exemplo n.º 1
0
// Assemble basic arithmetic operations requests.
func arithmetic() ([]string, error) {

	// Take a random value for A and B up to 1000.
	maxValue := 1000
	a, _ := utilities.Random(maxValue)
	b, _ := utilities.Random(maxValue)

	// Now we select a random operation.
	operationsList := []string{"sum", "sub", "mult", "div"}
	index, err := utilities.Random(len(operationsList))
	if err != nil {
		return nil, err
	}
	operation := operationsList[index]

	// Request ready, expect something like [mult 3 2]
	return []string{operation, strconv.Itoa(a), strconv.Itoa(b)}, nil
}
Exemplo n.º 2
0
// A encode text request.
func encode() ([]string, error) {

	// Same story once again.
	index, err := utilities.Random(len(content))
	if err != nil {
		return nil, err
	}

	// eg.: [encode hello, world]
	return []string{"encode", content[index]}, nil
}
Exemplo n.º 3
0
// A reverse text request.
func reverse() ([]string, error) {

	// Get a random number between zero and the content length,
	// which is equivalent to a line at text.in.
	index, err := utilities.Random(len(content))
	if err != nil {
		return nil, err
	}

	// eg.: [reverse Papa Americano]
	return []string{"reverse", content[index]}, nil
}
Exemplo n.º 4
0
// Randomly choose a message type and call the proper function to build
// that message. Then return the message back to Sender function.
func messenger() ([]string, error) {

	// The basic tasks that the octopus are prepared to handle.
	taskList := []string{"arithmetic", "fibonacci", "reverse", "encode"}

	// Choose a random index from taskList
	index, err := utilities.Random(len(taskList))
	if err != nil {
		// If you are here and you don't know why, check if taskList is empty.
		return nil, err
	}

	// Hold the message to be sent.
	var msg []string

	// Depending on which task has been chosen,
	// we call the corresponding assembler function.
	switch taskList[index] {

	case "arithmetic":
		msg, err = arithmetic()
		if err != nil {
			return nil, err
		}

	case "fibonacci":
		msg = fibonacci()

	case "reverse":
		msg, err = reverse()
		if err != nil {
			return nil, err
		}

	case "encode":
		msg, err = encode()
		if err != nil {
			return nil, err
		}
	}
	return msg, nil
}
Exemplo n.º 5
0
// A Fibonacci request.
func fibonacci() []string {

	// Fib 30 should be big enough for our case.
	n, _ := utilities.Random(30)
	return []string{"fibonacci", strconv.Itoa(n)}
}