vegapokerbot/internal/handler/fsmwizard/init.go

100 lines
2.3 KiB
Go
Raw Normal View History

package fsmwizard
2024-05-13 16:57:55 +03:00
import (
2024-05-14 14:44:41 +03:00
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/types"
"time"
2024-05-13 16:57:55 +03:00
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
2024-05-13 16:57:55 +03:00
)
var (
2024-05-14 14:44:41 +03:00
wizards *types.TTLMap[int64, fsm.IMachine[Wizard]]
2024-05-13 16:57:55 +03:00
states []fsm.IState[Wizard]
errorState *ErrorState
2024-05-14 14:44:41 +03:00
appPointer iface.App
2024-05-13 16:57:55 +03:00
)
func init() {
2024-05-14 14:44:41 +03:00
wizards = types.NewTTLMap[int64, fsm.IMachine[Wizard]](time.Hour * 24 * 7)
2024-05-13 16:57:55 +03:00
}
2024-05-13 19:46:55 +03:00
func Get(userID int64) fsm.IMachine[Wizard] {
2024-05-14 14:44:41 +03:00
if machine, ok := wizards.Get(userID); ok && machine != nil {
2024-05-13 16:57:55 +03:00
return machine
}
machine := newWizard()
2024-05-13 19:46:55 +03:00
wizards.Set(userID, machine)
2024-05-13 16:57:55 +03:00
return machine
}
func newWizard() fsm.IMachine[Wizard] {
2024-05-14 14:44:41 +03:00
return fsm.New[Wizard](states[0].ID(), wizardRouter, states, errorState)
2024-05-13 16:57:55 +03:00
}
// PopulateStates will init all state handlers for future use.
2024-05-13 16:57:55 +03:00
func PopulateStates(app iface.App) {
2024-05-14 14:44:41 +03:00
appPointer = app
2024-05-13 16:57:55 +03:00
states = []fsm.IState[Wizard]{
NewRegisterState(app),
2024-05-13 17:18:22 +03:00
NewWaitingForMemberWebhookState(app),
NewAddChatMemberState(app),
NewKeyboardChooserState(app),
2024-05-14 14:44:41 +03:00
NewConfigureRedmineQueryState(app),
2024-05-13 17:18:22 +03:00
NewRemoveChatMemberState(app),
2024-05-13 16:57:55 +03:00
NewHelpState(app),
NewUnknownCommandState(app),
2024-05-13 16:57:55 +03:00
}
errorState = NewErrorState(app.Log())
}
2024-05-14 14:44:41 +03:00
func wizardRouter(w *Wizard, mc fsm.MachineControlsWithState[Wizard]) {
if w.Data.Message != nil {
2024-05-14 14:44:41 +03:00
if w.UserID == 0 {
w.UserID = w.Data.Message.From.ID
}
if w.ChatID == 0 {
w.ChatID = w.Data.Message.Chat.ID
}
if w.User == nil {
user, err := appPointer.DB().ForUser().ByTelegramID(w.Data.Message.From.ID)
if err != nil {
return
}
if user != nil && user.ID > 0 {
w.User = user
}
}
if w.Chat == nil {
chat, err := appPointer.DB().ForChat().ByTelegramID(w.Data.Message.Chat.ID)
if err != nil {
return
}
if chat != nil && chat.ID > 0 {
w.Chat = chat
}
}
if w.TGChat.ID == 0 {
w.TGChat = w.Data.Message.Chat
}
switch {
case util.MatchCommand("start", w.Data.Message):
2024-05-14 14:44:41 +03:00
_ = mc.Move(RegisterStateID, w)
case util.MatchCommand("help", w.Data.Message):
2024-05-14 14:44:41 +03:00
_ = mc.Move(HelpStateID, w)
case util.HasCommand(w.Data.Message):
2024-05-14 14:44:41 +03:00
_ = mc.Move(UnknownCommandStateID, w)
default:
return
}
}
if w.Data.MyChatMember != nil {
2024-05-14 14:44:41 +03:00
_ = mc.Move(WaitingForMemberWebhookStateID, w)
}
}