86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package commands
|
|
|
|
import "fmt"
|
|
|
|
// InitializeCommands initializes and registers all command types
|
|
func InitializeCommands() (*CommandManager, error) {
|
|
cm := NewCommandManager()
|
|
|
|
// Register all command types
|
|
if err := RegisterPlayerCommands(cm); err != nil {
|
|
return nil, fmt.Errorf("failed to register player commands: %w", err)
|
|
}
|
|
|
|
if err := RegisterAdminCommands(cm); err != nil {
|
|
return nil, fmt.Errorf("failed to register admin commands: %w", err)
|
|
}
|
|
|
|
if err := RegisterConsoleCommands(cm); err != nil {
|
|
return nil, fmt.Errorf("failed to register console commands: %w", err)
|
|
}
|
|
|
|
return cm, nil
|
|
}
|
|
|
|
// GetCommandHelp returns help text for a specific command
|
|
func GetCommandHelp(cm *CommandManager, commandName string, adminLevel int) string {
|
|
command, exists := cm.GetCommand(commandName)
|
|
if !exists {
|
|
return fmt.Sprintf("Command '%s' not found", commandName)
|
|
}
|
|
|
|
if adminLevel < command.RequiredLevel {
|
|
return fmt.Sprintf("Command '%s' not available at your admin level", commandName)
|
|
}
|
|
|
|
help := fmt.Sprintf("Command: %s\n", command.Name)
|
|
help += fmt.Sprintf("Usage: %s\n", command.Usage)
|
|
help += fmt.Sprintf("Description: %s\n", command.Description)
|
|
help += fmt.Sprintf("Required Admin Level: %d\n", command.RequiredLevel)
|
|
help += fmt.Sprintf("Type: %s\n", command.Type.String())
|
|
|
|
if len(command.SubCommands) > 0 {
|
|
help += "\nSubcommands:\n"
|
|
for name, subCmd := range command.SubCommands {
|
|
if adminLevel >= subCmd.RequiredLevel {
|
|
help += fmt.Sprintf(" %s - %s\n", name, subCmd.Description)
|
|
}
|
|
}
|
|
}
|
|
|
|
return help
|
|
}
|
|
|
|
// GetAvailableCommands returns a list of commands available to the given admin level
|
|
func GetAvailableCommands(cm *CommandManager, adminLevel int) []string {
|
|
allCommands := cm.ListCommands()
|
|
available := make([]string, 0)
|
|
|
|
for _, cmdName := range allCommands {
|
|
if command, exists := cm.GetCommand(cmdName); exists {
|
|
if adminLevel >= command.RequiredLevel {
|
|
available = append(available, cmdName)
|
|
}
|
|
}
|
|
}
|
|
|
|
return available
|
|
}
|
|
|
|
// ExecuteCommand is a convenience function that parses and executes a command
|
|
func ExecuteCommand(cm *CommandManager, rawCommand string, client ClientInterface) error {
|
|
ctx := cm.ParseCommand(rawCommand, client)
|
|
if ctx == nil {
|
|
return fmt.Errorf("failed to parse command")
|
|
}
|
|
|
|
err := cm.Execute(ctx)
|
|
if err != nil {
|
|
ctx.AddErrorMessage(err.Error())
|
|
}
|
|
|
|
// Send all messages to client
|
|
ctx.SendMessages()
|
|
return err
|
|
}
|