You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
668 B
Go
38 lines
668 B
Go
package command
|
|
|
|
import (
|
|
pushPb "dcg/game/pb/push"
|
|
)
|
|
|
|
type HandlerFunc func(roomId int64, cmd string, user *pushPb.User)
|
|
|
|
type Manager struct {
|
|
handlers map[string]HandlerFunc
|
|
}
|
|
|
|
func NewManager() *Manager {
|
|
return &Manager{
|
|
handlers: make(map[string]HandlerFunc),
|
|
}
|
|
}
|
|
|
|
func (m *Manager) Register(h HandlerFunc, cmd string, alias ...string) {
|
|
if _, ok := m.handlers[cmd]; ok {
|
|
return
|
|
}
|
|
m.handlers[cmd] = h
|
|
// alias
|
|
for _, s := range alias {
|
|
if _, ok := m.handlers[cmd]; ok {
|
|
continue
|
|
}
|
|
m.handlers[s] = h
|
|
}
|
|
}
|
|
|
|
func (m *Manager) Handle(roomId int64, cmd string, user *pushPb.User) {
|
|
if h, ok := m.handlers[cmd]; ok {
|
|
h(roomId, cmd, user)
|
|
}
|
|
}
|