Extracted scenario

This commit is contained in:
Artyom Belousov 2021-06-06 11:00:50 +03:00
parent c02435ccaa
commit cc2058090c
13 changed files with 235 additions and 173 deletions

View file

@ -1,23 +1,18 @@
package vk
import (
"errors"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
)
type Handler struct {
Sender sender
Logger *log.Logger
Scenario *scenario.Scenario
SecretKey string
GroupId int64
ConfirmationString string
DictPath string
Cache cardCache
InfoFetcher cardInfoFetcher
}
type cardInfoFetcher interface {
@ -59,62 +54,16 @@ func (h *Handler) HandleMessage(c *gin.Context) {
case "confirmation":
h.handleConfirmation(c, &req)
case "message_new":
go h.handleSearch(&req)
go h.Scenario.HandleSearch(&scenario.UserMessage{
Body: req.Object.Body,
UserId: req.Object.UserId,
})
c.String(http.StatusOK, "ok")
}
}
func (h *Handler) handleSearch(req *messageRequest) {
cardName, err := h.getCardNameByCommand(req.Object.Body)
if err != nil {
h.Sender.send(req.Object.UserId, incorrectMessage)
h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
} else if cardName == "" {
h.Sender.send(req.Object.UserId, cardNotFoundMessage)
h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body)
} else {
message, err := h.getMessage(cardName)
if err != nil {
h.Sender.send(req.Object.UserId, pricesUnavailableMessage)
h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return
}
h.Sender.send(req.Object.UserId, message)
}
}
func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
if (req.Type == "confirmation") && (req.GroupId == h.GroupId) {
c.String(http.StatusOK, h.ConfirmationString)
}
}
func (h *Handler) getMessage(cardName string) (string, error) {
val, err := h.Cache.Get(cardName)
if err != nil {
message, err := h.InfoFetcher.GetFormattedCardPrices(cardName)
if err != nil {
return "", err
}
h.Cache.Set(cardName, message)
return message, nil
}
return val, nil
}
func (h *Handler) getCardNameByCommand(command string) (string, error) {
var name string
switch {
case strings.HasPrefix(command, "!s"):
split := strings.Split(command, " ")
if len(split) < 3 {
return "", errors.New("wrong command")
}
set := split[1]
number := split[2]
name = h.InfoFetcher.GetNameByCardId(set, number)
default:
name = h.InfoFetcher.GetOriginalName(command)
}
return name, nil
}