TryPetCommand checks whether the pet script intercepts a command typed by its owner.
(cmd string, rest string, userId int)
| 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) { |
| 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, |
no test coverage detected