FlameInTheDark /
dtbot
| 1 | package bot |
||
| 2 | |||
| 3 | type ( |
||
| 4 | // Command : Executable command function |
||
| 5 | Command func(Context) |
||
| 6 | |||
| 7 | // CmdMap : Map with executable functions |
||
| 8 | CmdMap map[string]Command |
||
| 9 | |||
| 10 | // CommandHandler : Command handler struct |
||
| 11 | CommandHandler struct { |
||
| 12 | cmds CmdMap |
||
| 13 | } |
||
| 14 | ) |
||
| 15 | |||
| 16 | // NewCommandHandler creates command handler |
||
| 17 | func NewCommandHandler() *CommandHandler { |
||
| 18 | return &CommandHandler{make(CmdMap)} |
||
| 19 | } |
||
| 20 | |||
| 21 | // GetCmds returns handler commands |
||
| 22 | func (handler CommandHandler) GetCmds() CmdMap { |
||
| 23 | return handler.cmds |
||
| 24 | } |
||
| 25 | |||
| 26 | // Get returns command by command name |
||
| 27 | func (handler CommandHandler) Get(name string) (*Command, bool) { |
||
| 28 | cmd, found := handler.cmds[name] |
||
| 29 | return &cmd, found |
||
| 30 | } |
||
| 31 | |||
| 32 | // Register adds new command in handler |
||
| 33 | func (handler CommandHandler) Register(name string, command Command) { |
||
| 34 | handler.cmds[name] = command |
||
| 35 | if len(name) > 1 { |
||
| 36 | handler.cmds[name[:1]] = command |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | |||
| 41 | // New command handler |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 42 | type NodeTree struct { |
||
| 43 | Elements map[string]*NodeElement |
||
| 44 | } |
||
| 45 | |||
| 46 | type NodeElement struct { |
||
|
0 ignored issues
–
show
|
|||
| 47 | Current string |
||
| 48 | Elements map[string]*NodeElement |
||
| 49 | Workers []NodeWorker |
||
| 50 | } |
||
| 51 | |||
| 52 | // NodeWorker contains workers of command |
||
| 53 | type NodeWorker struct { |
||
| 54 | CommandWorker func(Context) |
||
| 55 | Middlewares []func(*Context) bool |
||
| 56 | } |
||
| 57 | |||
| 58 | // CommandSignature contains data of command |
||
| 59 | type CommandSignature struct { |
||
| 60 | Path []string |
||
| 61 | Command func(Context) |
||
| 62 | Middlewares []func(*Context) bool |
||
| 63 | } |
||
| 64 | |||
| 65 | // NodeTree creates new node tree |
||
|
0 ignored issues
–
show
|
|||
| 66 | func NewTree() NodeTree { |
||
| 67 | return NodeTree{Elements:make(map[string]*NodeElement)} |
||
| 68 | } |
||
| 69 | |||
| 70 | func (n *NodeElement) AddElement(element string) { |
||
|
0 ignored issues
–
show
|
|||
| 71 | if n.Elements == nil { |
||
| 72 | n.Elements = make(map[string]*NodeElement) |
||
| 73 | } |
||
| 74 | n.Elements[element] = &NodeElement{Current:element, Elements:make(map[string]*NodeElement)} |
||
| 75 | } |
||
| 76 | |||
| 77 | func (n *NodeElement) GetElement(element string) *NodeElement { |
||
|
0 ignored issues
–
show
|
|||
| 78 | if n.Elements == nil { |
||
| 79 | return nil |
||
| 80 | } |
||
| 81 | return n.Elements[element] |
||
| 82 | } |
||
| 83 | |||
| 84 | func (t *NodeTree) Execute(ctx Context) { |
||
|
0 ignored issues
–
show
|
|||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | func (t *NodeTree) GetElement(element string) *NodeElement { |
||
|
0 ignored issues
–
show
|
|||
| 89 | if t.Elements == nil { |
||
| 90 | return nil |
||
| 91 | } |
||
| 92 | return t.Elements[element] |
||
| 93 | } |
||
| 94 | |||
| 95 | func (t *NodeTree) AddCommand(command *CommandSignature, ) { |
||
|
0 ignored issues
–
show
|
|||
| 96 | if len(command.Path) == 0 { |
||
| 97 | return |
||
| 98 | } |
||
| 99 | var element = t.GetElement(command.Path[0]) |
||
| 100 | if len(command.Path) > 1 { |
||
| 101 | for _, c := range command.Path[1:] { |
||
| 102 | element = element.GetElement(c) |
||
| 103 | } |
||
| 104 | } |
||
| 105 | element.Workers = append(element.Workers, NodeWorker{CommandWorker:command.Command, Middlewares:command.Middlewares}) |
||
| 106 | } |
||
| 107 | |||
| 108 | func (w *NodeWorker) CheckMiddlewares(ctx *Context) bool { |
||
|
0 ignored issues
–
show
|
|||
| 109 | for _, m := range w.Middlewares { |
||
| 110 | if m(ctx) == false { |
||
| 111 | return false |
||
| 112 | } |
||
| 113 | } |
||
| 114 | return true |
||
| 115 | } |
||
| 116 |