vegapokerbot/internal/handler/fsmwizard/init.go

54 lines
1.2 KiB
Go
Raw Normal View History

package fsmwizard
2024-05-13 16:57:55 +03:00
import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
2024-05-13 16:57:55 +03:00
"github.com/maypok86/otter"
"time"
)
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] {
return fsm.New[Wizard](states[0].ID(), Wizard{}, states, errorState)
}
// 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),
NewKeyboardChooserState(app),
2024-05-13 17:18:22 +03:00
NewRemoveChatMemberState(app),
2024-05-13 16:57:55 +03:00
NewHelpState(app),
}
errorState = NewErrorState(app.Log())
}