MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / TryPetCommand

Function TryPetCommand

internal/scripting/pet.go:89–167  ·  view source on GitHub ↗

TryPetCommand checks whether the pet script intercepts a command typed by its owner.

(cmd string, rest string, userId int)

Source from the content-addressed store, hash-verified

87
88// TryPetCommand checks whether the pet script intercepts a command typed by its owner.
89func TryPetCommand(cmd string, rest string, userId int) (bool, error) {
90
91 user := users.GetByUserId(userId)
92 if user == nil {
93 return false, fmt.Errorf("user not found")
94 }
95
96 if !user.Character.Pet.Exists() {
97 return false, ErrEventNotFound
98 }
99
100 sPet := GetPet(&user.Character.Pet, userId)
101 if sPet == nil {
102 return false, ErrEventNotFound
103 }
104
105 vmw, err := getPetVM(sPet)
106 if err != nil {
107 return false, err
108 }
109
110 timestart := time.Now()
111 defer func() {
112 mudlog.Debug("TryPetCommand()", "cmd", cmd, "petType", sPet.Type(), "userId", userId, "time", time.Since(timestart))
113 }()
114
115 sActor := GetActor(userId, 0)
116 sRoom := GetRoom(sActor.GetRoomId())
117
118 if onCommandFunc, ok := vmw.GetFunction(`onCommand_` + cmd); ok {
119
120 userTextWrap.Set(`script-text`, ``, ``)
121 roomTextWrap.Set(`script-text`, ``, ``)
122
123 res, err := runCallable(vmw, scriptPetTimeout, onCommandFunc,
124 vmw.VM.ToValue(rest),
125 vmw.VM.ToValue(sPet),
126 vmw.VM.ToValue(sActor),
127 vmw.VM.ToValue(sRoom),
128 )
129
130 userTextWrap.Reset()
131 roomTextWrap.Reset()
132
133 if err != nil {
134 return false, fmt.Errorf("onCommand_%s(): %w", cmd, err)
135 }
136
137 if boolVal, ok := res.Export().(bool); ok {
138 return boolVal, nil
139 }
140
141 } else if onCommandFunc, ok := vmw.GetFunction(`onCommand`); ok {
142
143 userTextWrap.Set(`script-text`, ``, ``)
144 roomTextWrap.Set(`script-text`, ``, ``)
145
146 res, err := runCallable(vmw, scriptPetTimeout, onCommandFunc,

Callers 1

TryCommandFunction · 0.92

Calls 13

GetByUserIdFunction · 0.92
DebugFunction · 0.92
GetPetFunction · 0.85
getPetVMFunction · 0.85
GetActorFunction · 0.85
GetRoomFunction · 0.85
runCallableFunction · 0.85
GetFunctionMethod · 0.80
TypeMethod · 0.65
SetMethod · 0.65
ExistsMethod · 0.45
GetRoomIdMethod · 0.45

Tested by

no test coverage detected