2024-05-13 19:21:34 +03:00
|
|
|
package fsmwizard
|
2024-05-13 16:57:55 +03:00
|
|
|
|
|
|
|
import (
|
2024-05-13 21:46:05 +03:00
|
|
|
"time"
|
|
|
|
|
2024-05-13 16:57:55 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
2024-05-13 21:46:05 +03:00
|
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
|
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/maypok86/otter"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
wizards otter.Cache[int64, fsm.IMachine[Wizard]]
|
|
|
|
states []fsm.IState[Wizard]
|
|
|
|
errorState *ErrorState
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
storage, err := otter.MustBuilder[int64, fsm.IMachine[Wizard]](1000).
|
|
|
|
Cost(func(key int64, value fsm.IMachine[Wizard]) uint32 {
|
|
|
|
return 1
|
|
|
|
}).
|
|
|
|
WithTTL(time.Hour * 24 * 7).
|
|
|
|
Build()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
wizards = storage
|
|
|
|
}
|
|
|
|
|
2024-05-13 19:46:55 +03:00
|
|
|
func Get(userID int64) fsm.IMachine[Wizard] {
|
|
|
|
if machine, ok := wizards.Get(userID); ok {
|
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-13 21:46:05 +03:00
|
|
|
return fsm.New[Wizard](states[0].ID(), Wizard{}, wizardPreHandle, states, errorState)
|
2024-05-13 16:57:55 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 19:21:34 +03:00
|
|
|
// PopulateStates will init all state handlers for future use.
|
2024-05-13 16:57:55 +03:00
|
|
|
func PopulateStates(app iface.App) {
|
|
|
|
states = []fsm.IState[Wizard]{
|
|
|
|
NewRegisterState(app),
|
2024-05-13 17:18:22 +03:00
|
|
|
NewWaitingForMemberWebhookState(app),
|
|
|
|
NewAddChatMemberState(app),
|
2024-05-13 19:08:46 +03:00
|
|
|
NewKeyboardChooserState(app),
|
2024-05-13 17:18:22 +03:00
|
|
|
NewRemoveChatMemberState(app),
|
2024-05-13 16:57:55 +03:00
|
|
|
NewHelpState(app),
|
2024-05-13 21:46:05 +03:00
|
|
|
NewUnknownCommandState(app),
|
2024-05-13 16:57:55 +03:00
|
|
|
}
|
|
|
|
errorState = NewErrorState(app.Log())
|
|
|
|
}
|
2024-05-13 21:46:05 +03:00
|
|
|
|
|
|
|
func wizardPreHandle(w *Wizard, mc fsm.MachineControls[*Wizard]) {
|
|
|
|
if w.Data.Message != nil {
|
|
|
|
switch {
|
|
|
|
case util.MatchCommand("start", w.Data.Message):
|
|
|
|
mc.Move(RegisterStateID, w)
|
|
|
|
case util.MatchCommand("help", w.Data.Message):
|
|
|
|
mc.Move(HelpStateID, w)
|
|
|
|
case util.HasCommand(w.Data.Message):
|
|
|
|
mc.Move(UnknownCommandStateID, w)
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Data.MyChatMember != nil {
|
|
|
|
mc.Move(WaitingForMemberWebhookStateID, w)
|
|
|
|
}
|
|
|
|
}
|