190 lines
4.4 KiB
Go

package commands
import (
"context"
"sync"
"eq2emu/internal/entity"
"eq2emu/internal/spawn"
)
// CommandType represents different types of commands
type CommandType int
const (
CommandTypePlayer CommandType = iota // Player commands (/say, /tell, etc.)
CommandTypeAdmin // Admin commands (/kick, /ban, etc.)
CommandTypeConsole // Console commands (shutdown, reload, etc.)
CommandTypeSpawn // Spawn manipulation commands
CommandTypeZone // Zone management commands
CommandTypeGuild // Guild commands
CommandTypeItem // Item commands
CommandTypeQuest // Quest commands
)
func (ct CommandType) String() string {
switch ct {
case CommandTypePlayer:
return "player"
case CommandTypeAdmin:
return "admin"
case CommandTypeConsole:
return "console"
case CommandTypeSpawn:
return "spawn"
case CommandTypeZone:
return "zone"
case CommandTypeGuild:
return "guild"
case CommandTypeItem:
return "item"
case CommandTypeQuest:
return "quest"
default:
return "unknown"
}
}
// CommandContext provides context for command execution
type CommandContext struct {
// Core context
Context context.Context
// Command information
CommandType CommandType
CommandName string
Arguments []string
RawArguments string
AdminLevel int
RequiredLevel int
// Game objects (nil if not applicable)
Client ClientInterface
Player *entity.Entity
Target *spawn.Spawn
Zone ZoneInterface
// Results and messages
Messages []CommandMessage
Results map[string]any
// Synchronization
mutex sync.RWMutex
}
// CommandMessage represents a message to send to the client
type CommandMessage struct {
Channel int
Color int
Message string
}
// CommandFunction represents a callable command function
type CommandFunction func(ctx *CommandContext) error
// Command represents a registered command
type Command struct {
Name string
Type CommandType
Description string
Usage string
RequiredLevel int
Handler CommandFunction
SubCommands map[string]*Command
}
// CommandManager manages command registration and execution
type CommandManager struct {
commands map[string]*Command
mutex sync.RWMutex
}
// ClientInterface defines the interface for client interactions
type ClientInterface interface {
GetPlayer() *entity.Entity
GetAccountID() int32
GetCharacterID() int32
GetAdminLevel() int
GetName() string
IsInZone() bool
GetZone() ZoneInterface
SendMessage(channel int, color int, message string)
SendPopupMessage(message string)
Disconnect()
}
// ZoneInterface defines the interface for zone interactions
type ZoneInterface interface {
GetID() int32
GetName() string
GetDescription() string
GetPlayers() []*entity.Entity
Shutdown()
SendZoneMessage(channel int, color int, message string)
GetSpawnByName(name string) *spawn.Spawn
GetSpawnByID(id int32) *spawn.Spawn
}
// Chat channel constants (from C++ Commands.h)
const (
ChannelAllText = 0
ChannelGameText = 1
ChannelDefault = 2
ChannelError = 3
ChannelStatus = 4
ChannelMOTD = 5
ChannelChatText = 6
ChannelNearbyCHat = 7
ChannelSay = 8
ChannelShout = 9
ChannelEmote = 10
ChannelYell = 11
ChannelNarrative = 12
ChannelNonPlayerSay = 13
ChannelGroupChat = 14
ChannelGroupSay = 15
ChannelRaidSay = 16
ChannelGuildChat = 17
ChannelGuildSay = 18
ChannelOfficerSay = 19
ChannelGuildMOTD = 20
ChannelPrivateChat = 25
ChannelPrivateTell = 28
ChannelOutOfCharacter = 32
ChannelBroadcast = 92
ChannelWho = 93
ChannelCommands = 94
)
// Chat color constants (from C++ Commands.h)
const (
ColorRed = 3
ColorChatRelation = 4
ColorYellow = 5
ColorNewLoot = 84
ColorNewestLoot = 89
ColorWhite = 254
)
// Admin level constants
const (
AdminLevelPlayer = 0
AdminLevelGuide = 100
AdminLevelGM = 200
AdminLevelAdmin = 300
)
// ErrorHandler defines how command errors should be handled
type ErrorHandler interface {
HandleCommandError(ctx *CommandContext, err error)
}
// CommandLogger defines logging interface for commands
type CommandLogger interface {
LogCommand(ctx *CommandContext, success bool, err error)
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}