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.

37 lines
577 B
Go

package command
import (
"dcg/game/pb"
"strings"
)
type HandlerFunc func(cmd string, user *pb.User)
type Manager struct {
handlers map[string]HandlerFunc
}
func NewManager() *Manager {
return &Manager{
handlers: make(map[string]HandlerFunc),
}
}
func (m *Manager) Register(cmd string, h HandlerFunc) {
if _, ok := m.handlers[cmd]; ok {
return
}
m.handlers[cmd] = h
}
func (m *Manager) Handle(cmd string, user *pb.User) {
if len(cmd) < 0 {
return
}
fChar := cmd[0]
c := strings.ToLower(string(fChar))
if h, ok := m.handlers[c]; ok {
h(cmd, user)
}
}