vegapokerbot/internal/handler/wizard/init.go
Neur0toxine 77eaceb152
All checks were successful
continuous-integration/drone/push Build is passing
move more logic to fsm
2024-05-13 17:18:22 +03:00

52 lines
1.1 KiB
Go

package wizard
import (
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/fsm"
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
"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
}
func Get(chatID int64) fsm.IMachine[Wizard] {
if machine, ok := wizards.Get(chatID); ok {
return machine
}
machine := newWizard()
wizards.Set(chatID, machine)
return machine
}
func newWizard() fsm.IMachine[Wizard] {
return fsm.New[Wizard](states[0].ID(), Wizard{}, states, errorState)
}
func PopulateStates(app iface.App) {
states = []fsm.IState[Wizard]{
NewRegisterState(app),
NewWaitingForMemberWebhookState(app),
NewAddChatMemberState(app),
NewRemoveChatMemberState(app),
NewHelpState(app),
}
errorState = NewErrorState(app.Log())
}