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

Function TryPetScriptEvent

internal/scripting/pet.go:33–86  ·  view source on GitHub ↗

TryPetScriptEvent fires a named event on the pet script (e.g. "onCommand", "PetAct"). userId is the owner of the pet.

(eventName string, userId int)

Source from the content-addressed store, hash-verified

31// TryPetScriptEvent fires a named event on the pet script (e.g. "onCommand", "PetAct").
32// userId is the owner of the pet.
33func TryPetScriptEvent(eventName string, userId int) (bool, error) {
34
35 user := users.GetByUserId(userId)
36 if user == nil {
37 return false, fmt.Errorf("user not found")
38 }
39
40 if !user.Character.Pet.Exists() {
41 return false, fmt.Errorf("user has no pet")
42 }
43
44 sPet := GetPet(&user.Character.Pet, userId)
45 if sPet == nil {
46 return false, fmt.Errorf("pet not found")
47 }
48
49 vmw, err := getPetVM(sPet)
50 if err != nil {
51 return false, err
52 }
53
54 timestart := time.Now()
55 defer func() {
56 mudlog.Debug("TryPetScriptEvent()", "eventName", eventName, "petType", sPet.Type(), "time", time.Since(timestart))
57 }()
58
59 if onFunc, ok := vmw.GetFunction(eventName); ok {
60
61 sActor := GetActor(userId, 0)
62 sRoom := GetRoom(sActor.GetRoomId())
63
64 userTextWrap.Set(`script-text`, ``, ``)
65 roomTextWrap.Set(`script-text`, ``, ``)
66
67 res, err := runCallable(vmw, scriptPetTimeout, onFunc,
68 vmw.VM.ToValue(sPet),
69 vmw.VM.ToValue(sActor),
70 vmw.VM.ToValue(sRoom),
71 )
72
73 userTextWrap.Reset()
74 roomTextWrap.Reset()
75
76 if err != nil {
77 return false, fmt.Errorf("%s(): %w", eventName, err)
78 }
79
80 if boolVal, ok := res.Export().(bool); ok {
81 return boolVal, nil
82 }
83 }
84
85 return false, ErrEventNotFound
86}
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) {

Callers 2

UserRoundTickFunction · 0.92
GoMissingMethod · 0.85

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