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

Function TryRoomCommand

internal/scripting/room.go:223–319  ·  view source on GitHub ↗
(cmd string, rest string, userId int)

Source from the content-addressed store, hash-verified

221}
222
223func TryRoomCommand(cmd string, rest string, userId int) (bool, error) {
224
225 user := users.GetByUserId(userId)
226 if user == nil {
227 return false, errors.New("user not found")
228 }
229
230 room := rooms.LoadRoom(user.Character.RoomId)
231 if room == nil {
232 return false, fmt.Errorf("room %d not found", user.Character.RoomId)
233 }
234
235 altCmd, _ := room.FindExitByName(cmd)
236
237 if room != nil {
238
239 for _, mobInstanceId := range room.GetMobs() {
240 if handled, err := TryMobCommand(cmd, rest, mobInstanceId, userId, `user`); err == nil {
241 if handled {
242 return true, nil
243 }
244 }
245
246 }
247 }
248
249 vmw, err := getRoomVM(user.Character.RoomId)
250 if err != nil {
251 return false, err
252 }
253
254 timestart := time.Now()
255 defer func() {
256 mudlog.Debug("TryRoomCommand()", "cmd", cmd, "roomId", user.Character.RoomId, "time", time.Since(timestart))
257 }()
258
259 onCommandFunc, cmdFound := vmw.GetFunction(`onCommand_` + cmd)
260 if !cmdFound && altCmd != `` {
261 onCommandFunc, cmdFound = vmw.GetFunction(`onCommand_` + altCmd)
262 }
263
264 if cmdFound {
265
266 // Set forced ansi tag wrappers
267 userTextWrap.Set(`script-text`, ``, ``)
268 roomTextWrap.Set(`script-text`, ``, ``)
269
270 sUser := GetUser(userId)
271 sRoom := GetRoom(user.Character.RoomId)
272
273 res, err := runCallable(vmw, scriptRoomTimeout, onCommandFunc,
274 vmw.VM.ToValue(rest),
275 vmw.VM.ToValue(sUser),
276 vmw.VM.ToValue(sRoom),
277 )
278
279 userTextWrap.Reset()
280 roomTextWrap.Reset()

Calls 14

GetByUserIdFunction · 0.92
LoadRoomFunction · 0.92
DebugFunction · 0.92
TryMobCommandFunction · 0.85
getRoomVMFunction · 0.85
GetUserFunction · 0.85
GetRoomFunction · 0.85
runCallableFunction · 0.85
NewMethod · 0.80
FindExitByNameMethod · 0.80
GetFunctionMethod · 0.80
SetMethod · 0.65