package fsmwizard import ( "time" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm" "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 } func Get(userID int64) fsm.IMachine[Wizard] { if machine, ok := wizards.Get(userID); ok { return machine } machine := newWizard() wizards.Set(userID, machine) return machine } func newWizard() fsm.IMachine[Wizard] { return fsm.New[Wizard](states[0].ID(), Wizard{}, wizardPreHandle, states, errorState) } // PopulateStates will init all state handlers for future use. func PopulateStates(app iface.App) { states = []fsm.IState[Wizard]{ NewRegisterState(app), NewWaitingForMemberWebhookState(app), NewAddChatMemberState(app), NewKeyboardChooserState(app), NewRemoveChatMemberState(app), NewHelpState(app), NewUnknownCommandState(app), } errorState = NewErrorState(app.Log()) } 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) } }