eq2go/internal/events/context.go

194 lines
4.8 KiB
Go

package events
import (
"context"
"fmt"
"eq2emu/internal/entity"
"eq2emu/internal/quests"
"eq2emu/internal/spawn"
)
// NewEventContext creates a new event context
func NewEventContext(eventType EventType, eventName, functionName string) *EventContext {
return &EventContext{
Context: context.Background(),
EventType: eventType,
EventName: eventName,
FunctionName: functionName,
Parameters: make(map[string]any),
Results: make(map[string]any),
}
}
// WithSpawn adds a spawn to the context
func (ctx *EventContext) WithSpawn(spawn *spawn.Spawn) *EventContext {
ctx.Spawn = spawn
return ctx
}
// WithCaster adds a caster to the context
func (ctx *EventContext) WithCaster(caster *entity.Entity) *EventContext {
ctx.Caster = caster
return ctx
}
// WithTarget adds a target to the context
func (ctx *EventContext) WithTarget(target *entity.Entity) *EventContext {
ctx.Target = target
return ctx
}
// WithQuest adds a quest to the context
func (ctx *EventContext) WithQuest(quest *quests.Quest) *EventContext {
ctx.Quest = quest
return ctx
}
// WithParameter adds a parameter to the context
func (ctx *EventContext) WithParameter(name string, value any) *EventContext {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
ctx.Parameters[name] = value
return ctx
}
// WithParameters adds multiple parameters to the context
func (ctx *EventContext) WithParameters(params map[string]any) *EventContext {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
for k, v := range params {
ctx.Parameters[k] = v
}
return ctx
}
// GetSpawn returns the spawn from context
func (ctx *EventContext) GetSpawn() *spawn.Spawn {
return ctx.Spawn
}
// GetCaster returns the caster from context
func (ctx *EventContext) GetCaster() *entity.Entity {
return ctx.Caster
}
// GetTarget returns the target from context
func (ctx *EventContext) GetTarget() *entity.Entity {
return ctx.Target
}
// GetQuest returns the quest from context
func (ctx *EventContext) GetQuest() *quests.Quest {
return ctx.Quest
}
// GetParameter retrieves a parameter
func (ctx *EventContext) GetParameter(name string) (any, bool) {
ctx.mutex.RLock()
defer ctx.mutex.RUnlock()
value, exists := ctx.Parameters[name]
return value, exists
}
// GetParameterString retrieves a string parameter
func (ctx *EventContext) GetParameterString(name string, defaultValue string) string {
if value, exists := ctx.GetParameter(name); exists {
if str, ok := value.(string); ok {
return str
}
}
return defaultValue
}
// GetParameterInt retrieves an integer parameter
func (ctx *EventContext) GetParameterInt(name string, defaultValue int) int {
if value, exists := ctx.GetParameter(name); exists {
switch v := value.(type) {
case int:
return v
case int32:
return int(v)
case int64:
return int(v)
case float32:
return int(v)
case float64:
return int(v)
}
}
return defaultValue
}
// GetParameterFloat retrieves a float parameter
func (ctx *EventContext) GetParameterFloat(name string, defaultValue float64) float64 {
if value, exists := ctx.GetParameter(name); exists {
switch v := value.(type) {
case float64:
return v
case float32:
return float64(v)
case int:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
}
}
return defaultValue
}
// GetParameterBool retrieves a boolean parameter
func (ctx *EventContext) GetParameterBool(name string, defaultValue bool) bool {
if value, exists := ctx.GetParameter(name); exists {
if b, ok := value.(bool); ok {
return b
}
}
return defaultValue
}
// SetResult sets a result value
func (ctx *EventContext) SetResult(name string, value any) {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
if ctx.Results == nil {
ctx.Results = make(map[string]any)
}
ctx.Results[name] = value
}
// GetResult retrieves a result value
func (ctx *EventContext) GetResult(name string) (any, bool) {
ctx.mutex.RLock()
defer ctx.mutex.RUnlock()
value, exists := ctx.Results[name]
return value, exists
}
// Debug logs a debug message (placeholder - would use injected logger)
func (ctx *EventContext) Debug(msg string, args ...any) {
fmt.Printf("[DEBUG] [%s:%s] %s\n", ctx.EventName, ctx.FunctionName, fmt.Sprintf(msg, args...))
}
// Info logs an info message (placeholder - would use injected logger)
func (ctx *EventContext) Info(msg string, args ...any) {
fmt.Printf("[INFO] [%s:%s] %s\n", ctx.EventName, ctx.FunctionName, fmt.Sprintf(msg, args...))
}
// Warn logs a warning message (placeholder - would use injected logger)
func (ctx *EventContext) Warn(msg string, args ...any) {
fmt.Printf("[WARN] [%s:%s] %s\n", ctx.EventName, ctx.FunctionName, fmt.Sprintf(msg, args...))
}
// Error logs an error message (placeholder - would use injected logger)
func (ctx *EventContext) Error(msg string, args ...any) {
fmt.Printf("[ERROR] [%s:%s] %s\n", ctx.EventName, ctx.FunctionName, fmt.Sprintf(msg, args...))
}