vegapokerbot/internal/handler/wizard/init.go
Neur0toxine 37b638712c
All checks were successful
continuous-integration/drone/push Build is passing
wip: moving setup logic to fsm
2024-05-13 16:57:55 +03:00

49 lines
1.0 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),
NewHelpState(app),
}
errorState = NewErrorState(app.Log())
}