Neur0toxine
68f2975201
All checks were successful
continuous-integration/drone/push Build is passing
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package wizard
|
|
|
|
import (
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
|
|
"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),
|
|
NewKeyboardChooserState(app),
|
|
NewRemoveChatMemberState(app),
|
|
NewHelpState(app),
|
|
}
|
|
errorState = NewErrorState(app.Log())
|
|
}
|