valera/internal/states/eat_bot_state/calories.go

53 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-04-09 06:31:01 +00:00
package eat_bot_state
2023-03-18 07:35:05 +00:00
import (
"strconv"
"strings"
)
var (
2023-03-18 12:02:32 +00:00
productsMap = map[string]struct {
caloriesIn100G int
awgWeightG int
}{
2023-04-09 06:31:01 +00:00
"чай": {caloriesIn100G: 65, awgWeightG: 200},
"яблоко": {caloriesIn100G: 52, awgWeightG: 242},
"хлеб": {caloriesIn100G: 245, awgWeightG: 35},
"огурец": {caloriesIn100G: 15, awgWeightG: 20},
"помидор": {caloriesIn100G: 18, awgWeightG: 18},
2023-04-28 09:56:05 +00:00
"слива": {caloriesIn100G: 50, awgWeightG: 50},
2023-03-18 07:35:05 +00:00
}
)
2023-04-09 06:31:01 +00:00
func calcCalories(text string) (int, error) {
2023-03-18 12:02:32 +00:00
product, ok := productsMap[strings.ToLower(text)]
2023-03-18 07:35:05 +00:00
if ok {
2023-03-18 12:02:32 +00:00
return product.caloriesIn100G * product.awgWeightG / 100, nil
2023-03-18 07:35:05 +00:00
}
arr := strings.Split(text, " ")
if len(arr) == 2 {
2023-03-18 12:02:32 +00:00
product, ok := productsMap[strings.ToLower(arr[0])]
if ok {
count, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
return product.caloriesIn100G * count / 100, nil
}
count1, err := strconv.Atoi(arr[0])
2023-03-18 07:35:05 +00:00
if err != nil {
return 0, nil
}
count2, err := strconv.Atoi(arr[1])
if err != nil {
return 0, nil
}
2023-03-18 12:02:32 +00:00
return count1 * count2 / 100, nil
2023-03-18 07:35:05 +00:00
}
count, err := strconv.Atoi(text)
if err != nil {
return 0, nil
}
return count, nil
}