TryPetScriptEvent fires a named event on the pet script (e.g. "onCommand", "PetAct"). userId is the owner of the pet.
(eventName string, userId int)
| 31 | // TryPetScriptEvent fires a named event on the pet script (e.g. "onCommand", "PetAct"). |
| 32 | // userId is the owner of the pet. |
| 33 | func 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. |
| 89 | func TryPetCommand(cmd string, rest string, userId int) (bool, error) { |
no test coverage detected