2023-08-10 15:49:25 +03:00
|
|
|
package tx
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2023-08-16 17:25:56 +03:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
2023-08-12 15:54:05 +03:00
|
|
|
|
2023-08-16 17:25:56 +03:00
|
|
|
// Jsonable Action.
|
|
|
|
type action struct {
|
|
|
|
Type string
|
|
|
|
Action Action
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAction(a Action) *action {
|
|
|
|
typ, ok := actionMapByReflect[reflect.TypeOf(a)]
|
|
|
|
if !ok {
|
|
|
|
panic(ActionNotDefinedErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &action{
|
|
|
|
Type: typ,
|
|
|
|
Action: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *action) Act(arg *A) {
|
|
|
|
if a.Action != nil {
|
|
|
|
a.Action.Act(arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Customized actions for the bot.
|
2023-08-13 15:37:36 +03:00
|
|
|
type Action interface {
|
|
|
|
Act(*Arg)
|
|
|
|
}
|
|
|
|
|
2023-08-16 17:25:56 +03:00
|
|
|
// Customized actions for the
|
2023-08-13 15:37:36 +03:00
|
|
|
type GroupAction interface {
|
|
|
|
Act(*GroupArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ActionFunc func(*Arg)
|
|
|
|
|
|
|
|
func (af ActionFunc) Act(a *Arg) {
|
|
|
|
af(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
type GroupActionFunc func(*GroupArg)
|
|
|
|
|
|
|
|
func (af GroupActionFunc) Act(a *GroupArg) {
|
|
|
|
af(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type implements changing screen to the underlying ScreenId
|
|
|
|
type ScreenChange ScreenId
|
|
|
|
|
|
|
|
func (sc ScreenChange) Act(c *Arg) {
|
|
|
|
if !c.B.behaviour.ScreenExist(ScreenId(sc)) {
|
|
|
|
panic(ScreenNotExistErr)
|
|
|
|
}
|
|
|
|
err := c.ChangeScreen(ScreenId(sc))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 15:54:05 +03:00
|
|
|
|
|
|
|
// The argument for handling.
|
|
|
|
type Arg struct {
|
|
|
|
// Current context.
|
|
|
|
*Context
|
|
|
|
// The update that made the action to be called.
|
|
|
|
U *Update
|
|
|
|
}
|
|
|
|
type A = Arg
|
|
|
|
|
2023-08-13 15:37:36 +03:00
|
|
|
// Changes screen of user to the Id one.
|
|
|
|
func (c *Arg) ChangeScreen(screenId ScreenId) error {
|
|
|
|
if !c.B.behaviour.ScreenExist(screenId) {
|
|
|
|
return ScreenNotExistErr
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// Stop the reading by sending the nil,
|
|
|
|
// since we change the screen and
|
|
|
|
// current goroutine needs to be stopped.
|
2023-08-13 15:37:36 +03:00
|
|
|
if c.readingUpdate {
|
|
|
|
c.updates <- nil
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// Getting the screen and changing to
|
|
|
|
// then executing its action.
|
2023-08-13 15:37:36 +03:00
|
|
|
screen := c.B.behaviour.Screens[screenId]
|
2023-08-15 16:02:14 +03:00
|
|
|
c.prevScreen = c.curScreen
|
|
|
|
c.curScreen = screen
|
2023-08-13 15:37:36 +03:00
|
|
|
screen.Render(c.Context)
|
|
|
|
if screen.Action != nil {
|
|
|
|
c.run(screen.Action, c.U)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The argument for handling in group behaviour.
|
2023-08-12 15:54:05 +03:00
|
|
|
type GroupArg struct {
|
2023-08-13 15:37:36 +03:00
|
|
|
*GroupContext
|
|
|
|
*Update
|
2023-08-12 15:54:05 +03:00
|
|
|
}
|
|
|
|
type GA = GroupArg
|
|
|
|
|
2023-08-13 15:37:36 +03:00
|
|
|
func (a *GA) SentFromSid() SessionId {
|
|
|
|
return SessionId(a.SentFrom().ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *GA) GetSessionValue() any {
|
|
|
|
v, _ := a.B.GetSessionValueBySid(a.SentFromSid())
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// The argument for handling in channenl behaviours.
|
|
|
|
type ChannelArg struct {
|
|
|
|
}
|
|
|
|
type CA = ChannelArg
|
|
|
|
type ChannelAction struct {
|
|
|
|
Act (*ChannelArg)
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:06:23 +03:00
|
|
|
type JsonTyper interface {
|
|
|
|
JsonType() string
|
|
|
|
}
|