2024-05-13 19:21:34 +03:00
|
|
|
package fsmwizard
|
2024-05-13 16:57:55 +03:00
|
|
|
|
|
|
|
import (
|
2024-05-13 19:08:46 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
2024-05-13 16:57:55 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
2024-05-14 14:44:41 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
|
2024-05-13 16:57:55 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/locale"
|
2024-05-13 19:08:46 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
|
2024-05-13 16:57:55 +03:00
|
|
|
"github.com/mymmrac/telego"
|
2024-05-13 19:08:46 +03:00
|
|
|
"golang.org/x/text/language"
|
2024-05-13 16:57:55 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Wizard struct {
|
2024-05-14 14:44:41 +03:00
|
|
|
UserID int64
|
|
|
|
ChatID int64
|
|
|
|
TGChat telego.Chat
|
|
|
|
User *model.User
|
|
|
|
Chat *model.Chat
|
|
|
|
Loc locale.Localizer
|
|
|
|
Data telego.Update
|
|
|
|
KeyClick util.Payload
|
|
|
|
IsMaster bool
|
2024-05-13 16:57:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type State struct {
|
|
|
|
fsm.State[Wizard]
|
|
|
|
App iface.App
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBase(app iface.App) State {
|
|
|
|
return State{App: app}
|
|
|
|
}
|
|
|
|
|
2024-05-13 19:08:46 +03:00
|
|
|
func (s State) Localizer(langCode ...string) locale.Localizer {
|
|
|
|
lang := language.English.String()
|
|
|
|
if len(langCode) > 0 {
|
|
|
|
lang = langCode[0]
|
2024-05-13 19:46:55 +03:00
|
|
|
} else {
|
2024-05-14 14:44:41 +03:00
|
|
|
if s.Payload != nil && s.Payload.User != nil {
|
2024-05-13 19:46:55 +03:00
|
|
|
lang = s.Payload.User.Language
|
2024-05-14 14:44:41 +03:00
|
|
|
} else if s.Payload != nil && s.Payload.UserID > 0 {
|
2024-05-13 19:46:55 +03:00
|
|
|
user, _ := s.App.DB().ForUser().ByTelegramID(s.Payload.UserID)
|
|
|
|
if user != nil && user.Language != "" {
|
|
|
|
lang = user.Language
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 19:08:46 +03:00
|
|
|
}
|
2024-05-14 14:44:41 +03:00
|
|
|
if s.Payload != nil && s.Payload.Loc != nil && (len(langCode) == 0 || s.hasLocalizerWithLocale(lang)) {
|
2024-05-13 19:08:46 +03:00
|
|
|
return s.Payload.Loc
|
|
|
|
}
|
2024-05-13 16:57:55 +03:00
|
|
|
lang = strings.ToLower(lang)
|
|
|
|
if len(lang) > 2 {
|
|
|
|
lang = lang[:2]
|
|
|
|
}
|
|
|
|
switch lang {
|
|
|
|
case "en", "ru":
|
|
|
|
return s.App.Localizer(lang)
|
|
|
|
default:
|
|
|
|
return s.App.Localizer("en")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 14:44:41 +03:00
|
|
|
func (s State) hasLocalizerWithLocale(lang string) bool {
|
|
|
|
if s.Payload.Loc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s.Payload.Loc.Tag().String() == lang
|
|
|
|
}
|
|
|
|
|
2024-05-13 16:57:55 +03:00
|
|
|
func (s State) LogError(err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.App.Conf().Debug {
|
|
|
|
s.App.Log().Errorf("handler error: %s, payload: %#v", err, s.Payload)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.App.Log().Errorf("handler error: %s, user id: %d, chat id: %d", err, s.Payload.UserID, s.Payload.ChatID)
|
|
|
|
}
|
2024-05-13 19:21:34 +03:00
|
|
|
|
2024-05-14 14:44:41 +03:00
|
|
|
func (s State) Move(mc fsm.MachineControls[Wizard], stateID fsm.StateID, pl *Wizard) {
|
2024-05-13 19:21:34 +03:00
|
|
|
s.LogError(mc.Move(stateID, pl))
|
|
|
|
}
|